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.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.SalMeterService;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.forwardingrules.manager.config.rev160511.ForwardingRulesManagerConfig;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.SalTableService;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * forwardingrules-manager
47 * org.opendaylight.openflowplugin.applications.frm.impl
49 * Manager and middle point for whole module.
50 * It contains ActiveNodeHolder and provide all RPC services.
53 public class ForwardingRulesManagerImpl implements ForwardingRulesManager {
54 private static final Logger LOG = LoggerFactory.getLogger(ForwardingRulesManagerImpl.class);
56 static final int STARTUP_LOOP_TICK = 500;
57 static final int STARTUP_LOOP_MAX_RETRIES = 8;
59 private final AtomicLong txNum = new AtomicLong();
60 private final DataBroker dataService;
61 private final SalFlowService salFlowService;
62 private final SalGroupService salGroupService;
63 private final SalMeterService salMeterService;
64 private final SalTableService salTableService;
65 private final ClusterSingletonServiceProvider clusterSingletonServiceProvider;
66 private final NotificationProviderService notificationService;
67 private final AutoCloseable configurationServiceRegistration;
68 private ForwardingRulesCommiter<Flow> flowListener;
69 private ForwardingRulesCommiter<Group> groupListener;
70 private ForwardingRulesCommiter<Meter> meterListener;
71 private ForwardingRulesCommiter<TableFeatures> tableListener;
72 private FlowNodeReconciliation nodeListener;
73 private FlowNodeConnectorInventoryTranslatorImpl flowNodeConnectorInventoryTranslatorImpl;
74 private DeviceMastershipManager deviceMastershipManager;
75 private boolean disableReconciliation;
76 private boolean staleMarkingEnabled;
77 private int reconciliationRetryCount;
79 public ForwardingRulesManagerImpl(final DataBroker dataBroker,
80 final RpcConsumerRegistry rpcRegistry,
81 final ForwardingRulesManagerConfig config,
82 final ClusterSingletonServiceProvider clusterSingletonService,
83 final NotificationProviderService notificationService,
84 final ConfigurationService configurationService) {
85 disableReconciliation = config.isDisableReconciliation();
86 staleMarkingEnabled = config.isStaleMarkingEnabled();
87 reconciliationRetryCount = config.getReconciliationRetryCount();
89 this.configurationServiceRegistration = configurationService.registerListener(this);
90 this.dataService = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
91 this.clusterSingletonServiceProvider = Preconditions.checkNotNull(clusterSingletonService,
92 "ClusterSingletonService provider can not be null");
93 this.notificationService = Preconditions.checkNotNull(notificationService, "Notification publisher configurationService is" +
96 Preconditions.checkArgument(rpcRegistry != null, "RpcConsumerRegistry can not be null !");
98 this.salFlowService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalFlowService.class),
99 "RPC SalFlowService not found.");
100 this.salGroupService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalGroupService.class),
101 "RPC SalGroupService not found.");
102 this.salMeterService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalMeterService.class),
103 "RPC SalMeterService not found.");
104 this.salTableService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalTableService.class),
105 "RPC SalTableService not found.");
109 public void start() {
110 this.nodeListener = new FlowNodeReconciliationImpl(this, dataService);
111 this.deviceMastershipManager = new DeviceMastershipManager(clusterSingletonServiceProvider,
115 flowNodeConnectorInventoryTranslatorImpl = new FlowNodeConnectorInventoryTranslatorImpl(this,dataService);
117 this.flowListener = new FlowForwarder(this, dataService);
118 this.groupListener = new GroupForwarder(this, dataService);
119 this.meterListener = new MeterForwarder(this, dataService);
120 this.tableListener = new TableForwarder(this, dataService);
121 LOG.info("ForwardingRulesManager has started successfully.");
125 public void close() throws Exception {
126 configurationServiceRegistration.close();
128 if (this.flowListener != null) {
129 this.flowListener.close();
130 this.flowListener = null;
132 if (this.groupListener != null) {
133 this.groupListener.close();
134 this.groupListener = null;
136 if (this.meterListener != null) {
137 this.meterListener.close();
138 this.meterListener = null;
140 if (this.tableListener != null) {
141 this.tableListener.close();
142 this.tableListener = null;
144 if (this.nodeListener != null) {
145 this.nodeListener.close();
146 this.nodeListener = null;
148 if (deviceMastershipManager != null) {
149 deviceMastershipManager.close();
154 public ReadOnlyTransaction getReadTranaction() {
155 return dataService.newReadOnlyTransaction();
159 public String getNewTransactionId() {
160 return "DOM-" + txNum.getAndIncrement();
164 public boolean isNodeActive(InstanceIdentifier<FlowCapableNode> ident) {
165 return deviceMastershipManager.isNodeActive(ident.firstKeyOf(Node.class).getId());
169 public boolean checkNodeInOperationalDataStore(InstanceIdentifier<FlowCapableNode> ident) {
170 boolean result = false;
171 InstanceIdentifier<Node> nodeIid = ident.firstIdentifierOf(Node.class);
172 final ReadOnlyTransaction transaction = dataService.newReadOnlyTransaction();
173 CheckedFuture<com.google.common.base.Optional<Node>, ReadFailedException> future = transaction.read(LogicalDatastoreType.OPERATIONAL, nodeIid);
175 com.google.common.base.Optional<Node> optionalDataObject = future.checkedGet();
176 if (optionalDataObject.isPresent()) {
179 LOG.debug("{}: Failed to read {}",
180 Thread.currentThread().getStackTrace()[1], nodeIid);
182 } catch (ReadFailedException e) {
183 LOG.warn("Failed to read {} ", nodeIid, e);
191 public SalFlowService getSalFlowService() {
192 return salFlowService;
196 public SalGroupService getSalGroupService() {
197 return salGroupService;
201 public SalMeterService getSalMeterService() {
202 return salMeterService;
206 public SalTableService getSalTableService() {
207 return salTableService;
211 public ForwardingRulesCommiter<Flow> getFlowCommiter() {
216 public ForwardingRulesCommiter<Group> getGroupCommiter() {
217 return groupListener;
221 public ForwardingRulesCommiter<Meter> getMeterCommiter() {
222 return meterListener;
226 public ForwardingRulesCommiter<TableFeatures> getTableFeaturesCommiter() {
227 return tableListener;
231 public boolean isReconciliationDisabled() {
232 return disableReconciliation;
236 public boolean isStaleMarkingEnabled() {
237 return staleMarkingEnabled;
241 public int getReconciliationRetryCount() {
242 return reconciliationRetryCount;
246 public FlowNodeConnectorInventoryTranslatorImpl getFlowNodeConnectorInventoryTranslatorImpl() {
247 return flowNodeConnectorInventoryTranslatorImpl;
251 public boolean isNodeOwner(InstanceIdentifier<FlowCapableNode> ident) {
252 return Objects.nonNull(ident) && deviceMastershipManager.isDeviceMastered(ident.firstKeyOf(Node.class).getId());
256 public void setDeviceMastershipManager(final DeviceMastershipManager deviceMastershipManager) {
257 this.deviceMastershipManager = deviceMastershipManager;
261 public void onPropertyChanged(@Nonnull final String propertyName, @Nonnull final String propertyValue) {
262 Optional.ofNullable(ForwardingRulesProperty.forValue(propertyName)).ifPresent(forwardingRulesProperty -> {
263 switch (forwardingRulesProperty) {
264 case DISABLE_RECONCILIATION:
265 disableReconciliation = Boolean.valueOf(propertyValue);
267 case STALE_MARKING_ENABLED:
268 staleMarkingEnabled = Boolean.valueOf(propertyValue);
270 case RECONCILIATION_RETRY_COUNT:
271 reconciliationRetryCount = Integer.valueOf(propertyValue);