2  * ============LICENSE_START=======================================================
 
   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
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  21 package org.onap.policy.drools.apps.controller.usecases;
 
  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;
 
  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.TargetType;
 
  37 import org.onap.policy.controlloop.actorserviceprovider.impl.OperationPartial;
 
  38 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
 
  39 import org.onap.policy.controlloop.eventmanager.StepContext;
 
  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.
 
  46 public class GetTargetEntityOperation2 extends OperationPartial {
 
  48     private final StepContext stepContext;
 
  49     private final VirtualControlLoopEvent event;
 
  53      * Constructs the operation as a preprocessing step for a policy's operation.
 
  55      * @param stepContext the step's context
 
  56      * @param event event being processed
 
  57      * @param params operation's parameters
 
  59     public GetTargetEntityOperation2(StepContext stepContext, VirtualControlLoopEvent event,
 
  60                     ControlLoopOperationParams params) {
 
  61         super(params, null, Collections.emptyList());
 
  63         this.stepContext = stepContext;
 
  67     public List<String> getPropertyNames() {
 
  68         String propName = detmTarget(params.getTargetType());
 
  69         return (propName == null ? Collections.emptyList() : List.of(propName));
 
  73     public CompletableFuture<OperationOutcome> start() {
 
  74         throw new UnsupportedOperationException("cannot start get-target-entity operation");
 
  78      * Determines the target entity.
 
  80      * @param targetType policy target type
 
  82      * @return the property containing the target entity, or {@code null} if the target
 
  83      *         entity is already known
 
  85     private String detmTarget(TargetType targetType) {
 
  86         if (stepContext.contains(OperationProperties.AAI_TARGET_ENTITY)) {
 
  87             // the target entity has already been determined
 
  91         if (targetType == null) {
 
  92             throw new IllegalArgumentException("The target type is null");
 
  97                 return detmPnfTarget();
 
 101                 return detmVfModuleTarget();
 
 103                 throw new IllegalArgumentException("The target type is not supported");
 
 108      * Determines the PNF target entity.
 
 110      * @return the property containing the target entity, or {@code null} if the target
 
 111      *         entity is already known
 
 113     private String detmPnfTarget() {
 
 114         if (!PNF_NAME.equalsIgnoreCase(event.getTarget())) {
 
 115             throw new IllegalArgumentException("Target does not match target type");
 
 118         String targetEntity = event.getAai().get(PNF_NAME);
 
 119         if (targetEntity == null) {
 
 120             throw new IllegalArgumentException("AAI section is missing " + PNF_NAME);
 
 123         setTargetEntity(targetEntity);
 
 129      * Determines the VF Module target entity.
 
 131      * @return the property containing the target entity, or {@code null} if the target
 
 132      *         entity is already known
 
 134     private String detmVfModuleTarget() {
 
 135         String targetFieldName = event.getTarget();
 
 136         if (targetFieldName == null) {
 
 137             throw new IllegalArgumentException("Target is null");
 
 142         switch (targetFieldName.toLowerCase()) {
 
 143             case VSERVER_VSERVER_NAME:
 
 144                 targetEntity = event.getAai().get(VSERVER_VSERVER_NAME);
 
 146             case GENERIC_VNF_VNF_ID:
 
 147                 targetEntity = event.getAai().get(GENERIC_VNF_VNF_ID);
 
 149             case GENERIC_VNF_VNF_NAME:
 
 150                 return detmVnfName();
 
 152                 throw new IllegalArgumentException("Target does not match target type");
 
 155         if (targetEntity == null) {
 
 156             throw new IllegalArgumentException("Enrichment data is missing " + targetFieldName);
 
 159         setTargetEntity(targetEntity);
 
 165      * Determines the VNF Name target entity.
 
 167      * @return the property containing the target entity, or {@code null} if the target
 
 168      *         entity is already known
 
 170     private String detmVnfName() {
 
 171         // if the onset is enriched with the vnf-id, we don't need an A&AI response
 
 172         String targetEntity = event.getAai().get(GENERIC_VNF_VNF_ID);
 
 173         if (targetEntity == null) {
 
 174             // don't have the data yet - add a step to retrieve it
 
 175             return AAI_DEFAULT_GENERIC_VNF;
 
 178         setTargetEntity(targetEntity);
 
 184     public void setProperty(String name, Object value) {
 
 185         // only care about one property
 
 186         if (UsecasesConstants.AAI_DEFAULT_GENERIC_VNF.equals(name)) {
 
 187             GenericVnf vnf = (GenericVnf) value;
 
 188             setTargetEntity(vnf.getVnfId());
 
 193      * Sets the target entity within the properties.
 
 195      * @param targetEntity the new target entity
 
 197     private void setTargetEntity(String targetEntity) {
 
 198         stepContext.setProperty(OperationProperties.AAI_TARGET_ENTITY, targetEntity);