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
9 package org.opendaylight.openflowplugin.applications.frm.impl;
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.Objects;
15 import java.util.Optional;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.atomic.AtomicLong;
18 import javax.annotation.Nonnull;
19 import javax.annotation.PostConstruct;
20 import javax.annotation.PreDestroy;
21 import javax.inject.Inject;
22 import javax.inject.Singleton;
23 import org.apache.aries.blueprint.annotation.service.Reference;
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
28 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
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.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.app.frm.reconciliation.service.rev180227.FrmReconciliationService;
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;
62 * forwardingrules-manager org.opendaylight.openflowplugin.applications.frm.impl
65 * Manager and middle point for whole module. It contains ActiveNodeHolder and
66 * provide all RPC services.
70 public class ForwardingRulesManagerImpl implements ForwardingRulesManager {
71 private static final Logger LOG = LoggerFactory.getLogger(ForwardingRulesManagerImpl.class);
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";
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 RpcProviderRegistry rpcRegistry;
89 private ForwardingRulesCommiter<Flow> flowListener;
90 private ForwardingRulesCommiter<Group> groupListener;
91 private ForwardingRulesCommiter<Meter> meterListener;
92 private ForwardingRulesCommiter<TableFeatures> tableListener;
93 private FlowNodeReconciliation nodeListener;
94 private NotificationRegistration reconciliationNotificationRegistration;
95 private FlowNodeConnectorInventoryTranslatorImpl flowNodeConnectorInventoryTranslatorImpl;
96 private DeviceMastershipManager deviceMastershipManager;
97 private final ReconciliationManager reconciliationManager;
98 private DevicesGroupRegistry devicesGroupRegistry;
99 private NodeConfigurator nodeConfigurator;
100 private ArbitratorReconcileService arbitratorReconciliationManager;
101 private boolean disableReconciliation;
102 private boolean staleMarkingEnabled;
103 private int reconciliationRetryCount;
104 private boolean isBundleBasedReconciliationEnabled;
105 private final OpenflowServiceRecoveryHandler openflowServiceRecoveryHandler;
106 private final ServiceRecoveryRegistry serviceRecoveryRegistry;
109 public ForwardingRulesManagerImpl(@Reference final DataBroker dataBroker,
110 @Reference final RpcProviderRegistry rpcRegistry,
111 final ForwardingRulesManagerConfig config,
112 @Reference final MastershipChangeServiceManager mastershipChangeServiceManager,
113 @Reference final ClusterSingletonServiceProvider clusterSingletonService,
114 @Reference final ConfigurationService configurationService,
115 @Reference final ReconciliationManager reconciliationManager,
116 final OpenflowServiceRecoveryHandler openflowServiceRecoveryHandler,
117 @Reference final ServiceRecoveryRegistry serviceRecoveryRegistry) {
118 disableReconciliation = config.isDisableReconciliation();
119 staleMarkingEnabled = config.isStaleMarkingEnabled();
120 reconciliationRetryCount = config.getReconciliationRetryCount();
121 isBundleBasedReconciliationEnabled = config.isBundleBasedReconciliationEnabled();
122 this.configurationServiceRegistration = configurationService.registerListener(this);
123 this.dataService = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
124 this.clusterSingletonServiceProvider = Preconditions.checkNotNull(clusterSingletonService,
125 "ClusterSingletonService provider can not be null");
126 this.reconciliationManager = reconciliationManager;
127 this.rpcRegistry = rpcRegistry;
128 this.mastershipChangeServiceManager = mastershipChangeServiceManager;
130 Preconditions.checkArgument(rpcRegistry != null, "RpcProviderRegistry can not be null !");
132 this.salFlowService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalFlowService.class),
133 "RPC SalFlowService not found.");
134 this.salGroupService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalGroupService.class),
135 "RPC SalGroupService not found.");
136 this.salMeterService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalMeterService.class),
137 "RPC SalMeterService not found.");
138 this.salTableService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalTableService.class),
139 "RPC SalTableService not found.");
140 this.salBundleService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalBundleService.class),
141 "RPC SalBundlService not found.");
142 this.openflowServiceRecoveryHandler = Preconditions.checkNotNull(openflowServiceRecoveryHandler,
143 "Openflow service recovery handler cannot be null");
144 this.serviceRecoveryRegistry = Preconditions.checkNotNull(serviceRecoveryRegistry,
145 "Service recovery registry cannot be null");
146 this.arbitratorReconciliationManager = Preconditions
147 .checkNotNull(rpcRegistry.getRpcService(ArbitratorReconcileService.class),
148 "ArbitratorReconciliationManager can not be null!");
153 public void start() {
154 nodeConfigurator = new NodeConfiguratorImpl();
155 this.devicesGroupRegistry = new DevicesGroupRegistry();
157 this.nodeListener = new FlowNodeReconciliationImpl(this, dataService, SERVICE_NAME, FRM_RECONCILIATION_PRIORITY,
158 ResultState.DONOTHING);
159 if (this.isReconciliationDisabled()) {
160 LOG.debug("Reconciliation is disabled by user");
162 this.reconciliationNotificationRegistration = reconciliationManager.registerService(this.nodeListener);
163 LOG.debug("Reconciliation is enabled by user and successfully registered to the reconciliation framework");
165 this.deviceMastershipManager = new DeviceMastershipManager(clusterSingletonServiceProvider, this.nodeListener,
166 dataService, mastershipChangeServiceManager);
167 this.deviceMastershipManager.setRoutedRpcReg(rpcRegistry.addRoutedRpcImplementation(
168 FrmReconciliationService.class, new FrmReconciliationServiceImpl(this)));
169 flowNodeConnectorInventoryTranslatorImpl = new FlowNodeConnectorInventoryTranslatorImpl(dataService);
171 this.flowListener = new FlowForwarder(this, dataService);
172 this.groupListener = new GroupForwarder(this, dataService);
173 this.meterListener = new MeterForwarder(this, dataService);
174 this.tableListener = new TableForwarder(this, dataService);
175 LOG.info("ForwardingRulesManager has started successfully.");
180 public void close() throws Exception {
181 configurationServiceRegistration.close();
183 if (this.flowListener != null) {
184 this.flowListener.close();
185 this.flowListener = null;
187 if (this.groupListener != null) {
188 this.groupListener.close();
189 this.groupListener = null;
191 if (this.meterListener != null) {
192 this.meterListener.close();
193 this.meterListener = null;
195 if (this.tableListener != null) {
196 this.tableListener.close();
197 this.tableListener = null;
199 if (this.nodeListener != null) {
200 this.nodeListener.close();
201 this.nodeListener = null;
203 if (deviceMastershipManager != null) {
204 deviceMastershipManager.close();
206 if (this.reconciliationNotificationRegistration != null) {
207 this.reconciliationNotificationRegistration.close();
208 this.reconciliationNotificationRegistration = null;
213 public ReadOnlyTransaction getReadTransaction() {
214 return dataService.newReadOnlyTransaction();
218 public String getNewTransactionId() {
219 return "DOM-" + txNum.getAndIncrement();
223 public boolean isNodeActive(InstanceIdentifier<FlowCapableNode> ident) {
224 return deviceMastershipManager.isNodeActive(ident.firstKeyOf(Node.class).getId());
228 public boolean checkNodeInOperationalDataStore(InstanceIdentifier<FlowCapableNode> ident) {
229 boolean result = false;
230 InstanceIdentifier<Node> nodeIid = ident.firstIdentifierOf(Node.class);
231 try (ReadOnlyTransaction transaction = dataService.newReadOnlyTransaction()) {
232 ListenableFuture<com.google.common.base.Optional<Node>> future = transaction
233 .read(LogicalDatastoreType.OPERATIONAL, nodeIid);
234 com.google.common.base.Optional<Node> optionalDataObject = future.get();
235 if (optionalDataObject.isPresent()) {
238 LOG.debug("{}: Failed to read {}", Thread.currentThread().getStackTrace()[1], nodeIid);
240 } catch (ExecutionException | InterruptedException e) {
241 LOG.warn("Failed to read {} ", nodeIid, e);
248 public SalFlowService getSalFlowService() {
249 return salFlowService;
253 public SalGroupService getSalGroupService() {
254 return salGroupService;
258 public SalMeterService getSalMeterService() {
259 return salMeterService;
263 public SalTableService getSalTableService() {
264 return salTableService;
268 public DevicesGroupRegistry getDevicesGroupRegistry() {
269 return this.devicesGroupRegistry;
273 public SalBundleService getSalBundleService() {
274 return salBundleService;
278 public ForwardingRulesCommiter<Flow> getFlowCommiter() {
283 public ForwardingRulesCommiter<Group> getGroupCommiter() {
284 return groupListener;
288 public ForwardingRulesCommiter<Meter> getMeterCommiter() {
289 return meterListener;
293 public ForwardingRulesCommiter<TableFeatures> getTableFeaturesCommiter() {
294 return tableListener;
298 public ArbitratorReconcileService getArbitratorReconciliationManager() {
299 return arbitratorReconciliationManager;
303 public boolean isReconciliationDisabled() {
304 return disableReconciliation;
308 public boolean isStaleMarkingEnabled() {
309 return staleMarkingEnabled;
313 public int getReconciliationRetryCount() {
314 return reconciliationRetryCount;
318 public void addRecoverableListener(RecoverableListener recoverableListener) {
319 serviceRecoveryRegistry.addRecoverableListener(openflowServiceRecoveryHandler.buildServiceRegistryKey(),
320 recoverableListener);
324 public FlowNodeConnectorInventoryTranslatorImpl getFlowNodeConnectorInventoryTranslatorImpl() {
325 return flowNodeConnectorInventoryTranslatorImpl;
329 public NodeConfigurator getNodeConfigurator() {
330 return nodeConfigurator;
333 public FlowNodeReconciliation getNodeListener() {
338 public boolean isBundleBasedReconciliationEnabled() {
339 return isBundleBasedReconciliationEnabled;
343 public boolean isNodeOwner(InstanceIdentifier<FlowCapableNode> ident) {
344 return Objects.nonNull(ident) && deviceMastershipManager.isDeviceMastered(ident.firstKeyOf(Node.class).getId());
348 public void setDeviceMastershipManager(final DeviceMastershipManager deviceMastershipManager) {
349 this.deviceMastershipManager = deviceMastershipManager;
353 public void onPropertyChanged(@Nonnull final String propertyName, @Nonnull final String propertyValue) {
354 Optional.ofNullable(ForwardingRulesProperty.forValue(propertyName)).ifPresent(forwardingRulesProperty -> {
355 switch (forwardingRulesProperty) {
356 case DISABLE_RECONCILIATION:
357 disableReconciliation = Boolean.valueOf(propertyValue);
359 case STALE_MARKING_ENABLED:
360 staleMarkingEnabled = Boolean.valueOf(propertyValue);
362 case RECONCILIATION_RETRY_COUNT:
363 reconciliationRetryCount = Integer.parseInt(propertyValue);
365 case BUNDLE_BASED_RECONCILIATION_ENABLED:
366 isBundleBasedReconciliationEnabled = Boolean.valueOf(propertyValue);
369 LOG.warn("No forwarding rule property found.");