2 * Copyright (c) 2014, 2017 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.applications.frm.impl;
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.configuration.ConfigurationService;
29 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeServiceManager;
30 import org.opendaylight.openflowplugin.applications.frm.BundleMessagesCommiter;
31 import org.opendaylight.openflowplugin.applications.frm.FlowNodeReconciliation;
32 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesCommiter;
33 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
34 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesProperty;
35 import org.opendaylight.openflowplugin.applications.frm.NodeConfigurator;
36 import org.opendaylight.openflowplugin.applications.frm.nodeconfigurator.NodeConfiguratorImpl;
37 import org.opendaylight.openflowplugin.applications.frm.recovery.OpenflowServiceRecoveryHandler;
38 import org.opendaylight.openflowplugin.applications.reconciliation.NotificationRegistration;
39 import org.opendaylight.openflowplugin.applications.reconciliation.ReconciliationManager;
40 import org.opendaylight.serviceutils.srm.RecoverableListener;
41 import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.SalMeterService;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.SalBundleService;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.arbitrator.reconcile.service.rev180227.ArbitratorReconcileService;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.forwardingrules.manager.config.rev160511.ForwardingRulesManagerConfig;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.rf.state.rev170713.ResultState;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.SalTableService;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
56 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
61 * forwardingrules-manager org.opendaylight.openflowplugin.applications.frm.impl
64 * Manager and middle point for whole module. It contains ActiveNodeHolder and
65 * provide all RPC services.
69 public class ForwardingRulesManagerImpl implements ForwardingRulesManager {
70 private static final Logger LOG = LoggerFactory.getLogger(ForwardingRulesManagerImpl.class);
72 static final int STARTUP_LOOP_TICK = 500;
73 static final int STARTUP_LOOP_MAX_RETRIES = 8;
74 private static final int FRM_RECONCILIATION_PRIORITY = Integer.getInteger("frm.reconciliation.priority", 1);
75 private static final String SERVICE_NAME = "FRM";
77 private final AtomicLong txNum = new AtomicLong();
78 private final DataBroker dataService;
79 private final SalFlowService salFlowService;
80 private final SalGroupService salGroupService;
81 private final SalMeterService salMeterService;
82 private final SalTableService salTableService;
83 private final ClusterSingletonServiceProvider clusterSingletonServiceProvider;
84 private final SalBundleService salBundleService;
85 private final AutoCloseable configurationServiceRegistration;
86 private final MastershipChangeServiceManager mastershipChangeServiceManager;
87 private final RpcProviderService rpcProviderService;
88 private ForwardingRulesCommiter<Flow> flowListener;
89 private ForwardingRulesCommiter<Group> groupListener;
90 private ForwardingRulesCommiter<Meter> meterListener;
91 private ForwardingRulesCommiter<TableFeatures> tableListener;
92 private BundleMessagesCommiter<Flow> bundleFlowListener;
93 private BundleMessagesCommiter<Group> bundleGroupListener;
94 private FlowNodeReconciliation nodeListener;
95 private NotificationRegistration reconciliationNotificationRegistration;
96 private FlowNodeConnectorInventoryTranslatorImpl flowNodeConnectorInventoryTranslatorImpl;
97 private DeviceMastershipManager deviceMastershipManager;
98 private final ReconciliationManager reconciliationManager;
99 private DevicesGroupRegistry devicesGroupRegistry;
100 private NodeConfigurator nodeConfigurator;
101 private final ArbitratorReconcileService arbitratorReconciliationManager;
102 private boolean disableReconciliation;
103 private boolean staleMarkingEnabled;
104 private int reconciliationRetryCount;
105 private boolean isBundleBasedReconciliationEnabled;
106 private final OpenflowServiceRecoveryHandler openflowServiceRecoveryHandler;
107 private final ServiceRecoveryRegistry serviceRecoveryRegistry;
110 public ForwardingRulesManagerImpl(@Reference final DataBroker dataBroker,
111 @Reference final RpcConsumerRegistry rpcRegistry,
112 @Reference final RpcProviderService rpcProviderService,
113 final ForwardingRulesManagerConfig config,
114 @Reference final MastershipChangeServiceManager mastershipChangeServiceManager,
115 @Reference final ClusterSingletonServiceProvider clusterSingletonService,
116 @Reference final ConfigurationService configurationService,
117 @Reference final ReconciliationManager reconciliationManager,
118 final OpenflowServiceRecoveryHandler openflowServiceRecoveryHandler,
119 @Reference final ServiceRecoveryRegistry serviceRecoveryRegistry) {
120 disableReconciliation = config.isDisableReconciliation();
121 staleMarkingEnabled = config.isStaleMarkingEnabled();
122 reconciliationRetryCount = config.getReconciliationRetryCount().toJava();
123 isBundleBasedReconciliationEnabled = config.isBundleBasedReconciliationEnabled();
124 this.configurationServiceRegistration = configurationService.registerListener(this);
125 this.dataService = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
126 this.clusterSingletonServiceProvider = Preconditions.checkNotNull(clusterSingletonService,
127 "ClusterSingletonService provider can not be null");
128 this.reconciliationManager = reconciliationManager;
129 this.rpcProviderService = rpcProviderService;
130 this.mastershipChangeServiceManager = mastershipChangeServiceManager;
132 Preconditions.checkArgument(rpcRegistry != null, "RpcProviderRegistry can not be null !");
134 this.salFlowService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalFlowService.class),
135 "RPC SalFlowService not found.");
136 this.salGroupService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalGroupService.class),
137 "RPC SalGroupService not found.");
138 this.salMeterService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalMeterService.class),
139 "RPC SalMeterService not found.");
140 this.salTableService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalTableService.class),
141 "RPC SalTableService not found.");
142 this.salBundleService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalBundleService.class),
143 "RPC SalBundlService not found.");
144 this.openflowServiceRecoveryHandler = Preconditions.checkNotNull(openflowServiceRecoveryHandler,
145 "Openflow service recovery handler cannot be null");
146 this.serviceRecoveryRegistry = Preconditions.checkNotNull(serviceRecoveryRegistry,
147 "Service recovery registry cannot be null");
148 this.arbitratorReconciliationManager = Preconditions
149 .checkNotNull(rpcRegistry.getRpcService(ArbitratorReconcileService.class),
150 "ArbitratorReconciliationManager can not be null!");
155 public void start() {
156 nodeConfigurator = new NodeConfiguratorImpl();
157 this.devicesGroupRegistry = new DevicesGroupRegistry();
159 this.nodeListener = new FlowNodeReconciliationImpl(this, dataService, SERVICE_NAME, FRM_RECONCILIATION_PRIORITY,
160 ResultState.DONOTHING);
161 if (this.isReconciliationDisabled()) {
162 LOG.debug("Reconciliation is disabled by user");
164 this.reconciliationNotificationRegistration = reconciliationManager.registerService(this.nodeListener);
165 LOG.debug("Reconciliation is enabled by user and successfully registered to the reconciliation framework");
167 this.deviceMastershipManager = new DeviceMastershipManager(clusterSingletonServiceProvider, this.nodeListener,
168 dataService, mastershipChangeServiceManager, rpcProviderService,
169 new FrmReconciliationServiceImpl(this));
170 flowNodeConnectorInventoryTranslatorImpl = new FlowNodeConnectorInventoryTranslatorImpl(dataService);
172 this.bundleFlowListener = new BundleFlowForwarder(this);
173 this.bundleGroupListener = new BundleGroupForwarder(this);
174 this.flowListener = new FlowForwarder(this, dataService);
175 this.groupListener = new GroupForwarder(this, dataService);
176 this.meterListener = new MeterForwarder(this, dataService);
177 this.tableListener = new TableForwarder(this, dataService);
178 LOG.info("ForwardingRulesManager has started successfully.");
183 public void close() throws Exception {
184 configurationServiceRegistration.close();
186 if (this.flowListener != null) {
187 this.flowListener.close();
188 this.flowListener = null;
190 if (this.groupListener != null) {
191 this.groupListener.close();
192 this.groupListener = null;
194 if (this.meterListener != null) {
195 this.meterListener.close();
196 this.meterListener = null;
198 if (this.tableListener != null) {
199 this.tableListener.close();
200 this.tableListener = null;
202 if (this.nodeListener != null) {
203 this.nodeListener.close();
204 this.nodeListener = null;
206 if (deviceMastershipManager != null) {
207 deviceMastershipManager.close();
209 if (this.reconciliationNotificationRegistration != null) {
210 this.reconciliationNotificationRegistration.close();
211 this.reconciliationNotificationRegistration = null;
216 public ReadTransaction getReadTransaction() {
217 return dataService.newReadOnlyTransaction();
221 public String getNewTransactionId() {
222 return "DOM-" + txNum.getAndIncrement();
226 public boolean isNodeActive(final InstanceIdentifier<FlowCapableNode> ident) {
227 return deviceMastershipManager.isNodeActive(ident.firstKeyOf(Node.class).getId());
231 public boolean checkNodeInOperationalDataStore(final InstanceIdentifier<FlowCapableNode> ident) {
232 boolean result = false;
233 InstanceIdentifier<Node> nodeIid = ident.firstIdentifierOf(Node.class);
234 try (ReadTransaction transaction = dataService.newReadOnlyTransaction()) {
235 ListenableFuture<Optional<Node>> future = transaction
236 .read(LogicalDatastoreType.OPERATIONAL, nodeIid);
237 Optional<Node> optionalDataObject = future.get();
238 if (optionalDataObject.isPresent()) {
241 LOG.debug("{}: Failed to read {}", Thread.currentThread().getStackTrace()[1], nodeIid);
243 } catch (ExecutionException | InterruptedException e) {
244 LOG.warn("Failed to read {} ", nodeIid, e);
251 public SalFlowService getSalFlowService() {
252 return salFlowService;
256 public SalGroupService getSalGroupService() {
257 return salGroupService;
261 public SalMeterService getSalMeterService() {
262 return salMeterService;
266 public SalTableService getSalTableService() {
267 return salTableService;
271 public DevicesGroupRegistry getDevicesGroupRegistry() {
272 return this.devicesGroupRegistry;
276 public SalBundleService getSalBundleService() {
277 return salBundleService;
281 public ForwardingRulesCommiter<Flow> getFlowCommiter() {
286 public ForwardingRulesCommiter<Group> getGroupCommiter() {
287 return groupListener;
291 public ForwardingRulesCommiter<Meter> getMeterCommiter() {
292 return meterListener;
296 public ForwardingRulesCommiter<TableFeatures> getTableFeaturesCommiter() {
297 return tableListener;
301 public BundleMessagesCommiter<Flow> getBundleFlowListener() {
302 return bundleFlowListener;
306 public BundleMessagesCommiter<Group> getBundleGroupListener() {
307 return bundleGroupListener;
311 public ArbitratorReconcileService getArbitratorReconciliationManager() {
312 return arbitratorReconciliationManager;
316 public boolean isReconciliationDisabled() {
317 return disableReconciliation;
321 public boolean isStaleMarkingEnabled() {
322 return staleMarkingEnabled;
326 public int getReconciliationRetryCount() {
327 return reconciliationRetryCount;
331 public void addRecoverableListener(final RecoverableListener recoverableListener) {
332 serviceRecoveryRegistry.addRecoverableListener(openflowServiceRecoveryHandler.buildServiceRegistryKey(),
333 recoverableListener);
337 public FlowNodeConnectorInventoryTranslatorImpl getFlowNodeConnectorInventoryTranslatorImpl() {
338 return flowNodeConnectorInventoryTranslatorImpl;
342 public NodeConfigurator getNodeConfigurator() {
343 return nodeConfigurator;
346 public FlowNodeReconciliation getNodeListener() {
351 public boolean isBundleBasedReconciliationEnabled() {
352 return isBundleBasedReconciliationEnabled;
356 public boolean isNodeOwner(final InstanceIdentifier<FlowCapableNode> ident) {
357 return ident != null && deviceMastershipManager.isDeviceMastered(ident.firstKeyOf(Node.class).getId());
361 public void setDeviceMastershipManager(final DeviceMastershipManager deviceMastershipManager) {
362 this.deviceMastershipManager = deviceMastershipManager;
366 public void onPropertyChanged(@NonNull final String propertyName, @NonNull final String propertyValue) {
367 final ForwardingRulesProperty forwardingRulesProperty = ForwardingRulesProperty.forValue(propertyName);
368 if (forwardingRulesProperty != null) {
369 switch (forwardingRulesProperty) {
370 case DISABLE_RECONCILIATION:
371 disableReconciliation = Boolean.valueOf(propertyValue);
373 case STALE_MARKING_ENABLED:
374 staleMarkingEnabled = Boolean.valueOf(propertyValue);
376 case RECONCILIATION_RETRY_COUNT:
377 reconciliationRetryCount = Integer.parseInt(propertyValue);
379 case BUNDLE_BASED_RECONCILIATION_ENABLED:
380 isBundleBasedReconciliationEnabled = Boolean.valueOf(propertyValue);
383 LOG.warn("No forwarding rule property found.");