Bug 6110: Fixed bugs in statistics manager due to race condition.
[openflowplugin.git] / applications / forwardingrules-manager / src / main / java / org / opendaylight / openflowplugin / applications / frm / impl / ForwardingRulesManagerImpl.java
1 /**
2  * Copyright (c) 2014, 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.applications.frm.impl;
10
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.Sets;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import java.util.Collections;
17 import java.util.Objects;
18 import java.util.Set;
19 import java.util.concurrent.atomic.AtomicLong;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
24 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
25 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
26 import org.opendaylight.openflowplugin.applications.frm.FlowNodeReconciliation;
27 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesCommiter;
28 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.SalMeterService;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.forwardingrules.manager.config.rev160511.ForwardingRulesManagerConfig;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.SalTableService;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 /**
45  * forwardingrules-manager
46  * org.opendaylight.openflowplugin.applications.frm.impl
47  *
48  * Manager and middle point for whole module.
49  * It contains ActiveNodeHolder and provide all RPC services.
50  *
51  */
52 public class ForwardingRulesManagerImpl implements ForwardingRulesManager {
53
54     private static final Logger LOG = LoggerFactory.getLogger(ForwardingRulesManagerImpl.class);
55     static final int STARTUP_LOOP_TICK = 500;
56     static final int STARTUP_LOOP_MAX_RETRIES = 8;
57
58     private final AtomicLong txNum = new AtomicLong();
59     private final Object lockObj = new Object();
60     private Set<InstanceIdentifier<FlowCapableNode>> activeNodes = Collections.emptySet();
61
62     private final DataBroker dataService;
63     private final SalFlowService salFlowService;
64     private final SalGroupService salGroupService;
65     private final SalMeterService salMeterService;
66     private final SalTableService salTableService;
67
68     private ForwardingRulesCommiter<Flow> flowListener;
69     private ForwardingRulesCommiter<Group> groupListener;
70     private ForwardingRulesCommiter<Meter> meterListener;
71     private ForwardingRulesCommiter<TableFeatures> tableListener;
72     private FlowNodeReconciliation nodeListener;
73
74     private final ForwardingRulesManagerConfig forwardingRulesManagerConfig;
75     private FlowNodeConnectorInventoryTranslatorImpl flowNodeConnectorInventoryTranslatorImpl;
76     private final ClusterSingletonServiceProvider clusterSingletonServiceProvider;
77     private DeviceMastershipManager deviceMastershipManager;
78
79     public ForwardingRulesManagerImpl(final DataBroker dataBroker,
80                                       final RpcConsumerRegistry rpcRegistry,
81                                       final ForwardingRulesManagerConfig config,
82                                       final ClusterSingletonServiceProvider clusterSingletonService) {
83         this.dataService = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
84         this.forwardingRulesManagerConfig = Preconditions.checkNotNull(config, "Configuration for FRM cannot be null");
85         this.clusterSingletonServiceProvider = Preconditions.checkNotNull(clusterSingletonService,
86                 "ClusterSingletonService provider can not be null");
87
88         Preconditions.checkArgument(rpcRegistry != null, "RpcConsumerRegistry can not be null !");
89
90         this.salFlowService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalFlowService.class),
91                 "RPC SalFlowService not found.");
92         this.salGroupService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalGroupService.class),
93                 "RPC SalGroupService not found.");
94         this.salMeterService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalMeterService.class),
95                 "RPC SalMeterService not found.");
96         this.salTableService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalTableService.class),
97                 "RPC SalTableService not found.");
98     }
99
100     @Override
101     public void start() {
102         this.deviceMastershipManager = new DeviceMastershipManager(clusterSingletonServiceProvider);
103         this.flowListener = new FlowForwarder(this, dataService);
104         this.groupListener = new GroupForwarder(this, dataService);
105         this.meterListener = new MeterForwarder(this, dataService);
106         this.tableListener = new TableForwarder(this, dataService);
107         this.nodeListener = new FlowNodeReconciliationImpl(this, dataService);
108         flowNodeConnectorInventoryTranslatorImpl =
109                 new FlowNodeConnectorInventoryTranslatorImpl(this,dataService);
110         LOG.info("ForwardingRulesManager has started successfully.");
111     }
112
113     @Override
114     public void close() throws Exception {
115         if (this.flowListener != null) {
116             this.flowListener.close();
117             this.flowListener = null;
118         }
119         if (this.groupListener != null) {
120             this.groupListener.close();
121             this.groupListener = null;
122         }
123         if (this.meterListener != null) {
124             this.meterListener.close();
125             this.meterListener = null;
126         }
127         if (this.tableListener != null) {
128             this.tableListener.close();
129             this.tableListener = null;
130         }
131         if (this.nodeListener != null) {
132             this.nodeListener.close();
133             this.nodeListener = null;
134         }
135     }
136
137     @Override
138     public ReadOnlyTransaction getReadTranaction() {
139         return dataService.newReadOnlyTransaction();
140     }
141
142     @Override
143     public String getNewTransactionId() {
144         return "DOM-" + txNum.getAndIncrement();
145     }
146
147     @Override
148     public boolean isNodeActive(InstanceIdentifier<FlowCapableNode> ident) {
149         return activeNodes.contains(ident);
150     }
151
152     @Override
153     public boolean checkNodeInOperationalDataStore(InstanceIdentifier<FlowCapableNode> ident) {
154         boolean result = false;
155         InstanceIdentifier<Node> nodeIid = ident.firstIdentifierOf(Node.class);
156         final ReadOnlyTransaction transaction = dataService.newReadOnlyTransaction();
157         Optional<Node> optionalDataObject;
158         CheckedFuture<Optional<Node>, ReadFailedException> future = transaction.read(LogicalDatastoreType.OPERATIONAL, nodeIid);
159         try {
160             optionalDataObject = future.checkedGet();
161             if (optionalDataObject.isPresent()) {
162                 result = true;
163             } else {
164                 LOG.debug("{}: Failed to read {}",
165                         Thread.currentThread().getStackTrace()[1], nodeIid);
166             }
167         } catch (ReadFailedException e) {
168             LOG.warn("Failed to read {} ", nodeIid, e);
169         }
170         transaction.close();
171
172         return result;
173     }
174
175     @Override
176     public void registrateNewNode(InstanceIdentifier<FlowCapableNode> ident) {
177         if (!activeNodes.contains(ident)) {
178             synchronized (lockObj) {
179                 if (!activeNodes.contains(ident)) {
180                     Set<InstanceIdentifier<FlowCapableNode>> set =
181                             Sets.newHashSet(activeNodes);
182                     set.add(ident);
183                     activeNodes = Collections.unmodifiableSet(set);
184                     deviceMastershipManager.onDeviceConnected(ident.firstKeyOf(Node.class).getId());
185                 }
186             }
187         }
188     }
189
190     @Override
191     public void unregistrateNode(InstanceIdentifier<FlowCapableNode> ident) {
192         if (activeNodes.contains(ident)) {
193             synchronized (lockObj) {
194                 if (activeNodes.contains(ident)) {
195                     Set<InstanceIdentifier<FlowCapableNode>> set =
196                             Sets.newHashSet(activeNodes);
197                     set.remove(ident);
198                     activeNodes = Collections.unmodifiableSet(set);
199                     deviceMastershipManager.onDeviceDisconnected(ident.firstKeyOf(Node.class).getId());
200                 }
201             }
202         }
203     }
204
205     @Override
206     public SalFlowService getSalFlowService() {
207         return salFlowService;
208     }
209
210     @Override
211     public SalGroupService getSalGroupService() {
212         return salGroupService;
213     }
214
215     @Override
216     public SalMeterService getSalMeterService() {
217         return salMeterService;
218     }
219
220     @Override
221     public SalTableService getSalTableService() {
222         return salTableService;
223     }
224
225     @Override
226     public ForwardingRulesCommiter<Flow> getFlowCommiter() {
227         return flowListener;
228     }
229
230     @Override
231     public ForwardingRulesCommiter<Group> getGroupCommiter() {
232         return groupListener;
233     }
234
235     @Override
236     public ForwardingRulesCommiter<Meter> getMeterCommiter() {
237         return meterListener;
238     }
239
240     @Override
241     public ForwardingRulesCommiter<TableFeatures> getTableFeaturesCommiter() {
242         return tableListener;
243     }
244
245     @Override
246     public FlowNodeReconciliation getFlowNodeReconciliation() {
247         return nodeListener;
248     }
249
250     @Override
251     public ForwardingRulesManagerConfig getConfiguration() {
252         return forwardingRulesManagerConfig;
253     }
254
255     @Override
256     public FlowNodeConnectorInventoryTranslatorImpl getFlowNodeConnectorInventoryTranslatorImpl() {
257         return flowNodeConnectorInventoryTranslatorImpl;
258     }
259
260     @Override
261     public boolean isNodeOwner(InstanceIdentifier<FlowCapableNode> ident) {
262         return Objects.nonNull(ident) && deviceMastershipManager.isDeviceMastered(ident.firstKeyOf(Node.class).getId());
263     }
264
265     @VisibleForTesting
266     public void setDeviceMastershipManager(final DeviceMastershipManager deviceMastershipManager) {
267         this.deviceMastershipManager = deviceMastershipManager;
268     }
269
270 }
271