Fix contains() check
[openflowplugin.git] / applications / southbound-cli / src / main / java / org / opendaylight / openflowplugin / applications / southboundcli / DefaultReconcileService.java
1 /*
2  * Copyright (c) 2018 Ericsson India Global Services Pvt Ltd. 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 package org.opendaylight.openflowplugin.applications.southboundcli;
9
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;
14
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;
22 import java.util.Map;
23 import java.util.Optional;
24 import java.util.Set;
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;
75
76 @Singleton
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;
82
83     static {
84         try {
85             ALARM_NAME = new ObjectName("SDNC.FM:name=NodeReconciliationOperationOngoingBean");
86         } catch (MalformedObjectNameException e) {
87             throw new ExceptionInInitializerError(e);
88         }
89     }
90
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;
96
97     private ExecutorService executor = Executors.newWorkStealingPool(10);
98     private boolean unregister = false;
99
100     @Inject
101     @Activate
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();
108
109         final var mbs = ManagementFactory.getPlatformMBeanServer();
110         if (!mbs.isRegistered(ALARM_NAME)) {
111             try {
112                 mbs.registerMBean(alarm, ALARM_NAME);
113                 unregister = true;
114                 LOG.info("Registered Mbean {} successfully", ALARM_NAME);
115             } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e) {
116                 LOG.error("Registeration failed for Mbean {}", ALARM_NAME, e);
117             }
118         }
119     }
120
121     @PreDestroy
122     @Deactivate
123     @Override
124     public void close() {
125         if (unregister) {
126             unregister = false;
127             try {
128                 ManagementFactory.getPlatformMBeanServer().unregisterMBean(ALARM_NAME);
129             } catch (MBeanRegistrationException | InstanceNotFoundException e) {
130                 LOG.error("Unregisteration failed for Mbean {}", ALARM_NAME, e);
131             }
132         }
133
134         if (executor != null) {
135             executor.shutdownNow();
136             executor = null;
137         }
138     }
139
140     @Override
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.");
144         }
145
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()));
152         }
153         return doReconcile(nodes.stream().map(Uint64::longValue).collect(Collectors.toList()));
154     }
155
156     @Override
157     public ListenableFuture<RpcResult<ReconcileOutput>> reconcileAll() {
158         return doReconcile(getAllNodes());
159     }
160
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");
165         }
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));
171             } else {
172                 final var alarmText = getAlarmText(nodeId,  " started reconciliation");
173                 final var source = getSourceText(nodeId);
174                 LOG.debug("Raising NodeReconciliationOperationOngoing alarm, alarmText {} source {}", alarmText,
175                     source);
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));
180             }
181         });
182         return RpcResultBuilder.success(new ReconcileOutputBuilder()
183             .setInprogressNodes(inprogressNodes.build())
184             .build())
185             .buildFuture();
186     }
187
188     @Override
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.");
196             }
197             return reconcileAll();
198         }
199         return reconcile(nodes == null ? Set.of() : nodes);
200     }
201
202     private ReconciliationState getReconciliationState(final Long nodeId) {
203         return reconciliationStates.get(nodeId.toString());
204     }
205
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)
210                 .buildFuture();
211     }
212
213     private List<Long> getAllNodes() {
214         return dpnTracker.currentNodes().stream().distinct().map(OFNode::getNodeId).collect(Collectors.toList());
215     }
216
217     /**
218      * Method gets the alarm text for the nodeId.
219      *
220      * @param nodeId Source of the alarm nodeId
221      * @param event reason for alarm invoke/clear
222      */
223     private static @NonNull String getAlarmText(final Long nodeId, final String event) {
224         return "OF Switch " + nodeId + event;
225     }
226
227     /**
228      * Method gets the source text for the nodeId.
229      *
230      * @param nodeId Source of the alarm nodeId
231      */
232     private static String getSourceText(final Long nodeId) {
233         return "Dpn=" + nodeId;
234     }
235
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;
240
241         private ReconciliationTask(final Uint64 nodeId, final NodeKey nodeKey) {
242             this.nodeId = nodeId;
243             this.nodeKey = nodeKey;
244         }
245
246         @Override
247         public void run() {
248             updateReconciliationState(STARTED);
249             final var reconOutput = flowNodeReconciliation.reconcileConfiguration(
250                 InstanceIdentifier.create(Nodes.class)
251                     .child(Node.class, nodeKey)
252                     .augmentation(FlowCapableNode.class));
253             try {
254                 final boolean rpcResult = reconOutput.get();
255                 increaseReconcileCount(rpcResult);
256                 if (rpcResult) {
257                     updateReconciliationState(COMPLETED);
258                     LOG.info("Reconciliation successfully completed for node {}", nodeId);
259                 } else {
260                     updateReconciliationState(FAILED);
261                     LOG.error("Reconciliation failed for node {}", nodeId);
262                 }
263             } catch (ExecutionException | InterruptedException e) {
264                 increaseReconcileCount(false);
265                 updateReconciliationState(FAILED);
266                 LOG.error("Error occurred while invoking reconcile RPC for node {}", nodeId, e);
267             } finally {
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);
273             }
274         }
275
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())));
287
288             if (isSuccess) {
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);
293                 } else {
294                     counterBuilder.setSuccessCount(Uint32.ONE);
295                 }
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);
300             } else {
301                 counterBuilder.setFailureCount(Uint32.ONE);
302             }
303             try {
304                 tx.mergeParentStructureMerge(LogicalDatastoreType.OPERATIONAL, instanceIdentifier,
305                         counterBuilder.build());
306                 tx.commit().get();
307             } catch (InterruptedException | ExecutionException e) {
308                 LOG.error("Exception while submitting counter for {}", nodeId, e);
309             }
310         }
311
312         private Optional<ReconcileCounter> getReconciliationCount(final ReadWriteTransaction tx,
313                 final InstanceIdentifier<ReconcileCounter> instanceIdentifier) {
314             try {
315                 return tx.read(LogicalDatastoreType.OPERATIONAL, instanceIdentifier).get();
316             } catch (InterruptedException | ExecutionException e) {
317                 LOG.error("Exception while reading counter for node: {}", nodeId, e);
318             }
319             return Optional.empty();
320         }
321
322         private void updateReconciliationState(final ReconciliationState.ReconciliationStatus status) {
323             reconciliationStates.put(nodeId.toString(), new ReconciliationState(status, LocalDateTime.now()));
324         }
325     }
326 }
327