fa4c24009aeb43e42b05c22a4b22fdae139c0e06
[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  * <p/>
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.flow.inventory.rev130819.tables.Table;
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<Table> tableListener;
66     private FlowNodeReconciliation nodeListener;
67
68     public ForwardingRulesManagerImpl(final DataBroker dataBroker,
69                                       final RpcConsumerRegistry rpcRegistry) {
70         this.dataService = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
71
72         Preconditions.checkArgument(rpcRegistry != null, "RpcConsumerRegistry can not be null !");
73
74         this.salFlowService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalFlowService.class),
75                 "RPC SalFlowService not found.");
76         this.salGroupService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalGroupService.class),
77                 "RPC SalGroupService not found.");
78         this.salMeterService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalMeterService.class),
79                 "RPC SalMeterService not found.");
80         this.salTableService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalTableService.class),
81                 "RPC SalTableService not found.");
82     }
83
84     @Override
85     public void start() {
86
87         this.flowListener = new FlowForwarder(this, dataService);
88         
89         this.groupListener = new GroupForwarder(this, dataService);
90         this.meterListener = new MeterForwarder(this, dataService);
91         
92         this.tableListener = new TableForwarder(this, dataService);
93         this.nodeListener = new FlowNodeReconciliationImpl(this, dataService);
94         LOG.info("ForwardingRulesManager has started successfully.");
95
96     }
97
98     @Override
99     public void close() throws Exception {
100         if (this.flowListener != null) {
101             this.flowListener.close();
102             this.flowListener = null;
103         }
104         if (this.groupListener != null) {
105             this.groupListener.close();
106             this.groupListener = null;
107         }
108         if (this.meterListener != null) {
109             this.meterListener.close();
110             this.meterListener = null;
111         }
112         if (this.tableListener != null) {
113             this.tableListener.close();
114             this.tableListener = null;
115         }
116         if (this.nodeListener != null) {
117             this.nodeListener.close();
118             this.nodeListener = null;
119         }
120     }
121
122     @Override
123     public ReadOnlyTransaction getReadTranaction() {
124         return dataService.newReadOnlyTransaction();
125     }
126
127     @Override
128     public String getNewTransactionId() {
129         return "DOM-" + txNum.getAndIncrement();
130     }
131
132     @Override
133     public boolean isNodeActive(InstanceIdentifier<FlowCapableNode> ident) {
134         return activeNodes.contains(ident);
135     }
136
137     @Override
138     public void registrateNewNode(InstanceIdentifier<FlowCapableNode> ident) {
139         if (!activeNodes.contains(ident)) {
140             synchronized (lockObj) {
141                 if (!activeNodes.contains(ident)) {
142                     Set<InstanceIdentifier<FlowCapableNode>> set =
143                             Sets.newHashSet(activeNodes);
144                     set.add(ident);
145                     activeNodes = Collections.unmodifiableSet(set);
146                 }
147             }
148         }
149     }
150
151     @Override
152     public void unregistrateNode(InstanceIdentifier<FlowCapableNode> ident) {
153         if (activeNodes.contains(ident)) {
154             synchronized (lockObj) {
155                 if (activeNodes.contains(ident)) {
156                     Set<InstanceIdentifier<FlowCapableNode>> set =
157                             Sets.newHashSet(activeNodes);
158                     set.remove(ident);
159                     activeNodes = Collections.unmodifiableSet(set);
160                 }
161             }
162         }
163     }
164
165     @Override
166     public SalFlowService getSalFlowService() {
167         return salFlowService;
168     }
169
170     @Override
171     public SalGroupService getSalGroupService() {
172         return salGroupService;
173     }
174
175     @Override
176     public SalMeterService getSalMeterService() {
177         return salMeterService;
178     }
179
180     @Override
181     public SalTableService getSalTableService() {
182         return salTableService;
183     }
184
185     @Override
186     public ForwardingRulesCommiter<Flow> getFlowCommiter() {
187         return flowListener;
188     }
189
190     @Override
191     public ForwardingRulesCommiter<Group> getGroupCommiter() {
192         return groupListener;
193     }
194
195     @Override
196     public ForwardingRulesCommiter<Meter> getMeterCommiter() {
197         return meterListener;
198     }
199
200     @Override
201     public ForwardingRulesCommiter<Table> getTableCommiter() {
202         return tableListener;
203     }
204
205     @Override
206     public FlowNodeReconciliation getFlowNodeReconciliation() {
207         return nodeListener;
208     }
209 }
210