Merge "Migrate uint/ByteBuf interactions"
[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     private final ListenerRegistrationHelper registrationHelper;
111
112     @Inject
113     public ForwardingRulesManagerImpl(@Reference final DataBroker dataBroker,
114                                       @Reference final RpcConsumerRegistry rpcRegistry,
115                                       @Reference final RpcProviderService rpcProviderService,
116                                       final ForwardingRulesManagerConfig config,
117                                       @Reference final MastershipChangeServiceManager mastershipChangeServiceManager,
118                                       @Reference final ClusterSingletonServiceProvider clusterSingletonService,
119                                       @Reference final ConfigurationService configurationService,
120                                       @Reference final ReconciliationManager reconciliationManager,
121                                       final OpenflowServiceRecoveryHandler openflowServiceRecoveryHandler,
122                                       @Reference final ServiceRecoveryRegistry serviceRecoveryRegistry,
123                                       @Reference final FlowGroupCacheManager flowGroupCacheManager,
124                                       final ListenerRegistrationHelper registrationHelper) {
125         disableReconciliation = config.isDisableReconciliation();
126         staleMarkingEnabled = config.isStaleMarkingEnabled();
127         reconciliationRetryCount = config.getReconciliationRetryCount().toJava();
128         isBundleBasedReconciliationEnabled = config.isBundleBasedReconciliationEnabled();
129         this.configurationServiceRegistration = configurationService.registerListener(this);
130         this.registrationHelper = Preconditions.checkNotNull(registrationHelper, "RegistrationHelper cannot be null");
131         this.dataService = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
132         this.clusterSingletonServiceProvider = Preconditions.checkNotNull(clusterSingletonService,
133                 "ClusterSingletonService provider can not be null");
134         this.reconciliationManager = reconciliationManager;
135         this.rpcProviderService = rpcProviderService;
136         this.mastershipChangeServiceManager = mastershipChangeServiceManager;
137         this.flowGroupCacheManager = flowGroupCacheManager;
138
139         Preconditions.checkArgument(rpcRegistry != null, "RpcProviderRegistry can not be null !");
140
141         this.salFlowService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalFlowService.class),
142                 "RPC SalFlowService not found.");
143         this.salGroupService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalGroupService.class),
144                 "RPC SalGroupService not found.");
145         this.salMeterService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalMeterService.class),
146                 "RPC SalMeterService not found.");
147         this.salTableService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalTableService.class),
148                 "RPC SalTableService not found.");
149         this.salBundleService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalBundleService.class),
150                 "RPC SalBundlService not found.");
151         this.openflowServiceRecoveryHandler = Preconditions.checkNotNull(openflowServiceRecoveryHandler,
152                 "Openflow service recovery handler cannot be null");
153         this.serviceRecoveryRegistry = Preconditions.checkNotNull(serviceRecoveryRegistry,
154                 "Service recovery registry cannot be null");
155         this.arbitratorReconciliationManager = Preconditions
156                 .checkNotNull(rpcRegistry.getRpcService(ArbitratorReconcileService.class),
157                         "ArbitratorReconciliationManager can not be null!");
158     }
159
160     @Override
161     @PostConstruct
162     public void start() {
163         nodeConfigurator = new NodeConfiguratorImpl();
164         this.devicesGroupRegistry = new DevicesGroupRegistry();
165         this.nodeListener = new FlowNodeReconciliationImpl(this, dataService, SERVICE_NAME, FRM_RECONCILIATION_PRIORITY,
166                 ResultState.DONOTHING, flowGroupCacheManager);
167         if (this.isReconciliationDisabled()) {
168             LOG.debug("Reconciliation is disabled by user");
169         } else {
170             this.reconciliationNotificationRegistration = reconciliationManager.registerService(this.nodeListener);
171             LOG.debug("Reconciliation is enabled by user and successfully registered to the reconciliation framework");
172         }
173         this.deviceMastershipManager = new DeviceMastershipManager(clusterSingletonServiceProvider, this.nodeListener,
174                 dataService, mastershipChangeServiceManager, rpcProviderService,
175                 new FrmReconciliationServiceImpl(this));
176         flowNodeConnectorInventoryTranslatorImpl = new FlowNodeConnectorInventoryTranslatorImpl(dataService);
177
178         this.bundleFlowListener = new BundleFlowForwarder(this);
179         this.bundleGroupListener = new BundleGroupForwarder(this);
180         this.flowListener = new FlowForwarder(this, dataService, registrationHelper);
181         this.groupListener = new GroupForwarder(this, dataService, registrationHelper);
182         this.meterListener = new MeterForwarder(this, dataService, registrationHelper);
183         this.tableListener = new TableForwarder(this, dataService, registrationHelper);
184         LOG.info("ForwardingRulesManager has started successfully.");
185     }
186
187     @Override
188     @PreDestroy
189     public void close() throws Exception {
190         configurationServiceRegistration.close();
191
192         if (this.flowListener != null) {
193             this.flowListener.close();
194             this.flowListener = null;
195         }
196         if (this.groupListener != null) {
197             this.groupListener.close();
198             this.groupListener = null;
199         }
200         if (this.meterListener != null) {
201             this.meterListener.close();
202             this.meterListener = null;
203         }
204         if (this.tableListener != null) {
205             this.tableListener.close();
206             this.tableListener = null;
207         }
208         if (this.nodeListener != null) {
209             this.nodeListener.close();
210             this.nodeListener = null;
211         }
212         if (deviceMastershipManager != null) {
213             deviceMastershipManager.close();
214         }
215         if (this.reconciliationNotificationRegistration != null) {
216             this.reconciliationNotificationRegistration.close();
217             this.reconciliationNotificationRegistration = null;
218         }
219     }
220
221     @Override
222     public ReadTransaction getReadTransaction() {
223         return dataService.newReadOnlyTransaction();
224     }
225
226     @Override
227     public String getNewTransactionId() {
228         return "DOM-" + txNum.getAndIncrement();
229     }
230
231     @Override
232     public boolean isNodeActive(final InstanceIdentifier<FlowCapableNode> ident) {
233         return deviceMastershipManager.isNodeActive(ident.firstKeyOf(Node.class).getId());
234     }
235
236     @Override
237     public boolean checkNodeInOperationalDataStore(final InstanceIdentifier<FlowCapableNode> ident) {
238         boolean result = false;
239         InstanceIdentifier<Node> nodeIid = ident.firstIdentifierOf(Node.class);
240         try (ReadTransaction transaction = dataService.newReadOnlyTransaction()) {
241             ListenableFuture<Optional<Node>> future = transaction
242                 .read(LogicalDatastoreType.OPERATIONAL, nodeIid);
243             Optional<Node> optionalDataObject = future.get();
244             if (optionalDataObject.isPresent()) {
245                 result = true;
246             } else {
247                 LOG.debug("{}: Failed to read {}", Thread.currentThread().getStackTrace()[1], nodeIid);
248             }
249         } catch (ExecutionException | InterruptedException e) {
250             LOG.warn("Failed to read {} ", nodeIid, e);
251         }
252
253         return result;
254     }
255
256     @Override
257     public SalFlowService getSalFlowService() {
258         return salFlowService;
259     }
260
261     @Override
262     public SalGroupService getSalGroupService() {
263         return salGroupService;
264     }
265
266     @Override
267     public SalMeterService getSalMeterService() {
268         return salMeterService;
269     }
270
271     @Override
272     public SalTableService getSalTableService() {
273         return salTableService;
274     }
275
276     @Override
277     public DevicesGroupRegistry getDevicesGroupRegistry() {
278         return this.devicesGroupRegistry;
279     }
280
281     @Override
282     public SalBundleService getSalBundleService() {
283         return salBundleService;
284     }
285
286     @Override
287     public ForwardingRulesCommiter<Flow> getFlowCommiter() {
288         return flowListener;
289     }
290
291     @Override
292     public ForwardingRulesCommiter<Group> getGroupCommiter() {
293         return groupListener;
294     }
295
296     @Override
297     public ForwardingRulesCommiter<Meter> getMeterCommiter() {
298         return meterListener;
299     }
300
301     @Override
302     public ForwardingRulesCommiter<TableFeatures> getTableFeaturesCommiter() {
303         return tableListener;
304     }
305
306     @Override
307     public BundleMessagesCommiter<Flow> getBundleFlowListener() {
308         return bundleFlowListener;
309     }
310
311     @Override
312     public BundleMessagesCommiter<Group> getBundleGroupListener() {
313         return bundleGroupListener;
314     }
315
316     @Override
317     public ArbitratorReconcileService getArbitratorReconciliationManager() {
318         return arbitratorReconciliationManager;
319     }
320
321     @Override
322     public boolean isReconciliationDisabled() {
323         return disableReconciliation;
324     }
325
326     @Override
327     public boolean isStaleMarkingEnabled() {
328         return staleMarkingEnabled;
329     }
330
331     @Override
332     public int getReconciliationRetryCount() {
333         return reconciliationRetryCount;
334     }
335
336     @Override
337     public void addRecoverableListener(final RecoverableListener recoverableListener) {
338         serviceRecoveryRegistry.addRecoverableListener(openflowServiceRecoveryHandler.buildServiceRegistryKey(),
339                 recoverableListener);
340     }
341
342     @Override
343     public FlowNodeConnectorInventoryTranslatorImpl getFlowNodeConnectorInventoryTranslatorImpl() {
344         return flowNodeConnectorInventoryTranslatorImpl;
345     }
346
347     @Override
348     public NodeConfigurator getNodeConfigurator() {
349         return nodeConfigurator;
350     }
351
352     public FlowNodeReconciliation getNodeListener() {
353         return nodeListener;
354     }
355
356     @Override
357     public boolean isBundleBasedReconciliationEnabled() {
358         return isBundleBasedReconciliationEnabled;
359     }
360
361     @Override
362     public boolean isNodeOwner(final InstanceIdentifier<FlowCapableNode> ident) {
363         return ident != null && deviceMastershipManager.isDeviceMastered(ident.firstKeyOf(Node.class).getId());
364     }
365
366     @VisibleForTesting
367     public void setDeviceMastershipManager(final DeviceMastershipManager deviceMastershipManager) {
368         this.deviceMastershipManager = deviceMastershipManager;
369     }
370
371     @Override
372     public void onPropertyChanged(@NonNull final String propertyName, @NonNull final String propertyValue) {
373         final ForwardingRulesProperty forwardingRulesProperty = ForwardingRulesProperty.forValue(propertyName);
374         if (forwardingRulesProperty != null) {
375             switch (forwardingRulesProperty) {
376                 case DISABLE_RECONCILIATION:
377                     disableReconciliation = Boolean.valueOf(propertyValue);
378                     break;
379                 case STALE_MARKING_ENABLED:
380                     staleMarkingEnabled = Boolean.valueOf(propertyValue);
381                     break;
382                 case RECONCILIATION_RETRY_COUNT:
383                     reconciliationRetryCount = Integer.parseInt(propertyValue);
384                     break;
385                 case BUNDLE_BASED_RECONCILIATION_ENABLED:
386                     isBundleBasedReconciliationEnabled = Boolean.valueOf(propertyValue);
387                     break;
388                 default:
389                     LOG.warn("No forwarding rule property found.");
390                     break;
391             }
392         }
393     }
394 }