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