2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowplugin.impl.registry.flow;
11 import java.util.Collection;
12 import java.util.Collections;
13 import java.util.HashMap;
14 import java.util.HashSet;
16 import javax.annotation.concurrent.GuardedBy;
17 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
18 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
19 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
20 import org.opendaylight.openflowplugin.impl.util.FlowUtil;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
27 * Created by Martin Bobak <mbobak@cisco.com> on 8.4.2015.
29 public class DeviceFlowRegistryImpl implements DeviceFlowRegistry {
31 @GuardedBy("flowRegistry")
32 private final Map<FlowRegistryKey, FlowDescriptor> flowRegistry = new HashMap<>();
34 private final Collection<FlowRegistryKey> marks = new HashSet<>();
35 private static final Logger LOG = LoggerFactory.getLogger(DeviceFlowRegistryImpl.class);
38 public FlowDescriptor retrieveIdForFlow(final FlowRegistryKey flowRegistryKey) {
39 FlowDescriptor flowDescriptor = flowRegistry.get(flowRegistryKey);
40 return flowDescriptor;
45 public void store(final FlowRegistryKey flowRegistryKey, final FlowDescriptor flowDescriptor) {
46 LOG.trace("Storing flowDescriptor with table ID : {} and flow ID : {} for flow hash : {}", flowDescriptor.getTableKey().getId(), flowDescriptor.getFlowId().getValue(), flowRegistryKey.hashCode());
47 synchronized (flowRegistry) {
48 flowRegistry.put(flowRegistryKey, flowDescriptor);
53 public FlowId storeIfNecessary(final FlowRegistryKey flowRegistryKey, final short tableId) {
56 FlowId alienFlowId = FlowUtil.createAlienFlowId(tableId);
57 FlowDescriptor alienFlowDescriptor = FlowDescriptorFactory.create(tableId, alienFlowId);
58 synchronized (flowRegistry) {
59 FlowDescriptor flowDescriptorFromRegistry = flowRegistry.get(flowRegistryKey);
60 if (flowDescriptorFromRegistry != null) {
61 return flowDescriptorFromRegistry.getFlowId();
63 LOG.trace("Flow descriptor for flow hash {} wasn't found.", flowRegistryKey.hashCode());
64 flowRegistry.put(flowRegistryKey, alienFlowDescriptor);
71 public void markToBeremoved(final FlowRegistryKey flowRegistryKey) {
72 synchronized (marks) {
73 marks.add(flowRegistryKey);
75 LOG.trace("Flow hash {} was marked for removal.", flowRegistryKey.hashCode());
80 public void removeMarked() {
81 synchronized (marks) {
82 for (FlowRegistryKey flowRegistryKey : marks) {
83 LOG.trace("Removing flowDescriptor for flow hash : {}", flowRegistryKey.hashCode());
84 synchronized (flowRegistry) {
85 flowRegistry.remove(flowRegistryKey);
94 public Map<FlowRegistryKey, FlowDescriptor> getAllFlowDescriptors() {
95 return Collections.unmodifiableMap(flowRegistry);
100 flowRegistry.clear();