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 java.lang.management.ManagementFactory;
18 import java.text.SimpleDateFormat;
19 import java.time.LocalDateTime;
20 import java.util.Date;
21 import java.util.List;
23 import java.util.Optional;
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.ExecutorService;
27 import java.util.concurrent.Executors;
28 import java.util.stream.Collectors;
29 import javax.annotation.PreDestroy;
30 import javax.inject.Inject;
31 import javax.inject.Singleton;
32 import javax.management.InstanceAlreadyExistsException;
33 import javax.management.InstanceNotFoundException;
34 import javax.management.MBeanRegistrationException;
35 import javax.management.MalformedObjectNameException;
36 import javax.management.NotCompliantMBeanException;
37 import javax.management.ObjectName;
38 import org.eclipse.jdt.annotation.NonNull;
39 import org.opendaylight.mdsal.binding.api.DataBroker;
40 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
41 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
42 import org.opendaylight.openflowplugin.api.openflow.FlowGroupCacheManager;
43 import org.opendaylight.openflowplugin.api.openflow.ReconciliationState;
44 import org.opendaylight.openflowplugin.applications.frm.FlowNodeReconciliation;
45 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
46 import org.opendaylight.openflowplugin.applications.southboundcli.alarm.NodeReconciliationAlarm;
47 import org.opendaylight.openflowplugin.applications.southboundcli.util.OFNode;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.Reconcile;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.ReconcileInput;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.ReconcileOutput;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.ReconcileOutputBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.ReconciliationCounter;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.reconciliation.counter.ReconcileCounter;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.reconciliation.counter.ReconcileCounterBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.reconciliation.counter.ReconcileCounterKey;
62 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
63 import org.opendaylight.yangtools.yang.common.ErrorTag;
64 import org.opendaylight.yangtools.yang.common.ErrorType;
65 import org.opendaylight.yangtools.yang.common.RpcResult;
66 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
67 import org.opendaylight.yangtools.yang.common.Uint32;
68 import org.opendaylight.yangtools.yang.common.Uint64;
69 import org.osgi.service.component.annotations.Activate;
70 import org.osgi.service.component.annotations.Component;
71 import org.osgi.service.component.annotations.Deactivate;
72 import org.osgi.service.component.annotations.Reference;
73 import org.slf4j.Logger;
74 import org.slf4j.LoggerFactory;
77 @Component(service = ReconcileService.class, immediate = true)
78 // FIXME: this should probably live in FRM, but how does it integrate with its functionality?
79 public final class DefaultReconcileService implements Reconcile, ReconcileService, AutoCloseable {
80 private static final Logger LOG = LoggerFactory.getLogger(DefaultReconcileService.class);
81 private static final ObjectName ALARM_NAME;
85 ALARM_NAME = new ObjectName("SDNC.FM:name=NodeReconciliationOperationOngoingBean");
86 } catch (MalformedObjectNameException e) {
87 throw new ExceptionInInitializerError(e);
91 private final NodeReconciliationAlarm alarm = new NodeReconciliationAlarm();
92 private final Map<String, ReconciliationState> reconciliationStates;
93 private final FlowNodeReconciliation flowNodeReconciliation;
94 private final DpnTracker dpnTracker;
95 private final DataBroker broker;
97 private ExecutorService executor = Executors.newWorkStealingPool(10);
98 private boolean unregister = false;
102 public DefaultReconcileService(@Reference final DataBroker broker, @Reference final ForwardingRulesManager frm,
103 @Reference final DpnTracker dpnTracker, @Reference final FlowGroupCacheManager flowGroupCacheManager) {
104 this.broker = requireNonNull(broker);
105 flowNodeReconciliation = frm.getFlowNodeReconciliation();
106 this.dpnTracker = requireNonNull(dpnTracker);
107 reconciliationStates = flowGroupCacheManager.getReconciliationStates();
109 final var mbs = ManagementFactory.getPlatformMBeanServer();
110 if (!mbs.isRegistered(ALARM_NAME)) {
112 mbs.registerMBean(alarm, ALARM_NAME);
114 LOG.info("Registered Mbean {} successfully", ALARM_NAME);
115 } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e) {
116 LOG.error("Registeration failed for Mbean {}", ALARM_NAME, e);
124 public void close() {
128 ManagementFactory.getPlatformMBeanServer().unregisterMBean(ALARM_NAME);
129 } catch (MBeanRegistrationException | InstanceNotFoundException e) {
130 LOG.error("Unregisteration failed for Mbean {}", ALARM_NAME, e);
134 if (executor != null) {
135 executor.shutdownNow();
141 public ListenableFuture<RpcResult<ReconcileOutput>> reconcile(final Set<Uint64> nodes) {
142 if (nodes == null || nodes.isEmpty()) {
143 return buildErrorResponse("Error executing command reconcile. No Node information was specified.");
146 final var allNodes = getAllNodes();
147 final var unresolvedNodes = nodes.stream().filter(node -> !allNodes.contains(node.longValue()))
148 .collect(Collectors.toList());
149 if (!unresolvedNodes.isEmpty()) {
150 return buildErrorResponse("Error executing command reconcile. "
151 + "Node(s) not found: " + String.join(", ", unresolvedNodes.toString()));
153 return doReconcile(nodes.stream().map(Uint64::longValue).collect(Collectors.toList()));
157 public ListenableFuture<RpcResult<ReconcileOutput>> reconcileAll() {
158 return doReconcile(getAllNodes());
161 private @NonNull ListenableFuture<RpcResult<ReconcileOutput>> doReconcile(final List<Long> nodes) {
162 if (!nodes.isEmpty()) {
163 return buildErrorResponse(
164 "Error executing command reconcile. No node information is found for reconciliation");
166 final var inprogressNodes = ImmutableSet.<Uint64>builder();
167 nodes.parallelStream().forEach(nodeId -> {
168 ReconciliationState state = getReconciliationState(nodeId);
169 if (state != null && state.getState().equals(STARTED)) {
170 inprogressNodes.add(Uint64.valueOf(nodeId));
172 final var alarmText = getAlarmText(nodeId, " started reconciliation");
173 final var source = getSourceText(nodeId);
174 LOG.debug("Raising NodeReconciliationOperationOngoing alarm, alarmText {} source {}", alarmText,
176 alarm.raiseAlarm("NodeReconciliationOperationOngoing", alarmText, source);
177 LOG.info("Executing reconciliation for node {} with state ", nodeId);
178 NodeKey nodeKey = new NodeKey(new NodeId("openflow:" + nodeId));
179 executor.execute(new ReconciliationTask(Uint64.valueOf(nodeId), nodeKey));
182 return RpcResultBuilder.success(new ReconcileOutputBuilder()
183 .setInprogressNodes(inprogressNodes.build())
189 public ListenableFuture<RpcResult<ReconcileOutput>> invoke(final ReconcileInput input) {
190 final var reconcileAllNodes = input.getReconcileAllNodes();
191 final var nodes = input.getNodes();
192 if (reconcileAllNodes != null && reconcileAllNodes) {
193 if (nodes != null && nodes.size() > 0) {
194 return buildErrorResponse("Error executing command reconcile. If 'all' option is enabled, no Node "
195 + "must be specified as input parameter.");
197 return reconcileAll();
199 return reconcile(nodes == null ? Set.of() : nodes);
202 private ReconciliationState getReconciliationState(final Long nodeId) {
203 return reconciliationStates.get(nodeId.toString());
206 private static @NonNull ListenableFuture<RpcResult<ReconcileOutput>> buildErrorResponse(final String msg) {
207 LOG.error("Error {}", msg);
208 return RpcResultBuilder.<ReconcileOutput>failed()
209 .withError(ErrorType.PROTOCOL, new ErrorTag("reconcile"), msg)
213 private List<Long> getAllNodes() {
214 return dpnTracker.currentNodes().stream().distinct().map(OFNode::getNodeId).collect(Collectors.toList());
218 * Method gets the alarm text for the nodeId.
220 * @param nodeId Source of the alarm nodeId
221 * @param event reason for alarm invoke/clear
223 private static @NonNull String getAlarmText(final Long nodeId, final String event) {
224 return "OF Switch " + nodeId + event;
228 * Method gets the source text for the nodeId.
230 * @param nodeId Source of the alarm nodeId
232 private static String getSourceText(final Long nodeId) {
233 return "Dpn=" + nodeId;
236 private final class ReconciliationTask implements Runnable {
237 private static final String DATE_AND_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
238 private final NodeKey nodeKey;
239 private final Uint64 nodeId;
241 private ReconciliationTask(final Uint64 nodeId, final NodeKey nodeKey) {
242 this.nodeId = nodeId;
243 this.nodeKey = nodeKey;
248 updateReconciliationState(STARTED);
249 final var reconOutput = flowNodeReconciliation.reconcileConfiguration(
250 InstanceIdentifier.create(Nodes.class)
251 .child(Node.class, nodeKey)
252 .augmentation(FlowCapableNode.class));
254 final boolean rpcResult = reconOutput.get();
255 increaseReconcileCount(rpcResult);
257 updateReconciliationState(COMPLETED);
258 LOG.info("Reconciliation successfully completed for node {}", nodeId);
260 updateReconciliationState(FAILED);
261 LOG.error("Reconciliation failed for node {}", nodeId);
263 } catch (ExecutionException | InterruptedException e) {
264 increaseReconcileCount(false);
265 updateReconciliationState(FAILED);
266 LOG.error("Error occurred while invoking reconcile RPC for node {}", nodeId, e);
268 final var dpnId = nodeId.longValue();
269 final var alarmText = getAlarmText(dpnId, " finished reconciliation");
270 final var source = getSourceText(dpnId);
271 LOG.debug("Clearing NodeReconciliationOperationOngoing alarm of source {}", source);
272 alarm.clearAlarm("NodeReconciliationOperationOngoing", alarmText, source);
276 private void increaseReconcileCount(final boolean isSuccess) {
277 // FIXME: do not use SimpleDateFormat
278 final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_AND_TIME_FORMAT);
279 InstanceIdentifier<ReconcileCounter> instanceIdentifier = InstanceIdentifier
280 .builder(ReconciliationCounter.class).child(ReconcileCounter.class,
281 new ReconcileCounterKey(nodeId)).build();
282 ReadWriteTransaction tx = broker.newReadWriteTransaction();
283 Optional<ReconcileCounter> count = getReconciliationCount(tx, instanceIdentifier);
284 ReconcileCounterBuilder counterBuilder = new ReconcileCounterBuilder()
285 .withKey(new ReconcileCounterKey(nodeId))
286 .setLastRequestTime(new DateAndTime(simpleDateFormat.format(new Date())));
289 if (count.isPresent()) {
290 long successCount = count.orElseThrow().getSuccessCount().toJava();
291 counterBuilder.setSuccessCount(Uint32.valueOf(++successCount));
292 LOG.debug("Reconcile success count {} for the node: {} ", successCount, nodeId);
294 counterBuilder.setSuccessCount(Uint32.ONE);
296 } else if (count.isPresent()) {
297 long failureCount = count.orElseThrow().getFailureCount().toJava();
298 counterBuilder.setFailureCount(Uint32.valueOf(++failureCount));
299 LOG.debug("Reconcile failure count {} for the node: {} ", failureCount, nodeId);
301 counterBuilder.setFailureCount(Uint32.ONE);
304 tx.mergeParentStructureMerge(LogicalDatastoreType.OPERATIONAL, instanceIdentifier,
305 counterBuilder.build());
307 } catch (InterruptedException | ExecutionException e) {
308 LOG.error("Exception while submitting counter for {}", nodeId, e);
312 private Optional<ReconcileCounter> getReconciliationCount(final ReadWriteTransaction tx,
313 final InstanceIdentifier<ReconcileCounter> instanceIdentifier) {
315 return tx.read(LogicalDatastoreType.OPERATIONAL, instanceIdentifier).get();
316 } catch (InterruptedException | ExecutionException e) {
317 LOG.error("Exception while reading counter for node: {}", nodeId, e);
319 return Optional.empty();
322 private void updateReconciliationState(final ReconciliationState.ReconciliationStatus status) {
323 reconciliationStates.put(nodeId.toString(), new ReconciliationState(status, LocalDateTime.now()));