2 * Copyright (c) 2014 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.Preconditions;
12 import com.google.common.collect.Sets;
13 import java.util.Collections;
15 import java.util.concurrent.atomic.AtomicLong;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
18 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
19 import org.opendaylight.openflowplugin.applications.frm.FlowNodeReconciliation;
20 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesCommiter;
21 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.SalMeterService;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.SalTableService;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * forwardingrules-manager
37 * org.opendaylight.openflowplugin.applications.frm.impl
39 * Manager and middle point for whole module.
40 * It contains ActiveNodeHolder and provide all RPC services.
42 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
44 * Created: Aug 25, 2014
46 public class ForwardingRulesManagerImpl implements ForwardingRulesManager {
48 private static final Logger LOG = LoggerFactory.getLogger(ForwardingRulesManagerImpl.class);
49 public static final int STARTUP_LOOP_TICK = 500;
50 public static final int STARTUP_LOOP_MAX_RETRIES = 8;
52 private final AtomicLong txNum = new AtomicLong();
53 private final Object lockObj = new Object();
54 private Set<InstanceIdentifier<FlowCapableNode>> activeNodes = Collections.emptySet();
56 private final DataBroker dataService;
57 private final SalFlowService salFlowService;
58 private final SalGroupService salGroupService;
59 private final SalMeterService salMeterService;
60 private final SalTableService salTableService;
62 private ForwardingRulesCommiter<Flow> flowListener;
63 private ForwardingRulesCommiter<Group> groupListener;
64 private ForwardingRulesCommiter<Meter> meterListener;
65 private ForwardingRulesCommiter<TableFeatures> tableListener;
66 private FlowNodeReconciliation nodeListener;
68 public ForwardingRulesManagerImpl(final DataBroker dataBroker,
69 final RpcConsumerRegistry rpcRegistry) {
70 this.dataService = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
72 Preconditions.checkArgument(rpcRegistry != null, "RpcConsumerRegistry can not be null !");
74 this.salFlowService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalFlowService.class),
75 "RPC SalFlowService not found.");
76 this.salGroupService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalGroupService.class),
77 "RPC SalGroupService not found.");
78 this.salMeterService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalMeterService.class),
79 "RPC SalMeterService not found.");
80 this.salTableService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalTableService.class),
81 "RPC SalTableService not found.");
87 this.flowListener = new FlowForwarder(this, dataService);
89 this.groupListener = new GroupForwarder(this, dataService);
90 this.meterListener = new MeterForwarder(this, dataService);
92 this.tableListener = new TableForwarder(this, dataService);
93 this.nodeListener = new FlowNodeReconciliationImpl(this, dataService);
94 LOG.info("ForwardingRulesManager has started successfully.");
99 public void close() throws Exception {
100 if (this.flowListener != null) {
101 this.flowListener.close();
102 this.flowListener = null;
104 if (this.groupListener != null) {
105 this.groupListener.close();
106 this.groupListener = null;
108 if (this.meterListener != null) {
109 this.meterListener.close();
110 this.meterListener = null;
112 if (this.tableListener != null) {
113 this.tableListener.close();
114 this.tableListener = null;
116 if (this.nodeListener != null) {
117 this.nodeListener.close();
118 this.nodeListener = null;
123 public ReadOnlyTransaction getReadTranaction() {
124 return dataService.newReadOnlyTransaction();
128 public String getNewTransactionId() {
129 return "DOM-" + txNum.getAndIncrement();
133 public boolean isNodeActive(InstanceIdentifier<FlowCapableNode> ident) {
134 return activeNodes.contains(ident);
138 public void registrateNewNode(InstanceIdentifier<FlowCapableNode> ident) {
139 if (!activeNodes.contains(ident)) {
140 synchronized (lockObj) {
141 if (!activeNodes.contains(ident)) {
142 Set<InstanceIdentifier<FlowCapableNode>> set =
143 Sets.newHashSet(activeNodes);
145 activeNodes = Collections.unmodifiableSet(set);
152 public void unregistrateNode(InstanceIdentifier<FlowCapableNode> ident) {
153 if (activeNodes.contains(ident)) {
154 synchronized (lockObj) {
155 if (activeNodes.contains(ident)) {
156 Set<InstanceIdentifier<FlowCapableNode>> set =
157 Sets.newHashSet(activeNodes);
159 activeNodes = Collections.unmodifiableSet(set);
166 public SalFlowService getSalFlowService() {
167 return salFlowService;
171 public SalGroupService getSalGroupService() {
172 return salGroupService;
176 public SalMeterService getSalMeterService() {
177 return salMeterService;
181 public SalTableService getSalTableService() {
182 return salTableService;
186 public ForwardingRulesCommiter<Flow> getFlowCommiter() {
191 public ForwardingRulesCommiter<Group> getGroupCommiter() {
192 return groupListener;
196 public ForwardingRulesCommiter<Meter> getMeterCommiter() {
197 return meterListener;
201 public ForwardingRulesCommiter<TableFeatures> getTableFeaturesCommiter() {
202 return tableListener;
206 public FlowNodeReconciliation getFlowNodeReconciliation() {