BUG-4062: Flows,Groups,Meters not getting deleted during switch flap
[openflowplugin.git] / applications / forwardingrules-manager / src / main / java / org / opendaylight / openflowplugin / applications / frm / impl / ForwardingRulesManagerImpl.java
1 /**
2  * Copyright (c) 2014, 2015 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.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;
34
35 /**
36  * forwardingrules-manager
37  * org.opendaylight.openflowplugin.applications.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     public static final int STARTUP_LOOP_TICK = 500;
50     public static final int STARTUP_LOOP_MAX_RETRIES = 8;
51
52     private final AtomicLong txNum = new AtomicLong();
53     private final Object lockObj = new Object();
54     private Set<InstanceIdentifier<FlowCapableNode>> activeNodes = Collections.emptySet();
55
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;
61
62     private ForwardingRulesCommiter<Flow> flowListener;
63     private ForwardingRulesCommiter<Group> groupListener;
64     private ForwardingRulesCommiter<Meter> meterListener;
65     private ForwardingRulesCommiter<TableFeatures> tableListener;
66     private FlowNodeReconciliation nodeListener;
67
68     private final ForwardingRulesManagerConfig forwardingRulesManagerConfig;
69
70     public ForwardingRulesManagerImpl(final DataBroker dataBroker,
71                                       final RpcConsumerRegistry rpcRegistry,
72                                       final ForwardingRulesManagerConfig config) {
73         this.dataService = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
74         this.forwardingRulesManagerConfig = Preconditions.checkNotNull(config, "Configuration for FRM cannot be null");
75
76         Preconditions.checkArgument(rpcRegistry != null, "RpcConsumerRegistry can not be null !");
77
78         this.salFlowService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalFlowService.class),
79                 "RPC SalFlowService not found.");
80         this.salGroupService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalGroupService.class),
81                 "RPC SalGroupService not found.");
82         this.salMeterService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalMeterService.class),
83                 "RPC SalMeterService not found.");
84         this.salTableService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalTableService.class),
85                 "RPC SalTableService not found.");
86     }
87
88     @Override
89     public void start() {
90
91         this.flowListener = new FlowForwarder(this, dataService);
92
93         this.groupListener = new GroupForwarder(this, dataService);
94         this.meterListener = new MeterForwarder(this, dataService);
95
96         this.tableListener = new TableForwarder(this, dataService);
97         this.nodeListener = new FlowNodeReconciliationImpl(this, dataService);
98         LOG.info("ForwardingRulesManager has started successfully.");
99
100     }
101
102     @Override
103     public void close() throws Exception {
104         if (this.flowListener != null) {
105             this.flowListener.close();
106             this.flowListener = null;
107         }
108         if (this.groupListener != null) {
109             this.groupListener.close();
110             this.groupListener = null;
111         }
112         if (this.meterListener != null) {
113             this.meterListener.close();
114             this.meterListener = null;
115         }
116         if (this.tableListener != null) {
117             this.tableListener.close();
118             this.tableListener = null;
119         }
120         if (this.nodeListener != null) {
121             this.nodeListener.close();
122             this.nodeListener = null;
123         }
124     }
125
126     @Override
127     public ReadOnlyTransaction getReadTranaction() {
128         return dataService.newReadOnlyTransaction();
129     }
130
131     @Override
132     public String getNewTransactionId() {
133         return "DOM-" + txNum.getAndIncrement();
134     }
135
136     @Override
137     public boolean isNodeActive(InstanceIdentifier<FlowCapableNode> ident) {
138         return activeNodes.contains(ident);
139     }
140
141     @Override
142     public void registrateNewNode(InstanceIdentifier<FlowCapableNode> ident) {
143         if (!activeNodes.contains(ident)) {
144             synchronized (lockObj) {
145                 if (!activeNodes.contains(ident)) {
146                     Set<InstanceIdentifier<FlowCapableNode>> set =
147                             Sets.newHashSet(activeNodes);
148                     set.add(ident);
149                     activeNodes = Collections.unmodifiableSet(set);
150                 }
151             }
152         }
153     }
154
155     @Override
156     public void unregistrateNode(InstanceIdentifier<FlowCapableNode> ident) {
157         if (activeNodes.contains(ident)) {
158             synchronized (lockObj) {
159                 if (activeNodes.contains(ident)) {
160                     Set<InstanceIdentifier<FlowCapableNode>> set =
161                             Sets.newHashSet(activeNodes);
162                     set.remove(ident);
163                     activeNodes = Collections.unmodifiableSet(set);
164                 }
165             }
166         }
167     }
168
169     @Override
170     public SalFlowService getSalFlowService() {
171         return salFlowService;
172     }
173
174     @Override
175     public SalGroupService getSalGroupService() {
176         return salGroupService;
177     }
178
179     @Override
180     public SalMeterService getSalMeterService() {
181         return salMeterService;
182     }
183
184     @Override
185     public SalTableService getSalTableService() {
186         return salTableService;
187     }
188
189     @Override
190     public ForwardingRulesCommiter<Flow> getFlowCommiter() {
191         return flowListener;
192     }
193
194     @Override
195     public ForwardingRulesCommiter<Group> getGroupCommiter() {
196         return groupListener;
197     }
198
199     @Override
200     public ForwardingRulesCommiter<Meter> getMeterCommiter() {
201         return meterListener;
202     }
203
204     @Override
205     public ForwardingRulesCommiter<TableFeatures> getTableFeaturesCommiter() {
206         return tableListener;
207     }
208
209     @Override
210     public FlowNodeReconciliation getFlowNodeReconciliation() {
211         return nodeListener;
212     }
213
214     @Override
215     public ForwardingRulesManagerConfig getConfiguration() {
216         return forwardingRulesManagerConfig;
217     }
218 }
219