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.RpcConsumerRegistry;
25 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
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.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.SalMeterService;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.forwardingrules.manager.config.rev160511.ForwardingRulesManagerConfig;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.SalTableService;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * forwardingrules-manager
46 * org.opendaylight.openflowplugin.applications.frm.impl
48 * Manager and middle point for whole module.
49 * It contains ActiveNodeHolder and provide all RPC services.
52 public class ForwardingRulesManagerImpl implements ForwardingRulesManager {
54 private static final Logger LOG = LoggerFactory.getLogger(ForwardingRulesManagerImpl.class);
55 static final int STARTUP_LOOP_TICK = 500;
56 static final int STARTUP_LOOP_MAX_RETRIES = 8;
58 private final AtomicLong txNum = new AtomicLong();
59 private final Object lockObj = new Object();
60 private Set<InstanceIdentifier<FlowCapableNode>> activeNodes = Collections.emptySet();
62 private final DataBroker dataService;
63 private final SalFlowService salFlowService;
64 private final SalGroupService salGroupService;
65 private final SalMeterService salMeterService;
66 private final SalTableService salTableService;
68 private ForwardingRulesCommiter<Flow> flowListener;
69 private ForwardingRulesCommiter<Group> groupListener;
70 private ForwardingRulesCommiter<Meter> meterListener;
71 private ForwardingRulesCommiter<TableFeatures> tableListener;
72 private FlowNodeReconciliation nodeListener;
74 private final ForwardingRulesManagerConfig forwardingRulesManagerConfig;
75 private FlowNodeConnectorInventoryTranslatorImpl flowNodeConnectorInventoryTranslatorImpl;
76 private final ClusterSingletonServiceProvider clusterSingletonServiceProvider;
77 private DeviceMastershipManager deviceMastershipManager;
79 public ForwardingRulesManagerImpl(final DataBroker dataBroker,
80 final RpcConsumerRegistry rpcRegistry,
81 final ForwardingRulesManagerConfig config,
82 final ClusterSingletonServiceProvider clusterSingletonService) {
83 this.dataService = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
84 this.forwardingRulesManagerConfig = Preconditions.checkNotNull(config, "Configuration for FRM cannot be null");
85 this.clusterSingletonServiceProvider = Preconditions.checkNotNull(clusterSingletonService,
86 "ClusterSingletonService provider can not be null");
88 Preconditions.checkArgument(rpcRegistry != null, "RpcConsumerRegistry can not be null !");
90 this.salFlowService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalFlowService.class),
91 "RPC SalFlowService not found.");
92 this.salGroupService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalGroupService.class),
93 "RPC SalGroupService not found.");
94 this.salMeterService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalMeterService.class),
95 "RPC SalMeterService not found.");
96 this.salTableService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalTableService.class),
97 "RPC SalTableService not found.");
101 public void start() {
102 this.deviceMastershipManager = new DeviceMastershipManager(clusterSingletonServiceProvider);
103 this.flowListener = new FlowForwarder(this, dataService);
104 this.groupListener = new GroupForwarder(this, dataService);
105 this.meterListener = new MeterForwarder(this, dataService);
106 this.tableListener = new TableForwarder(this, dataService);
107 this.nodeListener = new FlowNodeReconciliationImpl(this, dataService);
108 flowNodeConnectorInventoryTranslatorImpl =
109 new FlowNodeConnectorInventoryTranslatorImpl(this,dataService);
110 LOG.info("ForwardingRulesManager has started successfully.");
114 public void close() throws Exception {
115 if (this.flowListener != null) {
116 this.flowListener.close();
117 this.flowListener = null;
119 if (this.groupListener != null) {
120 this.groupListener.close();
121 this.groupListener = null;
123 if (this.meterListener != null) {
124 this.meterListener.close();
125 this.meterListener = null;
127 if (this.tableListener != null) {
128 this.tableListener.close();
129 this.tableListener = null;
131 if (this.nodeListener != null) {
132 this.nodeListener.close();
133 this.nodeListener = null;
138 public ReadOnlyTransaction getReadTranaction() {
139 return dataService.newReadOnlyTransaction();
143 public String getNewTransactionId() {
144 return "DOM-" + txNum.getAndIncrement();
148 public boolean isNodeActive(InstanceIdentifier<FlowCapableNode> ident) {
149 return activeNodes.contains(ident);
153 public boolean checkNodeInOperationalDataStore(InstanceIdentifier<FlowCapableNode> ident) {
154 boolean result = false;
155 InstanceIdentifier<Node> nodeIid = ident.firstIdentifierOf(Node.class);
156 final ReadOnlyTransaction transaction = dataService.newReadOnlyTransaction();
157 Optional<Node> optionalDataObject;
158 CheckedFuture<Optional<Node>, ReadFailedException> future = transaction.read(LogicalDatastoreType.OPERATIONAL, nodeIid);
160 optionalDataObject = future.checkedGet();
161 if (optionalDataObject.isPresent()) {
164 LOG.debug("{}: Failed to read {}",
165 Thread.currentThread().getStackTrace()[1], nodeIid);
167 } catch (ReadFailedException e) {
168 LOG.warn("Failed to read {} ", nodeIid, e);
176 public void registrateNewNode(InstanceIdentifier<FlowCapableNode> ident) {
177 if (!activeNodes.contains(ident)) {
178 synchronized (lockObj) {
179 if (!activeNodes.contains(ident)) {
180 Set<InstanceIdentifier<FlowCapableNode>> set =
181 Sets.newHashSet(activeNodes);
183 activeNodes = Collections.unmodifiableSet(set);
184 deviceMastershipManager.onDeviceConnected(ident.firstKeyOf(Node.class).getId());
191 public void unregistrateNode(InstanceIdentifier<FlowCapableNode> ident) {
192 if (activeNodes.contains(ident)) {
193 synchronized (lockObj) {
194 if (activeNodes.contains(ident)) {
195 Set<InstanceIdentifier<FlowCapableNode>> set =
196 Sets.newHashSet(activeNodes);
198 activeNodes = Collections.unmodifiableSet(set);
199 deviceMastershipManager.onDeviceDisconnected(ident.firstKeyOf(Node.class).getId());
206 public SalFlowService getSalFlowService() {
207 return salFlowService;
211 public SalGroupService getSalGroupService() {
212 return salGroupService;
216 public SalMeterService getSalMeterService() {
217 return salMeterService;
221 public SalTableService getSalTableService() {
222 return salTableService;
226 public ForwardingRulesCommiter<Flow> getFlowCommiter() {
231 public ForwardingRulesCommiter<Group> getGroupCommiter() {
232 return groupListener;
236 public ForwardingRulesCommiter<Meter> getMeterCommiter() {
237 return meterListener;
241 public ForwardingRulesCommiter<TableFeatures> getTableFeaturesCommiter() {
242 return tableListener;
246 public FlowNodeReconciliation getFlowNodeReconciliation() {
251 public ForwardingRulesManagerConfig getConfiguration() {
252 return forwardingRulesManagerConfig;
256 public FlowNodeConnectorInventoryTranslatorImpl getFlowNodeConnectorInventoryTranslatorImpl() {
257 return flowNodeConnectorInventoryTranslatorImpl;
261 public boolean isNodeOwner(InstanceIdentifier<FlowCapableNode> ident) {
262 return Objects.nonNull(ident) && deviceMastershipManager.isDeviceMastered(ident.firstKeyOf(Node.class).getId());
266 public void setDeviceMastershipManager(final DeviceMastershipManager deviceMastershipManager) {
267 this.deviceMastershipManager = deviceMastershipManager;