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