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.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Sets;
14 import java.util.Collections;
16 import java.util.concurrent.atomic.AtomicLong;
18 import com.google.common.util.concurrent.CheckedFuture;
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.clustering.Entity;
22 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
23 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
26 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
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.NodeId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.SalMeterService;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.SalTableService;
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.
52 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
54 * Created: Aug 25, 2014
56 public class ForwardingRulesManagerImpl implements ForwardingRulesManager {
58 private static final Logger LOG = LoggerFactory.getLogger(ForwardingRulesManagerImpl.class);
59 public static final int STARTUP_LOOP_TICK = 500;
60 public static final int STARTUP_LOOP_MAX_RETRIES = 8;
62 private final AtomicLong txNum = new AtomicLong();
63 private final Object lockObj = new Object();
64 private Set<InstanceIdentifier<FlowCapableNode>> activeNodes = Collections.emptySet();
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;
72 private ForwardingRulesCommiter<Flow> flowListener;
73 private ForwardingRulesCommiter<Group> groupListener;
74 private ForwardingRulesCommiter<Meter> meterListener;
75 private ForwardingRulesCommiter<TableFeatures> tableListener;
76 private FlowNodeReconciliation nodeListener;
78 private final ForwardingRulesManagerConfig forwardingRulesManagerConfig;
79 private final EntityOwnershipService entityOwnershipService;
81 public ForwardingRulesManagerImpl(final DataBroker dataBroker,
82 final RpcConsumerRegistry rpcRegistry,
83 final ForwardingRulesManagerConfig config,
84 final EntityOwnershipService eos) {
85 this.dataService = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
86 this.forwardingRulesManagerConfig = Preconditions.checkNotNull(config, "Configuration for FRM cannot be null");
87 this.entityOwnershipService = Preconditions.checkNotNull(eos, "EntityOwnership service can not be null");
89 Preconditions.checkArgument(rpcRegistry != null, "RpcConsumerRegistry can not be null !");
91 this.salFlowService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalFlowService.class),
92 "RPC SalFlowService not found.");
93 this.salGroupService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalGroupService.class),
94 "RPC SalGroupService not found.");
95 this.salMeterService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalMeterService.class),
96 "RPC SalMeterService not found.");
97 this.salTableService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalTableService.class),
98 "RPC SalTableService not found.");
102 public void start() {
104 this.flowListener = new FlowForwarder(this, dataService);
106 this.groupListener = new GroupForwarder(this, dataService);
107 this.meterListener = new MeterForwarder(this, dataService);
109 this.tableListener = new TableForwarder(this, dataService);
110 this.nodeListener = new FlowNodeReconciliationImpl(this, dataService);
111 LOG.info("ForwardingRulesManager has started successfully.");
116 public void close() throws Exception {
117 if (this.flowListener != null) {
118 this.flowListener.close();
119 this.flowListener = null;
121 if (this.groupListener != null) {
122 this.groupListener.close();
123 this.groupListener = null;
125 if (this.meterListener != null) {
126 this.meterListener.close();
127 this.meterListener = null;
129 if (this.tableListener != null) {
130 this.tableListener.close();
131 this.tableListener = null;
133 if (this.nodeListener != null) {
134 this.nodeListener.close();
135 this.nodeListener = null;
140 public ReadOnlyTransaction getReadTranaction() {
141 return dataService.newReadOnlyTransaction();
145 public String getNewTransactionId() {
146 return "DOM-" + txNum.getAndIncrement();
150 public boolean isNodeActive(InstanceIdentifier<FlowCapableNode> ident) {
151 return activeNodes.contains(ident);
155 public boolean checkNodeInOperationalDataStore(InstanceIdentifier<FlowCapableNode> ident) {
156 boolean result = false;
157 InstanceIdentifier<Node> nodeIid = ident.firstIdentifierOf(Node.class);
158 final ReadOnlyTransaction transaction = dataService.newReadOnlyTransaction();
159 Optional<Node> optionalDataObject;
160 CheckedFuture<Optional<Node>, ReadFailedException> future = transaction.read(LogicalDatastoreType.OPERATIONAL, nodeIid);
162 optionalDataObject = future.checkedGet();
163 if (optionalDataObject.isPresent()) {
166 LOG.debug("{}: Failed to read {}",
167 Thread.currentThread().getStackTrace()[1], nodeIid);
169 } catch (ReadFailedException e) {
170 LOG.warn("Failed to read {} ", nodeIid, e);
178 public void registrateNewNode(InstanceIdentifier<FlowCapableNode> ident) {
179 if (!activeNodes.contains(ident)) {
180 synchronized (lockObj) {
181 if (!activeNodes.contains(ident)) {
182 Set<InstanceIdentifier<FlowCapableNode>> set =
183 Sets.newHashSet(activeNodes);
185 activeNodes = Collections.unmodifiableSet(set);
192 public void unregistrateNode(InstanceIdentifier<FlowCapableNode> ident) {
193 if (activeNodes.contains(ident)) {
194 synchronized (lockObj) {
195 if (activeNodes.contains(ident)) {
196 Set<InstanceIdentifier<FlowCapableNode>> set =
197 Sets.newHashSet(activeNodes);
199 activeNodes = Collections.unmodifiableSet(set);
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 boolean isNodeOwner(InstanceIdentifier<FlowCapableNode> ident) {
257 NodeId nodeId = ident.firstKeyOf(Node.class).getId();
258 Entity entity = new Entity("openflow", nodeId.getValue());
259 Optional<EntityOwnershipState> eState = this.entityOwnershipService.getOwnershipState(entity);
260 if(eState.isPresent()) {
261 return eState.get().isOwner();