2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2017-2018 Intel Corp. All rights reserved.
4 * Modifications Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
20 package org.onap.policy.controlloop.actor.vfc;
22 import com.google.common.collect.ImmutableList;
23 import com.google.common.collect.ImmutableMap;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.UUID;
29 import org.onap.policy.aai.AaiGetVnfResponse;
30 import org.onap.policy.aai.AaiManager;
31 import org.onap.policy.controlloop.ControlLoopOperation;
32 import org.onap.policy.controlloop.VirtualControlLoopEvent;
33 import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
34 import org.onap.policy.controlloop.policy.Policy;
35 import org.onap.policy.drools.system.PolicyEngine;
36 import org.onap.policy.rest.RestManager;
37 import org.onap.policy.vfc.VfcHealActionVmInfo;
38 import org.onap.policy.vfc.VfcHealAdditionalParams;
39 import org.onap.policy.vfc.VfcHealRequest;
40 import org.onap.policy.vfc.VfcRequest;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 public class VfcActorServiceProvider implements Actor {
45 private static final Logger logger = LoggerFactory.getLogger(VfcActorServiceProvider.class);
47 // Strings for VFC Actor
48 private static final String VFC_ACTOR = "VFC";
50 // Strings for targets
51 private static final String TARGET_VM = "VM";
53 // Strings for recipes
54 private static final String RECIPE_RESTART = "Restart";
56 private static final ImmutableList<String> recipes = ImmutableList.of(RECIPE_RESTART);
57 private static final ImmutableMap<String, List<String>> targets =
58 new ImmutableMap.Builder<String, List<String>>().put(RECIPE_RESTART, ImmutableList.of(TARGET_VM)).build();
61 public String actor() {
66 public List<String> recipes() {
67 return ImmutableList.copyOf(recipes);
71 public List<String> recipeTargets(String recipe) {
72 return ImmutableList.copyOf(targets.getOrDefault(recipe, Collections.emptyList()));
76 public List<String> recipePayloads(String recipe) {
77 return Collections.emptyList();
81 * Construct a request.
83 * @param onset the onset event
84 * @param operation the control loop operation
85 * @param policy the policy
86 * @param vnfResponse the VNF response
87 * @return the constructed request
89 public static VfcRequest constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation,
90 Policy policy, AaiGetVnfResponse vnfResponse) {
92 // Construct an VFC request
93 VfcRequest request = new VfcRequest();
94 String serviceInstance = onset.getAai().get("service-instance.service-instance-id");
95 if (serviceInstance == null || "".equals(serviceInstance)) {
96 AaiGetVnfResponse tempVnfResp = vnfResponse;
97 if (tempVnfResp == null) { // if the response is null, we haven't queried
98 // This does the AAI query since we haven't already
99 tempVnfResp = getAaiServiceInstance(onset);
100 if (tempVnfResp == null) {
104 serviceInstance = tempVnfResp.getServiceId();
106 request.setNsInstanceId(serviceInstance);
107 request.setRequestId(onset.getRequestId());
108 request.setHealRequest(new VfcHealRequest());
109 request.getHealRequest().setVnfInstanceId(onset.getAai().get("generic-vnf.vnf-id"));
110 request.getHealRequest().setCause(operation.getMessage());
111 request.getHealRequest().setAdditionalParams(new VfcHealAdditionalParams());
113 if (policy.getRecipe().toLowerCase().equalsIgnoreCase(RECIPE_RESTART)) {
114 request.getHealRequest().getAdditionalParams().setAction("restartvm");
115 request.getHealRequest().getAdditionalParams().setActionInfo(new VfcHealActionVmInfo());
116 request.getHealRequest().getAdditionalParams().getActionInfo()
117 .setVmid(onset.getAai().get("vserver.vserver-id"));
118 request.getHealRequest().getAdditionalParams().getActionInfo()
119 .setVmname(onset.getAai().get("vserver.vserver-name"));
126 private static AaiGetVnfResponse getAaiServiceInstance(VirtualControlLoopEvent event) {
127 AaiGetVnfResponse response = null;
128 UUID requestId = event.getRequestId();
129 String vnfName = event.getAai().get("generic-vnf.vnf-name");
130 String vnfId = event.getAai().get("generic-vnf.vnf-id");
131 String aaiUrl = PolicyEngine.manager.getEnvironmentProperty("aai.url");
132 String aaiUsername = PolicyEngine.manager.getEnvironmentProperty("aai.username");
133 String aaiPassword = PolicyEngine.manager.getEnvironmentProperty("aai.password");
135 if (vnfName != null) {
136 String url = aaiUrl + "/aai/v11/network/generic-vnfs/generic-vnf?vnf-name=";
137 response = new AaiManager(new RestManager()).getQueryByVnfName(url, aaiUsername, aaiPassword, requestId,
139 } else if (vnfId != null) {
140 String url = aaiUrl + "/aai/v11/network/generic-vnfs/generic-vnf/";
141 response = new AaiManager(new RestManager()).getQueryByVnfId(url, aaiUsername, aaiPassword, requestId,
144 logger.error("getAAIServiceInstance failed");
146 } catch (Exception e) {
147 logger.error("getAAIServiceInstance exception: ", e);