4db243408a829c2e787a5bf5ce72be5dc01c71e9
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020-2021 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;
22
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;
26 import static org.onap.policy.drools.apps.controller.usecases.UsecasesConstants.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED;
27 import static org.onap.policy.drools.apps.controller.usecases.UsecasesConstants.GENERIC_VNF_PROV_STATUS;
28 import static org.onap.policy.drools.apps.controller.usecases.UsecasesConstants.GENERIC_VNF_VNF_ID;
29 import static org.onap.policy.drools.apps.controller.usecases.UsecasesConstants.GENERIC_VNF_VNF_NAME;
30 import static org.onap.policy.drools.apps.controller.usecases.UsecasesConstants.PNF_IS_IN_MAINT;
31 import static org.onap.policy.drools.apps.controller.usecases.UsecasesConstants.PNF_NAME;
32 import static org.onap.policy.drools.apps.controller.usecases.UsecasesConstants.PROV_STATUS_ACTIVE;
33 import static org.onap.policy.drools.apps.controller.usecases.UsecasesConstants.VM_NAME;
34 import static org.onap.policy.drools.apps.controller.usecases.UsecasesConstants.VNF_NAME;
35 import static org.onap.policy.drools.apps.controller.usecases.UsecasesConstants.VSERVER_IS_CLOSED_LOOP_DISABLED;
36 import static org.onap.policy.drools.apps.controller.usecases.UsecasesConstants.VSERVER_PROV_STATUS;
37 import static org.onap.policy.drools.apps.controller.usecases.UsecasesConstants.VSERVER_VSERVER_NAME;
38
39 import java.util.Deque;
40 import java.util.Map;
41 import java.util.Set;
42 import java.util.stream.Collectors;
43 import java.util.stream.Stream;
44 import org.drools.core.WorkingMemory;
45 import org.onap.policy.controlloop.ControlLoopException;
46 import org.onap.policy.controlloop.ControlLoopResponse;
47 import org.onap.policy.controlloop.VirtualControlLoopEvent;
48 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
49 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
50 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
51 import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
52 import org.onap.policy.controlloop.eventmanager.ActorConstants;
53 import org.onap.policy.controlloop.eventmanager.ClEventManagerWithEvent;
54 import org.onap.policy.controlloop.eventmanager.StepContext;
55 import org.onap.policy.drools.apps.controller.usecases.step.AaiCqStep2;
56 import org.onap.policy.drools.apps.controller.usecases.step.AaiGetPnfStep2;
57 import org.onap.policy.drools.apps.controller.usecases.step.AaiGetTenantStep2;
58 import org.onap.policy.drools.apps.controller.usecases.step.GetTargetEntityStep2;
59 import org.onap.policy.drools.apps.controller.usecases.step.GuardStep2;
60 import org.onap.policy.drools.apps.controller.usecases.step.LockStep2;
61 import org.onap.policy.drools.apps.controller.usecases.step.Step2;
62 import org.onap.policy.sdnr.PciMessage;
63
64 /**
65  * Manager for a single control loop event. Once this has been created, the event can be
66  * retracted from working memory. Processing progresses through each policy, which
67  * involves at least one step. As a step is processed, additional preprocessor steps may
68  * be pushed onto the queue (e.g., locks, A&AI queries, guards).
69  */
70 public class UsecasesEventManager extends ClEventManagerWithEvent<Step2> implements StepContext {
71
72     private static final long serialVersionUID = -1216568161322872641L;
73
74     /**
75      * If there's a failure from one of these actors, then the TOSCA processing should be
76      * aborted.
77      */
78     private static final Set<String> ABORT_ACTORS = Set.of(ActorConstants.CL_TIMEOUT_ACTOR, ActorConstants.LOCK_ACTOR);
79
80     private static final Set<String> VALID_TARGETS = Stream
81                     .of(VM_NAME, VNF_NAME, VSERVER_VSERVER_NAME, GENERIC_VNF_VNF_ID, GENERIC_VNF_VNF_NAME, PNF_NAME)
82                     .map(String::toLowerCase).collect(Collectors.toSet());
83
84     private static final Set<String> TRUE_VALUES = Set.of("true", "t", "yes", "y");
85
86     /**
87      * Names of Operation properties for which A&AI PNF query is needed.
88      */
89     private static final Set<String> PNF_PROPERTIES = Set.of(OperationProperties.AAI_PNF);
90
91     /**
92      * Names of Operation properties for which A&AI Tenant query is needed.
93      */
94     private static final Set<String> TENANT_PROPERTIES = Set.of(OperationProperties.AAI_VSERVER_LINK);
95
96     /**
97      * Names of Operation properties for which A&AI custom query is needed.
98      */
99     private static final Set<String> CQ_PROPERTIES = Set.of(OperationProperties.AAI_DEFAULT_CLOUD_REGION,
100                     OperationProperties.AAI_VNF, OperationProperties.AAI_SERVICE_MODEL,
101                     OperationProperties.AAI_VNF_MODEL, OperationProperties.AAI_SERVICE,
102                     OperationProperties.AAI_RESOURCE_VNF, UsecasesConstants.AAI_DEFAULT_GENERIC_VNF);
103
104
105     /**
106      * Constructs the object.
107      *
108      * @param params control loop parameters
109      * @param event event to be managed by this object
110      * @param workMem working memory to update if this changes
111      * @throws ControlLoopException if the event is invalid or if a YAML processor cannot
112      *         be created
113      */
114     public UsecasesEventManager(ControlLoopParams params, VirtualControlLoopEvent event, WorkingMemory workMem)
115                     throws ControlLoopException {
116
117         super(params, event, workMem);
118
119         if (isClosedLoopDisabled(event)) {
120             throw new IllegalStateException("is-closed-loop-disabled is set to true on VServer or VNF");
121         }
122
123         if (isProvStatusInactive(event)) {
124             throw new IllegalStateException("prov-status is not ACTIVE on VServer or VNF");
125         }
126     }
127
128     /*
129      * This is needed to satisfy drools.
130      */
131     @Override
132     public Deque<Step2> getSteps() {
133         return super.getSteps();
134     }
135
136     /**
137      * Loads the preprocessor steps needed by the step that's at the front of the queue.
138      */
139     public void loadPreprocessorSteps() {
140         super.loadPreprocessorSteps();
141
142         final Deque<Step2> steps = getSteps();
143         final Step2 step = getSteps().peek();
144
145         // determine if any A&AI queries are needed
146         boolean needCq = false;
147         boolean needPnf = false;
148         boolean needTenant = false;
149         boolean needTargetEntity = false;
150
151         for (String propName : step.getPropertyNames()) {
152             needCq = needCq || CQ_PROPERTIES.contains(propName);
153             needPnf = needPnf || PNF_PROPERTIES.contains(propName);
154             needTenant = needTenant || TENANT_PROPERTIES.contains(propName);
155             needTargetEntity = needTargetEntity || OperationProperties.AAI_TARGET_ENTITY.equals(propName);
156         }
157
158         /*
159          * The Policy's actual operation requires additional, implicit steps, such as
160          * locking and guards.
161          */
162         final boolean needPolicySteps = step.isPolicyStep();
163
164
165         /*
166          * NOTE: need to push steps onto the queue in the OPPOSITE order in which they
167          * must be performed.
168          */
169
170
171         // GUARD must be pushed first
172         if (needPolicySteps) {
173             steps.push(new GuardStep2(step, getClosedLoopControlName()));
174         }
175
176         // A&AI queries
177         if (needCq) {
178             steps.push(new AaiCqStep2(step));
179         }
180
181         if (needPnf) {
182             steps.push(new AaiGetPnfStep2(step));
183         }
184
185         if (needTenant) {
186             steps.push(new AaiGetTenantStep2(step));
187         }
188
189         // LOCK must be pushed after the queries
190         if (needPolicySteps) {
191             steps.push(new LockStep2(step));
192         }
193
194         // GET-TARGET-ENTITY should be pushed last
195         if (needTargetEntity) {
196             steps.push(new GetTargetEntityStep2(step));
197         }
198     }
199
200     /**
201      * Determines if the TOSCA should be aborted due to the given outcome.
202      *
203      * @param outcome outcome to examine
204      * @return {@code true} if the TOSCA should be aborted, {@code false} otherwise
205      */
206     public boolean isAbort(OperationOutcome outcome) {
207         return (super.isAbort(outcome) && ABORT_ACTORS.contains(outcome.getActor()));
208     }
209
210     /**
211      * Stores an operation outcome in the DB.
212      *
213      * @param outcome operation outcome to store
214      */
215     public void storeInDataBase(OperationOutcome2 outcome) {
216         storeInDataBase(outcome, getProperty(OperationProperties.AAI_TARGET_ENTITY));
217     }
218
219     /**
220      * Makes a control loop response.
221      *
222      * @param outcome operation outcome
223      * @return a new control loop response, or {@code null} if none is required
224      */
225     public ControlLoopResponse makeControlLoopResponse(OperationOutcome outcome) {
226         ControlLoopResponse clRsp = super.makeControlLoopResponse(outcome);
227
228         Object obj = outcome.getResponse();
229         if (!(obj instanceof PciMessage)) {
230             return clRsp;
231         }
232
233         PciMessage msg = (PciMessage) obj;
234         if (msg.getBody() != null && msg.getBody().getOutput() != null) {
235             clRsp.setPayload(msg.getBody().getOutput().getPayload());
236         }
237
238         return clRsp;
239     }
240
241     /**
242      * Check an event syntax.
243      *
244      * @param event the event syntax
245      * @throws ControlLoopException if an error occurs
246      */
247     protected void checkEventSyntax(VirtualControlLoopEvent event) throws ControlLoopException {
248         super.checkEventSyntax(event);
249         validateAaiData(event);
250     }
251
252     @Override
253     protected void validateTarget(VirtualControlLoopEvent event) throws ControlLoopException {
254         super.validateTarget(event);
255
256         if (!VALID_TARGETS.contains(event.getTarget().toLowerCase())) {
257             throw new ControlLoopException("target field invalid");
258         }
259     }
260
261     private void validateAaiData(VirtualControlLoopEvent event) throws ControlLoopException {
262         Map<String, String> eventAai = event.getAai();
263         if (eventAai == null) {
264             throw new ControlLoopException("AAI is null");
265         }
266         if (event.getTargetType() == null) {
267             throw new ControlLoopException("The Target type is null");
268         }
269         switch (event.getTargetType()) {
270             case VM:
271             case VNF:
272                 validateAaiVmVnfData(eventAai);
273                 return;
274             case PNF:
275                 validateAaiPnfData(eventAai);
276                 return;
277             default:
278                 throw new ControlLoopException("The target type is not supported");
279         }
280     }
281
282     private void validateAaiVmVnfData(Map<String, String> eventAai) throws ControlLoopException {
283         if (eventAai.get(GENERIC_VNF_VNF_ID) == null && eventAai.get(VSERVER_VSERVER_NAME) == null
284                         && eventAai.get(GENERIC_VNF_VNF_NAME) == null) {
285             throw new ControlLoopException(
286                             "generic-vnf.vnf-id or generic-vnf.vnf-name or vserver.vserver-name information missing");
287         }
288     }
289
290     private void validateAaiPnfData(Map<String, String> eventAai) throws ControlLoopException {
291         if (eventAai.get(PNF_NAME) == null) {
292             throw new ControlLoopException("AAI PNF object key pnf-name is missing");
293         }
294     }
295
296     /**
297      * Is closed loop disabled for an event.
298      *
299      * @param event the event
300      * @return <code>true</code> if the control loop is disabled, <code>false</code>
301      *         otherwise
302      */
303     private static boolean isClosedLoopDisabled(VirtualControlLoopEvent event) {
304         Map<String, String> aai = event.getAai();
305         return (isAaiTrue(aai.get(VSERVER_IS_CLOSED_LOOP_DISABLED))
306                         || isAaiTrue(aai.get(GENERIC_VNF_IS_CLOSED_LOOP_DISABLED))
307                         || isAaiTrue(aai.get(PNF_IS_IN_MAINT)));
308     }
309
310     /**
311      * Does provisioning status, for an event, have a value other than ACTIVE.
312      *
313      * @param event the event
314      * @return {@code true} if the provisioning status is neither ACTIVE nor {@code null},
315      *         {@code false} otherwise
316      */
317     private static boolean isProvStatusInactive(VirtualControlLoopEvent event) {
318         Map<String, String> aai = event.getAai();
319         return !(PROV_STATUS_ACTIVE.equalsIgnoreCase(aai.getOrDefault(VSERVER_PROV_STATUS, PROV_STATUS_ACTIVE))
320                         && PROV_STATUS_ACTIVE.equalsIgnoreCase(
321                                         aai.getOrDefault(GENERIC_VNF_PROV_STATUS, PROV_STATUS_ACTIVE)));
322     }
323
324     /**
325      * Determines the boolean value represented by the given AAI field value.
326      *
327      * @param aaiValue value to be examined
328      * @return the boolean value represented by the field value, or {@code false} if the
329      *         value is {@code null}
330      */
331     private static boolean isAaiTrue(String aaiValue) {
332         return (aaiValue != null && TRUE_VALUES.contains(aaiValue.toLowerCase()));
333     }
334
335     @Override
336     protected void loadPolicyStep(ControlLoopOperationParams params) {
337         getSteps().add(new Step2(this, params, getEvent()));
338     }
339 }