migrating FRM to config subsystem
[openflowplugin.git] / applications / forwardingrules-manager / src / main / java / org / opendaylight / openflowplugin / applications / frm / impl / ForwardingRulesManagerImpl.java
1 /**
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.applications.frm.impl;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Sets;
13 import java.util.Collections;
14 import java.util.Set;
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;
32
33 /**
34  * forwardingrules-manager
35  * org.opendaylight.openflowplugin.applications.frm.impl
36  *
37  * Manager and middle point for whole module.
38  * It contains ActiveNodeHolder and provide all RPC services.
39  *
40  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
41  *
42  * Created: Aug 25, 2014
43  */
44 public class ForwardingRulesManagerImpl implements ForwardingRulesManager {
45
46     private static final Logger LOG = LoggerFactory.getLogger(ForwardingRulesManagerImpl.class);
47
48     private final AtomicLong txNum = new AtomicLong();
49     private final Object lockObj = new Object();
50     private Set<InstanceIdentifier<FlowCapableNode>> activeNodes = Collections.emptySet();
51
52     private final DataBroker dataService;
53     private final SalFlowService salFlowService;
54     private final SalGroupService salGroupService;
55     private final SalMeterService salMeterService;
56
57     private ForwardingRulesCommiter<Flow> flowListener;
58     private ForwardingRulesCommiter<Group> groupListener;
59     private ForwardingRulesCommiter<Meter> meterListener;
60     private FlowNodeReconciliation nodeListener;
61
62     public ForwardingRulesManagerImpl(final DataBroker dataBroker,
63             final RpcConsumerRegistry rpcRegistry) {
64         this.dataService = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
65
66         Preconditions.checkArgument(rpcRegistry != null, "RpcConsumerRegistry can not be null !");
67
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.");
74     }
75
76     @Override
77     public void start() {
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.");
83     }
84
85     @Override
86     public void close() throws Exception {
87         if(this.flowListener != null) {
88             this.flowListener.close();
89             this.flowListener = null;
90         }
91         if (this.groupListener != null) {
92             this.groupListener.close();
93             this.groupListener = null;
94         }
95         if (this.meterListener != null) {
96             this.meterListener.close();
97             this.meterListener = null;
98         }
99         if (this.nodeListener != null) {
100             this.nodeListener.close();
101             this.nodeListener = null;
102         }
103     }
104
105     @Override
106     public ReadOnlyTransaction getReadTranaction() {
107         return dataService.newReadOnlyTransaction();
108     }
109
110     @Override
111     public String getNewTransactionId() {
112         return "DOM-" + txNum.getAndIncrement();
113     }
114
115     @Override
116     public boolean isNodeActive(InstanceIdentifier<FlowCapableNode> ident) {
117         return activeNodes.contains(ident);
118     }
119
120     @Override
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);
127                     set.add(ident);
128                     activeNodes = Collections.unmodifiableSet(set);
129                 }
130             }
131         }
132     }
133
134     @Override
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);
141                     set.remove(ident);
142                     activeNodes = Collections.unmodifiableSet(set);
143                 }
144             }
145         }
146     }
147
148     @Override
149     public SalFlowService getSalFlowService() {
150         return salFlowService;
151     }
152
153     @Override
154     public SalGroupService getSalGroupService() {
155         return salGroupService;
156     }
157
158     @Override
159     public SalMeterService getSalMeterService() {
160         return salMeterService;
161     }
162
163     @Override
164     public ForwardingRulesCommiter<Flow> getFlowCommiter() {
165         return flowListener;
166     }
167
168     @Override
169     public ForwardingRulesCommiter<Group> getGroupCommiter() {
170         return groupListener;
171     }
172
173     @Override
174     public ForwardingRulesCommiter<Meter> getMeterCommiter() {
175         return meterListener;
176     }
177
178     @Override
179     public FlowNodeReconciliation getFlowNodeReconciliation() {
180         return nodeListener;
181     }
182 }
183