2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.drools.apps.controller.usecases;
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;
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;
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.
47 public class GetTargetEntityOperation2 extends OperationPartial {
49 private final StepContext stepContext;
50 private final VirtualControlLoopEvent event;
54 * Constructs the operation as a preprocessing step for a policy's operation.
56 * @param stepContext the step's context
57 * @param event event being processed
58 * @param params operation's parameters
60 public GetTargetEntityOperation2(StepContext stepContext, VirtualControlLoopEvent event,
61 ControlLoopOperationParams params) {
62 super(params, null, Collections.emptyList());
64 this.stepContext = stepContext;
68 public List<String> getPropertyNames() {
69 String propName = detmTarget(params.getTargetType());
70 return (propName == null ? Collections.emptyList() : List.of(propName));
74 public CompletableFuture<OperationOutcome> start() {
75 throw new UnsupportedOperationException("cannot start get-target-entity operation");
79 * Determines the target entity.
81 * @param targetType policy target type
83 * @return the property containing the target entity, or {@code null} if the target
84 * entity is already known
86 private String detmTarget(TargetType targetType) {
87 if (stepContext.contains(OperationProperties.AAI_TARGET_ENTITY)) {
88 // the target entity has already been determined
92 if (targetType == null) {
93 throw new IllegalArgumentException("The target type is null");
98 return detmPnfTarget();
102 return detmVfModuleTarget();
104 throw new IllegalArgumentException("The target type is not supported");
109 * Determines the PNF target entity.
111 * @return the property containing the target entity, or {@code null} if the target
112 * entity is already known
114 private String detmPnfTarget() {
115 if (!PNF_NAME.equalsIgnoreCase(event.getTarget())) {
116 throw new IllegalArgumentException("Target does not match target type");
119 String targetEntity = event.getAai().get(PNF_NAME);
120 if (targetEntity == null) {
121 throw new IllegalArgumentException("AAI section is missing " + PNF_NAME);
124 setTargetEntity(targetEntity);
130 * Determines the VF Module target entity.
132 * @return the property containing the target entity, or {@code null} if the target
133 * entity is already known
135 private String detmVfModuleTarget() {
136 String targetFieldName = event.getTarget();
137 if (targetFieldName == null) {
138 throw new IllegalArgumentException("Target is null");
143 switch (targetFieldName.toLowerCase()) {
144 case VSERVER_VSERVER_NAME:
145 targetEntity = event.getAai().get(VSERVER_VSERVER_NAME);
147 case GENERIC_VNF_VNF_ID:
148 targetEntity = event.getAai().get(GENERIC_VNF_VNF_ID);
150 case GENERIC_VNF_VNF_NAME:
151 return detmVnfName();
153 throw new IllegalArgumentException("Target does not match target type");
156 if (targetEntity == null) {
157 throw new IllegalArgumentException("Enrichment data is missing " + targetFieldName);
160 setTargetEntity(targetEntity);
166 * Determines the VNF Name target entity.
168 * @return the property containing the target entity, or {@code null} if the target
169 * entity is already known
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;
179 setTargetEntity(targetEntity);
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());
194 * Sets the target entity within the properties.
196 * @param targetEntity the new target entity
198 private void setTargetEntity(String targetEntity) {
199 stepContext.setProperty(OperationProperties.AAI_TARGET_ENTITY, targetEntity);