Bug-4866 - [Clustering]: Switch state resync is not
[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.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Sets;
14 import java.util.Collections;
15 import java.util.Set;
16 import java.util.concurrent.atomic.AtomicLong;
17
18 import com.google.common.util.concurrent.CheckedFuture;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
21 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
22 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
23 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
26 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
27 import org.opendaylight.openflowplugin.applications.frm.FlowNodeReconciliation;
28 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesCommiter;
29 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.SalMeterService;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.SalTableService;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * forwardingrules-manager
47  * org.opendaylight.openflowplugin.applications.frm.impl
48  *
49  * Manager and middle point for whole module.
50  * It contains ActiveNodeHolder and provide all RPC services.
51  *
52  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
53  *
54  * Created: Aug 25, 2014
55  */
56 public class ForwardingRulesManagerImpl implements ForwardingRulesManager {
57
58     private static final Logger LOG = LoggerFactory.getLogger(ForwardingRulesManagerImpl.class);
59     public static final int STARTUP_LOOP_TICK = 500;
60     public static final int STARTUP_LOOP_MAX_RETRIES = 8;
61
62     private final AtomicLong txNum = new AtomicLong();
63     private final Object lockObj = new Object();
64     private Set<InstanceIdentifier<FlowCapableNode>> activeNodes = Collections.emptySet();
65
66     private final DataBroker dataService;
67     private final SalFlowService salFlowService;
68     private final SalGroupService salGroupService;
69     private final SalMeterService salMeterService;
70     private final SalTableService salTableService;
71
72     private ForwardingRulesCommiter<Flow> flowListener;
73     private ForwardingRulesCommiter<Group> groupListener;
74     private ForwardingRulesCommiter<Meter> meterListener;
75     private ForwardingRulesCommiter<TableFeatures> tableListener;
76     private FlowNodeReconciliation nodeListener;
77
78     private final ForwardingRulesManagerConfig forwardingRulesManagerConfig;
79     private final EntityOwnershipService entityOwnershipService;
80
81     public ForwardingRulesManagerImpl(final DataBroker dataBroker,
82                                       final RpcConsumerRegistry rpcRegistry,
83                                       final ForwardingRulesManagerConfig config,
84                                       final EntityOwnershipService eos) {
85         this.dataService = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
86         this.forwardingRulesManagerConfig = Preconditions.checkNotNull(config, "Configuration for FRM cannot be null");
87         this.entityOwnershipService = Preconditions.checkNotNull(eos, "EntityOwnership service can not be null");
88
89         Preconditions.checkArgument(rpcRegistry != null, "RpcConsumerRegistry can not be null !");
90
91         this.salFlowService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalFlowService.class),
92                 "RPC SalFlowService not found.");
93         this.salGroupService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalGroupService.class),
94                 "RPC SalGroupService not found.");
95         this.salMeterService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalMeterService.class),
96                 "RPC SalMeterService not found.");
97         this.salTableService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalTableService.class),
98                 "RPC SalTableService not found.");
99     }
100
101     @Override
102     public void start() {
103
104         this.flowListener = new FlowForwarder(this, dataService);
105
106         this.groupListener = new GroupForwarder(this, dataService);
107         this.meterListener = new MeterForwarder(this, dataService);
108
109         this.tableListener = new TableForwarder(this, dataService);
110         this.nodeListener = new FlowNodeReconciliationImpl(this, dataService);
111         LOG.info("ForwardingRulesManager has started successfully.");
112
113     }
114
115     @Override
116     public void close() throws Exception {
117         if (this.flowListener != null) {
118             this.flowListener.close();
119             this.flowListener = null;
120         }
121         if (this.groupListener != null) {
122             this.groupListener.close();
123             this.groupListener = null;
124         }
125         if (this.meterListener != null) {
126             this.meterListener.close();
127             this.meterListener = null;
128         }
129         if (this.tableListener != null) {
130             this.tableListener.close();
131             this.tableListener = null;
132         }
133         if (this.nodeListener != null) {
134             this.nodeListener.close();
135             this.nodeListener = null;
136         }
137     }
138
139     @Override
140     public ReadOnlyTransaction getReadTranaction() {
141         return dataService.newReadOnlyTransaction();
142     }
143
144     @Override
145     public String getNewTransactionId() {
146         return "DOM-" + txNum.getAndIncrement();
147     }
148
149     @Override
150     public boolean isNodeActive(InstanceIdentifier<FlowCapableNode> ident) {
151         return activeNodes.contains(ident);
152     }
153
154     @Override
155     public boolean checkNodeInOperationalDataStore(InstanceIdentifier<FlowCapableNode> ident) {
156         boolean result = false;
157         InstanceIdentifier<Node> nodeIid = ident.firstIdentifierOf(Node.class);
158         final ReadOnlyTransaction transaction = dataService.newReadOnlyTransaction();
159         Optional<Node> optionalDataObject;
160         CheckedFuture<Optional<Node>, ReadFailedException> future = transaction.read(LogicalDatastoreType.OPERATIONAL, nodeIid);
161         try {
162             optionalDataObject = future.checkedGet();
163             if (optionalDataObject.isPresent()) {
164                 result = true;
165             } else {
166                 LOG.debug("{}: Failed to read {}",
167                         Thread.currentThread().getStackTrace()[1], nodeIid);
168             }
169         } catch (ReadFailedException e) {
170             LOG.warn("Failed to read {} ", nodeIid, e);
171         }
172         transaction.close();
173
174         return result;
175     }
176
177     @Override
178     public void registrateNewNode(InstanceIdentifier<FlowCapableNode> ident) {
179         if (!activeNodes.contains(ident)) {
180             synchronized (lockObj) {
181                 if (!activeNodes.contains(ident)) {
182                     Set<InstanceIdentifier<FlowCapableNode>> set =
183                             Sets.newHashSet(activeNodes);
184                     set.add(ident);
185                     activeNodes = Collections.unmodifiableSet(set);
186                 }
187             }
188         }
189     }
190
191     @Override
192     public void unregistrateNode(InstanceIdentifier<FlowCapableNode> ident) {
193         if (activeNodes.contains(ident)) {
194             synchronized (lockObj) {
195                 if (activeNodes.contains(ident)) {
196                     Set<InstanceIdentifier<FlowCapableNode>> set =
197                             Sets.newHashSet(activeNodes);
198                     set.remove(ident);
199                     activeNodes = Collections.unmodifiableSet(set);
200                 }
201             }
202         }
203     }
204
205     @Override
206     public SalFlowService getSalFlowService() {
207         return salFlowService;
208     }
209
210     @Override
211     public SalGroupService getSalGroupService() {
212         return salGroupService;
213     }
214
215     @Override
216     public SalMeterService getSalMeterService() {
217         return salMeterService;
218     }
219
220     @Override
221     public SalTableService getSalTableService() {
222         return salTableService;
223     }
224
225     @Override
226     public ForwardingRulesCommiter<Flow> getFlowCommiter() {
227         return flowListener;
228     }
229
230     @Override
231     public ForwardingRulesCommiter<Group> getGroupCommiter() {
232         return groupListener;
233     }
234
235     @Override
236     public ForwardingRulesCommiter<Meter> getMeterCommiter() {
237         return meterListener;
238     }
239
240     @Override
241     public ForwardingRulesCommiter<TableFeatures> getTableFeaturesCommiter() {
242         return tableListener;
243     }
244
245     @Override
246     public FlowNodeReconciliation getFlowNodeReconciliation() {
247         return nodeListener;
248     }
249
250     @Override
251     public ForwardingRulesManagerConfig getConfiguration() {
252         return forwardingRulesManagerConfig;
253     }
254
255     @Override
256     public boolean isNodeOwner(InstanceIdentifier<FlowCapableNode> ident) {
257         NodeId nodeId = ident.firstKeyOf(Node.class).getId();
258         Entity entity = new Entity("openflow", nodeId.getValue());
259         Optional<EntityOwnershipState> eState = this.entityOwnershipService.getOwnershipState(entity);
260         if(eState.isPresent()) {
261             return eState.get().isOwner();
262         }
263         return false;
264     }
265 }
266