abc2bcb462186bd986732f507622e3740cb77bfb
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
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.drools.apps.controller.usecases.step;
22
23 import java.util.ArrayList;
24 import java.util.LinkedHashMap;
25 import java.util.List;
26 import java.util.Map;
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;
39
40 /**
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
46  * Create" or not.
47  */
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";
57
58     private final Operation policyOper;
59
60
61     /**
62      * Constructs the object using information from another step.
63      *
64      * @param otherStep step whose information should be used
65      */
66     public GuardStep2(Step2 otherStep, String closedLoopControlName) {
67         super(otherStep, GuardActor.NAME, DecisionOperation.NAME);
68
69         if (!otherStep.isInitialized()) {
70             throw new IllegalStateException("policy operation must be initialized before the guard operation");
71         }
72
73         this.policyOper = otherStep.getOperation();
74
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);
80
81         params = params.toBuilder().payload(payload).build();
82     }
83
84     @Override
85     public boolean acceptsEvent() {
86         return true;
87     }
88
89     /**
90      * Builds the list of properties on the policy's actual operation.
91      */
92     @Override
93     public List<String> getPropertyNames() {
94         List<String> names = new ArrayList<>(1);
95
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);
99         }
100
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);
105         }
106
107         return names;
108     }
109
110     /**
111      * Load the target entity into the payload instead of the operation's properties.
112      */
113     @Override
114     protected void loadTargetEntity(String propName) {
115         params.getPayload().put(PAYLOAD_KEY_TARGET_ENTITY, getTargetEntity());
116     }
117
118     /**
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.
121      */
122     @Override
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())) {
127             ++count;
128         } else {
129             --count;
130         }
131
132         params.getPayload().put(PAYLOAD_KEY_VF_COUNT, count);
133     }
134
135     @Override
136     protected void loadCloudRegion(String propName) {
137         // PNF does not support guard filters
138         if (TargetType.PNF.equals(params.getTargetType())) {
139             return;
140         }
141
142         params.getPayload().put(PAYLOAD_KEY_CLOUD_REGION_ID, getCloudRegion().getCloudRegionId());
143     }
144
145     @Override
146     protected void loadDefaultGenericVnf(String propName) {
147         // PNF does not support guard filters
148         if (TargetType.PNF.equals(params.getTargetType())) {
149             return;
150         }
151
152         // add in properties needed for filters
153         String targetEntity = getTargetEntity();
154         params.getPayload().put(PAYLOAD_KEY_VNF_ID, targetEntity);
155
156         AaiCqResponse cq = this.getCustomQueryData();
157         GenericVnf vnf = cq.getGenericVnfByVnfId(targetEntity);
158         if (vnf == null) {
159             return;
160         }
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());
164
165         String vserverName = getEnrichment(OperationProperties.ENRICHMENT_VSERVER_NAME);
166
167         params.getPayload().put(PAYLOAD_KEY_VSERVER_ID, findServerId(vnf, vserverName));
168     }
169
170     private String findServerId(GenericVnf vnf, String vserverName) {
171         for (Relationship relationship : vnf.getRelationshipList().getRelationship()) {
172             if (!"vserver".equals(relationship.getRelatedTo())) {
173                 continue;
174             }
175             String vserverId = findServerId(relationship, vserverName);
176             if (vserverId != null) {
177                 return vserverId;
178             }
179         }
180         return null;
181     }
182
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();
190                     }
191                 }
192             }
193         }
194         return null;
195     }
196
197 }