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