f22a57bc8aa1f26cc42c78393c0d6bad69f1fdd3
[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 com.google.common.util.concurrent.CheckedFuture;
15 import java.util.Collections;
16 import java.util.Set;
17 import java.util.concurrent.atomic.AtomicLong;
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.md.sal.common.api.clustering.Entity;
21 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
22 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
25 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
26 import org.opendaylight.openflowplugin.applications.frm.FlowNodeReconciliation;
27 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesCommiter;
28 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.SalMeterService;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.forwardingrules.manager.config.rev160511.ForwardingRulesManagerConfig;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.SalTableService;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
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  */
53 public class ForwardingRulesManagerImpl implements ForwardingRulesManager {
54
55     private static final Logger LOG = LoggerFactory.getLogger(ForwardingRulesManagerImpl.class);
56     static final int STARTUP_LOOP_TICK = 500;
57     static final int STARTUP_LOOP_MAX_RETRIES = 8;
58
59     private final AtomicLong txNum = new AtomicLong();
60     private final Object lockObj = new Object();
61     private Set<InstanceIdentifier<FlowCapableNode>> activeNodes = Collections.emptySet();
62
63     private final DataBroker dataService;
64     private final SalFlowService salFlowService;
65     private final SalGroupService salGroupService;
66     private final SalMeterService salMeterService;
67     private final SalTableService salTableService;
68
69     private ForwardingRulesCommiter<Flow> flowListener;
70     private ForwardingRulesCommiter<Group> groupListener;
71     private ForwardingRulesCommiter<Meter> meterListener;
72     private ForwardingRulesCommiter<TableFeatures> tableListener;
73     private FlowNodeReconciliation nodeListener;
74
75     private final ForwardingRulesManagerConfig forwardingRulesManagerConfig;
76     private FlowNodeConnectorInventoryTranslatorImpl flowNodeConnectorInventoryTranslatorImpl;
77     private final EntityOwnershipService entityOwnershipService;
78
79     public ForwardingRulesManagerImpl(final DataBroker dataBroker,
80                                       final RpcConsumerRegistry rpcRegistry,
81                                       final ForwardingRulesManagerConfig config,
82                                       final EntityOwnershipService eos) {
83         this.dataService = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
84         this.forwardingRulesManagerConfig = Preconditions.checkNotNull(config, "Configuration for FRM cannot be null");
85         this.entityOwnershipService = Preconditions.checkNotNull(eos, "EntityOwnership service can not be null");
86
87         Preconditions.checkArgument(rpcRegistry != null, "RpcConsumerRegistry can not be null !");
88
89         this.salFlowService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalFlowService.class),
90                 "RPC SalFlowService not found.");
91         this.salGroupService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalGroupService.class),
92                 "RPC SalGroupService not found.");
93         this.salMeterService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalMeterService.class),
94                 "RPC SalMeterService not found.");
95         this.salTableService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalTableService.class),
96                 "RPC SalTableService not found.");
97     }
98
99     @Override
100     public void start() {
101
102         this.flowListener = new FlowForwarder(this, dataService);
103
104         this.groupListener = new GroupForwarder(this, dataService);
105         this.meterListener = new MeterForwarder(this, dataService);
106
107         this.tableListener = new TableForwarder(this, dataService);
108         this.nodeListener = new FlowNodeReconciliationImpl(this, dataService);
109         flowNodeConnectorInventoryTranslatorImpl =
110                             new FlowNodeConnectorInventoryTranslatorImpl(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 FlowNodeConnectorInventoryTranslatorImpl getFlowNodeConnectorInventoryTranslatorImpl() {
257         return flowNodeConnectorInventoryTranslatorImpl;
258     }
259
260     @Override
261     public boolean isNodeOwner(InstanceIdentifier<FlowCapableNode> ident) {
262         NodeId nodeId = ident.firstKeyOf(Node.class).getId();
263         Entity entity = new Entity("openflow", nodeId.getValue());
264         Optional<EntityOwnershipState> eState = this.entityOwnershipService.getOwnershipState(entity);
265         if(eState.isPresent()) {
266             return eState.get().isOwner();
267         }
268         return false;
269     }
270 }
271