ddf48078f7e6e33e909bd90426cf06acb724c3f8
[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 java.util.Collection;
12 import java.util.Collections;
13 import java.util.HashMap;
14 import java.util.HashSet;
15 import java.util.Map;
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;
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<FlowRegistryKey, FlowDescriptor> flowRegistry = new HashMap<>();
31     private final Collection<FlowRegistryKey> marks = new HashSet<>();
32     private static final Logger LOG = LoggerFactory.getLogger(DeviceFlowRegistryImpl.class);
33
34     @Override
35     public FlowDescriptor retrieveIdForFlow(final FlowRegistryKey flowRegistryKey) {
36         FlowDescriptor flowDescriptor = flowRegistry.get(flowRegistryKey);
37         return flowDescriptor;
38     }
39
40
41     @Override
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);
46         }
47     }
48
49     @Override
50     public FlowId storeIfNecessary(final FlowRegistryKey flowRegistryKey, final short tableId) {
51
52
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();
59             } else {
60                 LOG.trace("Flow descriptor for flow hash {} wasn't found.", flowRegistryKey.hashCode());
61                 flowRegistry.put(flowRegistryKey, alienFlowDescriptor);
62                 return alienFlowId;
63             }
64         }
65     }
66
67     @Override
68     public void markToBeremoved(final FlowRegistryKey flowRegistryKey) {
69         synchronized (marks) {
70             marks.add(flowRegistryKey);
71         }
72         LOG.trace("Flow hash {} was marked for removal.", flowRegistryKey.hashCode());
73
74     }
75
76     @Override
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);
82             }
83         }
84         synchronized (marks) {
85             marks.clear();
86         }
87     }
88
89
90     @Override
91     public Map<FlowRegistryKey, FlowDescriptor> getAllFlowDescriptors() {
92         return Collections.unmodifiableMap(flowRegistry);
93     }
94
95     @Override
96     public void close() {
97         flowRegistry.clear();
98         marks.clear();
99     }
100 }