5f36bcc203a251f4a800692253aa215de9b0e6be
[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.LinkedList;
29 import java.util.UUID;
30
31 import org.onap.policy.aai.AaiGetVnfResponse;
32 import org.onap.policy.aai.AaiGetVserverResponse;
33 import org.onap.policy.aai.AaiManager;
34 import org.onap.policy.aai.util.AaiException;
35 import org.onap.policy.controlloop.ControlLoopEventStatus;
36 import org.onap.policy.controlloop.ControlLoopException;
37 import org.onap.policy.controlloop.ControlLoopNotificationType;
38 import org.onap.policy.controlloop.ControlLoopOperation;
39 import org.onap.policy.controlloop.VirtualControlLoopEvent;
40 import org.onap.policy.controlloop.VirtualControlLoopNotification;
41 import org.onap.policy.controlloop.policy.FinalResult;
42 import org.onap.policy.controlloop.policy.Policy;
43 import org.onap.policy.controlloop.processor.ControlLoopProcessor;
44 import org.onap.policy.drools.system.PolicyEngine;
45 import org.onap.policy.guard.GuardResult;
46 import org.onap.policy.guard.LockCallback;
47 import org.onap.policy.guard.PolicyGuard;
48 import org.onap.policy.guard.PolicyGuard.LockResult;
49 import org.onap.policy.guard.TargetLock;
50 import org.onap.policy.rest.RESTManager;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 public class ControlLoopEventManager implements LockCallback, Serializable {
55     private static final String VM_NAME = "VM_NAME";
56     private static final String VNF_NAME = "VNF_NAME";
57     private static final String GENERIC_VNF_VNF_ID = "generic-vnf.vnf-id";
58     private static final String GENERIC_VNF_VNF_NAME = "generic-vnf.vnf-name";
59     private static final String VSERVER_VSERVER_NAME = "vserver.vserver-name";
60     private static final String GENERIC_VNF_IS_CLOSED_LOOP_DISABLED = "generic-vnf.is-closed-loop-disabled";
61     private static final String VSERVER_IS_CLOSED_LOOP_DISABLED = "vserver.is-closed-loop-disabled";
62
63     /**
64      * Additional time, in seconds, to add to a "lock" request. This ensures that the lock
65      * won't expire right before an operation completes.
66      */
67     private static final int ADDITIONAL_LOCK_SEC = 60;
68
69     private static final Logger logger = LoggerFactory.getLogger(ControlLoopEventManager.class);
70
71     private static final long serialVersionUID = -1216568161322872641L;
72     public final String closedLoopControlName;
73     public final UUID requestID;
74
75     private String controlLoopResult;
76     private transient ControlLoopProcessor processor = null;
77     private VirtualControlLoopEvent onset;
78     private Integer numOnsets = 0;
79     private Integer numAbatements = 0;
80     private VirtualControlLoopEvent abatement;
81     private FinalResult controlLoopTimedOut = null;
82
83     private boolean isActivated = false;
84     private LinkedList<ControlLoopOperation> controlLoopHistory = new LinkedList<>();
85     private ControlLoopOperationManager currentOperation = null;
86     private transient TargetLock targetLock = null;
87     private AaiGetVnfResponse vnfResponse = null;
88     private AaiGetVserverResponse vserverResponse = null;
89
90     private static Collection<String> requiredAAIKeys = new ArrayList<>();
91
92     static {
93         requiredAAIKeys.add("AICVServerSelfLink");
94         requiredAAIKeys.add("AICIdentity");
95         requiredAAIKeys.add("is_closed_loop_disabled");
96         requiredAAIKeys.add(VM_NAME);
97     }
98
99     public ControlLoopEventManager(String closedLoopControlName, UUID requestID) {
100         this.closedLoopControlName = closedLoopControlName;
101         this.requestID = requestID;
102     }
103
104     public String getControlLoopResult() {
105         return controlLoopResult;
106     }
107
108     public void setControlLoopResult(String controlLoopResult) {
109         this.controlLoopResult = controlLoopResult;
110     }
111
112     public Integer getNumOnsets() {
113         return numOnsets;
114     }
115
116     public void setNumOnsets(Integer numOnsets) {
117         this.numOnsets = numOnsets;
118     }
119
120     public Integer getNumAbatements() {
121         return numAbatements;
122     }
123
124     public void setNumAbatements(Integer numAbatements) {
125         this.numAbatements = numAbatements;
126     }
127
128     public boolean isActivated() {
129         return isActivated;
130     }
131
132     public void setActivated(boolean isActivated) {
133         this.isActivated = isActivated;
134     }
135
136     public VirtualControlLoopEvent getOnsetEvent() {
137         return this.onset;
138     }
139
140     public VirtualControlLoopEvent getAbatementEvent() {
141         return this.abatement;
142     }
143
144     public ControlLoopProcessor getProcessor() {
145         return this.processor;
146     }
147
148     /**
149      * Activate a control loop event.
150      * 
151      * @param event the event
152      * @return the VirtualControlLoopNotification
153      */
154     public VirtualControlLoopNotification activate(VirtualControlLoopEvent event) {
155         VirtualControlLoopNotification notification = new VirtualControlLoopNotification(event);
156         try {
157             //
158             // This method should ONLY be called ONCE
159             //
160             if (this.isActivated) {
161                 throw new ControlLoopException("ControlLoopEventManager has already been activated.");
162             }
163             //
164             // Syntax check the event
165             //
166             checkEventSyntax(event);
167
168             //
169             // At this point we are good to go with this event
170             //
171             this.onset = event;
172             this.numOnsets = 1;
173             //
174             notification.setNotification(ControlLoopNotificationType.ACTIVE);
175             //
176             // Set ourselves as active
177             //
178             this.isActivated = true;
179         } catch (ControlLoopException e) {
180             logger.error("{}: activate by event threw: ", this, e);
181             notification.setNotification(ControlLoopNotificationType.REJECTED);
182             notification.setMessage(e.getMessage());
183         }
184         return notification;
185     }
186
187     /**
188      * Activate a control loop event.
189      * 
190      * @param yamlSpecification the yaml specification
191      * @param event the event
192      * @return the VirtualControlLoopNotification
193      */
194     public VirtualControlLoopNotification activate(String yamlSpecification, VirtualControlLoopEvent event) {
195         VirtualControlLoopNotification notification = new VirtualControlLoopNotification(event);
196         try {
197             //
198             // This method should ONLY be called ONCE
199             //
200             if (this.isActivated) {
201                 throw new ControlLoopException("ControlLoopEventManager has already been activated.");
202             }
203             //
204             // Syntax check the event
205             //
206             checkEventSyntax(event);
207
208             //
209             // Check the YAML
210             //
211             if (yamlSpecification == null || yamlSpecification.length() < 1) {
212                 throw new ControlLoopException("yaml specification is null or 0 length");
213             }
214         } catch (ControlLoopException e) {
215             logger.error("{}: activate by YAML specification and event threw: ", this, e);
216             notification.setNotification(ControlLoopNotificationType.REJECTED);
217             notification.setMessage(e.getMessage());
218             return notification;
219         }
220
221         String decodedYaml = null;
222         try {
223             decodedYaml = URLDecoder.decode(yamlSpecification, "UTF-8");
224             if (decodedYaml != null && decodedYaml.length() > 0) {
225                 yamlSpecification = decodedYaml;
226             }
227         } catch (UnsupportedEncodingException e) {
228             logger.error("{}: YAML decode in activate by YAML specification and event threw: ", this, e);
229             notification.setNotification(ControlLoopNotificationType.REJECTED);
230             notification.setMessage(e.getMessage());
231             return notification;
232         }
233
234         try {
235             //
236             // Parse the YAML specification
237             //
238             this.processor = new ControlLoopProcessor(yamlSpecification);
239
240             //
241             // At this point we are good to go with this event
242             //
243             this.onset = event;
244             this.numOnsets = 1;
245             //
246             //
247             //
248             notification.setNotification(ControlLoopNotificationType.ACTIVE);
249             //
250             // Set ourselves as active
251             //
252             this.isActivated = true;
253         } catch (ControlLoopException e) {
254             logger.error("{}: activate by YAML specification and event threw: ", this, e);
255             notification.setNotification(ControlLoopNotificationType.REJECTED);
256             notification.setMessage(e.getMessage());
257         }
258         return notification;
259     }
260
261     /**
262      * Check if the control loop is final.
263      * 
264      * @return a VirtualControlLoopNotification if the control loop is final, otherwise
265      *         <code>null</code> is returned
266      * @throws ControlLoopException if an error occurs
267      */
268     public VirtualControlLoopNotification isControlLoopFinal() throws ControlLoopException {
269         //
270         // Check if they activated us
271         //
272         if (!this.isActivated) {
273             throw new ControlLoopException("ControlLoopEventManager MUST be activated first.");
274         }
275         //
276         // Make sure we are expecting this call.
277         //
278         if (this.onset == null) {
279             throw new ControlLoopException("No onset event for ControlLoopEventManager.");
280         }
281         //
282         // Ok, start creating the notification
283         //
284         VirtualControlLoopNotification notification = new VirtualControlLoopNotification(this.onset);
285         //
286         // Check if the overall control loop has timed out
287         //
288         if (this.isControlLoopTimedOut()) {
289             //
290             // Yes we have timed out
291             //
292             notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
293             notification.setMessage("Control Loop timed out");
294             notification.getHistory().addAll(this.controlLoopHistory);
295             return notification;
296         }
297         //
298         // Check if the current policy is Final
299         //
300         FinalResult result = this.processor.checkIsCurrentPolicyFinal();
301         if (result == null) {
302             //
303             // we are not at a final result
304             //
305             return null;
306         }
307
308         switch (result) {
309             case FINAL_FAILURE_EXCEPTION:
310                 notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
311                 notification.setMessage("Exception in processing closed loop");
312                 break;
313             case FINAL_FAILURE:
314             case FINAL_FAILURE_RETRIES:
315             case FINAL_FAILURE_TIMEOUT:
316             case FINAL_FAILURE_GUARD:
317                 notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
318                 break;
319             case FINAL_OPENLOOP:
320                 notification.setNotification(ControlLoopNotificationType.FINAL_OPENLOOP);
321                 break;
322             case FINAL_SUCCESS:
323                 notification.setNotification(ControlLoopNotificationType.FINAL_SUCCESS);
324                 break;
325             default:
326                 return null;
327         }
328         //
329         // Be sure to add all the history
330         //
331         notification.getHistory().addAll(this.controlLoopHistory);
332         return notification;
333     }
334
335     /**
336      * Process the control loop.
337      * 
338      * @return a ControlLoopOperationManager
339      * @throws ControlLoopException if an error occurs
340      * @throws AaiException if an error occurs retrieving information from A&AI
341      */
342     public ControlLoopOperationManager processControlLoop() throws ControlLoopException, AaiException {
343         //
344         // Check if they activated us
345         //
346         if (!this.isActivated) {
347             throw new ControlLoopException("ControlLoopEventManager MUST be activated first.");
348         }
349         //
350         // Make sure we are expecting this call.
351         //
352         if (this.onset == null) {
353             throw new ControlLoopException("No onset event for ControlLoopEventManager.");
354         }
355         //
356         // Is there a current operation?
357         //
358         if (this.currentOperation != null) {
359             //
360             // Throw an exception, or simply return the current operation?
361             //
362             throw new ControlLoopException("Already working an Operation, do not call this method.");
363         }
364         //
365         // Ensure we are not FINAL
366         //
367         VirtualControlLoopNotification notification = this.isControlLoopFinal();
368         if (notification != null) {
369             //
370             // This is weird, we require them to call the isControlLoopFinal() method first
371             //
372             // We should really abstract this and avoid throwing an exception, because it really
373             // isn't an exception.
374             //
375             throw new ControlLoopException("Control Loop is in FINAL state, do not call this method.");
376         }
377         //
378         // Not final so get the policy that needs to be worked on.
379         //
380         Policy policy = this.processor.getCurrentPolicy();
381         if (policy == null) {
382             throw new ControlLoopException("ControlLoopEventManager: processor came upon null Policy.");
383         }
384         //
385         // And setup an operation
386         //
387         this.currentOperation = new ControlLoopOperationManager(this.onset, policy, this);
388         //
389         // Return it
390         //
391         return this.currentOperation;
392     }
393
394     /**
395      * Finish an operation.
396      * 
397      * @param operation the operation
398      */
399     public void finishOperation(ControlLoopOperationManager operation) throws ControlLoopException {
400         //
401         // Verify we have a current operation
402         //
403         if (this.currentOperation != null) {
404             //
405             // Validate they are finishing the current operation
406             // PLD - this is simply comparing the policy. Do we want to equals the whole object?
407             //
408             if (this.currentOperation.policy.equals(operation.policy)) {
409                 logger.debug("Finishing {} result is {}", this.currentOperation.policy.getRecipe(),
410                         this.currentOperation.getOperationResult());
411                 //
412                 // Save history
413                 //
414                 this.controlLoopHistory.addAll(this.currentOperation.getHistory());
415                 //
416                 // Move to the next Policy
417                 //
418                 this.processor.nextPolicyForResult(this.currentOperation.getOperationResult());
419                 //
420                 // Just null this out
421                 //
422                 this.currentOperation = null;
423                 //
424                 // TODO: Release our lock
425                 //
426                 return;
427             }
428             logger.debug("Cannot finish current operation {} does not match given operation {}",
429                     this.currentOperation.policy, operation.policy);
430             return;
431         }
432         throw new ControlLoopException("No operation to finish.");
433     }
434
435     /**
436      * Obtain a lock for the current operation.
437      * 
438      * @return the lock result
439      * @throws ControlLoopException if an error occurs
440      */
441     public synchronized LockResult<GuardResult, TargetLock> lockCurrentOperation() throws ControlLoopException {
442         //
443         // Sanity check
444         //
445         if (this.currentOperation == null) {
446             throw new ControlLoopException("Do not have a current operation.");
447         }
448         //
449         // Have we acquired it already?
450         //
451         if (this.targetLock != null) {
452             //
453             // TODO: Make sure the current lock is for the same target.
454             // Currently, it should be. But in the future it may not.
455             //
456             GuardResult result = PolicyGuard.lockTarget(targetLock,
457                             this.currentOperation.getOperationTimeout() + ADDITIONAL_LOCK_SEC);
458             return new LockResult<>(result, this.targetLock);
459         } else {
460             //
461             // Ask the Guard
462             //
463             LockResult<GuardResult, TargetLock> lockResult =
464                     PolicyGuard.lockTarget(this.currentOperation.policy.getTarget().getType(),
465                             this.currentOperation.getTargetEntity(), this.onset.getRequestId(), this,
466                             this.currentOperation.getOperationTimeout() + ADDITIONAL_LOCK_SEC);
467             //
468             // Was it acquired?
469             //
470             if (lockResult.getA().equals(GuardResult.LOCK_ACQUIRED)) {
471                 //
472                 // Yes, let's save it
473                 //
474                 this.targetLock = lockResult.getB();
475             }
476             return lockResult;
477         }
478     }
479
480     /**
481      * Release the lock for the current operation.
482      * 
483      * @return the target lock
484      */
485     public synchronized TargetLock unlockCurrentOperation() {
486         if (this.targetLock == null) {
487             return null;
488         }
489         if (PolicyGuard.unlockTarget(this.targetLock)) {
490             TargetLock returnLock = this.targetLock;
491             this.targetLock = null;
492             return returnLock;
493         }
494         return null;
495     }
496
497     public enum NEW_EVENT_STATUS {
498         FIRST_ONSET, SUBSEQUENT_ONSET, FIRST_ABATEMENT, SUBSEQUENT_ABATEMENT, SYNTAX_ERROR;
499     }
500
501     /**
502      * An event onset/abatement.
503      * 
504      * @param event the event
505      * @return the status
506      * @throws AaiException if an error occurs retrieving information from A&AI
507      */
508     public NEW_EVENT_STATUS onNewEvent(VirtualControlLoopEvent event) throws AaiException {
509         try {
510             this.checkEventSyntax(event);
511             if (event.getClosedLoopEventStatus() == ControlLoopEventStatus.ONSET) {
512                 //
513                 // Check if this is our original ONSET
514                 //
515                 if (event.equals(this.onset)) {
516                     //
517                     // Query A&AI if needed
518                     //
519                     queryAai(event);
520
521                     //
522                     // DO NOT retract it
523                     //
524                     return NEW_EVENT_STATUS.FIRST_ONSET;
525                 }
526                 //
527                 // Log that we got an onset
528                 //
529                 this.numOnsets++;
530                 return NEW_EVENT_STATUS.SUBSEQUENT_ONSET;
531             } else if (event.getClosedLoopEventStatus() == ControlLoopEventStatus.ABATED) {
532                 //
533                 // Have we already got an abatement?
534                 //
535                 if (this.abatement == null) {
536                     //
537                     // Save this
538                     //
539                     this.abatement = event;
540                     //
541                     // Keep track that we received another
542                     //
543                     this.numAbatements++;
544                     //
545                     //
546                     //
547                     return NEW_EVENT_STATUS.FIRST_ABATEMENT;
548                 } else {
549                     //
550                     // Keep track that we received another
551                     //
552                     this.numAbatements++;
553                     //
554                     //
555                     //
556                     return NEW_EVENT_STATUS.SUBSEQUENT_ABATEMENT;
557                 }
558             }
559         } catch (ControlLoopException e) {
560             logger.error("{}: onNewEvent threw: ", this, e);
561         }
562         return NEW_EVENT_STATUS.SYNTAX_ERROR;
563     }
564
565     /**
566      * Set the control loop time out.
567      * 
568      * @return a VirtualControlLoopNotification
569      */
570     public VirtualControlLoopNotification setControlLoopTimedOut() {
571         this.controlLoopTimedOut = FinalResult.FINAL_FAILURE_TIMEOUT;
572         VirtualControlLoopNotification notification = new VirtualControlLoopNotification(this.onset);
573         notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
574         notification.setMessage("Control Loop timed out");
575         notification.getHistory().addAll(this.controlLoopHistory);
576         return notification;
577     }
578
579     public boolean isControlLoopTimedOut() {
580         return (this.controlLoopTimedOut == FinalResult.FINAL_FAILURE_TIMEOUT);
581     }
582
583     /**
584      * Get the control loop timeout.
585      * 
586      * @param defaultTimeout the default timeout
587      * @return the timeout
588      */
589     public int getControlLoopTimeout(Integer defaultTimeout) {
590         if (this.processor != null && this.processor.getControlLoop() != null) {
591             return this.processor.getControlLoop().getTimeout();
592         }
593         if (defaultTimeout != null) {
594             return defaultTimeout;
595         }
596         return 0;
597     }
598
599     public AaiGetVnfResponse getVnfResponse() {
600         return vnfResponse;
601     }
602
603     public AaiGetVserverResponse getVserverResponse() {
604         return vserverResponse;
605     }
606
607     /**
608      * Check an event syntax.
609      * 
610      * @param event the event syntax
611      * @throws ControlLoopException if an error occurs
612      */
613     public void checkEventSyntax(VirtualControlLoopEvent event) throws ControlLoopException {
614         if (event.getClosedLoopEventStatus() == null
615                 || (event.getClosedLoopEventStatus() != ControlLoopEventStatus.ONSET
616                         && event.getClosedLoopEventStatus() != ControlLoopEventStatus.ABATED)) {
617             throw new ControlLoopException("Invalid value in closedLoopEventStatus");
618         }
619         if (event.getClosedLoopControlName() == null || event.getClosedLoopControlName().length() < 1) {
620             throw new ControlLoopException("No control loop name");
621         }
622         if (event.getRequestId() == null) {
623             throw new ControlLoopException("No request ID");
624         }
625         if (event.getClosedLoopEventStatus() == ControlLoopEventStatus.ABATED) {
626             return;
627         }
628         if (event.getTarget() == null || event.getTarget().length() < 1) {
629             throw new ControlLoopException("No target field");
630         } else if (!VM_NAME.equalsIgnoreCase(event.getTarget()) && !VNF_NAME.equalsIgnoreCase(event.getTarget())
631                 && !VSERVER_VSERVER_NAME.equalsIgnoreCase(event.getTarget())
632                 && !GENERIC_VNF_VNF_ID.equalsIgnoreCase(event.getTarget())
633                 && !GENERIC_VNF_VNF_NAME.equalsIgnoreCase(event.getTarget())) {
634             throw new ControlLoopException("target field invalid - expecting VM_NAME or VNF_NAME");
635         }
636         if (event.getAai() == null) {
637             throw new ControlLoopException("AAI is null");
638         }
639         if (event.getAai().get(GENERIC_VNF_VNF_ID) == null && event.getAai().get(VSERVER_VSERVER_NAME) == null
640                 && event.getAai().get(GENERIC_VNF_VNF_NAME) == null) {
641             throw new ControlLoopException(
642                     "generic-vnf.vnf-id or generic-vnf.vnf-name or vserver.vserver-name information missing");
643         }
644     }
645
646     /**
647      * Query A&AI for an event.
648      * 
649      * @param event the event
650      * @throws AaiException if an error occurs retrieving information from A&AI
651      */
652     public void queryAai(VirtualControlLoopEvent event) throws AaiException {        
653         if (event.getAai().get(VSERVER_IS_CLOSED_LOOP_DISABLED) != null
654                 || event.getAai().get(GENERIC_VNF_IS_CLOSED_LOOP_DISABLED) != null) {
655             
656             if(isClosedLoopDisabled(event)) {
657                 throw new AaiException("is-closed-loop-disabled is set to true on VServer or VNF");
658             }
659             
660             // no need to query, as we already have the data
661             return;
662         }
663         
664         if(vnfResponse != null || vserverResponse != null) {
665             // query has already been performed
666             return;
667         }
668
669         try {
670             if (event.getAai().get(GENERIC_VNF_VNF_ID) != null || event.getAai().get(GENERIC_VNF_VNF_NAME) != null) {
671                 vnfResponse = getAAIVnfInfo(event);
672                 processVNFResponse(vnfResponse, event.getAai().get(GENERIC_VNF_VNF_ID) != null);
673             } else if (event.getAai().get(VSERVER_VSERVER_NAME) != null) {
674                 vserverResponse = getAAIVserverInfo(event);
675                 processVServerResponse(vserverResponse);
676             }
677         } catch (Exception e) {
678             logger.error("Exception from queryAai: ", e);
679             throw new AaiException("Exception from queryAai: " + e.toString());
680         }
681     }
682
683     /**
684      * Process a response from A&AI for a VNF.
685      * 
686      * @param aaiResponse the response from A&AI
687      * @param queryByVnfId <code>true</code> if the query was based on vnf-id, <code>false</code> if
688      *        the query was based on vnf-name
689      * @throws AaiException if an error occurs processing the response
690      */
691     private static void processVNFResponse(AaiGetVnfResponse aaiResponse, boolean queryByVNFID) throws AaiException {
692         String queryTypeString = (queryByVNFID ? "vnf-id" : "vnf-name");
693
694         if (aaiResponse == null) {
695             throw new AaiException("AAI Response is null (query by " + queryTypeString + ")");
696         }
697         if (aaiResponse.getRequestError() != null) {
698             throw new AaiException("AAI Responded with a request error (query by " + queryTypeString + ")");
699         }
700
701         if (aaiResponse.getIsClosedLoopDisabled() != null) {
702             String value = aaiResponse.getIsClosedLoopDisabled();
703             if ("true".equalsIgnoreCase(value) || "T".equalsIgnoreCase(value) || "yes".equalsIgnoreCase(value)
704                     || "Y".equalsIgnoreCase(value)) {
705                 throw new AaiException("is-closed-loop-disabled is set to true (query by " + queryTypeString + ")");
706             }
707         }
708     }
709
710     private static void processVServerResponse(AaiGetVserverResponse aaiResponse) throws AaiException {
711         if (aaiResponse == null) {
712             throw new AaiException("AAI Response is null (query by vserver-name)");
713         }
714         if (aaiResponse.getRequestError() != null) {
715             throw new AaiException("AAI responded with a request error (query by vserver-name)");
716         }
717
718         if (aaiResponse.getIsClosedLoopDisabled() != null) {
719             String value = aaiResponse.getIsClosedLoopDisabled();
720             if ("true".equalsIgnoreCase(value) || "T".equalsIgnoreCase(value) || "yes".equalsIgnoreCase(value)
721                     || "Y".equalsIgnoreCase(value)) {
722                 throw new AaiException("is-closed-loop-disabled is set to true (query by vserver-name)");
723             }
724         }
725     }
726
727     /**
728      * Is closed loop disabled for an event.
729      * 
730      * @param event the event
731      * @return <code>true</code> if the contol loop is disabled, <code>false</code> otherwise
732      */
733     public static boolean isClosedLoopDisabled(VirtualControlLoopEvent event) {
734         if ("true".equalsIgnoreCase(event.getAai().get(VSERVER_IS_CLOSED_LOOP_DISABLED))
735                 || "T".equalsIgnoreCase(event.getAai().get(VSERVER_IS_CLOSED_LOOP_DISABLED))
736                 || "yes".equalsIgnoreCase(event.getAai().get(VSERVER_IS_CLOSED_LOOP_DISABLED))
737                 || "Y".equalsIgnoreCase(event.getAai().get(VSERVER_IS_CLOSED_LOOP_DISABLED))) {
738             return true;
739         }
740         return ("true".equalsIgnoreCase(event.getAai().get(GENERIC_VNF_IS_CLOSED_LOOP_DISABLED))
741                 || "T".equalsIgnoreCase(event.getAai().get(GENERIC_VNF_IS_CLOSED_LOOP_DISABLED))
742                 || "yes".equalsIgnoreCase(event.getAai().get(GENERIC_VNF_IS_CLOSED_LOOP_DISABLED))
743                 || "Y".equalsIgnoreCase(event.getAai().get(GENERIC_VNF_IS_CLOSED_LOOP_DISABLED)));
744     }
745
746     /**
747      * Get the A&AI VService information for an event.
748      * 
749      * @param event the event
750      * @return a AaiGetVserverResponse
751      * @throws ControlLoopException if an error occurs
752      */
753     public static AaiGetVserverResponse getAAIVserverInfo(VirtualControlLoopEvent event) throws ControlLoopException {
754         UUID requestId = event.getRequestId();
755         AaiGetVserverResponse response = null;
756         String vserverName = event.getAai().get(VSERVER_VSERVER_NAME);
757
758         try {
759             if (vserverName != null) {
760                 String aaiHostURL = PolicyEngine.manager.getEnvironmentProperty("aai.url");
761                 String aaiUser = PolicyEngine.manager.getEnvironmentProperty("aai.username");
762                 String aaiPassword = PolicyEngine.manager.getEnvironmentProperty("aai.password");
763                 String aaiGetQueryByVserver = "/aai/v11/nodes/vservers?vserver-name=";
764                 String url = aaiHostURL + aaiGetQueryByVserver;
765                 logger.info("AAI Host URL by VServer: {}", url);
766                 response = new AaiManager(new RESTManager()).getQueryByVserverName(url, aaiUser, aaiPassword, requestId,
767                         vserverName);
768             }
769         } catch (Exception e) {
770             logger.error("getAAIVserverInfo exception: ", e);
771             throw new ControlLoopException("Exception in getAAIVserverInfo: ", e);
772         }
773
774         return response;
775     }
776
777     /**
778      * Get A&AI VNF information for an event.
779      * 
780      * @param event the event
781      * @return a AaiGetVnfResponse
782      * @throws ControlLoopException if an error occurs
783      */
784     public static AaiGetVnfResponse getAAIVnfInfo(VirtualControlLoopEvent event) throws ControlLoopException {
785         UUID requestId = event.getRequestId();
786         AaiGetVnfResponse response = null;
787         String vnfName = event.getAai().get(GENERIC_VNF_VNF_NAME);
788         String vnfId = event.getAai().get(GENERIC_VNF_VNF_ID);
789
790         String aaiHostURL = PolicyEngine.manager.getEnvironmentProperty("aai.url");
791         String aaiUser = PolicyEngine.manager.getEnvironmentProperty("aai.username");
792         String aaiPassword = PolicyEngine.manager.getEnvironmentProperty("aai.password");
793
794         try {
795             if (vnfName != null) {
796                 String aaiGetQueryByVnfName = "/aai/v11/network/generic-vnfs/generic-vnf?vnf-name=";
797                 String url = aaiHostURL + aaiGetQueryByVnfName;
798                 logger.info("AAI Host URL by VNF name: {}", url);
799                 response = new AaiManager(new RESTManager()).getQueryByVnfName(url, aaiUser, aaiPassword, requestId,
800                         vnfName);
801             } else if (vnfId != null) {
802                 String aaiGetQueryByVnfId = "/aai/v11/network/generic-vnfs/generic-vnf/";
803                 String url = aaiHostURL + aaiGetQueryByVnfId;
804                 logger.info("AAI Host URL by VNF ID: {}", url);
805                 response =
806                         new AaiManager(new RESTManager()).getQueryByVnfId(url, aaiUser, aaiPassword, requestId, vnfId);
807             }
808         } catch (Exception e) {
809             logger.error("getAAIVnfInfo exception: ", e);
810             throw new ControlLoopException("Exception in getAAIVnfInfo: ", e);
811         }
812
813         return response;
814     }
815
816     @Override
817     public boolean isActive() {
818         // TODO
819         return true;
820     }
821
822     @Override
823     public boolean releaseLock() {
824         // TODO
825         return false;
826     }
827
828     @Override
829     public String toString() {
830         return "ControlLoopEventManager [closedLoopControlName=" + closedLoopControlName + ", requestID=" + requestID
831                 + ", processor=" + processor + ", onset=" + (onset != null ? onset.getRequestId() : "null")
832                 + ", numOnsets=" + numOnsets + ", numAbatements=" + numAbatements + ", isActivated=" + isActivated
833                 + ", currentOperation=" + currentOperation + ", targetLock=" + targetLock + "]";
834     }
835
836 }