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
8 package org.opendaylight.openflowplugin.impl.registry.flow;
10 import com.romix.scala.collection.concurrent.TrieMap;
11 import java.util.Collection;
12 import java.util.Collections;
13 import java.util.HashSet;
15 import java.util.concurrent.ConcurrentMap;
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;
26 * Created by Martin Bobak <mbobak@cisco.com> on 8.4.2015.
28 public class DeviceFlowRegistryImpl implements DeviceFlowRegistry {
29 private static final Logger LOG = LoggerFactory.getLogger(DeviceFlowRegistryImpl.class);
31 private final ConcurrentMap<FlowRegistryKey, FlowDescriptor> flowRegistry = new TrieMap<>();
33 private final Collection<FlowRegistryKey> marks = new HashSet<>();
36 public FlowDescriptor retrieveIdForFlow(final FlowRegistryKey flowRegistryKey) {
37 FlowDescriptor flowDescriptor = flowRegistry.get(flowRegistryKey);
38 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 flowRegistry.put(flowRegistryKey, flowDescriptor);
48 public FlowId storeIfNecessary(final FlowRegistryKey flowRegistryKey, final short tableId) {
49 final FlowId alienFlowId = FlowUtil.createAlienFlowId(tableId);
50 final FlowDescriptor alienFlowDescriptor = FlowDescriptorFactory.create(tableId, alienFlowId);
52 final FlowDescriptor previous = flowRegistry.putIfAbsent(flowRegistryKey, alienFlowDescriptor);
53 if (previous == null) {
54 LOG.trace("Flow descriptor for flow hash {} wasn't found.", flowRegistryKey.hashCode());
57 return previous.getFlowId();
62 public void markToBeremoved(final FlowRegistryKey flowRegistryKey) {
63 synchronized (marks) {
64 marks.add(flowRegistryKey);
66 LOG.trace("Flow hash {} was marked for removal.", flowRegistryKey.hashCode());
70 public void removeMarked() {
71 synchronized (marks) {
72 for (FlowRegistryKey flowRegistryKey : marks) {
73 LOG.trace("Removing flowDescriptor for flow hash : {}", flowRegistryKey.hashCode());
74 flowRegistry.remove(flowRegistryKey);
82 public Map<FlowRegistryKey, FlowDescriptor> getAllFlowDescriptors() {
83 return Collections.unmodifiableMap(flowRegistry);