BUG-3157: performance tuning - Use a TrieMap as the ConcurrentMap implementation
[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 package org.opendaylight.openflowplugin.impl.registry.flow;
9
10 import com.romix.scala.collection.concurrent.TrieMap;
11 import java.util.Collection;
12 import java.util.Collections;
13 import java.util.HashSet;
14 import java.util.Map;
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;
24
25 /**
26  * Created by Martin Bobak <mbobak@cisco.com> on 8.4.2015.
27  */
28 public class DeviceFlowRegistryImpl implements DeviceFlowRegistry {
29     private static final Logger LOG = LoggerFactory.getLogger(DeviceFlowRegistryImpl.class);
30
31     private final ConcurrentMap<FlowRegistryKey, FlowDescriptor> flowRegistry = new TrieMap<>();
32     @GuardedBy("marks")
33     private final Collection<FlowRegistryKey> marks = new HashSet<>();
34
35     @Override
36     public FlowDescriptor retrieveIdForFlow(final FlowRegistryKey flowRegistryKey) {
37         FlowDescriptor flowDescriptor = flowRegistry.get(flowRegistryKey);
38         return flowDescriptor;
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         flowRegistry.put(flowRegistryKey, flowDescriptor);
45     }
46
47     @Override
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);
51
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());
55             return alienFlowId;
56         } else {
57             return previous.getFlowId();
58         }
59     }
60
61     @Override
62     public void markToBeremoved(final FlowRegistryKey flowRegistryKey) {
63         synchronized (marks) {
64             marks.add(flowRegistryKey);
65         }
66         LOG.trace("Flow hash {} was marked for removal.", flowRegistryKey.hashCode());
67     }
68
69     @Override
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);
75             }
76
77             marks.clear();
78         }
79     }
80
81     @Override
82     public Map<FlowRegistryKey, FlowDescriptor> getAllFlowDescriptors() {
83         return Collections.unmodifiableMap(flowRegistry);
84     }
85
86     @Override
87     public void close() {
88         flowRegistry.clear();
89         marks.clear();
90     }
91 }