2 * ============LICENSE_START=======================================================
3 * AppcLcmActorServiceProvider
4 * ================================================================================
5 * Copyright (C) 2017 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.controlloop.actor.appclcm;
23 import com.google.common.collect.ImmutableList;
24 import com.google.common.collect.ImmutableMap;
26 import java.util.AbstractMap;
27 import java.util.AbstractMap.SimpleEntry;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.List;
32 import java.util.UUID;
34 import org.onap.policy.aai.AAINQF199.AAINQF199InstanceFilters;
35 import org.onap.policy.aai.AAINQF199.AAINQF199InventoryResponseItem;
36 import org.onap.policy.aai.AAINQF199.AAINQF199Manager;
37 import org.onap.policy.aai.AAINQF199.AAINQF199NamedQuery;
38 import org.onap.policy.aai.AAINQF199.AAINQF199QueryParameters;
39 import org.onap.policy.aai.AAINQF199.AAINQF199Request;
40 import org.onap.policy.aai.AAINQF199.AAINQF199Response;
41 import org.onap.policy.appclcm.LCMCommonHeader;
42 import org.onap.policy.appclcm.LCMRequest;
43 import org.onap.policy.appclcm.LCMRequestWrapper;
44 import org.onap.policy.appclcm.LCMResponse;
45 import org.onap.policy.appclcm.LCMResponseCode;
46 import org.onap.policy.appclcm.LCMResponseWrapper;
47 import org.onap.policy.controlloop.ControlLoopOperation;
48 import org.onap.policy.controlloop.VirtualControlLoopEvent;
49 import org.onap.policy.controlloop.actorServiceProvider.spi.Actor;
50 import org.onap.policy.controlloop.policy.Policy;
51 import org.onap.policy.controlloop.policy.PolicyResult;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
55 public class AppcLcmActorServiceProvider implements Actor {
57 private static final Logger logger = LoggerFactory.getLogger(AppcLcmActorServiceProvider.class);
59 /* The source vnf-id provided from the DCAE onset */
60 private static final String DCAE_VNF_ID = "generic-vnf.vnf-id";
62 /* To be used in future releases to restart a single vm */
63 private static final String APPC_VM_ID = "vm-id";
65 /* To be used in future releases when LCM ConfigModify is used */
66 private static final String APPC_REQUEST_PARAMS = "request-parameters";
67 private static final String APPC_CONFIG_PARAMS = "configuration-parameters";
69 private static final ImmutableList<String> recipes = ImmutableList.of("Restart", "Rebuild", "Migrate",
71 private static final ImmutableMap<String, List<String>> targets = new ImmutableMap.Builder<String, List<String>>()
72 .put("Restart", ImmutableList.of("VM")).put("Rebuild", ImmutableList.of("VM"))
73 .put("Migrate", ImmutableList.of("VM")).put("ConfigModify", ImmutableList.of("VNF")).build();
74 private static final ImmutableMap<String, List<String>> payloads = new ImmutableMap.Builder<String, List<String>>()
75 .put("Restart", ImmutableList.of(APPC_VM_ID))
76 .put("ConfigModify", ImmutableList.of(APPC_REQUEST_PARAMS, APPC_CONFIG_PARAMS)).build();
79 public String actor() {
84 public List<String> recipes() {
85 return ImmutableList.copyOf(recipes);
89 public List<String> recipeTargets(String recipe) {
90 return ImmutableList.copyOf(targets.getOrDefault(recipe, Collections.emptyList()));
94 public List<String> recipePayloads(String recipe) {
95 return ImmutableList.copyOf(payloads.getOrDefault(recipe, Collections.emptyList()));
99 * This method recursively traverses the A&AI named query response
100 * to find the generic-vnf object that contains a model-invariant-id
101 * that matches the resourceId of the policy. Once this match is found
102 * the generic-vnf object's vnf-id is returned.
105 * the list of items related to the vnf returned by A&AI
107 * the id of the target from the sdc catalog
109 * @return the vnf-id of the target vnf to act upon or null if not found
111 private static String parseAAIResponse(List<AAINQF199InventoryResponseItem> items, String resourceId) {
113 for (AAINQF199InventoryResponseItem item: items) {
114 if ((item.genericVNF != null)
115 && (item.genericVNF.modelInvariantId != null)
116 && (resourceId.equals(item.genericVNF.modelInvariantId))) {
117 vnfId = item.genericVNF.vnfID;
121 if((item.items != null) && (item.items.inventoryResponseItems != null)) {
122 vnfId = parseAAIResponse(item.items.inventoryResponseItems, resourceId);
130 * Constructs an A&AI Named Query using a source vnf-id to determine
131 * the vnf-id of the target entity specified in the policy to act upon.
134 * the id of the target from the sdc catalog
137 * the vnf id of the source entity reporting the alert
139 * @return the target entities vnf id to act upon
141 public static String vnfNamedQuery(String resourceId, String sourceVnfId) {
143 //TODO: This request id should not be hard coded in future releases
144 UUID requestId = UUID.fromString("a93ac487-409c-4e8c-9e5f-334ae8f99087");
146 AAINQF199Request aaiRequest = new AAINQF199Request();
147 aaiRequest.queryParameters = new AAINQF199QueryParameters();
148 aaiRequest.queryParameters.namedQuery = new AAINQF199NamedQuery();
149 aaiRequest.queryParameters.namedQuery.namedQueryUUID = requestId;
151 Map<String, Map<String, String>> filter = new HashMap<>();
152 Map<String, String> filterItem = new HashMap<>();
154 filterItem.put("vnf-id", sourceVnfId);
155 filter.put("generic-vnf", filterItem);
157 aaiRequest.instanceFilters = new AAINQF199InstanceFilters();
158 aaiRequest.instanceFilters.instanceFilter.add(filter);
160 //TODO: URL should not be hard coded for future releases
161 AAINQF199Response aaiResponse = AAINQF199Manager.postQuery(
162 "http://localhost:6666",
164 aaiRequest, requestId);
166 //TODO: What if the resourceId never matches?
167 String targetVnfId = parseAAIResponse(aaiResponse.inventoryResponseItems, resourceId);
173 * Constructs an APPC request conforming to the lcm API.
174 * The actual request is constructed and then placed in a
175 * wrapper object used to send through DMAAP.
178 * the event that is reporting the alert for policy
179 * to perform an action
181 * the control loop operation specifying the actor,
182 * operation, target, etc.
184 * the policy the was specified from the yaml generated
185 * by CLAMP or through the Policy GUI/API
186 * @return an APPC request conforming to the lcm API using the DMAAP wrapper
188 public static LCMRequestWrapper constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation,
191 /* Construct an APPC request using LCM Model */
194 * The actual LCM request is placed in a wrapper used to send
195 * through dmaap. The current version is 2.0 as of R1.
197 LCMRequestWrapper dmaapRequest = new LCMRequestWrapper();
198 dmaapRequest.setVersion("2.0");
199 dmaapRequest.setCorrelationId(onset.requestID + "-" + operation.subRequestId);
200 dmaapRequest.setRpcName(policy.getRecipe().toLowerCase());
201 dmaapRequest.setType("request");
203 /* This is the actual request that is placed in the dmaap wrapper. */
204 LCMRequest appcRequest = new LCMRequest();
206 /* The common header is a required field for all APPC requests. */
207 LCMCommonHeader requestCommonHeader = new LCMCommonHeader();
208 requestCommonHeader.setOriginatorId(onset.requestID.toString());
209 requestCommonHeader.setRequestId(onset.requestID);
210 requestCommonHeader.setSubRequestId(operation.subRequestId);
212 appcRequest.setCommonHeader(requestCommonHeader);
215 * Action Identifiers are required for APPC LCM requests.
216 * For R1, the recipes supported by Policy only require
219 HashMap<String, String> requestActionIdentifiers = new HashMap<>();
220 requestActionIdentifiers.put("vnf-id", onset.AAI.get(DCAE_VNF_ID));
221 appcRequest.setActionIdentifiers(requestActionIdentifiers);
224 * An action is required for all APPC requests, this will
225 * be the recipe specified in the policy.
227 appcRequest.setAction(policy.getRecipe().toLowerCase());
230 * For R1, the payloads will not be required for the Restart,
231 * Rebuild, or Migrate recipes. APPC will populate the payload
232 * based on A&AI look up of the vnd-id provided in the action
235 if ("Restart".equalsIgnoreCase(policy.getRecipe()) || "Rebuild".equalsIgnoreCase(policy.getRecipe())
236 || "Migrate".equalsIgnoreCase(policy.getRecipe())) {
237 appcRequest.setPayload(null);
241 * Once the LCM request is constructed, add it into the
242 * body of the dmaap wrapper.
244 dmaapRequest.setBody(appcRequest);
246 /* Return the request to be sent through dmaap. */
251 * Parses the operation attempt using the subRequestId
254 * @param subRequestId
255 * the sub id used to send to APPC, Policy sets
256 * this using the operation attempt
258 * @return the current operation attempt
260 public static Integer parseOperationAttempt(String subRequestId) {
261 Integer operationAttempt;
263 operationAttempt = Integer.parseInt(subRequestId);
264 } catch (NumberFormatException e) {
265 logger.debug("A NumberFormatException was thrown due to error in parsing the operation attempt");
268 return operationAttempt;
272 * Processes the APPC LCM response sent from APPC. Determines
273 * if the APPC operation was successful/unsuccessful and maps
274 * this to the corresponding Policy result.
276 * @param dmaapResponse
277 * the dmaap wrapper message that contains the
278 * actual APPC reponse inside the body field
280 * @return an key-value pair that contains the Policy result
281 * and APPC response message
283 public static SimpleEntry<PolicyResult, String> processResponse(LCMResponseWrapper dmaapResponse) {
284 /* The actual APPC response is inside the wrapper's body field. */
285 LCMResponse appcResponse = dmaapResponse.getBody();
287 /* The message returned in the APPC response. */
290 /* The Policy result determined from the APPC Response. */
293 /* If there is no status, Policy cannot determine if the request was successful. */
294 if (appcResponse.getStatus() == null) {
295 message = "Policy was unable to parse APP-C response status field (it was null).";
296 return new AbstractMap.SimpleEntry<>(PolicyResult.FAILURE_EXCEPTION, message);
299 /* If there is no code, Policy cannot determine if the request was successful. */
300 String responseValue = LCMResponseCode.toResponseValue(appcResponse.getStatus().getCode());
301 if (responseValue == null) {
302 message = "Policy was unable to parse APP-C response status code field.";
303 return new AbstractMap.SimpleEntry<>(PolicyResult.FAILURE_EXCEPTION, message);
306 /* Save the APPC response's message for Policy noticiation message. */
307 message = appcResponse.getStatus().getMessage();
309 /* Maps the APPC response result to a Policy result. */
310 switch (responseValue) {
311 case LCMResponseCode.ACCEPTED:
312 /* Nothing to do if code is accept, continue processing */
315 case LCMResponseCode.SUCCESS:
316 result = PolicyResult.SUCCESS;
318 case LCMResponseCode.FAILURE:
319 result = PolicyResult.FAILURE;
321 case LCMResponseCode.REJECT:
322 case LCMResponseCode.ERROR:
324 result = PolicyResult.FAILURE_EXCEPTION;
326 return new AbstractMap.SimpleEntry<>(result, message);