2 * Copyright (c) 2014, 2015 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.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.Sets;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import java.util.Collections;
17 import java.util.Objects;
19 import java.util.concurrent.atomic.AtomicLong;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
24 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
25 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
26 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
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.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 {
55 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 Object lockObj = new Object();
61 private Set<InstanceIdentifier<FlowCapableNode>> activeNodes = Collections.emptySet();
63 private final DataBroker dataService;
64 private final SalFlowService salFlowService;
65 private final SalGroupService salGroupService;
66 private final SalMeterService salMeterService;
67 private final SalTableService salTableService;
69 private ForwardingRulesCommiter<Flow> flowListener;
70 private ForwardingRulesCommiter<Group> groupListener;
71 private ForwardingRulesCommiter<Meter> meterListener;
72 private ForwardingRulesCommiter<TableFeatures> tableListener;
73 private FlowNodeReconciliation nodeListener;
75 private final ForwardingRulesManagerConfig forwardingRulesManagerConfig;
76 private FlowNodeConnectorInventoryTranslatorImpl flowNodeConnectorInventoryTranslatorImpl;
77 private final ClusterSingletonServiceProvider clusterSingletonServiceProvider;
78 private final NotificationProviderService notificationService;
79 private DeviceMastershipManager deviceMastershipManager;
81 public ForwardingRulesManagerImpl(final DataBroker dataBroker,
82 final RpcConsumerRegistry rpcRegistry,
83 final ForwardingRulesManagerConfig config,
84 final ClusterSingletonServiceProvider clusterSingletonService,
85 final NotificationProviderService notificationService) {
86 this.dataService = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
87 this.forwardingRulesManagerConfig = Preconditions.checkNotNull(config, "Configuration for FRM cannot be null");
88 this.clusterSingletonServiceProvider = Preconditions.checkNotNull(clusterSingletonService,
89 "ClusterSingletonService provider can not be null");
90 this.notificationService = Preconditions.checkNotNull(notificationService, "Notification publisher service is" +
93 Preconditions.checkArgument(rpcRegistry != null, "RpcConsumerRegistry can not be null !");
95 this.salFlowService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalFlowService.class),
96 "RPC SalFlowService not found.");
97 this.salGroupService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalGroupService.class),
98 "RPC SalGroupService not found.");
99 this.salMeterService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalMeterService.class),
100 "RPC SalMeterService not found.");
101 this.salTableService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalTableService.class),
102 "RPC SalTableService not found.");
106 public void start() {
107 this.deviceMastershipManager = new DeviceMastershipManager(clusterSingletonServiceProvider,
108 notificationService);
109 this.flowListener = new FlowForwarder(this, dataService);
110 this.groupListener = new GroupForwarder(this, dataService);
111 this.meterListener = new MeterForwarder(this, dataService);
112 this.tableListener = new TableForwarder(this, dataService);
113 this.nodeListener = new FlowNodeReconciliationImpl(this, dataService);
114 flowNodeConnectorInventoryTranslatorImpl =
115 new FlowNodeConnectorInventoryTranslatorImpl(this,dataService);
116 LOG.info("ForwardingRulesManager has started successfully.");
120 public void close() throws Exception {
121 if (this.flowListener != null) {
122 this.flowListener.close();
123 this.flowListener = null;
125 if (this.groupListener != null) {
126 this.groupListener.close();
127 this.groupListener = null;
129 if (this.meterListener != null) {
130 this.meterListener.close();
131 this.meterListener = null;
133 if (this.tableListener != null) {
134 this.tableListener.close();
135 this.tableListener = null;
137 if (this.nodeListener != null) {
138 this.nodeListener.close();
139 this.nodeListener = null;
141 if (deviceMastershipManager != null) {
142 deviceMastershipManager.close();
147 public ReadOnlyTransaction getReadTranaction() {
148 return dataService.newReadOnlyTransaction();
152 public String getNewTransactionId() {
153 return "DOM-" + txNum.getAndIncrement();
157 public boolean isNodeActive(InstanceIdentifier<FlowCapableNode> ident) {
158 return activeNodes.contains(ident);
162 public boolean checkNodeInOperationalDataStore(InstanceIdentifier<FlowCapableNode> ident) {
163 boolean result = false;
164 InstanceIdentifier<Node> nodeIid = ident.firstIdentifierOf(Node.class);
165 final ReadOnlyTransaction transaction = dataService.newReadOnlyTransaction();
166 Optional<Node> optionalDataObject;
167 CheckedFuture<Optional<Node>, ReadFailedException> future = transaction.read(LogicalDatastoreType.OPERATIONAL, nodeIid);
169 optionalDataObject = future.checkedGet();
170 if (optionalDataObject.isPresent()) {
173 LOG.debug("{}: Failed to read {}",
174 Thread.currentThread().getStackTrace()[1], nodeIid);
176 } catch (ReadFailedException e) {
177 LOG.warn("Failed to read {} ", nodeIid, e);
185 public void registrateNewNode(InstanceIdentifier<FlowCapableNode> ident) {
186 if (!activeNodes.contains(ident)) {
187 synchronized (lockObj) {
188 if (!activeNodes.contains(ident)) {
189 Set<InstanceIdentifier<FlowCapableNode>> set =
190 Sets.newHashSet(activeNodes);
192 activeNodes = Collections.unmodifiableSet(set);
193 deviceMastershipManager.onDeviceConnected(ident.firstKeyOf(Node.class).getId());
200 public void unregistrateNode(InstanceIdentifier<FlowCapableNode> ident) {
201 if (activeNodes.contains(ident)) {
202 synchronized (lockObj) {
203 if (activeNodes.contains(ident)) {
204 Set<InstanceIdentifier<FlowCapableNode>> set =
205 Sets.newHashSet(activeNodes);
207 activeNodes = Collections.unmodifiableSet(set);
208 deviceMastershipManager.onDeviceDisconnected(ident.firstKeyOf(Node.class).getId());
215 public SalFlowService getSalFlowService() {
216 return salFlowService;
220 public SalGroupService getSalGroupService() {
221 return salGroupService;
225 public SalMeterService getSalMeterService() {
226 return salMeterService;
230 public SalTableService getSalTableService() {
231 return salTableService;
235 public ForwardingRulesCommiter<Flow> getFlowCommiter() {
240 public ForwardingRulesCommiter<Group> getGroupCommiter() {
241 return groupListener;
245 public ForwardingRulesCommiter<Meter> getMeterCommiter() {
246 return meterListener;
250 public ForwardingRulesCommiter<TableFeatures> getTableFeaturesCommiter() {
251 return tableListener;
255 public FlowNodeReconciliation getFlowNodeReconciliation() {
260 public ForwardingRulesManagerConfig getConfiguration() {
261 return forwardingRulesManagerConfig;
265 public FlowNodeConnectorInventoryTranslatorImpl getFlowNodeConnectorInventoryTranslatorImpl() {
266 return flowNodeConnectorInventoryTranslatorImpl;
270 public boolean isNodeOwner(InstanceIdentifier<FlowCapableNode> ident) {
271 return Objects.nonNull(ident) && deviceMastershipManager.isDeviceMastered(ident.firstKeyOf(Node.class).getId());
275 public void setDeviceMastershipManager(final DeviceMastershipManager deviceMastershipManager) {
276 this.deviceMastershipManager = deviceMastershipManager;