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;
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.md.sal.common.api.data.ReadFailedException;
23 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
24 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
25 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
26 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationService;
27 import org.opendaylight.openflowplugin.applications.frm.FlowNodeReconciliation;
28 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesCommiter;
29 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
30 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesProperty;
31 import org.opendaylight.openflowplugin.applications.reconciliation.NotificationRegistration;
32 import org.opendaylight.openflowplugin.applications.reconciliation.ReconciliationManager;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.SalMeterService;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.SalBundleService;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.forwardingrules.manager.config.rev160511.ForwardingRulesManagerConfig;
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 ForwardingRulesCommiter<Flow> flowListener;
77 private ForwardingRulesCommiter<Group> groupListener;
78 private ForwardingRulesCommiter<Meter> meterListener;
79 private ForwardingRulesCommiter<TableFeatures> tableListener;
80 private FlowNodeReconciliation nodeListener;
81 private NotificationRegistration reconciliationNotificationRegistration;
82 private FlowNodeConnectorInventoryTranslatorImpl flowNodeConnectorInventoryTranslatorImpl;
83 private DeviceMastershipManager deviceMastershipManager;
84 private final ReconciliationManager reconciliationManager;
86 private boolean disableReconciliation;
87 private boolean staleMarkingEnabled;
88 private int reconciliationRetryCount;
89 private boolean isBundleBasedReconciliationEnabled;
91 public ForwardingRulesManagerImpl(final DataBroker dataBroker, final RpcConsumerRegistry rpcRegistry,
92 final ForwardingRulesManagerConfig config, final ClusterSingletonServiceProvider clusterSingletonService,
93 final NotificationProviderService notificationService, final ConfigurationService configurationService,
94 final ReconciliationManager reconciliationManager) {
95 disableReconciliation = config.isDisableReconciliation();
96 staleMarkingEnabled = config.isStaleMarkingEnabled();
97 reconciliationRetryCount = config.getReconciliationRetryCount();
98 isBundleBasedReconciliationEnabled = config.isBundleBasedReconciliationEnabled();
99 this.configurationServiceRegistration = configurationService.registerListener(this);
100 this.dataService = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
101 this.clusterSingletonServiceProvider = Preconditions.checkNotNull(clusterSingletonService,
102 "ClusterSingletonService provider can not be null");
103 this.notificationService = Preconditions.checkNotNull(notificationService,
104 "Notification publisher configurationService is" + " not available");
105 this.reconciliationManager = reconciliationManager;
107 Preconditions.checkArgument(rpcRegistry != null, "RpcConsumerRegistry can not be null !");
109 this.salFlowService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalFlowService.class),
110 "RPC SalFlowService not found.");
111 this.salGroupService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalGroupService.class),
112 "RPC SalGroupService not found.");
113 this.salMeterService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalMeterService.class),
114 "RPC SalMeterService not found.");
115 this.salTableService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalTableService.class),
116 "RPC SalTableService not found.");
117 this.salBundleService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalBundleService.class),
118 "RPC SalBundlService not found.");
122 public void start() {
123 this.nodeListener = new FlowNodeReconciliationImpl(this, dataService, SERVICE_NAME, FRM_RECONCILIATION_PRIORITY,
124 ResultState.DONOTHING);
125 if (this.isReconciliationDisabled()) {
126 LOG.debug("Reconciliation is disabled by user");
128 this.reconciliationNotificationRegistration = reconciliationManager.registerService(this.nodeListener);
129 LOG.debug("Reconciliation is enabled by user and successfully registered to the reconciliation framework");
131 this.deviceMastershipManager = new DeviceMastershipManager(clusterSingletonServiceProvider, notificationService,
132 this.nodeListener, dataService);
133 flowNodeConnectorInventoryTranslatorImpl = new FlowNodeConnectorInventoryTranslatorImpl(this, dataService);
135 this.flowListener = new FlowForwarder(this, dataService);
136 this.groupListener = new GroupForwarder(this, dataService);
137 this.meterListener = new MeterForwarder(this, dataService);
138 this.tableListener = new TableForwarder(this, dataService);
139 LOG.info("ForwardingRulesManager has started successfully.");
143 public void close() throws Exception {
144 configurationServiceRegistration.close();
146 if (this.flowListener != null) {
147 this.flowListener.close();
148 this.flowListener = null;
150 if (this.groupListener != null) {
151 this.groupListener.close();
152 this.groupListener = null;
154 if (this.meterListener != null) {
155 this.meterListener.close();
156 this.meterListener = null;
158 if (this.tableListener != null) {
159 this.tableListener.close();
160 this.tableListener = null;
162 if (this.nodeListener != null) {
163 this.nodeListener.close();
164 this.nodeListener = null;
166 if (deviceMastershipManager != null) {
167 deviceMastershipManager.close();
169 if (this.reconciliationNotificationRegistration != null) {
170 this.reconciliationNotificationRegistration.close();
171 this.reconciliationNotificationRegistration = null;
176 public ReadOnlyTransaction getReadTranaction() {
177 return dataService.newReadOnlyTransaction();
181 public String getNewTransactionId() {
182 return "DOM-" + txNum.getAndIncrement();
186 public boolean isNodeActive(InstanceIdentifier<FlowCapableNode> ident) {
187 return deviceMastershipManager.isNodeActive(ident.firstKeyOf(Node.class).getId());
191 public boolean checkNodeInOperationalDataStore(InstanceIdentifier<FlowCapableNode> ident) {
192 boolean result = false;
193 InstanceIdentifier<Node> nodeIid = ident.firstIdentifierOf(Node.class);
194 final ReadOnlyTransaction transaction = dataService.newReadOnlyTransaction();
195 CheckedFuture<com.google.common.base.Optional<Node>, ReadFailedException> future = transaction
196 .read(LogicalDatastoreType.OPERATIONAL, nodeIid);
198 com.google.common.base.Optional<Node> optionalDataObject = future.checkedGet();
199 if (optionalDataObject.isPresent()) {
202 LOG.debug("{}: Failed to read {}", Thread.currentThread().getStackTrace()[1], nodeIid);
204 } catch (ReadFailedException e) {
205 LOG.warn("Failed to read {} ", nodeIid, e);
213 public SalFlowService getSalFlowService() {
214 return salFlowService;
218 public SalGroupService getSalGroupService() {
219 return salGroupService;
223 public SalMeterService getSalMeterService() {
224 return salMeterService;
228 public SalTableService getSalTableService() {
229 return salTableService;
233 public SalBundleService getSalBundleService() {
234 return salBundleService;
238 public ForwardingRulesCommiter<Flow> getFlowCommiter() {
243 public ForwardingRulesCommiter<Group> getGroupCommiter() {
244 return groupListener;
248 public ForwardingRulesCommiter<Meter> getMeterCommiter() {
249 return meterListener;
253 public ForwardingRulesCommiter<TableFeatures> getTableFeaturesCommiter() {
254 return tableListener;
258 public boolean isReconciliationDisabled() {
259 return disableReconciliation;
263 public boolean isStaleMarkingEnabled() {
264 return staleMarkingEnabled;
268 public int getReconciliationRetryCount() {
269 return reconciliationRetryCount;
273 public FlowNodeConnectorInventoryTranslatorImpl getFlowNodeConnectorInventoryTranslatorImpl() {
274 return flowNodeConnectorInventoryTranslatorImpl;
278 public boolean isBundleBasedReconciliationEnabled() {
279 return isBundleBasedReconciliationEnabled;
283 public boolean isNodeOwner(InstanceIdentifier<FlowCapableNode> ident) {
284 return Objects.nonNull(ident) && deviceMastershipManager.isDeviceMastered(ident.firstKeyOf(Node.class).getId());
288 public void setDeviceMastershipManager(final DeviceMastershipManager deviceMastershipManager) {
289 this.deviceMastershipManager = deviceMastershipManager;
293 public void onPropertyChanged(@Nonnull final String propertyName, @Nonnull final String propertyValue) {
294 Optional.ofNullable(ForwardingRulesProperty.forValue(propertyName)).ifPresent(forwardingRulesProperty -> {
295 switch (forwardingRulesProperty) {
296 case DISABLE_RECONCILIATION:
297 disableReconciliation = Boolean.valueOf(propertyValue);
299 case STALE_MARKING_ENABLED:
300 staleMarkingEnabled = Boolean.valueOf(propertyValue);
302 case RECONCILIATION_RETRY_COUNT:
303 reconciliationRetryCount = Integer.valueOf(propertyValue);
305 case BUNDLE_BASED_RECONCILIATION_ENABLED:
306 isBundleBasedReconciliationEnabled = Boolean.valueOf(propertyValue);
309 LOG.warn("No forwarding rule property found.");