2  * ============LICENSE_START=======================================================
 
   3  * APPCActorServiceProvider
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved.
 
   6  * Modifications Copyright (C) 2019 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.controlloop.actor.appc;
 
  24 import com.google.common.collect.ImmutableList;
 
  25 import com.google.common.collect.ImmutableMap;
 
  26 import java.util.Collections;
 
  27 import java.util.List;
 
  29 import java.util.Map.Entry;
 
  30 import org.onap.policy.appc.CommonHeader;
 
  31 import org.onap.policy.appc.Request;
 
  32 import org.onap.policy.common.utils.coder.CoderException;
 
  33 import org.onap.policy.common.utils.coder.StandardCoder;
 
  34 import org.onap.policy.controlloop.ControlLoopOperation;
 
  35 import org.onap.policy.controlloop.VirtualControlLoopEvent;
 
  36 import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicActor;
 
  37 import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicOperator;
 
  38 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicActorParams;
 
  39 import org.onap.policy.controlloop.policy.Policy;
 
  40 import org.slf4j.Logger;
 
  41 import org.slf4j.LoggerFactory;
 
  44 public class AppcActorServiceProvider extends BidirectionalTopicActor<BidirectionalTopicActorParams> {
 
  45     public static final String NAME = "APPC";
 
  47     private static final Logger logger = LoggerFactory.getLogger(AppcActorServiceProvider.class);
 
  49     // TODO old code: remove lines down to **HERE**
 
  51     private static final StandardCoder coder = new StandardCoder();
 
  53     // Strings for targets
 
  54     private static final String TARGET_VM = "VM";
 
  55     private static final String TARGET_VNF = "VNF";
 
  57     // Strings for recipes
 
  58     private static final String RECIPE_RESTART = "Restart";
 
  59     private static final String RECIPE_REBUILD = "Rebuild";
 
  60     private static final String RECIPE_MIGRATE = "Migrate";
 
  61     private static final String RECIPE_MODIFY = "ModifyConfig";
 
  63     private static final ImmutableList<String> recipes =
 
  64                     ImmutableList.of(RECIPE_RESTART, RECIPE_REBUILD, RECIPE_MIGRATE, RECIPE_MODIFY);
 
  65     private static final ImmutableMap<String, List<String>> targets = new ImmutableMap.Builder<String, List<String>>()
 
  66                     .put(RECIPE_RESTART, ImmutableList.of(TARGET_VM)).put(RECIPE_REBUILD, ImmutableList.of(TARGET_VM))
 
  67                     .put(RECIPE_MIGRATE, ImmutableList.of(TARGET_VM)).put(RECIPE_MODIFY, ImmutableList.of(TARGET_VNF))
 
  69     private static final ImmutableMap<String, List<String>> payloads = new ImmutableMap.Builder<String, List<String>>()
 
  70                     .put(RECIPE_MODIFY, ImmutableList.of("generic-vnf.vnf-id")).build();
 
  75      * Constructs the object.
 
  77     public AppcActorServiceProvider() {
 
  78         super(NAME, BidirectionalTopicActorParams.class);
 
  80         addOperator(new BidirectionalTopicOperator(NAME, ModifyConfigOperation.NAME, this, AppcOperation.SELECTOR_KEYS,
 
  81                         ModifyConfigOperation::new));
 
  85     // TODO old code: remove lines down to **HERE**
 
  88     public String actor() {
 
  93     public List<String> recipes() {
 
  94         return ImmutableList.copyOf(recipes);
 
  98     public List<String> recipeTargets(String recipe) {
 
  99         return ImmutableList.copyOf(targets.getOrDefault(recipe, Collections.emptyList()));
 
 103     public List<String> recipePayloads(String recipe) {
 
 104         return ImmutableList.copyOf(payloads.getOrDefault(recipe, Collections.emptyList()));
 
 108      * Constructs an APPC request conforming to the legacy API. The legacy API will be
 
 109      * deprecated in future releases as all legacy functionality is moved into the LCM
 
 112      * @param onset the event that is reporting the alert for policy to perform an action
 
 113      * @param operation the control loop operation specifying the actor, operation,
 
 115      * @param policy the policy the was specified from the yaml generated by CLAMP or
 
 116      *        through the Policy GUI/API
 
 117      * @return an APPC request conforming to the legacy API
 
 119     public static Request constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation, Policy policy,
 
 122          * Construct an APPC request
 
 124         Request request = new Request();
 
 125         request.setCommonHeader(new CommonHeader());
 
 126         request.getCommonHeader().setRequestId(onset.getRequestId());
 
 127         request.getCommonHeader().setSubRequestId(operation.getSubRequestId());
 
 128         request.setAction(policy.getRecipe().substring(0, 1).toUpperCase() + policy.getRecipe().substring(1));
 
 130         // convert policy payload strings to objects
 
 131         if (policy.getPayload() == null) {
 
 132             logger.info("no APPC payload specified for policy {}", policy.getName());
 
 134             convertPayload(policy.getPayload(), request.getPayload());
 
 137         // add/replace specific values
 
 138         request.getPayload().put("generic-vnf.vnf-id", targetVnf);
 
 148      * Converts a payload. The original value is assumed to be a JSON string, which is
 
 149      * decoded into an object.
 
 151      * @param source source from which to get the values
 
 152      * @param target where to place the decoded values
 
 154     private static void convertPayload(Map<String, String> source, Map<String, Object> target) {
 
 155         for (Entry<String, String> ent : source.entrySet()) {
 
 157                 target.put(ent.getKey(), coder.decode(ent.getValue(), Object.class));
 
 159             } catch (CoderException e) {
 
 160                 logger.warn("cannot decode JSON value {}: {}", ent.getKey(), ent.getValue(), e);