2 * ============LICENSE_START=======================================================
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
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");
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");
104 * Determines the PNF target entity.
106 * @return the property containing the target entity, or {@code null} if the target
107 * entity is already known
109 private String detmPnfTarget() {
110 if (!PNF_NAME.equalsIgnoreCase(event.getTarget())) {
111 throw new IllegalArgumentException("Target does not match target type");
114 String targetEntity = event.getAai().get(PNF_NAME);
115 if (targetEntity == null) {
116 throw new IllegalArgumentException("AAI section is missing " + PNF_NAME);
119 setTargetEntity(targetEntity);
125 * Determines the VF Module target entity.
127 * @return the property containing the target entity, or {@code null} if the target
128 * entity is already known
130 private String detmVfModuleTarget() {
131 String targetFieldName = event.getTarget();
132 if (targetFieldName == null) {
133 throw new IllegalArgumentException("Target is null");
138 switch (targetFieldName.toLowerCase()) {
139 case VSERVER_VSERVER_NAME:
140 targetEntity = event.getAai().get(VSERVER_VSERVER_NAME);
142 case GENERIC_VNF_VNF_ID:
143 targetEntity = event.getAai().get(GENERIC_VNF_VNF_ID);
145 case GENERIC_VNF_VNF_NAME:
146 return detmVnfName();
148 throw new IllegalArgumentException("Target does not match target type");
151 if (targetEntity == null) {
152 throw new IllegalArgumentException("Enrichment data is missing " + targetFieldName);
155 setTargetEntity(targetEntity);
161 * Determines the VNF Name target entity.
163 * @return the property containing the target entity, or {@code null} if the target
164 * entity is already known
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;
174 setTargetEntity(targetEntity);
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());
189 * Sets the target entity within the properties.
191 * @param targetEntity the new target entity
193 private void setTargetEntity(String targetEntity) {
194 stepContext.setProperty(OperationProperties.AAI_TARGET_ENTITY, targetEntity);