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.CheckedFuture;
14 import java.util.Objects;
15 import java.util.Optional;
16 import java.util.concurrent.atomic.AtomicLong;
17 import javax.annotation.Nonnull;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
22 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
23 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
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.rf.state.rev170713.ResultState;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.SalTableService;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 * forwardingrules-manager org.opendaylight.openflowplugin.applications.frm.impl
53 * Manager and middle point for whole module. It contains ActiveNodeHolder and
54 * provide all RPC services.
57 public class ForwardingRulesManagerImpl implements ForwardingRulesManager {
58 private static final Logger LOG = LoggerFactory.getLogger(ForwardingRulesManagerImpl.class);
60 static final int STARTUP_LOOP_TICK = 500;
61 static final int STARTUP_LOOP_MAX_RETRIES = 8;
62 private static final int FRM_RECONCILIATION_PRIORITY = Integer.getInteger("frm.reconciliation.priority", 0);
63 private static final String SERVICE_NAME = "FRM";
65 private final AtomicLong txNum = new AtomicLong();
66 private final DataBroker dataService;
67 private final SalFlowService salFlowService;
68 private final SalGroupService salGroupService;
69 private final SalMeterService salMeterService;
70 private final SalTableService salTableService;
71 private final ClusterSingletonServiceProvider clusterSingletonServiceProvider;
72 private final NotificationProviderService notificationService;
73 private final SalBundleService salBundleService;
74 private final AutoCloseable configurationServiceRegistration;
75 private ForwardingRulesCommiter<Flow> flowListener;
76 private ForwardingRulesCommiter<Group> groupListener;
77 private ForwardingRulesCommiter<Meter> meterListener;
78 private ForwardingRulesCommiter<TableFeatures> tableListener;
79 private FlowNodeReconciliation nodeListener;
80 private NotificationRegistration reconciliationNotificationRegistration;
81 private FlowNodeConnectorInventoryTranslatorImpl flowNodeConnectorInventoryTranslatorImpl;
82 private DeviceMastershipManager deviceMastershipManager;
83 private final ReconciliationManager reconciliationManager;
85 private boolean disableReconciliation;
86 private boolean staleMarkingEnabled;
87 private int reconciliationRetryCount;
88 private boolean isBundleBasedReconciliationEnabled;
90 public ForwardingRulesManagerImpl(final DataBroker dataBroker, final RpcConsumerRegistry rpcRegistry,
91 final ForwardingRulesManagerConfig config, final ClusterSingletonServiceProvider clusterSingletonService,
92 final NotificationProviderService notificationService, final ConfigurationService configurationService,
93 final ReconciliationManager reconciliationManager) {
94 disableReconciliation = config.isDisableReconciliation();
95 staleMarkingEnabled = config.isStaleMarkingEnabled();
96 reconciliationRetryCount = config.getReconciliationRetryCount();
97 isBundleBasedReconciliationEnabled = config.isBundleBasedReconciliationEnabled();
98 this.configurationServiceRegistration = configurationService.registerListener(this);
99 this.dataService = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
100 this.clusterSingletonServiceProvider = Preconditions.checkNotNull(clusterSingletonService,
101 "ClusterSingletonService provider can not be null");
102 this.notificationService = Preconditions.checkNotNull(notificationService,
103 "Notification publisher configurationService is" + " not available");
104 this.reconciliationManager = reconciliationManager;
106 Preconditions.checkArgument(rpcRegistry != null, "RpcConsumerRegistry can not be null !");
108 this.salFlowService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalFlowService.class),
109 "RPC SalFlowService not found.");
110 this.salGroupService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalGroupService.class),
111 "RPC SalGroupService not found.");
112 this.salMeterService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalMeterService.class),
113 "RPC SalMeterService not found.");
114 this.salTableService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalTableService.class),
115 "RPC SalTableService not found.");
116 this.salBundleService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalBundleService.class),
117 "RPC SalBundlService not found.");
121 public void start() {
122 this.nodeListener = new FlowNodeReconciliationImpl(this, dataService, SERVICE_NAME, FRM_RECONCILIATION_PRIORITY,
123 ResultState.DONOTHING);
124 if (this.isReconciliationDisabled()) {
125 LOG.debug("Reconciliation is disabled by user");
127 this.reconciliationNotificationRegistration = reconciliationManager.registerService(this.nodeListener);
128 LOG.debug("Reconciliation is enabled by user and successfully registered to the reconciliation framework");
130 this.deviceMastershipManager = new DeviceMastershipManager(clusterSingletonServiceProvider, notificationService,
131 this.nodeListener, dataService);
132 flowNodeConnectorInventoryTranslatorImpl = new FlowNodeConnectorInventoryTranslatorImpl(dataService);
134 this.flowListener = new FlowForwarder(this, dataService);
135 this.groupListener = new GroupForwarder(this, dataService);
136 this.meterListener = new MeterForwarder(this, dataService);
137 this.tableListener = new TableForwarder(this, dataService);
138 LOG.info("ForwardingRulesManager has started successfully.");
142 public void close() throws Exception {
143 configurationServiceRegistration.close();
145 if (this.flowListener != null) {
146 this.flowListener.close();
147 this.flowListener = null;
149 if (this.groupListener != null) {
150 this.groupListener.close();
151 this.groupListener = null;
153 if (this.meterListener != null) {
154 this.meterListener.close();
155 this.meterListener = null;
157 if (this.tableListener != null) {
158 this.tableListener.close();
159 this.tableListener = null;
161 if (this.nodeListener != null) {
162 this.nodeListener.close();
163 this.nodeListener = null;
165 if (deviceMastershipManager != null) {
166 deviceMastershipManager.close();
168 if (this.reconciliationNotificationRegistration != null) {
169 this.reconciliationNotificationRegistration.close();
170 this.reconciliationNotificationRegistration = null;
175 public ReadOnlyTransaction getReadTranaction() {
176 return dataService.newReadOnlyTransaction();
180 public String getNewTransactionId() {
181 return "DOM-" + txNum.getAndIncrement();
185 public boolean isNodeActive(InstanceIdentifier<FlowCapableNode> ident) {
186 return deviceMastershipManager.isNodeActive(ident.firstKeyOf(Node.class).getId());
190 public boolean checkNodeInOperationalDataStore(InstanceIdentifier<FlowCapableNode> ident) {
191 boolean result = false;
192 InstanceIdentifier<Node> nodeIid = ident.firstIdentifierOf(Node.class);
193 final ReadOnlyTransaction transaction = dataService.newReadOnlyTransaction();
194 CheckedFuture<com.google.common.base.Optional<Node>, ReadFailedException> future = transaction
195 .read(LogicalDatastoreType.OPERATIONAL, nodeIid);
197 com.google.common.base.Optional<Node> optionalDataObject = future.checkedGet();
198 if (optionalDataObject.isPresent()) {
201 LOG.debug("{}: Failed to read {}", Thread.currentThread().getStackTrace()[1], nodeIid);
203 } catch (ReadFailedException e) {
204 LOG.warn("Failed to read {} ", nodeIid, e);
212 public SalFlowService getSalFlowService() {
213 return salFlowService;
217 public SalGroupService getSalGroupService() {
218 return salGroupService;
222 public SalMeterService getSalMeterService() {
223 return salMeterService;
227 public SalTableService getSalTableService() {
228 return salTableService;
232 public SalBundleService getSalBundleService() {
233 return salBundleService;
237 public ForwardingRulesCommiter<Flow> getFlowCommiter() {
242 public ForwardingRulesCommiter<Group> getGroupCommiter() {
243 return groupListener;
247 public ForwardingRulesCommiter<Meter> getMeterCommiter() {
248 return meterListener;
252 public ForwardingRulesCommiter<TableFeatures> getTableFeaturesCommiter() {
253 return tableListener;
257 public boolean isReconciliationDisabled() {
258 return disableReconciliation;
262 public boolean isStaleMarkingEnabled() {
263 return staleMarkingEnabled;
267 public int getReconciliationRetryCount() {
268 return reconciliationRetryCount;
272 public FlowNodeConnectorInventoryTranslatorImpl getFlowNodeConnectorInventoryTranslatorImpl() {
273 return flowNodeConnectorInventoryTranslatorImpl;
277 public boolean isBundleBasedReconciliationEnabled() {
278 return isBundleBasedReconciliationEnabled;
282 public boolean isNodeOwner(InstanceIdentifier<FlowCapableNode> ident) {
283 return Objects.nonNull(ident) && deviceMastershipManager.isDeviceMastered(ident.firstKeyOf(Node.class).getId());
287 public void setDeviceMastershipManager(final DeviceMastershipManager deviceMastershipManager) {
288 this.deviceMastershipManager = deviceMastershipManager;
292 public void onPropertyChanged(@Nonnull final String propertyName, @Nonnull final String propertyValue) {
293 Optional.ofNullable(ForwardingRulesProperty.forValue(propertyName)).ifPresent(forwardingRulesProperty -> {
294 switch (forwardingRulesProperty) {
295 case DISABLE_RECONCILIATION:
296 disableReconciliation = Boolean.valueOf(propertyValue);
298 case STALE_MARKING_ENABLED:
299 staleMarkingEnabled = Boolean.valueOf(propertyValue);
301 case RECONCILIATION_RETRY_COUNT:
302 reconciliationRetryCount = Integer.parseInt(propertyValue);
304 case BUNDLE_BASED_RECONCILIATION_ENABLED:
305 isBundleBasedReconciliationEnabled = Boolean.valueOf(propertyValue);
308 LOG.warn("No forwarding rule property found.");