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