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