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