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.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;
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 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;
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;
136 Preconditions.checkArgument(rpcRegistry != null, "RpcProviderRegistry can not be null !");
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!");
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");
167 this.reconciliationNotificationRegistration = reconciliationManager.registerService(this.nodeListener);
168 LOG.debug("Reconciliation is enabled by user and successfully registered to the reconciliation framework");
170 this.deviceMastershipManager = new DeviceMastershipManager(clusterSingletonServiceProvider, this.nodeListener,
171 dataService, mastershipChangeServiceManager, rpcProviderService,
172 new FrmReconciliationServiceImpl(this));
173 flowNodeConnectorInventoryTranslatorImpl = new FlowNodeConnectorInventoryTranslatorImpl(dataService);
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.");
186 public void close() throws Exception {
187 configurationServiceRegistration.close();
189 if (this.flowListener != null) {
190 this.flowListener.close();
191 this.flowListener = null;
193 if (this.groupListener != null) {
194 this.groupListener.close();
195 this.groupListener = null;
197 if (this.meterListener != null) {
198 this.meterListener.close();
199 this.meterListener = null;
201 if (this.tableListener != null) {
202 this.tableListener.close();
203 this.tableListener = null;
205 if (this.nodeListener != null) {
206 this.nodeListener.close();
207 this.nodeListener = null;
209 if (deviceMastershipManager != null) {
210 deviceMastershipManager.close();
212 if (this.reconciliationNotificationRegistration != null) {
213 this.reconciliationNotificationRegistration.close();
214 this.reconciliationNotificationRegistration = null;
219 public ReadTransaction getReadTransaction() {
220 return dataService.newReadOnlyTransaction();
224 public String getNewTransactionId() {
225 return "DOM-" + txNum.getAndIncrement();
229 public boolean isNodeActive(final InstanceIdentifier<FlowCapableNode> ident) {
230 return deviceMastershipManager.isNodeActive(ident.firstKeyOf(Node.class).getId());
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()) {
244 LOG.debug("{}: Failed to read {}", Thread.currentThread().getStackTrace()[1], nodeIid);
246 } catch (ExecutionException | InterruptedException e) {
247 LOG.warn("Failed to read {} ", nodeIid, e);
254 public SalFlowService getSalFlowService() {
255 return salFlowService;
259 public SalGroupService getSalGroupService() {
260 return salGroupService;
264 public SalMeterService getSalMeterService() {
265 return salMeterService;
269 public SalTableService getSalTableService() {
270 return salTableService;
274 public DevicesGroupRegistry getDevicesGroupRegistry() {
275 return this.devicesGroupRegistry;
279 public SalBundleService getSalBundleService() {
280 return salBundleService;
284 public ForwardingRulesCommiter<Flow> getFlowCommiter() {
289 public ForwardingRulesCommiter<Group> getGroupCommiter() {
290 return groupListener;
294 public ForwardingRulesCommiter<Meter> getMeterCommiter() {
295 return meterListener;
299 public ForwardingRulesCommiter<TableFeatures> getTableFeaturesCommiter() {
300 return tableListener;
304 public BundleMessagesCommiter<Flow> getBundleFlowListener() {
305 return bundleFlowListener;
309 public BundleMessagesCommiter<Group> getBundleGroupListener() {
310 return bundleGroupListener;
314 public ArbitratorReconcileService getArbitratorReconciliationManager() {
315 return arbitratorReconciliationManager;
319 public boolean isReconciliationDisabled() {
320 return disableReconciliation;
324 public boolean isStaleMarkingEnabled() {
325 return staleMarkingEnabled;
329 public int getReconciliationRetryCount() {
330 return reconciliationRetryCount;
334 public void addRecoverableListener(final RecoverableListener recoverableListener) {
335 serviceRecoveryRegistry.addRecoverableListener(openflowServiceRecoveryHandler.buildServiceRegistryKey(),
336 recoverableListener);
340 public FlowNodeConnectorInventoryTranslatorImpl getFlowNodeConnectorInventoryTranslatorImpl() {
341 return flowNodeConnectorInventoryTranslatorImpl;
345 public NodeConfigurator getNodeConfigurator() {
346 return nodeConfigurator;
349 public FlowNodeReconciliation getNodeListener() {
354 public boolean isBundleBasedReconciliationEnabled() {
355 return isBundleBasedReconciliationEnabled;
359 public boolean isNodeOwner(final InstanceIdentifier<FlowCapableNode> ident) {
360 return ident != null && deviceMastershipManager.isDeviceMastered(ident.firstKeyOf(Node.class).getId());
364 public void setDeviceMastershipManager(final DeviceMastershipManager deviceMastershipManager) {
365 this.deviceMastershipManager = deviceMastershipManager;
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);
376 case STALE_MARKING_ENABLED:
377 staleMarkingEnabled = Boolean.valueOf(propertyValue);
379 case RECONCILIATION_RETRY_COUNT:
380 reconciliationRetryCount = Integer.parseInt(propertyValue);
382 case BUNDLE_BASED_RECONCILIATION_ENABLED:
383 isBundleBasedReconciliationEnabled = Boolean.valueOf(propertyValue);
386 LOG.warn("No forwarding rule property found.");