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