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.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * forwardingrules-manager
35 * org.opendaylight.openflowplugin.applications.frm.impl
37 * Manager and middle point for whole module.
38 * It contains ActiveNodeHolder and provide all RPC services.
40 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
42 * Created: Aug 25, 2014
44 public class ForwardingRulesManagerImpl implements ForwardingRulesManager {
46 private static final Logger LOG = LoggerFactory.getLogger(ForwardingRulesManagerImpl.class);
48 private final AtomicLong txNum = new AtomicLong();
49 private final Object lockObj = new Object();
50 private Set<InstanceIdentifier<FlowCapableNode>> activeNodes = Collections.emptySet();
52 private final DataBroker dataService;
53 private final SalFlowService salFlowService;
54 private final SalGroupService salGroupService;
55 private final SalMeterService salMeterService;
57 private ForwardingRulesCommiter<Flow> flowListener;
58 private ForwardingRulesCommiter<Group> groupListener;
59 private ForwardingRulesCommiter<Meter> meterListener;
60 private FlowNodeReconciliation nodeListener;
62 public ForwardingRulesManagerImpl(final DataBroker dataBroker,
63 final RpcConsumerRegistry rpcRegistry) {
64 this.dataService = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
66 Preconditions.checkArgument(rpcRegistry != null, "RpcConsumerRegistry can not be null !");
68 this.salFlowService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalFlowService.class),
69 "RPC SalFlowService not found.");
70 this.salGroupService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalGroupService.class),
71 "RPC SalGroupService not found.");
72 this.salMeterService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalMeterService.class),
73 "RPC SalMeterService not found.");
78 this.flowListener = new FlowForwarder(this, dataService);
79 this.groupListener = new GroupForwarder(this, dataService);
80 this.meterListener = new MeterForwarder(this, dataService);
81 this.nodeListener = new FlowNodeReconciliationImpl(this, dataService);
82 LOG.info("ForwardingRulesManager has started successfully.");
86 public void close() throws Exception {
87 if(this.flowListener != null) {
88 this.flowListener.close();
89 this.flowListener = null;
91 if (this.groupListener != null) {
92 this.groupListener.close();
93 this.groupListener = null;
95 if (this.meterListener != null) {
96 this.meterListener.close();
97 this.meterListener = null;
99 if (this.nodeListener != null) {
100 this.nodeListener.close();
101 this.nodeListener = null;
106 public ReadOnlyTransaction getReadTranaction() {
107 return dataService.newReadOnlyTransaction();
111 public String getNewTransactionId() {
112 return "DOM-" + txNum.getAndIncrement();
116 public boolean isNodeActive(InstanceIdentifier<FlowCapableNode> ident) {
117 return activeNodes.contains(ident);
121 public void registrateNewNode(InstanceIdentifier<FlowCapableNode> ident) {
122 if ( ! activeNodes.contains(ident)) {
123 synchronized (lockObj) {
124 if ( ! activeNodes.contains(ident)) {
125 Set<InstanceIdentifier<FlowCapableNode>> set =
126 Sets.newHashSet(activeNodes);
128 activeNodes = Collections.unmodifiableSet(set);
135 public void unregistrateNode(InstanceIdentifier<FlowCapableNode> ident) {
136 if (activeNodes.contains(ident)) {
137 synchronized (lockObj) {
138 if (activeNodes.contains(ident)) {
139 Set<InstanceIdentifier<FlowCapableNode>> set =
140 Sets.newHashSet(activeNodes);
142 activeNodes = Collections.unmodifiableSet(set);
149 public SalFlowService getSalFlowService() {
150 return salFlowService;
154 public SalGroupService getSalGroupService() {
155 return salGroupService;
159 public SalMeterService getSalMeterService() {
160 return salMeterService;
164 public ForwardingRulesCommiter<Flow> getFlowCommiter() {
169 public ForwardingRulesCommiter<Group> getGroupCommiter() {
170 return groupListener;
174 public ForwardingRulesCommiter<Meter> getMeterCommiter() {
175 return meterListener;
179 public FlowNodeReconciliation getFlowNodeReconciliation() {