2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 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.drools.apps.controller.usecases.step;
23 import java.util.ArrayList;
24 import java.util.LinkedHashMap;
25 import java.util.List;
27 import org.onap.aai.domain.yang.GenericVnf;
28 import org.onap.aai.domain.yang.RelatedToProperty;
29 import org.onap.aai.domain.yang.Relationship;
30 import org.onap.aai.domain.yang.RelationshipData;
31 import org.onap.policy.aai.AaiCqResponse;
32 import org.onap.policy.controlloop.actor.guard.DecisionOperation;
33 import org.onap.policy.controlloop.actor.guard.GuardActor;
34 import org.onap.policy.controlloop.actor.so.VfModuleCreate;
35 import org.onap.policy.controlloop.actorserviceprovider.Operation;
36 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
37 import org.onap.policy.controlloop.actorserviceprovider.TargetType;
38 import org.onap.policy.drools.apps.controller.usecases.UsecasesConstants;
41 * Wrapper for a Guard operation. Note: this makes a clone of the operation parameters,
42 * replacing the payload. It overrides the operation's property names with that are
43 * relevant for guards. In addition, it overrides the relevant loadXxx() methods to load
44 * the data into the payload instead of into the operation's properties. It also
45 * increments or decrements the VF Count, depending whether the operation is a "VF Module
48 public class GuardStep2 extends Step2 {
49 public static final String PAYLOAD_KEY_TARGET_ENTITY = "target";
50 public static final String PAYLOAD_KEY_VF_COUNT = "vfCount";
51 public static final String PAYLOAD_KEY_VNF_NAME = "generic-vnf.vnf-name";
52 public static final String PAYLOAD_KEY_VNF_ID = "generic-vnf.vnf-id";
53 public static final String PAYLOAD_KEY_VNF_TYPE = "generic-vnf.vnf-type";
54 public static final String PAYLOAD_KEY_NF_NAMING_CODE = "generic-vnf.nf-naming-code";
55 public static final String PAYLOAD_KEY_VSERVER_ID = "vserver.vserver-id";
56 public static final String PAYLOAD_KEY_CLOUD_REGION_ID = "cloud-region.cloud-region-id";
58 private final Operation policyOper;
62 * Constructs the object using information from another step.
64 * @param otherStep step whose information should be used
66 public GuardStep2(Step2 otherStep, String closedLoopControlName) {
67 super(otherStep, GuardActor.NAME, DecisionOperation.NAME);
69 if (!otherStep.isInitialized()) {
70 throw new IllegalStateException("policy operation must be initialized before the guard operation");
73 this.policyOper = otherStep.getOperation();
75 Map<String, Object> payload = new LinkedHashMap<>();
76 payload.put("actor", otherStep.getActorName());
77 payload.put("operation", otherStep.getOperationName());
78 payload.put("requestId", params.getRequestId());
79 payload.put("clname", closedLoopControlName);
81 params = params.toBuilder().payload(payload).build();
85 public boolean acceptsEvent() {
90 * Builds the list of properties on the policy's actual operation.
93 public List<String> getPropertyNames() {
94 List<String> names = new ArrayList<>(1);
96 // include VF Count if the policy's operation needs it
97 if (policyOper.getPropertyNames().contains(OperationProperties.DATA_VF_COUNT)) {
98 names.add(OperationProperties.DATA_VF_COUNT);
101 // Only get filter properties if the vserver-name exists, which is needed to call cq
102 if (event.getAai().get("vserver.vserver-name") != null) {
103 names.add(UsecasesConstants.AAI_DEFAULT_GENERIC_VNF);
104 names.add(OperationProperties.AAI_DEFAULT_CLOUD_REGION);
111 * Load the target entity into the payload instead of the operation's properties.
114 protected void loadTargetEntity(String propName) {
115 params.getPayload().put(PAYLOAD_KEY_TARGET_ENTITY, getTargetEntity());
119 * Load the VF Count into the payload instead of the operation's properties.
120 * Increments the count for "VF Module Create". Decrements it otherwise.
123 protected void loadVfCount(String propName) {
124 // run guard with the proposed VF count
125 int count = getVfCount();
126 if (VfModuleCreate.NAME.equals(policyOper.getName())) {
132 params.getPayload().put(PAYLOAD_KEY_VF_COUNT, count);
136 protected void loadCloudRegion(String propName) {
137 // PNF does not support guard filters
138 if (TargetType.PNF.equals(params.getTargetType())) {
142 params.getPayload().put(PAYLOAD_KEY_CLOUD_REGION_ID, getCloudRegion().getCloudRegionId());
146 protected void loadDefaultGenericVnf(String propName) {
147 // PNF does not support guard filters
148 if (TargetType.PNF.equals(params.getTargetType())) {
152 // add in properties needed for filters
153 String targetEntity = getTargetEntity();
154 params.getPayload().put(PAYLOAD_KEY_VNF_ID, targetEntity);
156 AaiCqResponse cq = this.getCustomQueryData();
157 GenericVnf vnf = cq.getGenericVnfByVnfId(targetEntity);
161 params.getPayload().put(PAYLOAD_KEY_VNF_NAME, vnf.getVnfName());
162 params.getPayload().put(PAYLOAD_KEY_VNF_TYPE, vnf.getVnfType());
163 params.getPayload().put(PAYLOAD_KEY_NF_NAMING_CODE, vnf.getNfNamingCode());
165 String vserverName = getEnrichment(OperationProperties.ENRICHMENT_VSERVER_NAME);
167 params.getPayload().put(PAYLOAD_KEY_VSERVER_ID, findServerId(vnf, vserverName));
170 private String findServerId(GenericVnf vnf, String vserverName) {
171 for (Relationship relationship : vnf.getRelationshipList().getRelationship()) {
172 if (!"vserver".equals(relationship.getRelatedTo())) {
175 String vserverId = findServerId(relationship, vserverName);
176 if (vserverId != null) {
183 private String findServerId(Relationship relationship, String vserverName) {
184 for (RelatedToProperty to : relationship.getRelatedToProperty()) {
185 if ("vserver.vserver-name".equals(to.getPropertyKey()) && vserverName.equals(to.getPropertyValue())) {
186 // Found the right relationship server-name, now find the server-id
187 for (RelationshipData data : relationship.getRelationshipData()) {
188 if (PAYLOAD_KEY_VSERVER_ID.equals(data.getRelationshipKey())) {
189 return data.getRelationshipValue();