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 org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
23 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
24 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
25 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationService;
26 import org.opendaylight.openflowplugin.applications.frm.FlowNodeReconciliation;
27 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesCommiter;
28 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
29 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesProperty;
30 import org.opendaylight.openflowplugin.applications.reconciliation.NotificationRegistration;
31 import org.opendaylight.openflowplugin.applications.reconciliation.ReconciliationManager;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.SalMeterService;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.SalBundleService;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.forwardingrules.manager.config.rev160511.ForwardingRulesManagerConfig;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.frm.reconciliation.service.rev180227.FrmReconciliationService;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.rf.state.rev170713.ResultState;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.SalTableService;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
51 * forwardingrules-manager org.opendaylight.openflowplugin.applications.frm.impl
54 * Manager and middle point for whole module. It contains ActiveNodeHolder and
55 * provide all RPC services.
58 public class ForwardingRulesManagerImpl implements ForwardingRulesManager {
59 private static final Logger LOG = LoggerFactory.getLogger(ForwardingRulesManagerImpl.class);
61 static final int STARTUP_LOOP_TICK = 500;
62 static final int STARTUP_LOOP_MAX_RETRIES = 8;
63 private static final int FRM_RECONCILIATION_PRIORITY = Integer.getInteger("frm.reconciliation.priority", 0);
64 private static final String SERVICE_NAME = "FRM";
66 private final AtomicLong txNum = new AtomicLong();
67 private final DataBroker dataService;
68 private final SalFlowService salFlowService;
69 private final SalGroupService salGroupService;
70 private final SalMeterService salMeterService;
71 private final SalTableService salTableService;
72 private final ClusterSingletonServiceProvider clusterSingletonServiceProvider;
73 private final NotificationProviderService notificationService;
74 private final SalBundleService salBundleService;
75 private final AutoCloseable configurationServiceRegistration;
76 private final RpcProviderRegistry rpcRegistry;
77 private ForwardingRulesCommiter<Flow> flowListener;
78 private ForwardingRulesCommiter<Group> groupListener;
79 private ForwardingRulesCommiter<Meter> meterListener;
80 private ForwardingRulesCommiter<TableFeatures> tableListener;
81 private FlowNodeReconciliation nodeListener;
82 private NotificationRegistration reconciliationNotificationRegistration;
83 private FlowNodeConnectorInventoryTranslatorImpl flowNodeConnectorInventoryTranslatorImpl;
84 private DeviceMastershipManager deviceMastershipManager;
85 private final ReconciliationManager reconciliationManager;
87 private boolean disableReconciliation;
88 private boolean staleMarkingEnabled;
89 private int reconciliationRetryCount;
90 private boolean isBundleBasedReconciliationEnabled;
92 public ForwardingRulesManagerImpl(final DataBroker dataBroker, final RpcProviderRegistry rpcRegistry,
93 final ForwardingRulesManagerConfig config, final ClusterSingletonServiceProvider clusterSingletonService,
94 final NotificationProviderService notificationService, final ConfigurationService configurationService,
95 final ReconciliationManager reconciliationManager) {
96 disableReconciliation = config.isDisableReconciliation();
97 staleMarkingEnabled = config.isStaleMarkingEnabled();
98 reconciliationRetryCount = config.getReconciliationRetryCount();
99 isBundleBasedReconciliationEnabled = config.isBundleBasedReconciliationEnabled();
100 this.configurationServiceRegistration = configurationService.registerListener(this);
101 this.dataService = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
102 this.clusterSingletonServiceProvider = Preconditions.checkNotNull(clusterSingletonService,
103 "ClusterSingletonService provider can not be null");
104 this.notificationService = Preconditions.checkNotNull(notificationService,
105 "Notification publisher configurationService is" + " not available");
106 this.reconciliationManager = reconciliationManager;
107 this.rpcRegistry = rpcRegistry;
109 Preconditions.checkArgument(rpcRegistry != null, "RpcProviderRegistry can not be null !");
111 this.salFlowService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalFlowService.class),
112 "RPC SalFlowService not found.");
113 this.salGroupService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalGroupService.class),
114 "RPC SalGroupService not found.");
115 this.salMeterService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalMeterService.class),
116 "RPC SalMeterService not found.");
117 this.salTableService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalTableService.class),
118 "RPC SalTableService not found.");
119 this.salBundleService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalBundleService.class),
120 "RPC SalBundlService not found.");
124 public void start() {
125 this.nodeListener = new FlowNodeReconciliationImpl(this, dataService, SERVICE_NAME, FRM_RECONCILIATION_PRIORITY,
126 ResultState.DONOTHING);
127 if (this.isReconciliationDisabled()) {
128 LOG.debug("Reconciliation is disabled by user");
130 this.reconciliationNotificationRegistration = reconciliationManager.registerService(this.nodeListener);
131 LOG.debug("Reconciliation is enabled by user and successfully registered to the reconciliation framework");
133 this.deviceMastershipManager = new DeviceMastershipManager(clusterSingletonServiceProvider, notificationService,
134 this.nodeListener, dataService);
135 this.deviceMastershipManager.setRoutedRpcReg(rpcRegistry.addRoutedRpcImplementation(
136 FrmReconciliationService.class, new FrmReconciliationServiceImpl(this)));
137 flowNodeConnectorInventoryTranslatorImpl = new FlowNodeConnectorInventoryTranslatorImpl(dataService);
139 this.flowListener = new FlowForwarder(this, dataService);
140 this.groupListener = new GroupForwarder(this, dataService);
141 this.meterListener = new MeterForwarder(this, dataService);
142 this.tableListener = new TableForwarder(this, dataService);
143 LOG.info("ForwardingRulesManager has started successfully.");
147 public void close() throws Exception {
148 configurationServiceRegistration.close();
150 if (this.flowListener != null) {
151 this.flowListener.close();
152 this.flowListener = null;
154 if (this.groupListener != null) {
155 this.groupListener.close();
156 this.groupListener = null;
158 if (this.meterListener != null) {
159 this.meterListener.close();
160 this.meterListener = null;
162 if (this.tableListener != null) {
163 this.tableListener.close();
164 this.tableListener = null;
166 if (this.nodeListener != null) {
167 this.nodeListener.close();
168 this.nodeListener = null;
170 if (deviceMastershipManager != null) {
171 deviceMastershipManager.close();
173 if (this.reconciliationNotificationRegistration != null) {
174 this.reconciliationNotificationRegistration.close();
175 this.reconciliationNotificationRegistration = null;
180 public ReadOnlyTransaction getReadTranaction() {
181 return dataService.newReadOnlyTransaction();
185 public String getNewTransactionId() {
186 return "DOM-" + txNum.getAndIncrement();
190 public boolean isNodeActive(InstanceIdentifier<FlowCapableNode> ident) {
191 return deviceMastershipManager.isNodeActive(ident.firstKeyOf(Node.class).getId());
195 public boolean checkNodeInOperationalDataStore(InstanceIdentifier<FlowCapableNode> ident) {
196 boolean result = false;
197 InstanceIdentifier<Node> nodeIid = ident.firstIdentifierOf(Node.class);
198 final ReadOnlyTransaction transaction = dataService.newReadOnlyTransaction();
199 ListenableFuture<com.google.common.base.Optional<Node>> future = transaction
200 .read(LogicalDatastoreType.OPERATIONAL, nodeIid);
202 com.google.common.base.Optional<Node> optionalDataObject = future.get();
203 if (optionalDataObject.isPresent()) {
206 LOG.debug("{}: Failed to read {}", Thread.currentThread().getStackTrace()[1], nodeIid);
208 } catch (ExecutionException | InterruptedException e) {
209 LOG.warn("Failed to read {} ", nodeIid, e);
217 public SalFlowService getSalFlowService() {
218 return salFlowService;
222 public SalGroupService getSalGroupService() {
223 return salGroupService;
227 public SalMeterService getSalMeterService() {
228 return salMeterService;
232 public SalTableService getSalTableService() {
233 return salTableService;
237 public SalBundleService getSalBundleService() {
238 return salBundleService;
242 public ForwardingRulesCommiter<Flow> getFlowCommiter() {
247 public ForwardingRulesCommiter<Group> getGroupCommiter() {
248 return groupListener;
252 public ForwardingRulesCommiter<Meter> getMeterCommiter() {
253 return meterListener;
257 public ForwardingRulesCommiter<TableFeatures> getTableFeaturesCommiter() {
258 return tableListener;
262 public boolean isReconciliationDisabled() {
263 return disableReconciliation;
267 public boolean isStaleMarkingEnabled() {
268 return staleMarkingEnabled;
272 public int getReconciliationRetryCount() {
273 return reconciliationRetryCount;
277 public FlowNodeConnectorInventoryTranslatorImpl getFlowNodeConnectorInventoryTranslatorImpl() {
278 return flowNodeConnectorInventoryTranslatorImpl;
281 public FlowNodeReconciliation getNodeListener() {
286 public boolean isBundleBasedReconciliationEnabled() {
287 return isBundleBasedReconciliationEnabled;
291 public boolean isNodeOwner(InstanceIdentifier<FlowCapableNode> ident) {
292 return Objects.nonNull(ident) && deviceMastershipManager.isDeviceMastered(ident.firstKeyOf(Node.class).getId());
296 public void setDeviceMastershipManager(final DeviceMastershipManager deviceMastershipManager) {
297 this.deviceMastershipManager = deviceMastershipManager;
301 public void onPropertyChanged(@Nonnull final String propertyName, @Nonnull final String propertyValue) {
302 Optional.ofNullable(ForwardingRulesProperty.forValue(propertyName)).ifPresent(forwardingRulesProperty -> {
303 switch (forwardingRulesProperty) {
304 case DISABLE_RECONCILIATION:
305 disableReconciliation = Boolean.valueOf(propertyValue);
307 case STALE_MARKING_ENABLED:
308 staleMarkingEnabled = Boolean.valueOf(propertyValue);
310 case RECONCILIATION_RETRY_COUNT:
311 reconciliationRetryCount = Integer.parseInt(propertyValue);
313 case BUNDLE_BASED_RECONCILIATION_ENABLED:
314 isBundleBasedReconciliationEnabled = Boolean.valueOf(propertyValue);
317 LOG.warn("No forwarding rule property found.");