CSC to provide a view of DPN re-sync
[openflowplugin.git] / applications / forwardingrules-manager / src / main / java / org / opendaylight / openflowplugin / applications / frm / impl / ForwardingRulesManagerImpl.java
1 /*
2  * Copyright (c) 2014, 2017 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.applications.frm.impl;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.Optional;
14 import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.atomic.AtomicLong;
16 import javax.annotation.PostConstruct;
17 import javax.annotation.PreDestroy;
18 import javax.inject.Inject;
19 import javax.inject.Singleton;
20 import org.apache.aries.blueprint.annotation.service.Reference;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.opendaylight.mdsal.binding.api.DataBroker;
23 import org.opendaylight.mdsal.binding.api.ReadTransaction;
24 import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
25 import org.opendaylight.mdsal.binding.api.RpcProviderService;
26 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
27 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
28 import org.opendaylight.openflowplugin.api.openflow.FlowGroupCacheManager;
29 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationService;
30 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeServiceManager;
31 import org.opendaylight.openflowplugin.applications.frm.BundleMessagesCommiter;
32 import org.opendaylight.openflowplugin.applications.frm.FlowNodeReconciliation;
33 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesCommiter;
34 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
35 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesProperty;
36 import org.opendaylight.openflowplugin.applications.frm.NodeConfigurator;
37 import org.opendaylight.openflowplugin.applications.frm.nodeconfigurator.NodeConfiguratorImpl;
38 import org.opendaylight.openflowplugin.applications.frm.recovery.OpenflowServiceRecoveryHandler;
39 import org.opendaylight.openflowplugin.applications.reconciliation.NotificationRegistration;
40 import org.opendaylight.openflowplugin.applications.reconciliation.ReconciliationManager;
41 import org.opendaylight.serviceutils.srm.RecoverableListener;
42 import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.SalMeterService;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.SalBundleService;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.arbitrator.reconcile.service.rev180227.ArbitratorReconcileService;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.forwardingrules.manager.config.rev160511.ForwardingRulesManagerConfig;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.rf.state.rev170713.ResultState;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.SalTableService;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
57 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
60
61 /**
62  * forwardingrules-manager org.opendaylight.openflowplugin.applications.frm.impl
63  *
64  * <p>
65  * Manager and middle point for whole module. It contains ActiveNodeHolder and
66  * provide all RPC services.
67  *
68  */
69 @Singleton
70 public class ForwardingRulesManagerImpl implements ForwardingRulesManager {
71     private static final Logger LOG = LoggerFactory.getLogger(ForwardingRulesManagerImpl.class);
72
73     static final int STARTUP_LOOP_TICK = 500;
74     static final int STARTUP_LOOP_MAX_RETRIES = 8;
75     private static final int FRM_RECONCILIATION_PRIORITY = Integer.getInteger("frm.reconciliation.priority", 1);
76     private static final String SERVICE_NAME = "FRM";
77
78     private final AtomicLong txNum = new AtomicLong();
79     private final DataBroker dataService;
80     private final SalFlowService salFlowService;
81     private final SalGroupService salGroupService;
82     private final SalMeterService salMeterService;
83     private final SalTableService salTableService;
84     private final ClusterSingletonServiceProvider clusterSingletonServiceProvider;
85     private final SalBundleService salBundleService;
86     private final AutoCloseable configurationServiceRegistration;
87     private final MastershipChangeServiceManager mastershipChangeServiceManager;
88     private final RpcProviderService rpcProviderService;
89     private ForwardingRulesCommiter<Flow> flowListener;
90     private ForwardingRulesCommiter<Group> groupListener;
91     private ForwardingRulesCommiter<Meter> meterListener;
92     private ForwardingRulesCommiter<TableFeatures> tableListener;
93     private BundleMessagesCommiter<Flow> bundleFlowListener;
94     private BundleMessagesCommiter<Group> bundleGroupListener;
95     private FlowNodeReconciliation nodeListener;
96     private NotificationRegistration reconciliationNotificationRegistration;
97     private FlowNodeConnectorInventoryTranslatorImpl flowNodeConnectorInventoryTranslatorImpl;
98     private DeviceMastershipManager deviceMastershipManager;
99     private final ReconciliationManager reconciliationManager;
100     private DevicesGroupRegistry devicesGroupRegistry;
101     private NodeConfigurator nodeConfigurator;
102     private final ArbitratorReconcileService arbitratorReconciliationManager;
103     private boolean disableReconciliation;
104     private boolean staleMarkingEnabled;
105     private int reconciliationRetryCount;
106     private boolean isBundleBasedReconciliationEnabled;
107     private final OpenflowServiceRecoveryHandler openflowServiceRecoveryHandler;
108     private final ServiceRecoveryRegistry serviceRecoveryRegistry;
109     private final FlowGroupCacheManager flowGroupCacheManager;
110
111     @Inject
112     public ForwardingRulesManagerImpl(@Reference final DataBroker dataBroker,
113                                       @Reference final RpcConsumerRegistry rpcRegistry,
114                                       @Reference final RpcProviderService rpcProviderService,
115                                       final ForwardingRulesManagerConfig config,
116                                       @Reference final MastershipChangeServiceManager mastershipChangeServiceManager,
117                                       @Reference final ClusterSingletonServiceProvider clusterSingletonService,
118                                       @Reference final ConfigurationService configurationService,
119                                       @Reference final ReconciliationManager reconciliationManager,
120                                       final OpenflowServiceRecoveryHandler openflowServiceRecoveryHandler,
121                                       @Reference final ServiceRecoveryRegistry serviceRecoveryRegistry,
122                                       @Reference final FlowGroupCacheManager flowGroupCacheManager) {
123         disableReconciliation = config.isDisableReconciliation();
124         staleMarkingEnabled = config.isStaleMarkingEnabled();
125         reconciliationRetryCount = config.getReconciliationRetryCount().toJava();
126         isBundleBasedReconciliationEnabled = config.isBundleBasedReconciliationEnabled();
127         this.configurationServiceRegistration = configurationService.registerListener(this);
128         this.dataService = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
129         this.clusterSingletonServiceProvider = Preconditions.checkNotNull(clusterSingletonService,
130                 "ClusterSingletonService provider can not be null");
131         this.reconciliationManager = reconciliationManager;
132         this.rpcProviderService = rpcProviderService;
133         this.mastershipChangeServiceManager = mastershipChangeServiceManager;
134         this.flowGroupCacheManager = flowGroupCacheManager;
135
136         Preconditions.checkArgument(rpcRegistry != null, "RpcProviderRegistry can not be null !");
137
138         this.salFlowService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalFlowService.class),
139                 "RPC SalFlowService not found.");
140         this.salGroupService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalGroupService.class),
141                 "RPC SalGroupService not found.");
142         this.salMeterService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalMeterService.class),
143                 "RPC SalMeterService not found.");
144         this.salTableService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalTableService.class),
145                 "RPC SalTableService not found.");
146         this.salBundleService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalBundleService.class),
147                 "RPC SalBundlService not found.");
148         this.openflowServiceRecoveryHandler = Preconditions.checkNotNull(openflowServiceRecoveryHandler,
149                 "Openflow service recovery handler cannot be null");
150         this.serviceRecoveryRegistry = Preconditions.checkNotNull(serviceRecoveryRegistry,
151                 "Service recovery registry cannot be null");
152         this.arbitratorReconciliationManager = Preconditions
153                 .checkNotNull(rpcRegistry.getRpcService(ArbitratorReconcileService.class),
154                         "ArbitratorReconciliationManager can not be null!");
155     }
156
157     @Override
158     @PostConstruct
159     public void start() {
160         nodeConfigurator = new NodeConfiguratorImpl();
161         this.devicesGroupRegistry = new DevicesGroupRegistry();
162         this.nodeListener = new FlowNodeReconciliationImpl(this, dataService, SERVICE_NAME, FRM_RECONCILIATION_PRIORITY,
163                 ResultState.DONOTHING, flowGroupCacheManager);
164         if (this.isReconciliationDisabled()) {
165             LOG.debug("Reconciliation is disabled by user");
166         } else {
167             this.reconciliationNotificationRegistration = reconciliationManager.registerService(this.nodeListener);
168             LOG.debug("Reconciliation is enabled by user and successfully registered to the reconciliation framework");
169         }
170         this.deviceMastershipManager = new DeviceMastershipManager(clusterSingletonServiceProvider, this.nodeListener,
171                 dataService, mastershipChangeServiceManager, rpcProviderService,
172                 new FrmReconciliationServiceImpl(this));
173         flowNodeConnectorInventoryTranslatorImpl = new FlowNodeConnectorInventoryTranslatorImpl(dataService);
174
175         this.bundleFlowListener = new BundleFlowForwarder(this);
176         this.bundleGroupListener = new BundleGroupForwarder(this);
177         this.flowListener = new FlowForwarder(this, dataService);
178         this.groupListener = new GroupForwarder(this, dataService);
179         this.meterListener = new MeterForwarder(this, dataService);
180         this.tableListener = new TableForwarder(this, dataService);
181         LOG.info("ForwardingRulesManager has started successfully.");
182     }
183
184     @Override
185     @PreDestroy
186     public void close() throws Exception {
187         configurationServiceRegistration.close();
188
189         if (this.flowListener != null) {
190             this.flowListener.close();
191             this.flowListener = null;
192         }
193         if (this.groupListener != null) {
194             this.groupListener.close();
195             this.groupListener = null;
196         }
197         if (this.meterListener != null) {
198             this.meterListener.close();
199             this.meterListener = null;
200         }
201         if (this.tableListener != null) {
202             this.tableListener.close();
203             this.tableListener = null;
204         }
205         if (this.nodeListener != null) {
206             this.nodeListener.close();
207             this.nodeListener = null;
208         }
209         if (deviceMastershipManager != null) {
210             deviceMastershipManager.close();
211         }
212         if (this.reconciliationNotificationRegistration != null) {
213             this.reconciliationNotificationRegistration.close();
214             this.reconciliationNotificationRegistration = null;
215         }
216     }
217
218     @Override
219     public ReadTransaction getReadTransaction() {
220         return dataService.newReadOnlyTransaction();
221     }
222
223     @Override
224     public String getNewTransactionId() {
225         return "DOM-" + txNum.getAndIncrement();
226     }
227
228     @Override
229     public boolean isNodeActive(final InstanceIdentifier<FlowCapableNode> ident) {
230         return deviceMastershipManager.isNodeActive(ident.firstKeyOf(Node.class).getId());
231     }
232
233     @Override
234     public boolean checkNodeInOperationalDataStore(final InstanceIdentifier<FlowCapableNode> ident) {
235         boolean result = false;
236         InstanceIdentifier<Node> nodeIid = ident.firstIdentifierOf(Node.class);
237         try (ReadTransaction transaction = dataService.newReadOnlyTransaction()) {
238             ListenableFuture<Optional<Node>> future = transaction
239                 .read(LogicalDatastoreType.OPERATIONAL, nodeIid);
240             Optional<Node> optionalDataObject = future.get();
241             if (optionalDataObject.isPresent()) {
242                 result = true;
243             } else {
244                 LOG.debug("{}: Failed to read {}", Thread.currentThread().getStackTrace()[1], nodeIid);
245             }
246         } catch (ExecutionException | InterruptedException e) {
247             LOG.warn("Failed to read {} ", nodeIid, e);
248         }
249
250         return result;
251     }
252
253     @Override
254     public SalFlowService getSalFlowService() {
255         return salFlowService;
256     }
257
258     @Override
259     public SalGroupService getSalGroupService() {
260         return salGroupService;
261     }
262
263     @Override
264     public SalMeterService getSalMeterService() {
265         return salMeterService;
266     }
267
268     @Override
269     public SalTableService getSalTableService() {
270         return salTableService;
271     }
272
273     @Override
274     public DevicesGroupRegistry getDevicesGroupRegistry() {
275         return this.devicesGroupRegistry;
276     }
277
278     @Override
279     public SalBundleService getSalBundleService() {
280         return salBundleService;
281     }
282
283     @Override
284     public ForwardingRulesCommiter<Flow> getFlowCommiter() {
285         return flowListener;
286     }
287
288     @Override
289     public ForwardingRulesCommiter<Group> getGroupCommiter() {
290         return groupListener;
291     }
292
293     @Override
294     public ForwardingRulesCommiter<Meter> getMeterCommiter() {
295         return meterListener;
296     }
297
298     @Override
299     public ForwardingRulesCommiter<TableFeatures> getTableFeaturesCommiter() {
300         return tableListener;
301     }
302
303     @Override
304     public BundleMessagesCommiter<Flow> getBundleFlowListener() {
305         return bundleFlowListener;
306     }
307
308     @Override
309     public BundleMessagesCommiter<Group> getBundleGroupListener() {
310         return bundleGroupListener;
311     }
312
313     @Override
314     public ArbitratorReconcileService getArbitratorReconciliationManager() {
315         return arbitratorReconciliationManager;
316     }
317
318     @Override
319     public boolean isReconciliationDisabled() {
320         return disableReconciliation;
321     }
322
323     @Override
324     public boolean isStaleMarkingEnabled() {
325         return staleMarkingEnabled;
326     }
327
328     @Override
329     public int getReconciliationRetryCount() {
330         return reconciliationRetryCount;
331     }
332
333     @Override
334     public void addRecoverableListener(final RecoverableListener recoverableListener) {
335         serviceRecoveryRegistry.addRecoverableListener(openflowServiceRecoveryHandler.buildServiceRegistryKey(),
336                 recoverableListener);
337     }
338
339     @Override
340     public FlowNodeConnectorInventoryTranslatorImpl getFlowNodeConnectorInventoryTranslatorImpl() {
341         return flowNodeConnectorInventoryTranslatorImpl;
342     }
343
344     @Override
345     public NodeConfigurator getNodeConfigurator() {
346         return nodeConfigurator;
347     }
348
349     public FlowNodeReconciliation getNodeListener() {
350         return nodeListener;
351     }
352
353     @Override
354     public boolean isBundleBasedReconciliationEnabled() {
355         return isBundleBasedReconciliationEnabled;
356     }
357
358     @Override
359     public boolean isNodeOwner(final InstanceIdentifier<FlowCapableNode> ident) {
360         return ident != null && deviceMastershipManager.isDeviceMastered(ident.firstKeyOf(Node.class).getId());
361     }
362
363     @VisibleForTesting
364     public void setDeviceMastershipManager(final DeviceMastershipManager deviceMastershipManager) {
365         this.deviceMastershipManager = deviceMastershipManager;
366     }
367
368     @Override
369     public void onPropertyChanged(@NonNull final String propertyName, @NonNull final String propertyValue) {
370         final ForwardingRulesProperty forwardingRulesProperty = ForwardingRulesProperty.forValue(propertyName);
371         if (forwardingRulesProperty != null) {
372             switch (forwardingRulesProperty) {
373                 case DISABLE_RECONCILIATION:
374                     disableReconciliation = Boolean.valueOf(propertyValue);
375                     break;
376                 case STALE_MARKING_ENABLED:
377                     staleMarkingEnabled = Boolean.valueOf(propertyValue);
378                     break;
379                 case RECONCILIATION_RETRY_COUNT:
380                     reconciliationRetryCount = Integer.parseInt(propertyValue);
381                     break;
382                 case BUNDLE_BASED_RECONCILIATION_ENABLED:
383                     isBundleBasedReconciliationEnabled = Boolean.valueOf(propertyValue);
384                     break;
385                 default:
386                     LOG.warn("No forwarding rule property found.");
387                     break;
388             }
389         }
390     }
391 }