Encapsulate OpenFlowPlugin configuration
[openflowplugin.git] / applications / forwardingrules-manager / src / main / java / org / opendaylight / openflowplugin / applications / frm / impl / ForwardingRulesManagerImpl.java
1 /**
2  * Copyright (c) 2014, 2017 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.annotations.VisibleForTesting;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import java.util.Objects;
15 import java.util.Optional;
16 import java.util.concurrent.atomic.AtomicLong;
17 import javax.annotation.Nonnull;
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.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
22 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
23 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
24 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
25 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationService;
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.openflowplugin.applications.frm.ForwardingRulesProperty;
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.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     private static final Logger LOG = LoggerFactory.getLogger(ForwardingRulesManagerImpl.class);
55
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 DataBroker dataService;
61     private final SalFlowService salFlowService;
62     private final SalGroupService salGroupService;
63     private final SalMeterService salMeterService;
64     private final SalTableService salTableService;
65     private final ClusterSingletonServiceProvider clusterSingletonServiceProvider;
66     private final NotificationProviderService notificationService;
67     private final AutoCloseable configurationServiceRegistration;
68     private ForwardingRulesCommiter<Flow> flowListener;
69     private ForwardingRulesCommiter<Group> groupListener;
70     private ForwardingRulesCommiter<Meter> meterListener;
71     private ForwardingRulesCommiter<TableFeatures> tableListener;
72     private FlowNodeReconciliation nodeListener;
73     private FlowNodeConnectorInventoryTranslatorImpl flowNodeConnectorInventoryTranslatorImpl;
74     private DeviceMastershipManager deviceMastershipManager;
75     private boolean disableReconciliation;
76     private boolean staleMarkingEnabled;
77     private int reconciliationRetryCount;
78
79     public ForwardingRulesManagerImpl(final DataBroker dataBroker,
80                                       final RpcConsumerRegistry rpcRegistry,
81                                       final ForwardingRulesManagerConfig config,
82                                       final ClusterSingletonServiceProvider clusterSingletonService,
83                                       final NotificationProviderService notificationService,
84                                       final ConfigurationService configurationService) {
85         disableReconciliation = config.isDisableReconciliation();
86         staleMarkingEnabled = config.isStaleMarkingEnabled();
87         reconciliationRetryCount = config.getReconciliationRetryCount();
88
89         this.configurationServiceRegistration = configurationService.registerListener(this);
90         this.dataService = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
91         this.clusterSingletonServiceProvider = Preconditions.checkNotNull(clusterSingletonService,
92                 "ClusterSingletonService provider can not be null");
93         this.notificationService = Preconditions.checkNotNull(notificationService, "Notification publisher configurationService is" +
94                 " not available");
95
96         Preconditions.checkArgument(rpcRegistry != null, "RpcConsumerRegistry can not be null !");
97
98         this.salFlowService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalFlowService.class),
99                 "RPC SalFlowService not found.");
100         this.salGroupService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalGroupService.class),
101                 "RPC SalGroupService not found.");
102         this.salMeterService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalMeterService.class),
103                 "RPC SalMeterService not found.");
104         this.salTableService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalTableService.class),
105                 "RPC SalTableService not found.");
106     }
107
108     @Override
109     public void start() {
110         this.nodeListener = new FlowNodeReconciliationImpl(this, dataService);
111         this.deviceMastershipManager = new DeviceMastershipManager(clusterSingletonServiceProvider,
112                 notificationService,
113                 this.nodeListener,
114                 dataService);
115         flowNodeConnectorInventoryTranslatorImpl = new FlowNodeConnectorInventoryTranslatorImpl(this,dataService);
116
117         this.flowListener = new FlowForwarder(this, dataService);
118         this.groupListener = new GroupForwarder(this, dataService);
119         this.meterListener = new MeterForwarder(this, dataService);
120         this.tableListener = new TableForwarder(this, dataService);
121         LOG.info("ForwardingRulesManager has started successfully.");
122     }
123
124     @Override
125     public void close() throws Exception {
126         configurationServiceRegistration.close();
127
128         if (this.flowListener != null) {
129             this.flowListener.close();
130             this.flowListener = null;
131         }
132         if (this.groupListener != null) {
133             this.groupListener.close();
134             this.groupListener = null;
135         }
136         if (this.meterListener != null) {
137             this.meterListener.close();
138             this.meterListener = null;
139         }
140         if (this.tableListener != null) {
141             this.tableListener.close();
142             this.tableListener = null;
143         }
144         if (this.nodeListener != null) {
145             this.nodeListener.close();
146             this.nodeListener = null;
147         }
148         if (deviceMastershipManager != null) {
149             deviceMastershipManager.close();
150         }
151     }
152
153     @Override
154     public ReadOnlyTransaction getReadTranaction() {
155         return dataService.newReadOnlyTransaction();
156     }
157
158     @Override
159     public String getNewTransactionId() {
160         return "DOM-" + txNum.getAndIncrement();
161     }
162
163     @Override
164     public boolean isNodeActive(InstanceIdentifier<FlowCapableNode> ident) {
165         return deviceMastershipManager.isNodeActive(ident.firstKeyOf(Node.class).getId());
166     }
167
168     @Override
169     public boolean checkNodeInOperationalDataStore(InstanceIdentifier<FlowCapableNode> ident) {
170         boolean result = false;
171         InstanceIdentifier<Node> nodeIid = ident.firstIdentifierOf(Node.class);
172         final ReadOnlyTransaction transaction = dataService.newReadOnlyTransaction();
173         CheckedFuture<com.google.common.base.Optional<Node>, ReadFailedException> future = transaction.read(LogicalDatastoreType.OPERATIONAL, nodeIid);
174         try {
175             com.google.common.base.Optional<Node> optionalDataObject = future.checkedGet();
176             if (optionalDataObject.isPresent()) {
177                 result = true;
178             } else {
179                 LOG.debug("{}: Failed to read {}",
180                         Thread.currentThread().getStackTrace()[1], nodeIid);
181             }
182         } catch (ReadFailedException e) {
183             LOG.warn("Failed to read {} ", nodeIid, e);
184         }
185         transaction.close();
186
187         return result;
188     }
189
190     @Override
191     public SalFlowService getSalFlowService() {
192         return salFlowService;
193     }
194
195     @Override
196     public SalGroupService getSalGroupService() {
197         return salGroupService;
198     }
199
200     @Override
201     public SalMeterService getSalMeterService() {
202         return salMeterService;
203     }
204
205     @Override
206     public SalTableService getSalTableService() {
207         return salTableService;
208     }
209
210     @Override
211     public ForwardingRulesCommiter<Flow> getFlowCommiter() {
212         return flowListener;
213     }
214
215     @Override
216     public ForwardingRulesCommiter<Group> getGroupCommiter() {
217         return groupListener;
218     }
219
220     @Override
221     public ForwardingRulesCommiter<Meter> getMeterCommiter() {
222         return meterListener;
223     }
224
225     @Override
226     public ForwardingRulesCommiter<TableFeatures> getTableFeaturesCommiter() {
227         return tableListener;
228     }
229
230     @Override
231     public boolean isReconciliationDisabled() {
232         return disableReconciliation;
233     }
234
235     @Override
236     public boolean isStaleMarkingEnabled() {
237         return staleMarkingEnabled;
238     }
239
240     @Override
241     public int getReconciliationRetryCount() {
242         return reconciliationRetryCount;
243     }
244
245     @Override
246     public FlowNodeConnectorInventoryTranslatorImpl getFlowNodeConnectorInventoryTranslatorImpl() {
247         return flowNodeConnectorInventoryTranslatorImpl;
248     }
249
250     @Override
251     public boolean isNodeOwner(InstanceIdentifier<FlowCapableNode> ident) {
252         return Objects.nonNull(ident) && deviceMastershipManager.isDeviceMastered(ident.firstKeyOf(Node.class).getId());
253     }
254
255     @VisibleForTesting
256     public void setDeviceMastershipManager(final DeviceMastershipManager deviceMastershipManager) {
257         this.deviceMastershipManager = deviceMastershipManager;
258     }
259
260     @Override
261     public void onPropertyChanged(@Nonnull final String propertyName, @Nonnull final String propertyValue) {
262         Optional.ofNullable(ForwardingRulesProperty.forValue(propertyName)).ifPresent(forwardingRulesProperty -> {
263             switch (forwardingRulesProperty) {
264                 case DISABLE_RECONCILIATION:
265                     disableReconciliation = Boolean.valueOf(propertyValue);
266                     break;
267                 case STALE_MARKING_ENABLED:
268                     staleMarkingEnabled = Boolean.valueOf(propertyValue);
269                     break;
270                 case RECONCILIATION_RETRY_COUNT:
271                     reconciliationRetryCount = Integer.valueOf(propertyValue);
272                     break;
273             }
274         });
275     }
276 }