93535efc1fa0df185643fa82406b9a5276cc7aa7
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * controlloop event manager
4  * ================================================================================
5  * Copyright (C) 2017-2018 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 java.io.Serializable;
24 import java.io.UnsupportedEncodingException;
25 import java.net.URLDecoder;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.HashMap;
29 import java.util.LinkedList;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.UUID;
33 import java.util.NoSuchElementException;
34
35 import org.onap.policy.aai.AaiGetVnfResponse;
36 import org.onap.policy.aai.AaiGetVserverResponse;
37 import org.onap.policy.aai.AaiManager;
38 import org.onap.policy.aai.AaiNqInstanceFilters;
39 import org.onap.policy.aai.AaiNqNamedQuery;
40 import org.onap.policy.aai.AaiNqQueryParameters;
41 import org.onap.policy.aai.AaiNqRequest;
42 import org.onap.policy.aai.AaiNqResponse;
43 import org.onap.policy.aai.AaiNqResponseWrapper;
44 import org.onap.policy.aai.AaiNqVServer;
45 import org.onap.policy.aai.util.AaiException;
46 import org.onap.policy.controlloop.ControlLoopEventStatus;
47 import org.onap.policy.controlloop.ControlLoopException;
48 import org.onap.policy.controlloop.ControlLoopNotificationType;
49 import org.onap.policy.controlloop.ControlLoopOperation;
50 import org.onap.policy.controlloop.VirtualControlLoopEvent;
51 import org.onap.policy.controlloop.VirtualControlLoopNotification;
52 import org.onap.policy.controlloop.policy.FinalResult;
53 import org.onap.policy.controlloop.policy.Policy;
54 import org.onap.policy.controlloop.processor.ControlLoopProcessor;
55 import org.onap.policy.drools.system.PolicyEngine;
56 import org.onap.policy.guard.GuardResult;
57 import org.onap.policy.guard.LockCallback;
58 import org.onap.policy.guard.PolicyGuard;
59 import org.onap.policy.guard.PolicyGuard.LockResult;
60 import org.onap.policy.guard.TargetLock;
61 import org.onap.policy.rest.RESTManager;
62 import org.onap.policy.so.util.Serialization;
63 import org.slf4j.Logger;
64 import org.slf4j.LoggerFactory;
65
66 public class ControlLoopEventManager implements LockCallback, Serializable {
67     public static final String PROV_STATUS_ACTIVE = "ACTIVE";
68     private static final String VM_NAME = "VM_NAME";
69     private static final String VNF_NAME = "VNF_NAME";
70     public static final String GENERIC_VNF_VNF_ID = "generic-vnf.vnf-id";
71     public static final String GENERIC_VNF_VNF_NAME = "generic-vnf.vnf-name";
72     public static final String VSERVER_VSERVER_NAME = "vserver.vserver-name";
73     public static final String GENERIC_VNF_IS_CLOSED_LOOP_DISABLED = "generic-vnf.is-closed-loop-disabled";
74     public static final String VSERVER_IS_CLOSED_LOOP_DISABLED = "vserver.is-closed-loop-disabled";
75     public static final String GENERIC_VNF_PROV_STATUS = "generic-vnf.prov-status";
76     public static final String VSERVER_PROV_STATUS = "vserver.prov-status";
77
78     private static final String AAI_URL = "aai.url";
79     private static final String AAI_USERNAME = "aai.username";
80     private static final String AAI_PASSWD = "aai.password";
81
82     private static final String QUERY_AAI_ERROR_MSG = "Exception from queryAai: ";
83
84     /**
85      * Additional time, in seconds, to add to a "lock" request. This ensures that the lock
86      * won't expire right before an operation completes.
87      */
88     private static final int ADDITIONAL_LOCK_SEC = 60;
89
90     private static final Logger logger = LoggerFactory.getLogger(ControlLoopEventManager.class);
91
92     private static final long serialVersionUID = -1216568161322872641L;
93     public final String closedLoopControlName;
94     public final UUID requestID;
95
96     private String controlLoopResult;
97     private ControlLoopProcessor processor = null;
98     private VirtualControlLoopEvent onset;
99     private Integer numOnsets = 0;
100     private Integer numAbatements = 0;
101     private VirtualControlLoopEvent abatement;
102     private FinalResult controlLoopTimedOut = null;
103
104     private boolean isActivated = false;
105     private LinkedList<ControlLoopOperation> controlLoopHistory = new LinkedList<>();
106     private ControlLoopOperationManager currentOperation = null;
107     private ControlLoopOperationManager lastOperationManager = null;
108     private transient TargetLock targetLock = null;
109     private AaiGetVnfResponse vnfResponse = null;
110     private AaiGetVserverResponse vserverResponse = null;
111     private boolean useTargetLock = true;
112
113     /**
114      * Wrapper for AAI vserver named-query response. This is initialized in a lazy
115      * fashion.
116      */
117     private AaiNqResponseWrapper nqVserverResponse = null;
118
119     private static Collection<String> requiredAAIKeys = new ArrayList<>();
120
121     static {
122         requiredAAIKeys.add("AICVServerSelfLink");
123         requiredAAIKeys.add("AICIdentity");
124         requiredAAIKeys.add("is_closed_loop_disabled");
125         requiredAAIKeys.add(VM_NAME);
126     }
127
128     public ControlLoopEventManager(String closedLoopControlName, UUID requestID) {
129         this.closedLoopControlName = closedLoopControlName;
130         this.requestID = requestID;
131     }
132
133     public String getClosedLoopControlName() {
134         return closedLoopControlName;
135     }
136
137     public String getControlLoopResult() {
138         return controlLoopResult;
139     }
140
141     public void setControlLoopResult(String controlLoopResult) {
142         this.controlLoopResult = controlLoopResult;
143     }
144
145     public Integer getNumOnsets() {
146         return numOnsets;
147     }
148
149     public void setNumOnsets(Integer numOnsets) {
150         this.numOnsets = numOnsets;
151     }
152
153     public Integer getNumAbatements() {
154         return numAbatements;
155     }
156
157     public void setNumAbatements(Integer numAbatements) {
158         this.numAbatements = numAbatements;
159     }
160
161     public boolean isActivated() {
162         return isActivated;
163     }
164
165     public void setActivated(boolean isActivated) {
166         this.isActivated = isActivated;
167     }
168
169     public boolean useTargetLock() {
170         return useTargetLock();
171     }
172
173     public void setUseTargetLock(boolean useTargetLock) {
174         this.useTargetLock = useTargetLock;
175     }
176
177     public VirtualControlLoopEvent getOnsetEvent() {
178         return this.onset;
179     }
180
181     public VirtualControlLoopEvent getAbatementEvent() {
182         return this.abatement;
183     }
184
185     public ControlLoopProcessor getProcessor() {
186         return this.processor;
187     }
188
189     public UUID getRequestID() {
190         return requestID;
191     }
192
193     /**
194      * Activate a control loop event.
195      *
196      * @param event the event
197      * @return the VirtualControlLoopNotification
198      */
199     public VirtualControlLoopNotification activate(VirtualControlLoopEvent event) {
200         VirtualControlLoopNotification notification = new VirtualControlLoopNotification(event);
201         try {
202             //
203             // This method should ONLY be called ONCE
204             //
205             if (this.isActivated) {
206                 throw new ControlLoopException("ControlLoopEventManager has already been activated.");
207             }
208             //
209             // Syntax check the event
210             //
211             checkEventSyntax(event);
212
213             //
214             // At this point we are good to go with this event
215             //
216             this.onset = event;
217             this.numOnsets = 1;
218             //
219             notification.setNotification(ControlLoopNotificationType.ACTIVE);
220             //
221             // Set ourselves as active
222             //
223             this.isActivated = true;
224         } catch (ControlLoopException e) {
225             logger.error("{}: activate by event threw: ", this, e);
226             notification.setNotification(ControlLoopNotificationType.REJECTED);
227             notification.setMessage(e.getMessage());
228         }
229         return notification;
230     }
231
232     /**
233      * Activate a control loop event.
234      *
235      * @param yamlSpecification the yaml specification
236      * @param event the event
237      * @return the VirtualControlLoopNotification
238      */
239     public VirtualControlLoopNotification activate(String yamlSpecification, VirtualControlLoopEvent event) {
240         VirtualControlLoopNotification notification = new VirtualControlLoopNotification(event);
241         try {
242             //
243             // This method should ONLY be called ONCE
244             //
245             if (this.isActivated) {
246                 throw new ControlLoopException("ControlLoopEventManager has already been activated.");
247             }
248             //
249             // Syntax check the event
250             //
251             checkEventSyntax(event);
252
253             //
254             // Check the YAML
255             //
256             if (yamlSpecification == null || yamlSpecification.length() < 1) {
257                 throw new ControlLoopException("yaml specification is null or 0 length");
258             }
259         } catch (ControlLoopException e) {
260             logger.error("{}: activate by YAML specification and event threw: ", this, e);
261             notification.setNotification(ControlLoopNotificationType.REJECTED);
262             notification.setMessage(e.getMessage());
263             return notification;
264         }
265
266         String decodedYaml = null;
267         try {
268             decodedYaml = URLDecoder.decode(yamlSpecification, "UTF-8");
269             if (decodedYaml != null && decodedYaml.length() > 0) {
270                 yamlSpecification = decodedYaml;
271             }
272         } catch (UnsupportedEncodingException e) {
273             logger.error("{}: YAML decode in activate by YAML specification and event threw: ", this, e);
274             notification.setNotification(ControlLoopNotificationType.REJECTED);
275             notification.setMessage(e.getMessage());
276             return notification;
277         }
278
279         try {
280             //
281             // Parse the YAML specification
282             //
283             this.processor = new ControlLoopProcessor(yamlSpecification);
284             //
285             // At this point we are good to go with this event
286             //
287             this.onset = event;
288             this.numOnsets = 1;
289             //
290             //
291             //
292             notification.setNotification(ControlLoopNotificationType.ACTIVE);
293             //
294             // Set ourselves as active
295             //
296             this.isActivated = true;
297         } catch (ControlLoopException e) {
298             logger.error("{}: activate by YAML specification and event threw: ", this, e);
299             notification.setNotification(ControlLoopNotificationType.REJECTED);
300             notification.setMessage(e.getMessage());
301         }
302         return notification;
303     }
304
305     /**
306      * Check if the control loop is final.
307      *
308      * @return a VirtualControlLoopNotification if the control loop is final, otherwise
309      *         <code>null</code> is returned
310      * @throws ControlLoopException if an error occurs
311      */
312     public VirtualControlLoopNotification isControlLoopFinal() throws ControlLoopException {
313         //
314         // Check if they activated us
315         //
316         if (!this.isActivated) {
317             throw new ControlLoopException("ControlLoopEventManager MUST be activated first.");
318         }
319         //
320         // Make sure we are expecting this call.
321         //
322         if (this.onset == null) {
323             throw new ControlLoopException("No onset event for ControlLoopEventManager.");
324         }
325         //
326         // Ok, start creating the notification
327         //
328         VirtualControlLoopNotification notification = new VirtualControlLoopNotification(this.onset);
329         //
330         // Check if the overall control loop has timed out
331         //
332         if (this.isControlLoopTimedOut()) {
333             //
334             // Yes we have timed out
335             //
336             notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
337             notification.setMessage("Control Loop timed out");
338             notification.getHistory().addAll(this.controlLoopHistory);
339             return notification;
340         }
341         //
342         // Check if the current policy is Final
343         //
344         FinalResult result = this.processor.checkIsCurrentPolicyFinal();
345         if (result == null) {
346             //
347             // we are not at a final result
348             //
349             return null;
350         }
351
352         switch (result) {
353             case FINAL_FAILURE_EXCEPTION:
354                 notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
355                 notification.setMessage("Exception in processing closed loop");
356                 break;
357             case FINAL_FAILURE:
358             case FINAL_FAILURE_RETRIES:
359             case FINAL_FAILURE_TIMEOUT:
360             case FINAL_FAILURE_GUARD:
361                 notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
362                 break;
363             case FINAL_OPENLOOP:
364                 notification.setNotification(ControlLoopNotificationType.FINAL_OPENLOOP);
365                 break;
366             case FINAL_SUCCESS:
367                 notification.setNotification(ControlLoopNotificationType.FINAL_SUCCESS);
368                 break;
369             default:
370                 return null;
371         }
372         //
373         // Be sure to add all the history
374         //
375         notification.getHistory().addAll(this.controlLoopHistory);
376         return notification;
377     }
378
379     /**
380      * Process the control loop.
381      *
382      * @return a ControlLoopOperationManager
383      * @throws ControlLoopException if an error occurs
384      * @throws AaiException if an error occurs retrieving information from A&AI
385      */
386     public ControlLoopOperationManager processControlLoop() throws ControlLoopException, AaiException {
387         //
388         // Check if they activated us
389         //
390         if (!this.isActivated) {
391             throw new ControlLoopException("ControlLoopEventManager MUST be activated first.");
392         }
393         //
394         // Make sure we are expecting this call.
395         //
396         if (this.onset == null) {
397             throw new ControlLoopException("No onset event for ControlLoopEventManager.");
398         }
399         //
400         // Is there a current operation?
401         //
402         if (this.currentOperation != null) {
403             //
404             // Throw an exception, or simply return the current operation?
405             //
406             throw new ControlLoopException("Already working an Operation, do not call this method.");
407         }
408         //
409         // Ensure we are not FINAL
410         //
411         VirtualControlLoopNotification notification = this.isControlLoopFinal();
412         if (notification != null) {
413             //
414             // This is weird, we require them to call the isControlLoopFinal() method first
415             //
416             // We should really abstract this and avoid throwing an exception, because it really
417             // isn't an exception.
418             //
419             throw new ControlLoopException("Control Loop is in FINAL state, do not call this method.");
420         }
421         //
422         // Not final so get the policy that needs to be worked on.
423         //
424         Policy policy = this.processor.getCurrentPolicy();
425         if (policy == null) {
426             throw new ControlLoopException("ControlLoopEventManager: processor came upon null Policy.");
427         }
428         //
429         // And setup an operation
430         //
431         this.lastOperationManager = this.currentOperation;
432         this.currentOperation = new ControlLoopOperationManager(this.onset, policy, this);
433         //
434         // Return it
435         //
436         return this.currentOperation;
437     }
438
439     /**
440      * Finish an operation.
441      *
442      * @param operation the operation
443      */
444     public void finishOperation(ControlLoopOperationManager operation) throws ControlLoopException {
445         //
446         // Verify we have a current operation
447         //
448         if (this.currentOperation != null) {
449             //
450             // Validate they are finishing the current operation
451             // PLD - this is simply comparing the policy. Do we want to equals the whole object?
452             //
453             if (this.currentOperation.policy.equals(operation.policy)) {
454                 logger.debug("Finishing {} result is {}", this.currentOperation.policy.getRecipe(),
455                         this.currentOperation.getOperationResult());
456                 //
457                 // Save history
458                 //
459                 this.controlLoopHistory.addAll(this.currentOperation.getHistory());
460                 //
461                 // Move to the next Policy
462                 //
463                 this.processor.nextPolicyForResult(this.currentOperation.getOperationResult());
464                 //
465                 // Just null this out
466                 //
467                 this.lastOperationManager = this.currentOperation;
468                 this.currentOperation = null;
469                 //
470                 // TODO: Release our lock
471                 //
472                 return;
473             }
474             logger.debug("Cannot finish current operation {} does not match given operation {}",
475                     this.currentOperation.policy, operation.policy);
476             return;
477         }
478         throw new ControlLoopException("No operation to finish.");
479     }
480
481     /**
482      * Obtain a lock for the current operation.
483      *
484      * @return the lock result
485      * @throws ControlLoopException if an error occurs
486      */
487     public synchronized LockResult<GuardResult, TargetLock> lockCurrentOperation() throws ControlLoopException {
488         //
489         // Sanity check
490         //
491         if (this.currentOperation == null) {
492             throw new ControlLoopException("Do not have a current operation.");
493         }
494         //
495         // Not using target locks? Create and return a lock w/o actually locking.
496         //
497         if (!this.useTargetLock) {
498             TargetLock lock = PolicyGuard.createTargetLock(this.currentOperation.policy.getTarget().getType(),
499                                                            this.currentOperation.getTargetEntity(),
500                                                            this.onset.getRequestId(), this);
501             this.targetLock = lock;
502             LockResult<GuardResult, TargetLock> lockResult = LockResult.createLockResult(GuardResult.LOCK_ACQUIRED, lock);
503             return lockResult;
504         }
505         //
506         // Have we acquired it already?
507         //
508         if (this.targetLock != null) {
509             //
510             // TODO: Make sure the current lock is for the same target.
511             // Currently, it should be. But in the future it may not.
512             //
513             GuardResult result = PolicyGuard.lockTarget(targetLock,
514                             this.currentOperation.getOperationTimeout() + ADDITIONAL_LOCK_SEC);
515             return new LockResult<>(result, this.targetLock);
516         } else {
517             //
518             // Ask the Guard
519             //
520             LockResult<GuardResult, TargetLock> lockResult =
521                     PolicyGuard.lockTarget(this.currentOperation.policy.getTarget().getType(),
522                             this.currentOperation.getTargetEntity(), this.onset.getRequestId(), this,
523                             this.currentOperation.getOperationTimeout() + ADDITIONAL_LOCK_SEC);
524             //
525             // Was it acquired?
526             //
527             if (lockResult.getA().equals(GuardResult.LOCK_ACQUIRED)) {
528                 //
529                 // Yes, let's save it
530                 //
531                 this.targetLock = lockResult.getB();
532             }
533             return lockResult;
534         }
535     }
536
537     /**
538      * Release the lock for the current operation.
539      *
540      * @return the target lock
541      */
542     public synchronized TargetLock unlockCurrentOperation() {
543         if (this.targetLock == null) {
544             return null;
545         }
546
547         TargetLock returnLock = this.targetLock;
548         this.targetLock = null;
549         //
550         // if using target locking unlock before returning
551         //
552         if (this.useTargetLock) {
553             PolicyGuard.unlockTarget(returnLock);
554         }
555
556         // always return the old target lock so rules can retract it
557         return returnLock;
558     }
559
560     public enum NEW_EVENT_STATUS {
561         FIRST_ONSET, SUBSEQUENT_ONSET, FIRST_ABATEMENT, SUBSEQUENT_ABATEMENT, SYNTAX_ERROR;
562     }
563
564     /**
565      * An event onset/abatement.
566      *
567      * @param event the event
568      * @return the status
569      * @throws AaiException if an error occurs retrieving information from A&AI
570      */
571     public NEW_EVENT_STATUS onNewEvent(VirtualControlLoopEvent event) throws AaiException {
572         try {
573             this.checkEventSyntax(event);
574             if (event.getClosedLoopEventStatus() == ControlLoopEventStatus.ONSET) {
575                 //
576                 // Check if this is our original ONSET
577                 //
578                 if (event.equals(this.onset)) {
579                     //
580                     // Query A&AI if needed
581                     //
582                     queryAai(event);
583
584                     //
585                     // DO NOT retract it
586                     //
587                     return NEW_EVENT_STATUS.FIRST_ONSET;
588                 }
589                 //
590                 // Log that we got an onset
591                 //
592                 this.numOnsets++;
593                 return NEW_EVENT_STATUS.SUBSEQUENT_ONSET;
594             } else if (event.getClosedLoopEventStatus() == ControlLoopEventStatus.ABATED) {
595                 //
596                 // Have we already got an abatement?
597                 //
598                 if (this.abatement == null) {
599                     //
600                     // Save this
601                     //
602                     this.abatement = event;
603                     //
604                     // Keep track that we received another
605                     //
606                     this.numAbatements++;
607                     //
608                     //
609                     //
610                     return NEW_EVENT_STATUS.FIRST_ABATEMENT;
611                 } else {
612                     //
613                     // Keep track that we received another
614                     //
615                     this.numAbatements++;
616                     //
617                     //
618                     //
619                     return NEW_EVENT_STATUS.SUBSEQUENT_ABATEMENT;
620                 }
621             }
622         } catch (ControlLoopException e) {
623             logger.error("{}: onNewEvent threw: ", this, e);
624         }
625         return NEW_EVENT_STATUS.SYNTAX_ERROR;
626     }
627
628
629     /**
630      * Commit the abatement to the history database.
631      *
632      * @param message the abatement message
633      * @param outcome the abatement outcome
634      */
635     public void commitAbatement(String message, String outcome) {
636         if (this.lastOperationManager == null) {
637             logger.error("{}: commitAbatement: no operation manager", this);
638             return;
639         }
640         try{
641             this.lastOperationManager.commitAbatement(message,outcome);          
642         } catch (NoSuchElementException e) {
643             logger.error("{}: commitAbatement threw an exception ", this, e);
644         }
645     }
646
647     
648     /**
649      * Set the control loop time out.
650      *
651      * @return a VirtualControlLoopNotification
652      */
653     public VirtualControlLoopNotification setControlLoopTimedOut() {
654         this.controlLoopTimedOut = FinalResult.FINAL_FAILURE_TIMEOUT;
655         VirtualControlLoopNotification notification = new VirtualControlLoopNotification(this.onset);
656         notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
657         notification.setMessage("Control Loop timed out");
658         notification.getHistory().addAll(this.controlLoopHistory);
659         return notification;
660     }
661
662     public boolean isControlLoopTimedOut() {
663         return (this.controlLoopTimedOut == FinalResult.FINAL_FAILURE_TIMEOUT);
664     }
665
666     /**
667      * Get the control loop timeout.
668      *
669      * @param defaultTimeout the default timeout
670      * @return the timeout
671      */
672     public int getControlLoopTimeout(Integer defaultTimeout) {
673         if (this.processor != null && this.processor.getControlLoop() != null) {
674             return this.processor.getControlLoop().getTimeout();
675         }
676         if (defaultTimeout != null) {
677             return defaultTimeout;
678         }
679         return 0;
680     }
681
682     public AaiGetVnfResponse getVnfResponse() {
683         return vnfResponse;
684     }
685
686     public AaiGetVserverResponse getVserverResponse() {
687         return vserverResponse;
688     }
689
690     /**
691      * Check an event syntax.
692      *
693      * @param event the event syntax
694      * @throws ControlLoopException if an error occurs
695      */
696     public void checkEventSyntax(VirtualControlLoopEvent event) throws ControlLoopException {
697         if (event.getClosedLoopEventStatus() == null
698                 || (event.getClosedLoopEventStatus() != ControlLoopEventStatus.ONSET
699                         && event.getClosedLoopEventStatus() != ControlLoopEventStatus.ABATED)) {
700             throw new ControlLoopException("Invalid value in closedLoopEventStatus");
701         }
702         if (event.getClosedLoopControlName() == null || event.getClosedLoopControlName().length() < 1) {
703             throw new ControlLoopException("No control loop name");
704         }
705         if (event.getRequestId() == null) {
706             throw new ControlLoopException("No request ID");
707         }
708         if (event.getClosedLoopEventStatus() == ControlLoopEventStatus.ABATED) {
709             return;
710         }
711         if (event.getTarget() == null || event.getTarget().length() < 1) {
712             throw new ControlLoopException("No target field");
713         } else if (!VM_NAME.equalsIgnoreCase(event.getTarget()) && !VNF_NAME.equalsIgnoreCase(event.getTarget())
714                 && !VSERVER_VSERVER_NAME.equalsIgnoreCase(event.getTarget())
715                 && !GENERIC_VNF_VNF_ID.equalsIgnoreCase(event.getTarget())
716                 && !GENERIC_VNF_VNF_NAME.equalsIgnoreCase(event.getTarget())) {
717             throw new ControlLoopException("target field invalid - expecting VM_NAME or VNF_NAME");
718         }
719         if (event.getAai() == null) {
720             throw new ControlLoopException("AAI is null");
721         }
722         if (event.getAai().get(GENERIC_VNF_VNF_ID) == null && event.getAai().get(VSERVER_VSERVER_NAME) == null
723                 && event.getAai().get(GENERIC_VNF_VNF_NAME) == null) {
724             throw new ControlLoopException(
725                     "generic-vnf.vnf-id or generic-vnf.vnf-name or vserver.vserver-name information missing");
726         }
727     }
728
729     /**
730      * Query A&AI for an event.
731      *
732      * @param event the event
733      * @throws AaiException if an error occurs retrieving information from A&AI
734      */
735     public void queryAai(VirtualControlLoopEvent event) throws AaiException {
736
737         Map<String, String> aai = event.getAai();
738
739         if (aai.containsKey(VSERVER_IS_CLOSED_LOOP_DISABLED) || aai.containsKey(GENERIC_VNF_IS_CLOSED_LOOP_DISABLED)) {
740
741             if (isClosedLoopDisabled(event)) {
742                 throw new AaiException("is-closed-loop-disabled is set to true on VServer or VNF");
743             }
744
745             if (isProvStatusInactive(event)) {
746                 throw new AaiException("prov-status is not ACTIVE on VServer or VNF");
747             }
748
749             // no need to query, as we already have the data
750             return;
751         }
752
753         if (vnfResponse != null || vserverResponse != null) {
754             // query has already been performed
755             return;
756         }
757
758         try {
759             if (aai.containsKey(GENERIC_VNF_VNF_ID) || aai.containsKey(GENERIC_VNF_VNF_NAME)) {
760                 vnfResponse = getAAIVnfInfo(event);
761                 processVNFResponse(vnfResponse, aai.containsKey(GENERIC_VNF_VNF_ID));
762             } else if (aai.containsKey(VSERVER_VSERVER_NAME)) {
763                 vserverResponse = getAAIVserverInfo(event);
764                 processVServerResponse(vserverResponse);
765             }
766         } catch (AaiException e) {
767             logger.error(QUERY_AAI_ERROR_MSG, e);
768             throw e;
769         } catch (Exception e) {
770             logger.error(QUERY_AAI_ERROR_MSG, e);
771             throw new AaiException(QUERY_AAI_ERROR_MSG + e.toString());
772         }
773     }
774
775     /**
776      * Process a response from A&AI for a VNF.
777      *
778      * @param aaiResponse the response from A&AI
779      * @param queryByVnfId <code>true</code> if the query was based on vnf-id,
780      *        <code>false</code> if the query was based on vnf-name
781      * @throws AaiException if an error occurs processing the response
782      */
783     private static void processVNFResponse(AaiGetVnfResponse aaiResponse, boolean queryByVNFID) throws AaiException {
784         String queryTypeString = (queryByVNFID ? "vnf-id" : "vnf-name");
785
786         if (aaiResponse == null) {
787             throw new AaiException("AAI Response is null (query by " + queryTypeString + ")");
788         }
789         if (aaiResponse.getRequestError() != null) {
790             throw new AaiException("AAI Responded with a request error (query by " + queryTypeString + ")");
791         }
792
793         if (aaiResponse.getIsClosedLoopDisabled()) {
794             throw new AaiException("is-closed-loop-disabled is set to true (query by " + queryTypeString + ")");
795         }
796
797         if (!PROV_STATUS_ACTIVE.equals(aaiResponse.getProvStatus())) {
798             throw new AaiException("prov-status is not ACTIVE (query by " + queryTypeString + ")");
799         }
800     }
801
802     /**
803      * Process a response from A&AI for a VServer.
804      *
805      * @param aaiResponse the response from A&AI
806      * @throws AaiException if an error occurs processing the response
807      */
808     private static void processVServerResponse(AaiGetVserverResponse aaiResponse) throws AaiException {
809         if (aaiResponse == null) {
810             throw new AaiException("AAI Response is null (query by vserver-name)");
811         }
812         if (aaiResponse.getRequestError() != null) {
813             throw new AaiException("AAI Responded with a request error (query by vserver-name)");
814         }
815
816         List<AaiNqVServer> lst = aaiResponse.getVserver();
817         if (lst.isEmpty()) {
818             return;
819         }
820
821         AaiNqVServer svr = lst.get(0);
822         if (svr.getIsClosedLoopDisabled()) {
823             throw new AaiException("is-closed-loop-disabled is set to true (query by vserver-name)");
824         }
825
826         if (!PROV_STATUS_ACTIVE.equals(svr.getProvStatus())) {
827             throw new AaiException("prov-status is not ACTIVE (query by vserver-name)");
828         }
829     }
830
831     /**
832      * Is closed loop disabled for an event.
833      *
834      * @param event the event
835      * @return <code>true</code> if the control loop is disabled, <code>false</code>
836      *         otherwise
837      */
838     public static boolean isClosedLoopDisabled(VirtualControlLoopEvent event) {
839         Map<String, String> aai = event.getAai();
840         return (isAaiTrue(aai.get(VSERVER_IS_CLOSED_LOOP_DISABLED))
841                         || isAaiTrue(aai.get(GENERIC_VNF_IS_CLOSED_LOOP_DISABLED)));
842     }
843
844     /**
845      * Does provisioning status, for an event, have a value other than ACTIVE.
846      *
847      * @param event the event
848      * @return {@code true} if the provisioning status is neither ACTIVE nor {@code null},
849      *         {@code false} otherwise
850      */
851     protected static boolean isProvStatusInactive(VirtualControlLoopEvent event) {
852         Map<String, String> aai = event.getAai();
853         return (!PROV_STATUS_ACTIVE.equals(aai.getOrDefault(VSERVER_PROV_STATUS, PROV_STATUS_ACTIVE))
854                         || !PROV_STATUS_ACTIVE.equals(aai.getOrDefault(GENERIC_VNF_PROV_STATUS, PROV_STATUS_ACTIVE)));
855     }
856
857     /**
858      * Determines the boolean value represented by the given AAI field value.
859      *
860      * @param aaiValue value to be examined
861      * @return the boolean value represented by the field value, or {@code false} if the
862      *         value is {@code null}
863      */
864     protected static boolean isAaiTrue(String aaiValue) {
865         return ("true".equalsIgnoreCase(aaiValue) || "T".equalsIgnoreCase(aaiValue) || "yes".equalsIgnoreCase(aaiValue)
866                         || "Y".equalsIgnoreCase(aaiValue));
867     }
868
869     /**
870      * Get the A&AI VService information for an event.
871      *
872      * @param event the event
873      * @return a AaiGetVserverResponse
874      * @throws ControlLoopException if an error occurs
875      */
876     public static AaiGetVserverResponse getAAIVserverInfo(VirtualControlLoopEvent event) throws ControlLoopException {
877         UUID requestId = event.getRequestId();
878         AaiGetVserverResponse response = null;
879         String vserverName = event.getAai().get(VSERVER_VSERVER_NAME);
880
881         try {
882             if (vserverName != null) {
883                 String aaiHostUrl = PolicyEngine.manager.getEnvironmentProperty(AAI_URL);
884                 String aaiUser = PolicyEngine.manager.getEnvironmentProperty(AAI_USERNAME);
885                 String aaiPassword = PolicyEngine.manager.getEnvironmentProperty(AAI_PASSWD);
886                 String aaiGetQueryByVserver = "/aai/v11/nodes/vservers?vserver-name=";
887                 String url = aaiHostUrl + aaiGetQueryByVserver;
888                 logger.info("AAI Host URL by VServer: {}", url);
889                 response = new AaiManager(new RESTManager()).getQueryByVserverName(url, aaiUser, aaiPassword, requestId,
890                         vserverName);
891             }
892         } catch (Exception e) {
893             logger.error("getAAIVserverInfo exception: ", e);
894             throw new ControlLoopException("Exception in getAAIVserverInfo: ", e);
895         }
896
897         return response;
898     }
899
900     /**
901      * Get A&AI VNF information for an event.
902      *
903      * @param event the event
904      * @return a AaiGetVnfResponse
905      * @throws ControlLoopException if an error occurs
906      */
907     public static AaiGetVnfResponse getAAIVnfInfo(VirtualControlLoopEvent event) throws ControlLoopException {
908         UUID requestId = event.getRequestId();
909         AaiGetVnfResponse response = null;
910         String vnfName = event.getAai().get(GENERIC_VNF_VNF_NAME);
911         String vnfId = event.getAai().get(GENERIC_VNF_VNF_ID);
912
913         String aaiHostUrl = PolicyEngine.manager.getEnvironmentProperty(AAI_URL);
914         String aaiUser = PolicyEngine.manager.getEnvironmentProperty(AAI_USERNAME);
915         String aaiPassword = PolicyEngine.manager.getEnvironmentProperty(AAI_PASSWD);
916
917         try {
918             if (vnfName != null) {
919                 String aaiGetQueryByVnfName = "/aai/v11/network/generic-vnfs/generic-vnf?vnf-name=";
920                 String url = aaiHostUrl + aaiGetQueryByVnfName;
921                 logger.info("AAI Host URL by VNF name: {}", url);
922                 response = new AaiManager(new RESTManager()).getQueryByVnfName(url, aaiUser, aaiPassword, requestId,
923                         vnfName);
924             } else if (vnfId != null) {
925                 String aaiGetQueryByVnfId = "/aai/v11/network/generic-vnfs/generic-vnf/";
926                 String url = aaiHostUrl + aaiGetQueryByVnfId;
927                 logger.info("AAI Host URL by VNF ID: {}", url);
928                 response =
929                         new AaiManager(new RESTManager()).getQueryByVnfId(url, aaiUser, aaiPassword, requestId, vnfId);
930             }
931         } catch (Exception e) {
932             logger.error("getAAIVnfInfo exception: ", e);
933             throw new ControlLoopException("Exception in getAAIVnfInfo: ", e);
934         }
935
936         return response;
937     }
938
939     /**
940      * Gets the output from the AAI vserver named-query, using the cache, if appropriate.
941      * @return output from the AAI vserver named-query
942      */
943     public AaiNqResponseWrapper getNqVserverFromAai() {
944         if (nqVserverResponse != null) {
945             // already queried
946             return nqVserverResponse;
947         }
948
949         String vserverName = onset.getAai().get(VSERVER_VSERVER_NAME);
950         if (vserverName == null) {
951             logger.warn("Missing vserver-name for AAI request {}", onset.getRequestId());
952             return null;
953         }
954
955         // create AAI named-query request with UUID started with ""
956         AaiNqRequest aaiNqRequest = new AaiNqRequest();
957         AaiNqQueryParameters aaiNqQueryParam = new AaiNqQueryParameters();
958         AaiNqNamedQuery aaiNqNamedQuery = new AaiNqNamedQuery();
959         final AaiNqInstanceFilters aaiNqInstanceFilter = new AaiNqInstanceFilters();
960
961         // queryParameters
962         aaiNqNamedQuery.setNamedQueryUuid(UUID.fromString("4ff56a54-9e3f-46b7-a337-07a1d3c6b469"));
963         aaiNqQueryParam.setNamedQuery(aaiNqNamedQuery);
964         aaiNqRequest.setQueryParameters(aaiNqQueryParam);
965         //
966         // instanceFilters
967         //
968         Map<String, Map<String, String>> aaiNqInstanceFilterMap = new HashMap<>();
969         Map<String, String> aaiNqInstanceFilterMapItem = new HashMap<>();
970         aaiNqInstanceFilterMapItem.put("vserver-name", vserverName);
971         aaiNqInstanceFilterMap.put("vserver", aaiNqInstanceFilterMapItem);
972         aaiNqInstanceFilter.getInstanceFilter().add(aaiNqInstanceFilterMap);
973         aaiNqRequest.setInstanceFilters(aaiNqInstanceFilter);
974
975         if (logger.isDebugEnabled()) {
976             logger.debug("AAI Request sent: {}", Serialization.gsonPretty.toJson(aaiNqRequest));
977         }
978
979         AaiNqResponse aaiNqResponse = new AaiManager(new RESTManager()).postQuery(getPeManagerEnvProperty(AAI_URL),
980                 getPeManagerEnvProperty(AAI_USERNAME), getPeManagerEnvProperty(AAI_PASSWD), aaiNqRequest,
981                 onset.getRequestId());
982
983         // Check AAI response
984         if (aaiNqResponse == null) {
985             logger.warn("No response received from AAI for request {}", aaiNqRequest);
986             return null;
987         }
988
989         // Create AAINQResponseWrapper
990         nqVserverResponse = new AaiNqResponseWrapper(onset.getRequestId(), aaiNqResponse);
991
992         if (logger.isDebugEnabled()) {
993             logger.debug("AAI Named Query Response: ");
994             logger.debug(Serialization.gsonPretty.toJson(nqVserverResponse.getAaiNqResponse()));
995         }
996
997         return nqVserverResponse;
998     }
999
1000     /**
1001      * This method reads and validates environmental properties coming from the policy engine. Null
1002      * properties cause an {@link IllegalArgumentException} runtime exception to be thrown
1003      *
1004      * @param enginePropertyName the name of the parameter to retrieve
1005      * @return the property value
1006      */
1007     private static String getPeManagerEnvProperty(String enginePropertyName) {
1008         String enginePropertyValue = PolicyEngine.manager.getEnvironmentProperty(enginePropertyName);
1009         if (enginePropertyValue == null) {
1010             throw new IllegalArgumentException("The value of policy engine manager environment property \""
1011                     + enginePropertyName + "\" may not be null");
1012         }
1013         return enginePropertyValue;
1014     }
1015
1016     @Override
1017     public boolean isActive() {
1018         // TODO
1019         return true;
1020     }
1021
1022     @Override
1023     public boolean releaseLock() {
1024         // TODO
1025         return false;
1026     }
1027
1028     @Override
1029     public String toString() {
1030         return "ControlLoopEventManager [closedLoopControlName=" + closedLoopControlName + ", requestID=" + requestID
1031                 + ", processor=" + processor + ", onset=" + (onset != null ? onset.getRequestId() : "null")
1032                 + ", numOnsets=" + numOnsets + ", numAbatements=" + numAbatements + ", isActivated=" + isActivated
1033                 + ", currentOperation=" + currentOperation + ", targetLock=" + targetLock + "]";
1034     }
1035
1036 }