Bug 1699 - FRM: Change data change listener scope from BASE to SUBTREE
[controller.git] / opendaylight / md-sal / forwardingrules-manager / src / main / java / org / opendaylight / controller / 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.controller.frm.impl;
10
11 import java.util.Collections;
12 import java.util.Set;
13 import java.util.concurrent.atomic.AtomicLong;
14
15 import org.opendaylight.controller.frm.FlowNodeReconciliation;
16 import org.opendaylight.controller.frm.ForwardingRulesCommiter;
17 import org.opendaylight.controller.frm.ForwardingRulesManager;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
20 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.SalMeterService;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import com.google.common.base.Preconditions;
33 import com.google.common.collect.Sets;
34
35 /**
36  * forwardingrules-manager
37  * org.opendaylight.controller.frm.impl
38  *
39  * Manager and middle point for whole module.
40  * It contains ActiveNodeHolder and provide all RPC services.
41  *
42  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
43  *
44  * Created: Aug 25, 2014
45  */
46 public class ForwardingRulesManagerImpl implements ForwardingRulesManager {
47
48     private static final Logger LOG = LoggerFactory.getLogger(ForwardingRulesManagerImpl.class);
49
50     private final AtomicLong txNum = new AtomicLong();
51     private final Object lockObj = new Object();
52     private Set<InstanceIdentifier<FlowCapableNode>> activeNodes = Collections.emptySet();
53
54     private final DataBroker dataService;
55     private final SalFlowService salFlowService;
56     private final SalGroupService salGroupService;
57     private final SalMeterService salMeterService;
58
59     private ForwardingRulesCommiter<Flow> flowListener;
60     private ForwardingRulesCommiter<Group> groupListener;
61     private ForwardingRulesCommiter<Meter> meterListener;
62     private FlowNodeReconciliation nodeListener;
63
64     public ForwardingRulesManagerImpl(final DataBroker dataBroker,
65             final RpcConsumerRegistry rpcRegistry) {
66         this.dataService = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
67
68         Preconditions.checkArgument(rpcRegistry != null, "RpcConsumerRegistry can not be null !");
69
70         this.salFlowService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalFlowService.class),
71                 "RPC SalFlowService not found.");
72         this.salGroupService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalGroupService.class),
73                 "RPC SalGroupService not found.");
74         this.salMeterService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalMeterService.class),
75                 "RPC SalMeterService not found.");
76     }
77
78     @Override
79     public void start() {
80         this.flowListener = new FlowForwarder(this, dataService);
81         this.groupListener = new GroupForwarder(this, dataService);
82         this.meterListener = new MeterForwarder(this, dataService);
83         this.nodeListener = new FlowNodeReconciliationImpl(this, dataService);
84         LOG.info("ForwardingRulesManager has started successfull.");
85     }
86
87     @Override
88     public void close() throws Exception {
89         if(this.flowListener != null) {
90             this.flowListener.close();
91             this.flowListener = null;
92         }
93         if (this.groupListener != null) {
94             this.groupListener.close();
95             this.groupListener = null;
96         }
97         if (this.meterListener != null) {
98             this.meterListener.close();
99             this.meterListener = null;
100         }
101         if (this.nodeListener != null) {
102             this.nodeListener.close();
103             this.nodeListener = null;
104         }
105     }
106
107     @Override
108     public ReadOnlyTransaction getReadTranaction() {
109         return dataService.newReadOnlyTransaction();
110     }
111
112     @Override
113     public String getNewTransactionId() {
114         return "DOM-" + txNum.getAndIncrement();
115     }
116
117     @Override
118     public boolean isNodeActive(InstanceIdentifier<FlowCapableNode> ident) {
119         return activeNodes.contains(ident);
120     }
121
122     @Override
123     public void registrateNewNode(InstanceIdentifier<FlowCapableNode> ident) {
124         if ( ! activeNodes.contains(ident)) {
125             synchronized (lockObj) {
126                 if ( ! activeNodes.contains(ident)) {
127                     Set<InstanceIdentifier<FlowCapableNode>> set =
128                             Sets.newHashSet(activeNodes);
129                     set.add(ident);
130                     activeNodes = Collections.unmodifiableSet(set);
131                 }
132             }
133         }
134     }
135
136     @Override
137     public void unregistrateNode(InstanceIdentifier<FlowCapableNode> ident) {
138         if (activeNodes.contains(ident)) {
139             synchronized (lockObj) {
140                 if (activeNodes.contains(ident)) {
141                     Set<InstanceIdentifier<FlowCapableNode>> set =
142                             Sets.newHashSet(activeNodes);
143                     set.remove(ident);
144                     activeNodes = Collections.unmodifiableSet(set);
145                 }
146             }
147         }
148     }
149
150     @Override
151     public SalFlowService getSalFlowService() {
152         return salFlowService;
153     }
154
155     @Override
156     public SalGroupService getSalGroupService() {
157         return salGroupService;
158     }
159
160     @Override
161     public SalMeterService getSalMeterService() {
162         return salMeterService;
163     }
164
165     @Override
166     public ForwardingRulesCommiter<Flow> getFlowCommiter() {
167         return flowListener;
168     }
169
170     @Override
171     public ForwardingRulesCommiter<Group> getGroupCommiter() {
172         return groupListener;
173     }
174
175     @Override
176     public ForwardingRulesCommiter<Meter> getMeterCommiter() {
177         return meterListener;
178     }
179
180     @Override
181     public FlowNodeReconciliation getFlowNodeReconciliation() {
182         return nodeListener;
183     }
184 }
185