f2e99a9ad4b855fcd71851d84cf2eed9a849a450
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.controlloop.eventmanager;
22
23 import static org.onap.policy.controlloop.ControlLoopTargetType.PNF;
24 import static org.onap.policy.controlloop.ControlLoopTargetType.VM;
25 import static org.onap.policy.controlloop.ControlLoopTargetType.VNF;
26
27 import java.io.Serializable;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.LinkedList;
31 import java.util.Map;
32 import java.util.Set;
33 import java.util.UUID;
34 import java.util.concurrent.CompletableFuture;
35 import java.util.concurrent.Executor;
36 import java.util.concurrent.ExecutorService;
37 import java.util.concurrent.ForkJoinPool;
38 import java.util.concurrent.TimeUnit;
39 import java.util.concurrent.atomic.AtomicReference;
40 import java.util.function.Consumer;
41 import java.util.stream.Collectors;
42 import java.util.stream.Stream;
43 import lombok.Getter;
44 import lombok.ToString;
45 import org.apache.commons.lang3.StringUtils;
46 import org.drools.core.WorkingMemory;
47 import org.kie.api.runtime.rule.FactHandle;
48 import org.onap.policy.controlloop.ControlLoopEventStatus;
49 import org.onap.policy.controlloop.ControlLoopException;
50 import org.onap.policy.controlloop.ControlLoopNotificationType;
51 import org.onap.policy.controlloop.ControlLoopOperation;
52 import org.onap.policy.controlloop.VirtualControlLoopEvent;
53 import org.onap.policy.controlloop.VirtualControlLoopNotification;
54 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
55 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
56 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
57 import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
58 import org.onap.policy.controlloop.ophistory.OperationHistoryDataManager;
59 import org.onap.policy.controlloop.policy.FinalResult;
60 import org.onap.policy.controlloop.policy.Policy;
61 import org.onap.policy.controlloop.processor.ControlLoopProcessor;
62 import org.onap.policy.drools.core.lock.LockCallback;
63 import org.onap.policy.drools.system.PolicyEngineConstants;
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
66
67 /**
68  * Manager for a single control loop event. Once this has been created, the event can be
69  * retracted from working memory. Once this has been created, {@link #start()} should be
70  * invoked, and then {@link #nextStep()} should be invoked continually until
71  * {@link #isActive()} returns {@code false}, indicating that all steps have completed.
72  */
73 @ToString(onlyExplicitlyIncluded = true)
74 public class ControlLoopEventManager2 implements ManagerContext, Serializable {
75     private static final Logger logger = LoggerFactory.getLogger(ControlLoopEventManager2.class);
76     private static final long serialVersionUID = -1216568161322872641L;
77
78     private static final String EVENT_MANAGER_SERVICE_CONFIG = "config/event-manager.properties";
79     public static final String PROV_STATUS_ACTIVE = "ACTIVE";
80     private static final String VM_NAME = "VM_NAME";
81     private static final String VNF_NAME = "VNF_NAME";
82     public static final String GENERIC_VNF_VNF_ID = "generic-vnf.vnf-id";
83     public static final String GENERIC_VNF_VNF_NAME = "generic-vnf.vnf-name";
84     public static final String VSERVER_VSERVER_NAME = "vserver.vserver-name";
85     public static final String GENERIC_VNF_IS_CLOSED_LOOP_DISABLED = "generic-vnf.is-closed-loop-disabled";
86     public static final String VSERVER_IS_CLOSED_LOOP_DISABLED = "vserver.is-closed-loop-disabled";
87     public static final String PNF_IS_IN_MAINT = "pnf.in-maint";
88     public static final String GENERIC_VNF_PROV_STATUS = "generic-vnf.prov-status";
89     public static final String VSERVER_PROV_STATUS = "vserver.prov-status";
90     public static final String PNF_ID = "pnf.pnf-id";
91     public static final String PNF_NAME = "pnf.pnf-name";
92
93     private static final Set<String> VALID_TARGETS = Stream
94                     .of(VM_NAME, VNF_NAME, VSERVER_VSERVER_NAME, GENERIC_VNF_VNF_ID, GENERIC_VNF_VNF_NAME, PNF_NAME)
95                     .map(String::toLowerCase).collect(Collectors.toSet());
96
97     private static final Set<String> TRUE_VALUES = Set.of("true", "t", "yes", "y");
98
99     public enum NewEventStatus {
100         FIRST_ONSET, SUBSEQUENT_ONSET, FIRST_ABATEMENT, SUBSEQUENT_ABATEMENT, SYNTAX_ERROR
101     }
102
103     // TODO limit the number of policies that may be executed for a single event?
104
105     /**
106      * {@code True} if this object was created by this JVM instance, {@code false}
107      * otherwise. This will be {@code false} if this object is reconstituted from a
108      * persistent store or by transfer from another server.
109      */
110     private transient boolean createdByThisJvmInstance;
111
112     @Getter
113     @ToString.Include
114     public final String closedLoopControlName;
115     @Getter
116     @ToString.Include
117     private final UUID requestId;
118     private final ControlLoopEventContext context;
119     @ToString.Include
120     private int numOnsets = 1;
121     @ToString.Include
122     private int numAbatements = 0;
123     private VirtualControlLoopEvent abatement = null;
124
125     /**
126      * Time, in milliseconds, when the control loop will time out.
127      */
128     @Getter
129     private final long endTimeMs;
130
131     // fields extracted from the ControlLoopParams
132     @Getter
133     private final String policyName;
134     private final String policyScope;
135     private final String policyVersion;
136
137     private final LinkedList<ControlLoopOperation> controlLoopHistory = new LinkedList<>();
138
139     /**
140      * Maps a target entity to its lock.
141      */
142     private final transient Map<String, LockData> target2lock = new HashMap<>();
143
144     private final ControlLoopProcessor processor;
145     private final AtomicReference<ControlLoopOperationManager2> currentOperation = new AtomicReference<>();
146
147     private FinalResult finalResult = null;
148
149     @Getter
150     private VirtualControlLoopNotification notification;
151
152     @Getter
153     private boolean updated = false;
154
155     private final transient WorkingMemory workMem;
156     private transient FactHandle factHandle;
157
158
159     /**
160      * Constructs the object.
161      *
162      * @param params control loop parameters
163      * @param event event to be managed by this object
164      * @param workMem working memory to update if this changes
165      * @throws ControlLoopException if the event is invalid or if a YAML processor cannot
166      *         be created
167      */
168     public ControlLoopEventManager2(ControlLoopParams params, VirtualControlLoopEvent event, WorkingMemory workMem)
169                     throws ControlLoopException {
170
171         checkEventSyntax(event);
172
173         if (isClosedLoopDisabled(event)) {
174             throw new IllegalStateException("is-closed-loop-disabled is set to true on VServer or VNF");
175         }
176
177         if (isProvStatusInactive(event)) {
178             throw new IllegalStateException("prov-status is not ACTIVE on VServer or VNF");
179         }
180
181         this.createdByThisJvmInstance = true;
182         this.closedLoopControlName = params.getClosedLoopControlName();
183         this.requestId = event.getRequestId();
184         this.context = new ControlLoopEventContext(event);
185         this.policyName = params.getPolicyName();
186         this.policyScope = params.getPolicyScope();
187         this.policyVersion = params.getPolicyVersion();
188         this.processor = new ControlLoopProcessor(params.getToscaPolicy());
189         this.workMem = workMem;
190         this.endTimeMs = System.currentTimeMillis() + detmControlLoopTimeoutMs();
191     }
192
193     /**
194      * Starts the manager.
195      *
196      * @throws ControlLoopException if the processor cannot get a policy
197      */
198     public void start() throws ControlLoopException {
199         if (!isActive()) {
200             throw new IllegalStateException("manager is no longer active");
201         }
202
203         if ((factHandle = workMem.getFactHandle(this)) == null) {
204             throw new IllegalStateException("manager is not in working memory");
205         }
206
207         if (currentOperation.get() != null) {
208             throw new IllegalStateException("manager already started");
209         }
210
211         startOperation();
212     }
213
214     /**
215      * Starts an operation for the current processor policy.
216      *
217      * @throws ControlLoopException if the processor cannot get a policy
218      */
219     private synchronized void startOperation() throws ControlLoopException {
220
221         if ((finalResult = processor.checkIsCurrentPolicyFinal()) == null) {
222             // not final - start the next operation
223             currentOperation.set(makeOperationManager(context, processor.getCurrentPolicy()));
224             currentOperation.get().start(endTimeMs - System.currentTimeMillis());
225             return;
226         }
227
228         logger.info("final={} oper state={} for {}", finalResult, currentOperation.get().getState(), requestId);
229
230         notification = makeNotification();
231         notification.setHistory(controlLoopHistory);
232
233         switch (finalResult) {
234             case FINAL_FAILURE_EXCEPTION:
235                 notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
236                 notification.setMessage("Exception in processing closed loop");
237                 break;
238             case FINAL_SUCCESS:
239                 notification.setNotification(ControlLoopNotificationType.FINAL_SUCCESS);
240                 break;
241             case FINAL_OPENLOOP:
242                 notification.setNotification(ControlLoopNotificationType.FINAL_OPENLOOP);
243                 break;
244             case FINAL_FAILURE:
245             default:
246                 notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
247                 break;
248         }
249     }
250
251     /**
252      * Starts the next step, whatever that may be.
253      */
254     public void nextStep() {
255         if (!isActive()) {
256             return;
257         }
258
259         updated = false;
260
261         try {
262             if (!currentOperation.get().nextStep()) {
263                 // current operation is done - try the next
264                 controlLoopHistory.addAll(currentOperation.get().getHistory());
265                 processor.nextPolicyForResult(currentOperation.get().getOperationResult());
266                 startOperation();
267             }
268
269         } catch (ControlLoopException | RuntimeException e) {
270             // processor problem - this is fatal
271             logger.warn("{}: cannot start next step for {}", closedLoopControlName, requestId, e);
272             finalResult = FinalResult.FINAL_FAILURE_EXCEPTION;
273             notification = makeNotification();
274             notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
275             notification.setMessage("Policy processing aborted due to policy error");
276             notification.setHistory(controlLoopHistory);
277         }
278     }
279
280     /**
281      * Determines if the manager is still active.
282      *
283      * @return {@code true} if the manager is still active, {@code false} otherwise
284      */
285     public boolean isActive() {
286         return (createdByThisJvmInstance && finalResult == null);
287     }
288
289     /**
290      * Updates working memory if this changes.
291      *
292      * @param operation operation manager that was updated
293      */
294     @Override
295     public synchronized void updated(ControlLoopOperationManager2 operation) {
296         if (!isActive() || operation != currentOperation.get()) {
297             // no longer working on the given operation
298             return;
299         }
300
301         notification = makeNotification();
302
303         VirtualControlLoopEvent event = context.getEvent();
304
305         switch (operation.getState()) {
306             case LOCK_DENIED:
307                 notification.setNotification(ControlLoopNotificationType.REJECTED);
308                 notification.setMessage("The target " + event.getAai().get(event.getTarget()) + " is already locked");
309                 break;
310             case LOCK_LOST:
311                 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
312                 notification.setMessage("The target " + event.getAai().get(event.getTarget()) + " is no longer locked");
313                 break;
314             case GUARD_STARTED:
315                 notification.setNotification(ControlLoopNotificationType.OPERATION);
316                 notification.setMessage(
317                                 "Sending guard query for " + operation.getActor() + " " + operation.getOperation());
318                 break;
319             case GUARD_PERMITTED:
320                 notification.setNotification(ControlLoopNotificationType.OPERATION);
321                 notification.setMessage("Guard result for " + operation.getActor() + " " + operation.getOperation()
322                                 + " is Permit");
323                 break;
324             case GUARD_DENIED:
325                 notification.setNotification(ControlLoopNotificationType.OPERATION);
326                 notification.setMessage("Guard result for " + operation.getActor() + " " + operation.getOperation()
327                                 + " is Deny");
328                 break;
329             case OPERATION_STARTED:
330                 notification.setNotification(ControlLoopNotificationType.OPERATION);
331                 notification.setMessage(operation.getOperationMessage());
332                 notification.setHistory(Collections.emptyList());
333                 break;
334             case OPERATION_SUCCESS:
335                 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
336                 break;
337
338             case CONTROL_LOOP_TIMEOUT:
339                 logger.warn("{}: control loop timed out for {}", closedLoopControlName, requestId);
340                 controlLoopHistory.addAll(currentOperation.get().getHistory());
341                 notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
342                 notification.setMessage("Control Loop timed out");
343                 notification.setHistory(controlLoopHistory);
344                 finalResult = FinalResult.FINAL_FAILURE;
345                 break;
346
347             case OPERATION_FAILURE:
348             default:
349                 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
350                 break;
351         }
352
353         updated = true;
354         workMem.update(factHandle, this);
355     }
356
357     /**
358      * Cancels the current operation and frees all locks.
359      */
360     public void destroy() {
361         ControlLoopOperationManager2 oper = currentOperation.get();
362         if (oper != null) {
363             oper.cancel();
364         }
365
366         getBlockingExecutor().execute(this::freeAllLocks);
367     }
368
369     /**
370      * Frees all locks.
371      */
372     private void freeAllLocks() {
373         target2lock.values().forEach(LockData::free);
374     }
375
376     /**
377      * Makes a notification message for the current operation.
378      *
379      * @return a new notification
380      */
381     public VirtualControlLoopNotification makeNotification() {
382         VirtualControlLoopNotification notif = new VirtualControlLoopNotification(context.getEvent());
383         notif.setNotification(ControlLoopNotificationType.OPERATION);
384         notif.setFrom("policy");
385         notif.setPolicyScope(policyScope);
386         notif.setPolicyVersion(policyVersion);
387
388         if (finalResult == null) {
389             ControlLoopOperationManager2 oper = currentOperation.get();
390             if (oper != null) {
391                 notif.setMessage(oper.getOperationHistory());
392                 notif.setHistory(oper.getHistory());
393             }
394         }
395
396         return notif;
397     }
398
399     /**
400      * An event onset/abatement.
401      *
402      * @param event the event
403      * @return the status
404      */
405     public NewEventStatus onNewEvent(VirtualControlLoopEvent event) {
406         try {
407             checkEventSyntax(event);
408
409             if (event.getClosedLoopEventStatus() == ControlLoopEventStatus.ONSET) {
410                 if (event.equals(context.getEvent())) {
411                     return NewEventStatus.FIRST_ONSET;
412                 }
413
414                 numOnsets++;
415                 return NewEventStatus.SUBSEQUENT_ONSET;
416
417             } else {
418                 if (abatement == null) {
419                     abatement = event;
420                     numAbatements++;
421                     return NewEventStatus.FIRST_ABATEMENT;
422                 } else {
423                     numAbatements++;
424                     return NewEventStatus.SUBSEQUENT_ABATEMENT;
425                 }
426             }
427         } catch (ControlLoopException e) {
428             logger.error("{}: onNewEvent threw an exception", this, e);
429             return NewEventStatus.SYNTAX_ERROR;
430         }
431     }
432
433     /**
434      * Determines the overall control loop timeout.
435      *
436      * @return the policy timeout, in milliseconds, if specified, a default timeout
437      *         otherwise
438      */
439     private long detmControlLoopTimeoutMs() {
440         // validation checks preclude null or 0 timeout values in the policy
441         Integer timeout = processor.getControlLoop().getTimeout();
442         return TimeUnit.MILLISECONDS.convert(timeout, TimeUnit.SECONDS);
443     }
444
445     /**
446      * Check an event syntax.
447      *
448      * @param event the event syntax
449      * @throws ControlLoopException if an error occurs
450      */
451     public void checkEventSyntax(VirtualControlLoopEvent event) throws ControlLoopException {
452         validateStatus(event);
453         if (StringUtils.isBlank(event.getClosedLoopControlName())) {
454             throw new ControlLoopException("No control loop name");
455         }
456         if (event.getRequestId() == null) {
457             throw new ControlLoopException("No request ID");
458         }
459         if (event.getClosedLoopEventStatus() == ControlLoopEventStatus.ABATED) {
460             return;
461         }
462         if (StringUtils.isBlank(event.getTarget())) {
463             throw new ControlLoopException("No target field");
464         } else if (!VALID_TARGETS.contains(event.getTarget().toLowerCase())) {
465             throw new ControlLoopException("target field invalid");
466         }
467         validateAaiData(event);
468     }
469
470     private void validateStatus(VirtualControlLoopEvent event) throws ControlLoopException {
471         if (event.getClosedLoopEventStatus() != ControlLoopEventStatus.ONSET
472                         && event.getClosedLoopEventStatus() != ControlLoopEventStatus.ABATED) {
473             throw new ControlLoopException("Invalid value in closedLoopEventStatus");
474         }
475     }
476
477     private void validateAaiData(VirtualControlLoopEvent event) throws ControlLoopException {
478         Map<String, String> eventAai = event.getAai();
479         if (eventAai == null) {
480             throw new ControlLoopException("AAI is null");
481         }
482         if (event.getTargetType() == null) {
483             throw new ControlLoopException("The Target type is null");
484         }
485         switch (event.getTargetType()) {
486             case VM:
487             case VNF:
488                 validateAaiVmVnfData(eventAai);
489                 return;
490             case PNF:
491                 validateAaiPnfData(eventAai);
492                 return;
493             default:
494                 throw new ControlLoopException("The target type is not supported");
495         }
496     }
497
498     private void validateAaiVmVnfData(Map<String, String> eventAai) throws ControlLoopException {
499         if (eventAai.get(GENERIC_VNF_VNF_ID) == null && eventAai.get(VSERVER_VSERVER_NAME) == null
500                         && eventAai.get(GENERIC_VNF_VNF_NAME) == null) {
501             throw new ControlLoopException(
502                             "generic-vnf.vnf-id or generic-vnf.vnf-name or vserver.vserver-name information missing");
503         }
504     }
505
506     private void validateAaiPnfData(Map<String, String> eventAai) throws ControlLoopException {
507         if (eventAai.get(PNF_NAME) == null) {
508             throw new ControlLoopException("AAI PNF object key pnf-name is missing");
509         }
510     }
511
512     /**
513      * Is closed loop disabled for an event.
514      *
515      * @param event the event
516      * @return <code>true</code> if the control loop is disabled, <code>false</code>
517      *         otherwise
518      */
519     public static boolean isClosedLoopDisabled(VirtualControlLoopEvent event) {
520         Map<String, String> aai = event.getAai();
521         return (isAaiTrue(aai.get(VSERVER_IS_CLOSED_LOOP_DISABLED))
522                         || isAaiTrue(aai.get(GENERIC_VNF_IS_CLOSED_LOOP_DISABLED))
523                         || isAaiTrue(aai.get(PNF_IS_IN_MAINT)));
524     }
525
526     /**
527      * Does provisioning status, for an event, have a value other than ACTIVE.
528      *
529      * @param event the event
530      * @return {@code true} if the provisioning status is neither ACTIVE nor {@code null},
531      *         {@code false} otherwise
532      */
533     protected static boolean isProvStatusInactive(VirtualControlLoopEvent event) {
534         Map<String, String> aai = event.getAai();
535         return !(PROV_STATUS_ACTIVE.equals(aai.getOrDefault(VSERVER_PROV_STATUS, PROV_STATUS_ACTIVE))
536                         && PROV_STATUS_ACTIVE.equals(aai.getOrDefault(GENERIC_VNF_PROV_STATUS, PROV_STATUS_ACTIVE)));
537     }
538
539     /**
540      * Determines the boolean value represented by the given AAI field value.
541      *
542      * @param aaiValue value to be examined
543      * @return the boolean value represented by the field value, or {@code false} if the
544      *         value is {@code null}
545      */
546     protected static boolean isAaiTrue(String aaiValue) {
547         return (aaiValue != null && TRUE_VALUES.contains(aaiValue.toLowerCase()));
548     }
549
550     /**
551      * Requests a lock. This requests the lock for the time that remains before the
552      * timeout expires. This avoids having to extend the lock.
553      *
554      * @param targetEntity entity to be locked
555      * @param lockUnavailableCallback function to be invoked if the lock is
556      *        unavailable/lost
557      * @return a future that can be used to await the lock
558      */
559     @Override
560     public synchronized CompletableFuture<OperationOutcome> requestLock(String targetEntity,
561                     Consumer<OperationOutcome> lockUnavailableCallback) {
562
563         long remainingMs = endTimeMs - System.currentTimeMillis();
564         int remainingSec = 15 + Math.max(0, (int) TimeUnit.SECONDS.convert(remainingMs, TimeUnit.MILLISECONDS));
565
566         LockData data = target2lock.computeIfAbsent(targetEntity, key -> {
567             LockData data2 = new LockData(key, requestId);
568             makeLock(targetEntity, requestId.toString(), remainingSec, data2);
569             return data2;
570         });
571
572         data.addUnavailableCallback(lockUnavailableCallback);
573
574         return data.getFuture();
575     }
576
577     /**
578      * Initializes various components, on demand.
579      */
580     private static class LazyInitData {
581         private static final OperationHistoryDataManager DATA_MANAGER;
582         private static final ActorService ACTOR_SERVICE;
583
584         static {
585             EventManagerServices services = new EventManagerServices(EVENT_MANAGER_SERVICE_CONFIG);
586             ACTOR_SERVICE = services.getActorService();
587             DATA_MANAGER = services.getDataManager();
588         }
589     }
590
591     // the following methods may be overridden by junit tests
592
593     protected ControlLoopOperationManager2 makeOperationManager(ControlLoopEventContext ctx, Policy policy) {
594         return new ControlLoopOperationManager2(this, ctx, policy, getExecutor());
595     }
596
597     protected Executor getExecutor() {
598         return ForkJoinPool.commonPool();
599     }
600
601     protected ExecutorService getBlockingExecutor() {
602         return PolicyEngineConstants.getManager().getExecutorService();
603     }
604
605     protected void makeLock(String targetEntity, String requestId, int holdSec, LockCallback callback) {
606         PolicyEngineConstants.getManager().createLock(targetEntity, requestId, holdSec, callback, false);
607     }
608
609     @Override
610     public ActorService getActorService() {
611         return LazyInitData.ACTOR_SERVICE;
612     }
613
614     @Override
615     public OperationHistoryDataManager getDataManager() {
616         return LazyInitData.DATA_MANAGER;
617     }
618 }