f9d90af4da7f0116566e9d61f354faf08285fb3c
[policy/drools-applications.git] / controlloop / common / controller-usecases / src / main / java / org / onap / policy / drools / apps / controller / usecases / GetTargetEntityOperation2.java
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;
22
23 import static org.onap.policy.drools.apps.controller.usecases.UsecasesConstants.AAI_DEFAULT_GENERIC_VNF;
24 import static org.onap.policy.drools.apps.controller.usecases.UsecasesConstants.GENERIC_VNF_VNF_ID;
25 import static org.onap.policy.drools.apps.controller.usecases.UsecasesConstants.GENERIC_VNF_VNF_NAME;
26 import static org.onap.policy.drools.apps.controller.usecases.UsecasesConstants.PNF_NAME;
27 import static org.onap.policy.drools.apps.controller.usecases.UsecasesConstants.VSERVER_VSERVER_NAME;
28
29 import java.util.Collections;
30 import java.util.List;
31 import java.util.concurrent.CompletableFuture;
32 import org.onap.aai.domain.yang.GenericVnf;
33 import org.onap.policy.controlloop.VirtualControlLoopEvent;
34 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
35 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
36 import org.onap.policy.controlloop.actorserviceprovider.TargetType;
37 import org.onap.policy.controlloop.actorserviceprovider.impl.OperationPartial;
38 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
39 import org.onap.policy.controlloop.eventmanager.StepContext;
40
41 /**
42  * An operation to get the target entity. This is a "pseudo" operation; it is not found
43  * within an ActorService, but is created directly. It gets/sets the targetEntity found
44  * within the step context's properties.
45  */
46 public class GetTargetEntityOperation2 extends OperationPartial {
47
48     private final StepContext stepContext;
49     private final VirtualControlLoopEvent event;
50
51
52     /**
53      * Constructs the operation as a preprocessing step for a policy's operation.
54      *
55      * @param stepContext the step's context
56      * @param event event being processed
57      * @param params operation's parameters
58      */
59     public GetTargetEntityOperation2(StepContext stepContext, VirtualControlLoopEvent event,
60                     ControlLoopOperationParams params) {
61         super(params, null, Collections.emptyList());
62         this.event = event;
63         this.stepContext = stepContext;
64     }
65
66     @Override
67     public List<String> getPropertyNames() {
68         String propName = detmTarget(params.getTargetType());
69         return (propName == null ? Collections.emptyList() : List.of(propName));
70     }
71
72     @Override
73     public CompletableFuture<OperationOutcome> start() {
74         throw new UnsupportedOperationException("cannot start get-target-entity operation");
75     }
76
77     /**
78      * Determines the target entity.
79      *
80      * @param targetType policy target type
81      *
82      * @return the property containing the target entity, or {@code null} if the target
83      *         entity is already known
84      */
85     private String detmTarget(TargetType targetType) {
86         if (stepContext.contains(OperationProperties.AAI_TARGET_ENTITY)) {
87             // the target entity has already been determined
88             return null;
89         }
90
91         if (targetType == null) {
92             throw new IllegalArgumentException("The target type is null");
93         }
94
95         switch (targetType) {
96             case PNF:
97                 return detmPnfTarget();
98             case VM:
99             case VNF:
100             case VFMODULE:
101                 return detmVfModuleTarget();
102             default:
103                 throw new IllegalArgumentException("The target type is not supported");
104         }
105     }
106
107     /**
108      * Determines the PNF target entity.
109      *
110      * @return the property containing the target entity, or {@code null} if the target
111      *         entity is already known
112      */
113     private String detmPnfTarget() {
114         if (!PNF_NAME.equalsIgnoreCase(event.getTarget())) {
115             throw new IllegalArgumentException("Target does not match target type");
116         }
117
118         String targetEntity = event.getAai().get(PNF_NAME);
119         if (targetEntity == null) {
120             throw new IllegalArgumentException("AAI section is missing " + PNF_NAME);
121         }
122
123         setTargetEntity(targetEntity);
124
125         return null;
126     }
127
128     /**
129      * Determines the VF Module target entity.
130      *
131      * @return the property containing the target entity, or {@code null} if the target
132      *         entity is already known
133      */
134     private String detmVfModuleTarget() {
135         String targetFieldName = event.getTarget();
136         if (targetFieldName == null) {
137             throw new IllegalArgumentException("Target is null");
138         }
139
140         String targetEntity;
141
142         switch (targetFieldName.toLowerCase()) {
143             case VSERVER_VSERVER_NAME:
144                 targetEntity = event.getAai().get(VSERVER_VSERVER_NAME);
145                 break;
146             case GENERIC_VNF_VNF_ID:
147                 targetEntity = event.getAai().get(GENERIC_VNF_VNF_ID);
148                 break;
149             case GENERIC_VNF_VNF_NAME:
150                 return detmVnfName();
151             default:
152                 throw new IllegalArgumentException("Target does not match target type");
153         }
154
155         if (targetEntity == null) {
156             throw new IllegalArgumentException("Enrichment data is missing " + targetFieldName);
157         }
158
159         setTargetEntity(targetEntity);
160
161         return null;
162     }
163
164     /**
165      * Determines the VNF Name target entity.
166      *
167      * @return the property containing the target entity, or {@code null} if the target
168      *         entity is already known
169      */
170     private String detmVnfName() {
171         // if the onset is enriched with the vnf-id, we don't need an A&AI response
172         String targetEntity = event.getAai().get(GENERIC_VNF_VNF_ID);
173         if (targetEntity == null) {
174             // don't have the data yet - add a step to retrieve it
175             return AAI_DEFAULT_GENERIC_VNF;
176         }
177
178         setTargetEntity(targetEntity);
179
180         return null;
181     }
182
183     @Override
184     public void setProperty(String name, Object value) {
185         // only care about one property
186         if (UsecasesConstants.AAI_DEFAULT_GENERIC_VNF.equals(name)) {
187             GenericVnf vnf = (GenericVnf) value;
188             setTargetEntity(vnf.getVnfId());
189         }
190     }
191
192     /**
193      * Sets the target entity within the properties.
194      *
195      * @param targetEntity the new target entity
196      */
197     private void setTargetEntity(String targetEntity) {
198         stepContext.setProperty(OperationProperties.AAI_TARGET_ENTITY, targetEntity);
199     }
200 }