2 * ============LICENSE_START=======================================================
3 * controlloop event manager
4 * ================================================================================
5 * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controlloop.eventmanager;
23 import static org.onap.policy.controlloop.ControlLoopTargetType.PNF;
24 import static org.onap.policy.controlloop.ControlLoopTargetType.VM;
25 import static org.onap.policy.controlloop.ControlLoopTargetType.VNF;
27 import java.io.Serializable;
28 import java.io.UnsupportedEncodingException;
29 import java.net.URLDecoder;
30 import java.util.Collections;
31 import java.util.HashSet;
32 import java.util.LinkedList;
34 import java.util.NoSuchElementException;
36 import java.util.UUID;
37 import java.util.stream.Collectors;
38 import java.util.stream.Stream;
39 import org.apache.commons.lang3.StringUtils;
40 import org.onap.policy.aai.AaiCqResponse;
41 import org.onap.policy.aai.AaiManager;
42 import org.onap.policy.aai.util.AaiException;
43 import org.onap.policy.controlloop.ControlLoopEventStatus;
44 import org.onap.policy.controlloop.ControlLoopException;
45 import org.onap.policy.controlloop.ControlLoopNotificationType;
46 import org.onap.policy.controlloop.ControlLoopOperation;
47 import org.onap.policy.controlloop.VirtualControlLoopEvent;
48 import org.onap.policy.controlloop.VirtualControlLoopNotification;
49 import org.onap.policy.controlloop.policy.FinalResult;
50 import org.onap.policy.controlloop.policy.Policy;
51 import org.onap.policy.controlloop.processor.ControlLoopProcessor;
52 import org.onap.policy.drools.core.lock.Lock;
53 import org.onap.policy.drools.core.lock.LockCallback;
54 import org.onap.policy.drools.core.lock.LockImpl;
55 import org.onap.policy.drools.core.lock.LockState;
56 import org.onap.policy.drools.system.PolicyEngineConstants;
57 import org.onap.policy.drools.utils.Pair;
58 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
59 import org.onap.policy.rest.RestManager;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
63 public class ControlLoopEventManager implements Serializable {
64 public static final String PROV_STATUS_ACTIVE = "ACTIVE";
65 private static final String VM_NAME = "VM_NAME";
66 private static final String VNF_NAME = "VNF_NAME";
67 public static final String GENERIC_VNF_VNF_ID = "generic-vnf.vnf-id";
68 public static final String GENERIC_VNF_VNF_NAME = "generic-vnf.vnf-name";
69 public static final String VSERVER_VSERVER_NAME = "vserver.vserver-name";
70 public static final String GENERIC_VNF_IS_CLOSED_LOOP_DISABLED = "generic-vnf.is-closed-loop-disabled";
71 public static final String VSERVER_IS_CLOSED_LOOP_DISABLED = "vserver.is-closed-loop-disabled";
72 private static final String PNF_IS_IN_MAINT = "pnf.in-maint";
73 public static final String GENERIC_VNF_PROV_STATUS = "generic-vnf.prov-status";
74 public static final String VSERVER_PROV_STATUS = "vserver.prov-status";
75 public static final String PNF_ID = "pnf.pnf-id";
76 public static final String PNF_NAME = "pnf.pnf-name";
78 public static final String AAI_URL = "aai.url";
79 public static final String AAI_USERNAME_PROPERTY = "aai.username";
80 public static final String AAI_PASS_PROPERTY = "aai.password";
84 * Additional time, in seconds, to add to a "lock" request. This ensures that the lock won't expire right before an
85 * operation completes.
87 private static final int ADDITIONAL_LOCK_SEC = 60;
89 private static final Logger logger = LoggerFactory.getLogger(ControlLoopEventManager.class);
91 private static final long serialVersionUID = -1216568161322872641L;
93 private static final Set<String> VALID_TARGETS;
96 VALID_TARGETS = Collections.unmodifiableSet(new HashSet<>(
97 Stream.of(VM_NAME, VNF_NAME, VSERVER_VSERVER_NAME, GENERIC_VNF_VNF_ID, GENERIC_VNF_VNF_NAME, PNF_NAME)
98 .map(String::toLowerCase).collect(Collectors.toList())));
101 public final String closedLoopControlName;
102 private final UUID requestId;
104 private String controlLoopResult;
105 private ControlLoopProcessor processor = null;
106 private VirtualControlLoopEvent onset;
107 private Integer numOnsets = 0;
108 private Integer numAbatements = 0;
109 private VirtualControlLoopEvent abatement;
110 private FinalResult controlLoopTimedOut = null;
112 private boolean isActivated = false;
113 private LinkedList<ControlLoopOperation> controlLoopHistory = new LinkedList<>();
114 private ControlLoopOperationManager currentOperation = null;
115 private ControlLoopOperationManager lastOperationManager = null;
116 private transient Lock targetLock = null;
117 private boolean useTargetLock = true;
120 * Constructs the object.
122 * @param closedLoopControlName name of the control loop
123 * @param requestId ID of the request with which this manager is associated
125 public ControlLoopEventManager(String closedLoopControlName, UUID requestId) {
126 this.closedLoopControlName = closedLoopControlName;
127 this.requestId = requestId;
130 public String getClosedLoopControlName() {
131 return closedLoopControlName;
134 public String getControlLoopResult() {
135 return controlLoopResult;
138 public void setControlLoopResult(String controlLoopResult) {
139 this.controlLoopResult = controlLoopResult;
142 public Integer getNumOnsets() {
146 public void setNumOnsets(Integer numOnsets) {
147 this.numOnsets = numOnsets;
150 public Integer getNumAbatements() {
151 return numAbatements;
154 public void setNumAbatements(Integer numAbatements) {
155 this.numAbatements = numAbatements;
158 public boolean isActivated() {
162 public void setActivated(boolean isActivated) {
163 this.isActivated = isActivated;
166 public boolean useTargetLock() {
167 return useTargetLock();
170 public void setUseTargetLock(boolean useTargetLock) {
171 this.useTargetLock = useTargetLock;
174 public VirtualControlLoopEvent getOnsetEvent() {
178 public VirtualControlLoopEvent getAbatementEvent() {
179 return this.abatement;
182 public ControlLoopProcessor getProcessor() {
183 return this.processor;
186 public UUID getRequestId() {
191 private VirtualControlLoopNotification rejectNotification(VirtualControlLoopEvent event, String message) {
192 VirtualControlLoopNotification notification = new VirtualControlLoopNotification(event);
193 notification.setNotification(ControlLoopNotificationType.REJECTED);
194 notification.setMessage(message);
199 * Preactivations check for an event.
201 * @param event the event
202 * @return the VirtualControlLoopNotification
204 private VirtualControlLoopNotification preActivationChecks(VirtualControlLoopEvent event) {
207 // This method should ONLY be called ONCE
209 if (this.isActivated) {
210 return rejectNotification(event, "ControlLoopEventManager has already been activated.");
214 // Syntax check the event
216 checkEventSyntax(event);
217 } catch (ControlLoopException e) {
218 logger.warn("{}: invalid event syntax: ", this, e);
219 return rejectNotification(event, e.getMessage());
223 return new VirtualControlLoopNotification(event);
227 * Activate a control loop event.
229 * @param event the event
230 * @return the VirtualControlLoopNotification
232 public VirtualControlLoopNotification activate(VirtualControlLoopEvent event) {
233 VirtualControlLoopNotification notification = preActivationChecks(event);
234 if (notification.getNotification() == ControlLoopNotificationType.REJECTED) {
238 return postActivate(event, notification);
242 * Activate a control loop event.
244 * @param yamlSpecification the yaml specification
245 * @param event the event
246 * @return the VirtualControlLoopNotification
248 public VirtualControlLoopNotification activate(String yamlSpecification, VirtualControlLoopEvent event) {
249 VirtualControlLoopNotification notification = preActivationChecks(event);
250 if (notification.getNotification() == ControlLoopNotificationType.REJECTED) {
254 if (yamlSpecification == null || yamlSpecification.length() < 1) {
255 return rejectNotification(event, "yaml specification is null or 0 length");
258 String decodedYaml = null;
260 decodedYaml = URLDecoder.decode(yamlSpecification, "UTF-8");
261 if (decodedYaml != null && decodedYaml.length() > 0) {
262 yamlSpecification = decodedYaml;
264 } catch (UnsupportedEncodingException e) {
265 logger.warn("{}: YAML decode in activate by YAML specification and event threw: ", this, e);
266 return rejectNotification(event, e.getMessage());
271 // Parse the YAML specification
273 this.processor = new ControlLoopProcessor(yamlSpecification);
274 } catch (ControlLoopException e) {
275 logger.error("{}: activate by YAML specification and event threw: ", this, e);
276 return rejectNotification(event, e.getMessage());
279 return postActivate(event, notification);
283 * Activate a control loop event.
285 * @param toscaPolicy the tosca policy
286 * @param event the event
287 * @return the VirtualControlLoopNotification
289 public VirtualControlLoopNotification activate(ToscaPolicy toscaPolicy, VirtualControlLoopEvent event) {
290 VirtualControlLoopNotification notification = preActivationChecks(event);
291 if (notification.getNotification() == ControlLoopNotificationType.REJECTED) {
296 this.processor = new ControlLoopProcessor(toscaPolicy);
297 } catch (ControlLoopException e) {
298 logger.error("{}: activate from Tosca Policy threw: ", this, e);
299 return rejectNotification(event, e.getMessage());
302 return postActivate(event, notification);
305 private VirtualControlLoopNotification postActivate(
306 VirtualControlLoopEvent event, VirtualControlLoopNotification notification) {
308 // At this point we are good to go with this event
314 // Set ourselves as active
316 this.isActivated = true;
318 notification.setNotification(ControlLoopNotificationType.ACTIVE);
323 * Check if the control loop is final.
325 * @return a VirtualControlLoopNotification if the control loop is final, otherwise <code>null</code> is returned
326 * @throws ControlLoopException if an error occurs
328 public VirtualControlLoopNotification isControlLoopFinal() throws ControlLoopException {
329 validateFinalControlLoop();
331 // Ok, start creating the notification
333 VirtualControlLoopNotification notification = new VirtualControlLoopNotification(this.onset);
335 // Check if the overall control loop has timed out
337 if (this.isControlLoopTimedOut()) {
339 // Yes we have timed out
341 notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
342 notification.setMessage("Control Loop timed out");
343 notification.getHistory().addAll(this.controlLoopHistory);
347 // Check if the current policy is Final
349 FinalResult result = this.processor.checkIsCurrentPolicyFinal();
350 if (result == null) {
352 // we are not at a final result
358 case FINAL_FAILURE_EXCEPTION:
359 notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
360 notification.setMessage("Exception in processing closed loop");
363 case FINAL_FAILURE_RETRIES:
364 case FINAL_FAILURE_TIMEOUT:
365 case FINAL_FAILURE_GUARD:
366 notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
369 notification.setNotification(ControlLoopNotificationType.FINAL_OPENLOOP);
372 notification.setNotification(ControlLoopNotificationType.FINAL_SUCCESS);
378 // Be sure to add all the history
380 notification.getHistory().addAll(this.controlLoopHistory);
384 private void validateFinalControlLoop() throws ControlLoopException {
386 // Check if they activated us
388 if (!this.isActivated) {
389 throw new ControlLoopException("ControlLoopEventManager MUST be activated first.");
392 // Make sure we are expecting this call.
394 if (this.onset == null) {
395 throw new ControlLoopException("No onset event for ControlLoopEventManager.");
400 * Process the control loop.
402 * @return a ControlLoopOperationManager
403 * @throws ControlLoopException if an error occurs
405 public ControlLoopOperationManager processControlLoop() throws ControlLoopException {
406 validateFinalControlLoop();
408 // Is there a current operation?
410 if (this.currentOperation != null) {
412 // Throw an exception, or simply return the current operation?
414 throw new ControlLoopException("Already working an Operation, do not call this method.");
417 // Ensure we are not FINAL
419 VirtualControlLoopNotification notification = this.isControlLoopFinal();
420 if (notification != null) {
422 // This is weird, we require them to call the isControlLoopFinal() method first
424 // We should really abstract this and avoid throwing an exception, because it really
425 // isn't an exception.
427 throw new ControlLoopException("Control Loop is in FINAL state, do not call this method.");
430 // Not final so get the policy that needs to be worked on.
432 Policy policy = this.processor.getCurrentPolicy();
433 if (policy == null) {
434 throw new ControlLoopException("ControlLoopEventManager: processor came upon null Policy.");
437 // And setup an operation
439 this.lastOperationManager = this.currentOperation;
440 this.currentOperation = new ControlLoopOperationManager(this.onset, policy, this);
444 return this.currentOperation;
448 * Finish an operation.
450 * @param operation the operation
452 public void finishOperation(ControlLoopOperationManager operation) throws ControlLoopException {
454 // Verify we have a current operation
456 if (this.currentOperation != null) {
458 // Validate they are finishing the current operation
459 // PLD - this is simply comparing the policy. Do we want to equals the whole object?
461 if (this.currentOperation.policy.equals(operation.policy)) {
462 logger.debug("Finishing {} result is {}", this.currentOperation.policy.getRecipe(),
463 this.currentOperation.getOperationResult());
467 this.controlLoopHistory.addAll(this.currentOperation.getHistory());
469 // Move to the next Policy
471 this.processor.nextPolicyForResult(this.currentOperation.getOperationResult());
473 // Just null this out
475 this.lastOperationManager = this.currentOperation;
476 this.currentOperation = null;
479 // Don't release the lock - it may be re-used by the next operation
484 logger.debug("Cannot finish current operation {} does not match given operation {}",
485 this.currentOperation.policy, operation.policy);
488 throw new ControlLoopException("No operation to finish.");
492 * Obtain a lock for the current operation.
494 * @param callback call-back to be invoked when the lock state changes
495 * @return a pair containing the old lock and the new lock, either of which may be null
496 * @throws ControlLoopException if an error occurs
498 public synchronized Pair<Lock, Lock> lockCurrentOperation(LockCallback callback) throws ControlLoopException {
502 if (this.currentOperation == null) {
503 throw new ControlLoopException("Do not have a current operation.");
507 // Release the old lock if it's for a different resource.
510 if (this.targetLock != null
511 && !this.targetLock.getResourceId().equals(this.currentOperation.getTargetEntity())) {
512 logger.debug("{}: different resource - releasing old lock", getClosedLoopControlName());
513 oldLock = this.targetLock;
514 this.targetLock = null;
517 // keep the lock a little longer than the operation, including retries
518 int optimeout = Math.max(1, this.currentOperation.getOperationTimeout());
519 int nattempts = 1 + Math.max(0, this.currentOperation.getMaxRetries());
520 int holdSec = optimeout * nattempts + ADDITIONAL_LOCK_SEC;
523 // Have we acquired it already?
525 if (this.targetLock != null) {
526 // we have the lock - just extend it
527 this.targetLock.extend(holdSec, callback);
528 return new Pair<>(oldLock, null);
530 } else if (this.useTargetLock) {
531 this.targetLock = createRealLock(this.currentOperation.getTargetEntity(), this.onset.getRequestId(),
533 return new Pair<>(oldLock, this.targetLock);
536 // Not using target locks - create a lock w/o actually locking.
537 logger.debug("{}: not using target locking; using pseudo locks", getClosedLoopControlName());
538 this.targetLock = createPseudoLock(this.currentOperation.getTargetEntity(), this.onset.getRequestId(),
541 // Note: no need to invoke callback, as the lock is already ACTIVE
543 return new Pair<>(oldLock, this.targetLock);
548 * Releases the lock for the current operation, deleting it from working memory.
550 * @return the lock, if the operation was locked, {@code null} otherwise
552 public synchronized Lock unlockCurrentOperation() {
553 if (this.targetLock == null) {
557 Lock lock = this.targetLock;
558 this.targetLock = null;
565 public enum NewEventStatus {
566 FIRST_ONSET, SUBSEQUENT_ONSET, FIRST_ABATEMENT, SUBSEQUENT_ABATEMENT, SYNTAX_ERROR;
570 * An event onset/abatement.
572 * @param event the event
574 * @throws AaiException if an error occurs retrieving information from A&AI
576 public NewEventStatus onNewEvent(VirtualControlLoopEvent event) throws AaiException {
578 this.checkEventSyntax(event);
579 if (event.getClosedLoopEventStatus() == ControlLoopEventStatus.ONSET) {
581 // Check if this is our original ONSET
583 if (event.equals(this.onset)) {
587 return NewEventStatus.FIRST_ONSET;
590 // Log that we got an onset
593 return NewEventStatus.SUBSEQUENT_ONSET;
594 } else if (event.getClosedLoopEventStatus() == ControlLoopEventStatus.ABATED) {
596 // Have we already got an abatement?
598 if (this.abatement == null) {
602 this.abatement = event;
604 // Keep track that we received another
606 this.numAbatements++;
610 return NewEventStatus.FIRST_ABATEMENT;
613 // Keep track that we received another
615 this.numAbatements++;
619 return NewEventStatus.SUBSEQUENT_ABATEMENT;
622 } catch (ControlLoopException e) {
623 logger.error("{}: onNewEvent threw: ", this, e);
625 return NewEventStatus.SYNTAX_ERROR;
630 * Commit the abatement to the history database.
632 * @param message the abatement message
633 * @param outcome the abatement outcome
635 public void commitAbatement(String message, String outcome) {
636 if (this.lastOperationManager == null) {
637 logger.error("{}: commitAbatement: no operation manager", this);
641 this.lastOperationManager.commitAbatement(message, outcome);
642 } catch (NoSuchElementException e) {
643 logger.error("{}: commitAbatement threw an exception ", this, e);
649 * Set the control loop time out.
651 * @return a VirtualControlLoopNotification
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);
662 public boolean isControlLoopTimedOut() {
663 return (this.controlLoopTimedOut == FinalResult.FINAL_FAILURE_TIMEOUT);
667 * Get the control loop timeout.
669 * @param defaultTimeout the default timeout
670 * @return the timeout
672 public int getControlLoopTimeout(Integer defaultTimeout) {
673 if (this.processor != null && this.processor.getControlLoop() != null) {
674 Integer timeout = this.processor.getControlLoop().getTimeout();
675 if (timeout != null && timeout > 0) {
679 if (defaultTimeout != null) {
680 return defaultTimeout;
686 * Check an event syntax.
688 * @param event the event syntax
689 * @throws ControlLoopException if an error occurs
691 public void checkEventSyntax(VirtualControlLoopEvent event) throws ControlLoopException {
692 validateStatus(event);
693 if (StringUtils.isBlank(event.getClosedLoopControlName())) {
694 throw new ControlLoopException("No control loop name");
696 if (event.getRequestId() == null) {
697 throw new ControlLoopException("No request ID");
699 if (event.getClosedLoopEventStatus() == ControlLoopEventStatus.ABATED) {
702 if (StringUtils.isBlank(event.getTarget())) {
703 throw new ControlLoopException("No target field");
704 } else if (!VALID_TARGETS.contains(event.getTarget().toLowerCase())) {
705 throw new ControlLoopException("target field invalid - expecting VM_NAME or VNF_NAME");
707 validateAaiData(event);
710 private void validateStatus(VirtualControlLoopEvent event) throws ControlLoopException {
711 if (event.getClosedLoopEventStatus() == null
712 || (event.getClosedLoopEventStatus() != ControlLoopEventStatus.ONSET
713 && event.getClosedLoopEventStatus() != ControlLoopEventStatus.ABATED)) {
714 throw new ControlLoopException("Invalid value in closedLoopEventStatus");
718 private void validateAaiData(VirtualControlLoopEvent event) throws ControlLoopException {
719 Map<String, String> eventAai = event.getAai();
720 if (eventAai == null) {
721 throw new ControlLoopException("AAI is null");
723 switch (event.getTargetType()) {
726 validateAaiVmVnfData(eventAai);
729 validateAaiPnfData(eventAai);
732 throw new ControlLoopException("The target type is not supported");
736 private void validateAaiVmVnfData(Map<String, String> eventAai) throws ControlLoopException {
737 if (eventAai.get(GENERIC_VNF_VNF_ID) == null && eventAai.get(VSERVER_VSERVER_NAME) == null
738 && eventAai.get(GENERIC_VNF_VNF_NAME) == null) {
739 throw new ControlLoopException(
740 "generic-vnf.vnf-id or generic-vnf.vnf-name or vserver.vserver-name information missing");
744 private void validateAaiPnfData(Map<String, String> eventAai) throws ControlLoopException {
745 if (eventAai.get(PNF_NAME) == null) {
746 throw new ControlLoopException("AAI PNF object key pnf-name is missing");
751 * Is closed loop disabled for an event.
753 * @param event the event
754 * @return <code>true</code> if the control loop is disabled, <code>false</code> otherwise
756 public static boolean isClosedLoopDisabled(VirtualControlLoopEvent event) {
757 Map<String, String> aai = event.getAai();
758 return (isAaiTrue(aai.get(VSERVER_IS_CLOSED_LOOP_DISABLED))
759 || isAaiTrue(aai.get(GENERIC_VNF_IS_CLOSED_LOOP_DISABLED))
760 || isAaiTrue(aai.get(PNF_IS_IN_MAINT)));
764 * Does provisioning status, for an event, have a value other than ACTIVE.
766 * @param event the event
767 * @return {@code true} if the provisioning status is neither ACTIVE nor {@code null}, {@code false} otherwise
769 protected static boolean isProvStatusInactive(VirtualControlLoopEvent event) {
770 Map<String, String> aai = event.getAai();
771 return (!PROV_STATUS_ACTIVE.equals(aai.getOrDefault(VSERVER_PROV_STATUS, PROV_STATUS_ACTIVE))
772 || !PROV_STATUS_ACTIVE.equals(aai.getOrDefault(GENERIC_VNF_PROV_STATUS, PROV_STATUS_ACTIVE)));
776 * Determines the boolean value represented by the given AAI field value.
778 * @param aaiValue value to be examined
779 * @return the boolean value represented by the field value, or {@code false} if the value is {@code null}
781 protected static boolean isAaiTrue(String aaiValue) {
782 return ("true".equalsIgnoreCase(aaiValue) || "T".equalsIgnoreCase(aaiValue) || "yes".equalsIgnoreCase(aaiValue)
783 || "Y".equalsIgnoreCase(aaiValue));
787 public String toString() {
788 return "ControlLoopEventManager [closedLoopControlName=" + closedLoopControlName + ", requestId=" + requestId
789 + ", processor=" + processor + ", onset=" + (onset != null ? onset.getRequestId() : "null")
790 + ", numOnsets=" + numOnsets + ", numAbatements=" + numAbatements + ", isActivated=" + isActivated
791 + ", currentOperation=" + currentOperation + ", targetLock=" + targetLock + "]";
795 * This function calls Aai Custom Query and responds with the AaiCqResponse.
797 * @param event input event
798 * @return AaiCqResponse Response from Aai for custom query. Can not be null.
799 * @throws AaiException if error occurs
801 public AaiCqResponse getCqResponse(VirtualControlLoopEvent event) throws AaiException {
803 Map<String, String> aai = event.getAai();
805 if (aai.containsKey(VSERVER_IS_CLOSED_LOOP_DISABLED) || aai.containsKey(GENERIC_VNF_IS_CLOSED_LOOP_DISABLED)) {
807 if (isClosedLoopDisabled(event)) {
808 throw new AaiException("is-closed-loop-disabled is set to true on VServer or VNF");
811 if (isProvStatusInactive(event)) {
812 throw new AaiException("prov-status is not ACTIVE on VServer or VNF");
816 if (!aai.containsKey(VSERVER_VSERVER_NAME)) {
817 throw new AaiException("Vserver name is missing");
820 UUID reqId = event.getRequestId();
821 AaiCqResponse response = null;
822 String vserverId = event.getAai().get(VSERVER_VSERVER_NAME);
824 String aaiHostUrl = PolicyEngineConstants.getManager().getEnvironmentProperty(AAI_URL);
825 String aaiUser = PolicyEngineConstants.getManager().getEnvironmentProperty(AAI_USERNAME_PROPERTY);
826 String aaiPassword = PolicyEngineConstants.getManager().getEnvironmentProperty(AAI_PASS_PROPERTY);
828 response = new AaiManager(new RestManager()).getCustomQueryResponse(aaiHostUrl, aaiUser, aaiPassword, reqId,
831 if (response == null) {
832 throw new AaiException("Target vnf-id could not be found");
840 * Get the specified pnf data from aai.
841 * @param event the event containing pnf id.
842 * @return pnf key value data.
843 * @throws AaiException if an aai error occurs.
845 public Map<String, String> getPnf(VirtualControlLoopEvent event) throws AaiException {
846 Map<String, String> aai = event.getAai();
848 if (!aai.containsKey(PNF_NAME)) {
849 throw new AaiException("Missing unique identifier for PNF AAI object in the event.");
852 UUID reqId = event.getRequestId();
853 String pnfName = event.getAai().get(PNF_NAME);
854 String aaiHostUrl = PolicyEngineConstants.getManager().getEnvironmentProperty(AAI_URL);
855 String aaiUser = PolicyEngineConstants.getManager().getEnvironmentProperty(AAI_USERNAME_PROPERTY);
856 String aaiPassword = PolicyEngineConstants.getManager().getEnvironmentProperty(AAI_PASS_PROPERTY);
858 Map<String, String> pnfParams =
859 new AaiManager(new RestManager()).getPnf(aaiHostUrl, aaiUser, aaiPassword, reqId, pnfName);
861 if (pnfParams == null) {
862 throw new AaiException("Aai response is undefined");
868 // the following methods may be overridden by junit tests
870 protected Lock createRealLock(String targetEntity, UUID requestId, int holdSec, LockCallback callback) {
871 return PolicyEngineConstants.getManager().createLock(targetEntity, requestId.toString(), holdSec, callback,
875 // note: the "callback" is required, because it will be invoked when lock.extend() is
877 protected Lock createPseudoLock(String targetEntity, UUID requestId, int holdSec, LockCallback callback) {
878 return new LockImpl(LockState.ACTIVE, targetEntity, requestId.toString(), holdSec, callback);