0da1e2a2708d4a37b698192d5d25af53d99915b2
[policy/models.git] / models-interactions / model-actors / actor.appc / src / main / java / org / onap / policy / controlloop / actor / appc / AppcActorServiceProvider.java
1 /*-
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
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.controlloop.actor.appc;
23
24 import com.google.common.collect.ImmutableList;
25 import com.google.common.collect.ImmutableMap;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Map;
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.ActorImpl;
37 import org.onap.policy.controlloop.policy.Policy;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41
42 public class AppcActorServiceProvider extends ActorImpl {
43     private static final String NAME = "APPC";
44
45     private static final Logger logger = LoggerFactory.getLogger(AppcActorServiceProvider.class);
46
47     private static final StandardCoder coder = new StandardCoder();
48
49     // Strings for targets
50     private static final String TARGET_VM = "VM";
51     private static final String TARGET_VNF = "VNF";
52
53     // Strings for recipes
54     private static final String RECIPE_RESTART = "Restart";
55     private static final String RECIPE_REBUILD = "Rebuild";
56     private static final String RECIPE_MIGRATE = "Migrate";
57     private static final String RECIPE_MODIFY = "ModifyConfig";
58
59     private static final ImmutableList<String> recipes =
60             ImmutableList.of(RECIPE_RESTART, RECIPE_REBUILD, RECIPE_MIGRATE, RECIPE_MODIFY);
61     private static final ImmutableMap<String, List<String>> targets = new ImmutableMap.Builder<String, List<String>>()
62             .put(RECIPE_RESTART, ImmutableList.of(TARGET_VM)).put(RECIPE_REBUILD, ImmutableList.of(TARGET_VM))
63             .put(RECIPE_MIGRATE, ImmutableList.of(TARGET_VM)).put(RECIPE_MODIFY, ImmutableList.of(TARGET_VNF)).build();
64     private static final ImmutableMap<String, List<String>> payloads = new ImmutableMap.Builder<String, List<String>>()
65             .put(RECIPE_MODIFY, ImmutableList.of("generic-vnf.vnf-id")).build();
66
67     public AppcActorServiceProvider() {
68         super(NAME);
69     }
70
71     @Override
72     public String actor() {
73         return NAME;
74     }
75
76     @Override
77     public List<String> recipes() {
78         return ImmutableList.copyOf(recipes);
79     }
80
81     @Override
82     public List<String> recipeTargets(String recipe) {
83         return ImmutableList.copyOf(targets.getOrDefault(recipe, Collections.emptyList()));
84     }
85
86     @Override
87     public List<String> recipePayloads(String recipe) {
88         return ImmutableList.copyOf(payloads.getOrDefault(recipe, Collections.emptyList()));
89     }
90
91     /**
92      * Constructs an APPC request conforming to the legacy API. The legacy API will be deprecated in
93      * future releases as all legacy functionality is moved into the LCM API.
94      *
95      * @param onset the event that is reporting the alert for policy to perform an action
96      * @param operation the control loop operation specifying the actor, operation, target, etc.
97      * @param policy the policy the was specified from the yaml generated by CLAMP or through the
98      *        Policy GUI/API
99      * @return an APPC request conforming to the legacy API
100      */
101     public static Request constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation, Policy policy,
102             String targetVnf) {
103         /*
104          * Construct an APPC request
105          */
106         Request request = new Request();
107         request.setCommonHeader(new CommonHeader());
108         request.getCommonHeader().setRequestId(onset.getRequestId());
109         request.getCommonHeader().setSubRequestId(operation.getSubRequestId());
110         request.setAction(policy.getRecipe().substring(0, 1).toUpperCase() + policy.getRecipe().substring(1));
111
112         // convert policy payload strings to objects
113         if (policy.getPayload() == null) {
114             logger.info("no APPC payload specified for policy {}", policy.getName());
115         } else {
116             convertPayload(policy.getPayload(), request.getPayload());
117         }
118
119         // add/replace specific values
120         request.getPayload().put("generic-vnf.vnf-id", targetVnf);
121
122         /*
123          * Return the request
124          */
125
126         return request;
127     }
128
129     /**
130      * Converts a payload. The original value is assumed to be a JSON string, which is
131      * decoded into an object.
132      *
133      * @param source source from which to get the values
134      * @param target where to place the decoded values
135      */
136     private static void convertPayload(Map<String, String> source, Map<String, Object> target) {
137         for (Entry<String, String> ent : source.entrySet()) {
138             try {
139                 target.put(ent.getKey(), coder.decode(ent.getValue(), Object.class));
140
141             } catch (CoderException e) {
142                 logger.warn("cannot decode JSON value {}: {}", ent.getKey(), ent.getValue(), e);
143             }
144         }
145     }
146
147 }