e1259e48d51b37f9fb3efdbea715b76a2578d5d2
[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;
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.impl.OperationPartial;
37 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
38 import org.onap.policy.controlloop.eventmanager.StepContext;
39 import org.onap.policy.controlloop.policy.Target;
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.getTarget());
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 target policy target
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(Target target) {
86         if (stepContext.contains(OperationProperties.AAI_TARGET_ENTITY)) {
87             // the target entity has already been determined
88             return null;
89         }
90
91         if (target == null) {
92             throw new IllegalArgumentException("The target is null");
93         }
94
95         if (target.getType() == null) {
96             throw new IllegalArgumentException("The target type is null");
97         }
98
99         switch (target.getType()) {
100             case PNF:
101                 return detmPnfTarget();
102             case VM:
103             case VNF:
104             case VFMODULE:
105                 return detmVfModuleTarget();
106             default:
107                 throw new IllegalArgumentException("The target type is not supported");
108         }
109     }
110
111     /**
112      * Determines the PNF target entity.
113      *
114      * @return the property containing the target entity, or {@code null} if the target
115      *         entity is already known
116      */
117     private String detmPnfTarget() {
118         if (!PNF_NAME.equalsIgnoreCase(event.getTarget())) {
119             throw new IllegalArgumentException("Target does not match target type");
120         }
121
122         String targetEntity = event.getAai().get(PNF_NAME);
123         if (targetEntity == null) {
124             throw new IllegalArgumentException("AAI section is missing " + PNF_NAME);
125         }
126
127         setTargetEntity(targetEntity);
128
129         return null;
130     }
131
132     /**
133      * Determines the VF Module target entity.
134      *
135      * @return the property containing the target entity, or {@code null} if the target
136      *         entity is already known
137      */
138     private String detmVfModuleTarget() {
139         String targetFieldName = event.getTarget();
140         if (targetFieldName == null) {
141             throw new IllegalArgumentException("Target is null");
142         }
143
144         String targetEntity;
145
146         switch (targetFieldName.toLowerCase()) {
147             case VSERVER_VSERVER_NAME:
148                 targetEntity = event.getAai().get(VSERVER_VSERVER_NAME);
149                 break;
150             case GENERIC_VNF_VNF_ID:
151                 targetEntity = event.getAai().get(GENERIC_VNF_VNF_ID);
152                 break;
153             case GENERIC_VNF_VNF_NAME:
154                 return detmVnfName();
155             default:
156                 throw new IllegalArgumentException("Target does not match target type");
157         }
158
159         if (targetEntity == null) {
160             throw new IllegalArgumentException("Enrichment data is missing " + targetFieldName);
161         }
162
163         setTargetEntity(targetEntity);
164
165         return null;
166     }
167
168     /**
169      * Determines the VNF Name target entity.
170      *
171      * @return the property containing the target entity, or {@code null} if the target
172      *         entity is already known
173      */
174     private String detmVnfName() {
175         // if the onset is enriched with the vnf-id, we don't need an A&AI response
176         String targetEntity = event.getAai().get(GENERIC_VNF_VNF_ID);
177         if (targetEntity == null) {
178             // don't have the data yet - add a step to retrieve it
179             return AAI_DEFAULT_GENERIC_VNF;
180         }
181
182         setTargetEntity(targetEntity);
183
184         return null;
185     }
186
187     @Override
188     public void setProperty(String name, Object value) {
189         // only care about one property
190         if (UsecasesConstants.AAI_DEFAULT_GENERIC_VNF.equals(name)) {
191             GenericVnf vnf = (GenericVnf) value;
192             setTargetEntity(vnf.getVnfId());
193         }
194     }
195
196     /**
197      * Sets the target entity within the properties.
198      *
199      * @param targetEntity the new target entity
200      */
201     private void setTargetEntity(String targetEntity) {
202         stepContext.setProperty(OperationProperties.AAI_TARGET_ENTITY, targetEntity);
203     }
204 }