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 org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
17 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
18 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
19 import org.opendaylight.openflowplugin.impl.util.FlowUtil;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
26 * Created by Martin Bobak <mbobak@cisco.com> on 8.4.2015.
28 public class DeviceFlowRegistryImpl implements DeviceFlowRegistry {
30 private final Map<FlowRegistryKey, FlowDescriptor> flowRegistry = new HashMap<>();
31 private final Collection<FlowRegistryKey> marks = new HashSet<>();
32 private static final Logger LOG = LoggerFactory.getLogger(DeviceFlowRegistryImpl.class);
35 public FlowDescriptor retrieveIdForFlow(final FlowRegistryKey flowRegistryKey) {
36 FlowDescriptor flowDescriptor = flowRegistry.get(flowRegistryKey);
37 return flowDescriptor;
42 public void store(final FlowRegistryKey flowRegistryKey, final FlowDescriptor flowDescriptor) {
43 LOG.trace("Storing flowDescriptor with table ID : {} and flow ID : {} for flow hash : {}", flowDescriptor.getTableKey().getId(), flowDescriptor.getFlowId().getValue(), flowRegistryKey.hashCode());
44 synchronized (flowRegistry) {
45 flowRegistry.put(flowRegistryKey, flowDescriptor);
50 public FlowId storeIfNecessary(final FlowRegistryKey flowRegistryKey, final short tableId) {
53 FlowId alienFlowId = FlowUtil.createAlienFlowId(tableId);
54 FlowDescriptor alienFlowDescriptor = FlowDescriptorFactory.create(tableId, alienFlowId);
55 synchronized (flowRegistry) {
56 FlowDescriptor flowDescriptorFromRegistry = flowRegistry.get(flowRegistryKey);
57 if (flowDescriptorFromRegistry != null) {
58 return flowDescriptorFromRegistry.getFlowId();
60 LOG.trace("Flow descriptor for flow hash {} wasn't found.", flowRegistryKey.hashCode());
61 flowRegistry.put(flowRegistryKey, alienFlowDescriptor);
68 public void markToBeremoved(final FlowRegistryKey flowRegistryKey) {
69 synchronized (marks) {
70 marks.add(flowRegistryKey);
72 LOG.trace("Flow hash {} was marked for removal.", flowRegistryKey.hashCode());
77 public void removeMarked() {
78 synchronized (flowRegistry) {
79 for (FlowRegistryKey flowRegistryKey : marks) {
80 LOG.trace("Removing flowDescriptor for flow hash : {}", flowRegistryKey.hashCode());
81 flowRegistry.remove(flowRegistryKey);
84 synchronized (marks) {
91 public Map<FlowRegistryKey, FlowDescriptor> getAllFlowDescriptors() {
92 return Collections.unmodifiableMap(flowRegistry);