ca5874ba948756275215ff7fd490ae49aa207228
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / registry / flow / DeviceFlowRegistryImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.impl.registry.flow;
10
11 import com.google.common.collect.ImmutableMap;
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.concurrent.ConcurrentHashMap;
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.FlowHash;
20 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryException;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24
25 /**
26  * Created by Martin Bobak <mbobak@cisco.com> on 8.4.2015.
27  */
28 public class DeviceFlowRegistryImpl implements DeviceFlowRegistry {
29
30     private final Map<FlowHash, FlowDescriptor> flowRegistry = new ConcurrentHashMap<>();
31     private final List<FlowHash> marks = Collections.synchronizedList(new ArrayList());
32     private final Logger LOG = LoggerFactory.getLogger(DeviceFlowRegistryImpl.class);
33
34     @Override
35     public FlowDescriptor retrieveIdForFlow(final FlowHash flowHash) throws FlowRegistryException {
36         FlowDescriptor flowDescriptor = flowRegistry.get(flowHash);
37         if (null != flowDescriptor) {
38             return flowDescriptor;
39         }
40         throw new FlowRegistryException("Flow hash not registered.");
41     }
42
43
44     @Override
45     public void store(final FlowHash flowHash, final FlowDescriptor flowDescriptor) {
46         LOG.trace("Storing flowDescriptor with table ID : {} and flow ID : {} for flow hash : {}", flowDescriptor.getTableKey().getId(), flowDescriptor.getFlowId().getValue(), flowHash.hashCode());
47         flowRegistry.put(flowHash, flowDescriptor);
48     }
49
50     @Override
51     public void markToBeremoved(final FlowHash flowHash) {
52         marks.add(flowHash);
53         LOG.trace("Flow hash {} was marked for removal.", flowHash.hashCode());
54
55     }
56
57     @Override
58     public void removeMarked() {
59         for (FlowHash flowHash : marks) {
60             LOG.trace("Removing flowDescriptor for flow hash : {}", flowHash.hashCode());
61             flowRegistry.remove(flowHash);
62         }
63         marks.clear();
64     }
65
66
67     @Override
68     public Map<FlowHash, FlowDescriptor> getAllFlowDescriptors() {
69         return ImmutableMap.copyOf(flowRegistry);
70     }
71
72     @Override
73     public void close() throws Exception {
74         flowRegistry.clear();
75         marks.clear();
76     }
77 }