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
8 package org.opendaylight.openflowplugin.applications.southboundcli;
10 import static java.util.Objects.requireNonNull;
11 import static org.opendaylight.openflowplugin.api.openflow.ReconciliationState.ReconciliationStatus.COMPLETED;
12 import static org.opendaylight.openflowplugin.api.openflow.ReconciliationState.ReconciliationStatus.FAILED;
13 import static org.opendaylight.openflowplugin.api.openflow.ReconciliationState.ReconciliationStatus.STARTED;
15 import com.google.common.collect.ImmutableSet;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import com.google.common.util.concurrent.SettableFuture;
18 import java.lang.management.ManagementFactory;
19 import java.text.SimpleDateFormat;
20 import java.time.LocalDateTime;
21 import java.util.Date;
22 import java.util.List;
24 import java.util.Optional;
26 import java.util.concurrent.ExecutionException;
27 import java.util.concurrent.ExecutorService;
28 import java.util.concurrent.Executors;
29 import java.util.stream.Collectors;
30 import javax.management.InstanceAlreadyExistsException;
31 import javax.management.InstanceNotFoundException;
32 import javax.management.MBeanRegistrationException;
33 import javax.management.MalformedObjectNameException;
34 import javax.management.NotCompliantMBeanException;
35 import javax.management.ObjectName;
36 import org.eclipse.jdt.annotation.NonNull;
37 import org.opendaylight.mdsal.binding.api.DataBroker;
38 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
39 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
40 import org.opendaylight.openflowplugin.api.openflow.FlowGroupCacheManager;
41 import org.opendaylight.openflowplugin.api.openflow.ReconciliationState;
42 import org.opendaylight.openflowplugin.applications.frm.FlowNodeReconciliation;
43 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
44 import org.opendaylight.openflowplugin.applications.southboundcli.alarm.NodeReconciliationAlarm;
45 import org.opendaylight.openflowplugin.applications.southboundcli.util.OFNode;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.ReconcileInput;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.ReconcileOutput;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.ReconcileOutputBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.ReconciliationCounter;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.ReconciliationService;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.reconciliation.counter.ReconcileCounter;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.reconciliation.counter.ReconcileCounterBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.reconciliation.counter.ReconcileCounterKey;
60 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
61 import org.opendaylight.yangtools.yang.common.ErrorTag;
62 import org.opendaylight.yangtools.yang.common.ErrorType;
63 import org.opendaylight.yangtools.yang.common.RpcResult;
64 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
65 import org.opendaylight.yangtools.yang.common.Uint32;
66 import org.opendaylight.yangtools.yang.common.Uint64;
67 import org.slf4j.Logger;
68 import org.slf4j.LoggerFactory;
70 // FIXME: this is not just a CLI component, it should live somewhere else
71 public final class ReconciliationServiceImpl implements ReconciliationService, AutoCloseable {
72 private static final Logger LOG = LoggerFactory.getLogger(ReconciliationServiceImpl.class);
73 private static final ObjectName ALARM_NAME;
77 ALARM_NAME = new ObjectName("SDNC.FM:name=NodeReconciliationOperationOngoingBean");
78 } catch (MalformedObjectNameException e) {
79 throw new ExceptionInInitializerError(e);
83 private final NodeReconciliationAlarm alarm = new NodeReconciliationAlarm();
84 private final Map<String, ReconciliationState> reconciliationStates;
85 private final FlowNodeReconciliation flowNodeReconciliation;
86 private final DpnTracker dpnTracker;
87 private final DataBroker broker;
89 private ExecutorService executor = Executors.newWorkStealingPool(10);
90 private boolean unregister;
92 public ReconciliationServiceImpl(final DataBroker broker, final ForwardingRulesManager frm,
93 final DpnTracker dpnTracker, final FlowGroupCacheManager flowGroupCacheManager) {
94 this.broker = requireNonNull(broker);
95 flowNodeReconciliation = frm.getFlowNodeReconciliation();
96 this.dpnTracker = requireNonNull(dpnTracker);
97 reconciliationStates = flowGroupCacheManager.getReconciliationStates();
100 final var mbs = ManagementFactory.getPlatformMBeanServer();
101 if (!mbs.isRegistered(ALARM_NAME)) {
103 mbs.registerMBean(alarm, ALARM_NAME);
105 LOG.info("Registered Mbean {} successfully", ALARM_NAME);
106 } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e) {
107 LOG.error("Registeration failed for Mbean {}", ALARM_NAME, e);
113 public void close() {
117 ManagementFactory.getPlatformMBeanServer().unregisterMBean(ALARM_NAME);
118 } catch (MBeanRegistrationException | InstanceNotFoundException e) {
119 LOG.error("Unregisteration failed for Mbean {}", ALARM_NAME, e);
123 if (executor != null) {
124 executor.shutdownNow();
130 public ListenableFuture<RpcResult<ReconcileOutput>> reconcile(final ReconcileInput input) {
131 boolean reconcileAllNodes = input.getReconcileAllNodes();
132 Set<Uint64> inputNodes = input.getNodes();
133 if (inputNodes == null) {
134 inputNodes = Set.of();
136 if (reconcileAllNodes && inputNodes.size() > 0) {
137 return buildErrorResponse("Error executing command reconcile. "
138 + "If 'all' option is enabled, no Node must be specified as input parameter.");
140 if (!reconcileAllNodes && inputNodes.size() == 0) {
141 return buildErrorResponse("Error executing command reconcile. No Node information was specified.");
143 SettableFuture<RpcResult<ReconcileOutput>> result = SettableFuture.create();
144 List<Long> nodeList = getAllNodes();
145 List<Long> nodesToReconcile = reconcileAllNodes ? nodeList :
146 inputNodes.stream().distinct().map(Uint64::longValue).collect(Collectors.toList());
147 if (nodesToReconcile.size() > 0) {
148 List<Long> unresolvedNodes =
149 nodesToReconcile.stream().filter(node -> !nodeList.contains(node)).collect(Collectors.toList());
150 if (!unresolvedNodes.isEmpty()) {
151 return buildErrorResponse("Error executing command reconcile. "
152 + "Node(s) not found: " + String.join(", ", unresolvedNodes.toString()));
154 ImmutableSet.Builder<Uint64> inprogressNodes = ImmutableSet.builder();
155 nodesToReconcile.parallelStream().forEach(nodeId -> {
156 ReconciliationState state = getReconciliationState(nodeId);
157 if (state != null && state.getState().equals(STARTED)) {
158 inprogressNodes.add(Uint64.valueOf(nodeId));
160 final var alarmText = getAlarmText(nodeId, " started reconciliation");
161 final var source = getSourceText(nodeId);
162 LOG.debug("Raising NodeReconciliationOperationOngoing alarm, alarmText {} source {}", alarmText,
164 alarm.raiseAlarm("NodeReconciliationOperationOngoing", alarmText, source);
165 LOG.info("Executing reconciliation for node {} with state ", nodeId);
166 NodeKey nodeKey = new NodeKey(new NodeId("openflow:" + nodeId));
167 executor.execute(new ReconciliationTask(Uint64.valueOf(nodeId), nodeKey));
170 ReconcileOutput reconcilingInProgress = new ReconcileOutputBuilder()
171 .setInprogressNodes(inprogressNodes.build())
173 result.set(RpcResultBuilder.success(reconcilingInProgress).build());
176 return buildErrorResponse("Error executing command reconcile. "
177 + "No node information is found for reconciliation");
181 private ReconciliationState getReconciliationState(final Long nodeId) {
182 return reconciliationStates.get(nodeId.toString());
185 private static ListenableFuture<RpcResult<ReconcileOutput>> buildErrorResponse(final String msg) {
186 LOG.error("Error {}", msg);
187 return RpcResultBuilder.<ReconcileOutput>failed()
188 .withError(ErrorType.PROTOCOL, new ErrorTag("reconcile"), msg)
192 private List<Long> getAllNodes() {
193 return dpnTracker.currentNodes().stream().distinct().map(OFNode::getNodeId).collect(Collectors.toList());
197 * Method gets the alarm text for the nodeId.
199 * @param nodeId Source of the alarm nodeId
200 * @param event reason for alarm invoke/clear
202 private static @NonNull String getAlarmText(final Long nodeId, final String event) {
203 return "OF Switch " + nodeId + event;
207 * Method gets the source text for the nodeId.
209 * @param nodeId Source of the alarm nodeId
211 private static String getSourceText(final Long nodeId) {
212 return "Dpn=" + nodeId;
215 private final class ReconciliationTask implements Runnable {
216 private static final String DATE_AND_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
217 private final NodeKey nodeKey;
218 private final Uint64 nodeId;
220 private ReconciliationTask(final Uint64 nodeId, final NodeKey nodeKey) {
221 this.nodeId = nodeId;
222 this.nodeKey = nodeKey;
227 updateReconciliationState(STARTED);
228 final var reconOutput = flowNodeReconciliation.reconcileConfiguration(
229 InstanceIdentifier.create(Nodes.class)
230 .child(Node.class, nodeKey)
231 .augmentation(FlowCapableNode.class));
233 final boolean rpcResult = reconOutput.get();
234 increaseReconcileCount(rpcResult);
236 updateReconciliationState(COMPLETED);
237 LOG.info("Reconciliation successfully completed for node {}", nodeId);
239 updateReconciliationState(FAILED);
240 LOG.error("Reconciliation failed for node {}", nodeId);
242 } catch (ExecutionException | InterruptedException e) {
243 increaseReconcileCount(false);
244 updateReconciliationState(FAILED);
245 LOG.error("Error occurred while invoking reconcile RPC for node {}", nodeId, e);
247 final var dpnId = nodeId.longValue();
248 final var alarmText = getAlarmText(dpnId, " finished reconciliation");
249 final var source = getSourceText(dpnId);
250 LOG.debug("Clearing NodeReconciliationOperationOngoing alarm of source {}", source);
251 alarm.clearAlarm("NodeReconciliationOperationOngoing", alarmText, source);
255 private void increaseReconcileCount(final boolean isSuccess) {
256 // FIXME: do not use SimpleDateFormat
257 final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_AND_TIME_FORMAT);
258 InstanceIdentifier<ReconcileCounter> instanceIdentifier = InstanceIdentifier
259 .builder(ReconciliationCounter.class).child(ReconcileCounter.class,
260 new ReconcileCounterKey(nodeId)).build();
261 ReadWriteTransaction tx = broker.newReadWriteTransaction();
262 Optional<ReconcileCounter> count = getReconciliationCount(tx, instanceIdentifier);
263 ReconcileCounterBuilder counterBuilder = new ReconcileCounterBuilder()
264 .withKey(new ReconcileCounterKey(nodeId))
265 .setLastRequestTime(new DateAndTime(simpleDateFormat.format(new Date())));
268 if (count.isPresent()) {
269 long successCount = count.orElseThrow().getSuccessCount().toJava();
270 counterBuilder.setSuccessCount(Uint32.valueOf(++successCount));
271 LOG.debug("Reconcile success count {} for the node: {} ", successCount, nodeId);
273 counterBuilder.setSuccessCount(Uint32.ONE);
275 } else if (count.isPresent()) {
276 long failureCount = count.orElseThrow().getFailureCount().toJava();
277 counterBuilder.setFailureCount(Uint32.valueOf(++failureCount));
278 LOG.debug("Reconcile failure count {} for the node: {} ", failureCount, nodeId);
280 counterBuilder.setFailureCount(Uint32.ONE);
283 tx.mergeParentStructureMerge(LogicalDatastoreType.OPERATIONAL, instanceIdentifier,
284 counterBuilder.build());
286 } catch (InterruptedException | ExecutionException e) {
287 LOG.error("Exception while submitting counter for {}", nodeId, e);
291 private Optional<ReconcileCounter> getReconciliationCount(final ReadWriteTransaction tx,
292 final InstanceIdentifier<ReconcileCounter> instanceIdentifier) {
294 return tx.read(LogicalDatastoreType.OPERATIONAL, instanceIdentifier).get();
295 } catch (InterruptedException | ExecutionException e) {
296 LOG.error("Exception while reading counter for node: {}", nodeId, e);
298 return Optional.empty();
301 private void updateReconciliationState(final ReconciliationState.ReconciliationStatus status) {
302 reconciliationStates.put(nodeId.toString(), new ReconciliationState(status, LocalDateTime.now()));