BUG-3363: code optimization and cleanup - Fix unsynchronized access to marks
[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 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;
24
25
26 /**
27  * Created by Martin Bobak <mbobak@cisco.com> on 8.4.2015.
28  */
29 public class DeviceFlowRegistryImpl implements DeviceFlowRegistry {
30
31     @GuardedBy("flowRegistry")
32     private final Map<FlowRegistryKey, FlowDescriptor> flowRegistry = new HashMap<>();
33     @GuardedBy("marks")
34     private final Collection<FlowRegistryKey> marks = new HashSet<>();
35     private static final Logger LOG = LoggerFactory.getLogger(DeviceFlowRegistryImpl.class);
36
37     @Override
38     public FlowDescriptor retrieveIdForFlow(final FlowRegistryKey flowRegistryKey) {
39         FlowDescriptor flowDescriptor = flowRegistry.get(flowRegistryKey);
40         return flowDescriptor;
41     }
42
43
44     @Override
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);
49         }
50     }
51
52     @Override
53     public FlowId storeIfNecessary(final FlowRegistryKey flowRegistryKey, final short tableId) {
54
55
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();
62             } else {
63                 LOG.trace("Flow descriptor for flow hash {} wasn't found.", flowRegistryKey.hashCode());
64                 flowRegistry.put(flowRegistryKey, alienFlowDescriptor);
65                 return alienFlowId;
66             }
67         }
68     }
69
70     @Override
71     public void markToBeremoved(final FlowRegistryKey flowRegistryKey) {
72         synchronized (marks) {
73             marks.add(flowRegistryKey);
74         }
75         LOG.trace("Flow hash {} was marked for removal.", flowRegistryKey.hashCode());
76
77     }
78
79     @Override
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);
86                 }
87             }
88
89             marks.clear();
90         }
91     }
92
93     @Override
94     public Map<FlowRegistryKey, FlowDescriptor> getAllFlowDescriptors() {
95         return Collections.unmodifiableMap(flowRegistry);
96     }
97
98     @Override
99     public void close() {
100         flowRegistry.clear();
101         marks.clear();
102     }
103 }