2 * Copyright (c) 2018 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.applications.arbitratorreconciliation.impl;
11 import com.google.common.base.Function;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.ListeningExecutorService;
17 import com.google.common.util.concurrent.MoreExecutors;
18 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.List;
23 import java.util.concurrent.Callable;
24 import java.util.concurrent.ConcurrentHashMap;
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.atomic.AtomicLong;
28 import javax.annotation.PostConstruct;
29 import javax.annotation.PreDestroy;
30 import javax.inject.Inject;
31 import javax.inject.Singleton;
32 import org.apache.aries.blueprint.annotation.service.Reference;
33 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
34 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
35 import org.opendaylight.openflowplugin.api.OFConstants;
36 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
37 import org.opendaylight.openflowplugin.applications.reconciliation.NotificationRegistration;
38 import org.opendaylight.openflowplugin.applications.reconciliation.ReconciliationManager;
39 import org.opendaylight.openflowplugin.applications.reconciliation.ReconciliationNotificationListener;
40 import org.opendaylight.serviceutils.upgrade.UpgradeState;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.AddBundleMessagesInput;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.AddBundleMessagesInputBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.AddBundleMessagesOutput;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.ControlBundleInput;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.ControlBundleInputBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.ControlBundleOutput;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.SalBundleService;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.add.bundle.messages.input.Messages;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.add.bundle.messages.input.MessagesBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.add.bundle.messages.input.messages.Message;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.add.bundle.messages.input.messages.MessageBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.BundleRemoveFlowCase;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.BundleRemoveFlowCaseBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.BundleRemoveGroupCase;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.BundleRemoveGroupCaseBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.bundle.remove.flow._case.RemoveFlowCaseDataBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.bundle.remove.group._case.RemoveGroupCaseDataBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundleControlType;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundleFlags;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundleId;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.arbitrator.reconcile.service.rev180227.ArbitratorReconcileService;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.arbitrator.reconcile.service.rev180227.CommitActiveBundleInput;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.arbitrator.reconcile.service.rev180227.CommitActiveBundleOutput;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.arbitrator.reconcile.service.rev180227.CommitActiveBundleOutputBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.arbitrator.reconcile.service.rev180227.GetActiveBundleInput;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.arbitrator.reconcile.service.rev180227.GetActiveBundleOutput;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.arbitrator.reconcile.service.rev180227.GetActiveBundleOutputBuilder;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.rf.state.rev170713.ResultState;
79 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
80 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
81 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
82 import org.opendaylight.yangtools.yang.common.RpcError;
83 import org.opendaylight.yangtools.yang.common.RpcResult;
84 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
85 import org.opendaylight.yangtools.yang.common.Uint64;
86 import org.slf4j.Logger;
87 import org.slf4j.LoggerFactory;
90 public class ArbitratorReconciliationManagerImpl implements ArbitratorReconcileService,
91 ReconciliationNotificationListener, AutoCloseable {
93 private static final Logger LOG = LoggerFactory.getLogger(ArbitratorReconciliationManagerImpl.class);
94 private static final int THREAD_POOL_SIZE = 4;
95 private static final AtomicLong BUNDLE_ID = new AtomicLong();
96 private static final BundleFlags BUNDLE_FLAGS = new BundleFlags(true, true);
97 private static final int ARBITRATOR_RECONCILIATION_PRIORITY = Integer
98 .getInteger("arbitrator.reconciliation.manager.priority", 0/*default*/);
99 private static final String SERVICE_NAME = "ArbitratorReconciliationManager";
100 private static final String SEPARATOR = ":";
102 private static final BundleRemoveFlowCase DELETE_ALL_FLOW = new BundleRemoveFlowCaseBuilder()
103 .setRemoveFlowCaseData(
104 new RemoveFlowCaseDataBuilder(new FlowBuilder().setTableId(OFConstants.OFPTT_ALL).build()).build())
106 private static final BundleRemoveGroupCase DELETE_ALL_GROUP = new BundleRemoveGroupCaseBuilder()
107 .setRemoveGroupCaseData(new RemoveGroupCaseDataBuilder(new GroupBuilder()
108 .setGroupType(GroupTypes.GroupAll)
109 .setGroupId(new GroupId(OFConstants.OFPG_ALL))
113 private final SalBundleService salBundleService;
114 private final ReconciliationManager reconciliationManager;
115 private final RoutedRpcRegistration routedRpcReg;
116 private final UpgradeState upgradeState;
117 private NotificationRegistration registration;
118 private final ListeningExecutorService executor = MoreExecutors.listeningDecorator(
119 Executors.newFixedThreadPool(THREAD_POOL_SIZE));
120 private final Map<Uint64, BundleDetails> bundleIdMap = new ConcurrentHashMap<>();
123 public ArbitratorReconciliationManagerImpl(@Reference RpcProviderRegistry rpcRegistry,
124 @Reference ReconciliationManager reconciliationManager, @Reference UpgradeState upgradeState) {
125 Preconditions.checkArgument(rpcRegistry != null, "RpcConsumerRegistry cannot be null !");
126 this.reconciliationManager = Preconditions.checkNotNull(reconciliationManager,
127 "ReconciliationManager cannot be null!");
128 this.salBundleService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalBundleService.class),
129 "RPC SalBundlService not found.");
130 this.routedRpcReg = rpcRegistry.addRoutedRpcImplementation(ArbitratorReconcileService.class,
132 this.upgradeState = Preconditions.checkNotNull(upgradeState, "UpgradeState cannot be null!");
136 public void start() {
137 registration = reconciliationManager.registerService(this);
138 LOG.info("ArbitratorReconciliationManager has started successfully.");
143 public void close() throws Exception {
145 if (registration != null) {
146 registration.close();
152 public ListenableFuture<RpcResult<CommitActiveBundleOutput>> commitActiveBundle(
153 CommitActiveBundleInput input) {
154 Uint64 nodeId = input.getNodeId();
155 if (bundleIdMap.containsKey(nodeId)) {
156 BundleId bundleId = bundleIdMap.get(nodeId).getBundleId();
157 if (bundleId != null) {
158 final ControlBundleInput commitBundleInput = new ControlBundleInputBuilder()
159 .setNode(input.getNode())
160 .setBundleId(bundleId)
161 .setFlags(BUNDLE_FLAGS)
162 .setType(BundleControlType.ONFBCTCOMMITREQUEST)
164 ListenableFuture<RpcResult<ControlBundleOutput>> rpcResult = salBundleService
165 .controlBundle(commitBundleInput);
166 bundleIdMap.put(nodeId, new BundleDetails(bundleId, rpcResult));
168 Futures.addCallback(rpcResult,
169 new CommitActiveBundleCallback(nodeId),
170 MoreExecutors.directExecutor());
171 return Futures.transform(
173 this.createRpcResultCondenser("committed active bundle"),
174 MoreExecutors.directExecutor());
177 return RpcResultBuilder.success(new CommitActiveBundleOutputBuilder()
178 .setResult(null).build())
179 .withRpcErrors(Collections.singleton(RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION,
180 null, "No active bundle found for the node" + nodeId.toString()))).buildFuture();
184 public ListenableFuture<RpcResult<GetActiveBundleOutput>> getActiveBundle(final GetActiveBundleInput input) {
185 Uint64 nodeId = input.getNodeId();
186 BundleDetails bundleDetails = bundleIdMap.get(nodeId);
187 if (bundleDetails != null) {
189 //This blocking call is used to prevent the applications from pushing flows and groups via the default
190 // pipeline when the commit bundle is ongoing.
191 bundleDetails.getResult().get();
192 return RpcResultBuilder.success(new GetActiveBundleOutputBuilder()
193 .setResult(bundleDetails.getBundleId())
196 } catch (InterruptedException | ExecutionException | NullPointerException e) {
197 return RpcResultBuilder.<GetActiveBundleOutput>failed()
198 .withRpcErrors(Collections.singleton(RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION,
199 null, e.getMessage()))).buildFuture();
202 return RpcResultBuilder.success(new GetActiveBundleOutputBuilder()
203 .setResult(null).build()).buildFuture();
207 public ListenableFuture<Boolean> startReconciliation(DeviceInfo node) {
209 if (upgradeState.isUpgradeInProgress()) {
210 LOG.trace("Starting arbitrator reconciliation for node {}", node.getDatapathId());
211 return reconcileConfiguration(node);
213 LOG.trace("arbitrator reconciliation is disabled");
214 return FluentFutures.immediateTrueFluentFuture();
218 public ListenableFuture<Boolean> endReconciliation(DeviceInfo node) {
219 Uint64 datapathId = node.getDatapathId();
220 LOG.trace("Stopping arbitrator reconciliation for node {}", datapathId);
221 bundleIdMap.remove(datapathId);
223 return FluentFutures.immediateTrueFluentFuture();
227 public int getPriority() {
228 return ARBITRATOR_RECONCILIATION_PRIORITY;
232 public String getName() {
237 public ResultState getResultState() {
238 return ResultState.DONOTHING;
241 private ListenableFuture<Boolean> reconcileConfiguration(DeviceInfo node) {
242 LOG.info("Triggering arbitrator reconciliation for device {}", node.getDatapathId());
243 ArbitratorReconciliationTask upgradeReconTask = new ArbitratorReconciliationTask(node);
244 return executor.submit(upgradeReconTask);
247 @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
248 justification = "https://github.com/spotbugs/spotbugs/issues/811")
249 private Messages createMessages(final NodeRef nodeRef) {
250 final List<Message> messages = new ArrayList<>();
251 messages.add(new MessageBuilder()
253 .setBundleInnerMessage(DELETE_ALL_FLOW).build());
254 messages.add(new MessageBuilder()
256 .setBundleInnerMessage(DELETE_ALL_GROUP).build());
257 LOG.debug("The size of the flows and group messages created in createMessage() {}", messages.size());
258 return new MessagesBuilder().setMessage(messages).build();
261 private class ArbitratorReconciliationTask implements Callable<Boolean> {
262 final DeviceInfo deviceInfo;
264 ArbitratorReconciliationTask(final DeviceInfo deviceInfo) {
265 this.deviceInfo = deviceInfo;
269 public Boolean call() {
270 InstanceIdentifier<FlowCapableNode> nodeIdentity = deviceInfo.getNodeInstanceIdentifier()
271 .augmentation(FlowCapableNode.class);
272 String node = nodeIdentity.firstKeyOf(Node.class).getId().getValue();
273 BundleId bundleIdValue = new BundleId(BUNDLE_ID.getAndIncrement());
274 LOG.debug("Triggering arbitrator reconciliation for device :{}", node);
275 final NodeRef nodeRef = new NodeRef(nodeIdentity.firstIdentifierOf(Node.class));
277 final ControlBundleInput closeBundleInput = new ControlBundleInputBuilder()
279 .setBundleId(bundleIdValue)
280 .setFlags(BUNDLE_FLAGS)
281 .setType(BundleControlType.ONFBCTCLOSEREQUEST)
284 final ControlBundleInput openBundleInput = new ControlBundleInputBuilder()
286 .setBundleId(bundleIdValue)
287 .setFlags(BUNDLE_FLAGS)
288 .setType(BundleControlType.ONFBCTOPENREQUEST)
291 final AddBundleMessagesInput addBundleMessagesInput = new AddBundleMessagesInputBuilder()
293 .setBundleId(bundleIdValue)
294 .setFlags(BUNDLE_FLAGS)
295 .setMessages(createMessages(nodeRef))
298 ListenableFuture<RpcResult<ControlBundleOutput>> closeBundle = salBundleService
299 .controlBundle(closeBundleInput);
301 ListenableFuture<RpcResult<ControlBundleOutput>> openBundleMessagesFuture = Futures
302 .transformAsync(closeBundle, rpcResult -> salBundleService
303 .controlBundle(openBundleInput), MoreExecutors.directExecutor());
305 ListenableFuture<RpcResult<AddBundleMessagesOutput>> addBundleMessagesFuture = Futures
306 .transformAsync(openBundleMessagesFuture, rpcResult -> {
307 if (rpcResult.isSuccessful()) {
308 return salBundleService
309 .addBundleMessages(addBundleMessagesInput);
311 return FluentFutures.immediateNullFluentFuture();
312 }, MoreExecutors.directExecutor());
313 Uint64 nodeId = getDpnIdFromNodeName(node);
315 if (addBundleMessagesFuture.get().isSuccessful()) {
316 bundleIdMap.put(nodeId, new BundleDetails(bundleIdValue,FluentFutures.immediateNullFluentFuture()));
317 LOG.debug("Arbitrator reconciliation initial task has been completed for node {} ", nodeId);
320 LOG.error("Error while performing arbitrator reconciliation for device:{}", nodeId);
323 } catch (InterruptedException | ExecutionException e) {
324 LOG.error("Error while performing arbitrator reconciliation for device:{}", nodeId, e);
330 public final class CommitActiveBundleCallback implements FutureCallback<RpcResult<?>> {
331 private final Uint64 nodeId;
333 private CommitActiveBundleCallback(final Uint64 nodeId) {
334 this.nodeId = nodeId;
338 public void onSuccess(RpcResult<?> rpcResult) {
339 LOG.debug("Completed arbitrator reconciliation for device:{}", nodeId);
340 bundleIdMap.remove(nodeId);
344 public void onFailure(Throwable throwable) {
345 LOG.error("Error while performing arbitrator reconciliation for device {}", nodeId, throwable);
349 private <D> Function<RpcResult<D>,
350 RpcResult<CommitActiveBundleOutput>> createRpcResultCondenser(final String action) {
352 final RpcResultBuilder<CommitActiveBundleOutput> resultSink;
354 List<RpcError> errors = new ArrayList<>();
355 if (!input.isSuccessful()) {
356 errors.addAll(input.getErrors());
357 resultSink = RpcResultBuilder.<CommitActiveBundleOutput>failed().withRpcErrors(errors);
359 resultSink = RpcResultBuilder.success();
362 resultSink = RpcResultBuilder.<CommitActiveBundleOutput>failed()
363 .withError(RpcError.ErrorType.APPLICATION, "action of " + action + " failed");
365 return resultSink.build();
369 private void registerRpc(DeviceInfo node) {
370 KeyedInstanceIdentifier<Node, NodeKey> path = InstanceIdentifier.create(Nodes.class)
371 .child(Node.class, new NodeKey(node.getNodeId()));
372 LOG.debug("The path is registered : {}", path);
373 routedRpcReg.registerPath(NodeContext.class, path);
376 private void deregisterRpc(DeviceInfo node) {
377 KeyedInstanceIdentifier<Node, NodeKey> path = InstanceIdentifier.create(Nodes.class)
378 .child(Node.class, new NodeKey(node.getNodeId()));
379 LOG.debug("The path is unregistered : {}", path);
380 routedRpcReg.unregisterPath(NodeContext.class, path);
383 private static class BundleDetails {
384 private final BundleId bundleId;
385 private final ListenableFuture<RpcResult<ControlBundleOutput>> result;
387 BundleDetails(BundleId bundleId, ListenableFuture<RpcResult<ControlBundleOutput>> result) {
388 this.bundleId = bundleId;
389 this.result = result;
392 public BundleId getBundleId() {
396 public ListenableFuture<RpcResult<ControlBundleOutput>> getResult() {
401 @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
402 justification = "https://github.com/spotbugs/spotbugs/issues/811")
403 private Uint64 getDpnIdFromNodeName(String nodeName) {
404 String dpnId = nodeName.substring(nodeName.lastIndexOf(SEPARATOR) + 1);
405 return Uint64.valueOf(dpnId);