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 * Modifications Copyright (C) 2019 Nordix Foundation.
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.vfc;
23 import com.google.common.collect.ImmutableList;
24 import com.google.common.collect.ImmutableMap;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.UUID;
28 import org.onap.policy.aai.AaiCqResponse;
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.rest.RestManager;
36 import org.onap.policy.vfc.VfcHealActionVmInfo;
37 import org.onap.policy.vfc.VfcHealAdditionalParams;
38 import org.onap.policy.vfc.VfcHealRequest;
39 import org.onap.policy.vfc.VfcRequest;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 public class VfcActorServiceProvider implements Actor {
44 private static final Logger logger = LoggerFactory.getLogger(VfcActorServiceProvider.class);
46 // Strings for VFC Actor
47 private static final String VFC_ACTOR = "VFC";
49 // Strings for targets
50 private static final String TARGET_VM = "VM";
52 // Strings for recipes
53 private static final String RECIPE_RESTART = "Restart";
55 private static final ImmutableList<String> recipes = ImmutableList.of(RECIPE_RESTART);
56 private static final ImmutableMap<String, List<String>> targets =
57 new ImmutableMap.Builder<String, List<String>>().put(RECIPE_RESTART, ImmutableList.of(TARGET_VM)).build();
60 public String actor() {
65 public List<String> recipes() {
66 return ImmutableList.copyOf(recipes);
70 public List<String> recipeTargets(String recipe) {
71 return ImmutableList.copyOf(targets.getOrDefault(recipe, Collections.emptyList()));
75 public List<String> recipePayloads(String recipe) {
76 return Collections.emptyList();
80 * Construct a request.
82 * @param onset the onset event
83 * @param operation the control loop operation
84 * @param policy the policy
85 * @param vnfResponse the VNF response
86 * @return the constructed request
88 public static VfcRequest constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation,
89 Policy policy, AaiGetVnfResponse vnfResponse, String aaiUrl, String aaiUsername, String aaiPassword) {
91 // Construct an VFC request
92 VfcRequest request = new VfcRequest();
93 String serviceInstance = onset.getAai().get("service-instance.service-instance-id");
94 if (serviceInstance == null || "".equals(serviceInstance)) {
95 AaiGetVnfResponse tempVnfResp = vnfResponse;
96 if (tempVnfResp == null) { // if the response is null, we haven't queried
97 // This does the AAI query since we haven't already
98 tempVnfResp = getAaiServiceInstance(onset, aaiUrl, aaiUsername, aaiPassword);
99 if (tempVnfResp == null) {
103 serviceInstance = tempVnfResp.getServiceId();
105 request.setNsInstanceId(serviceInstance);
106 request.setRequestId(onset.getRequestId());
107 request.setHealRequest(new VfcHealRequest());
108 request.getHealRequest().setVnfInstanceId(onset.getAai().get("generic-vnf.vnf-id"));
109 request.getHealRequest().setCause(operation.getMessage());
110 request.getHealRequest().setAdditionalParams(new VfcHealAdditionalParams());
112 if (policy.getRecipe().toLowerCase().equalsIgnoreCase(RECIPE_RESTART)) {
113 request.getHealRequest().getAdditionalParams().setAction("restartvm");
114 request.getHealRequest().getAdditionalParams().setActionInfo(new VfcHealActionVmInfo());
115 request.getHealRequest().getAdditionalParams().getActionInfo()
116 .setVmid(onset.getAai().get("vserver.vserver-id"));
117 request.getHealRequest().getAdditionalParams().getActionInfo()
118 .setVmname(onset.getAai().get("vserver.vserver-name"));
125 private static AaiGetVnfResponse getAaiServiceInstance(VirtualControlLoopEvent event, String aaiUrl,
126 String aaiUsername, String aaiPassword) {
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");
132 if (vnfName != null) {
133 String url = aaiUrl + "/aai/v11/network/generic-vnfs/generic-vnf?vnf-name=";
134 response = new AaiManager(new RestManager()).getQueryByVnfName(url, aaiUsername, aaiPassword, requestId,
136 } else if (vnfId != null) {
137 String url = aaiUrl + "/aai/v11/network/generic-vnfs/generic-vnf/";
138 response = new AaiManager(new RestManager()).getQueryByVnfId(url, aaiUsername, aaiPassword, requestId,
141 logger.error("getAAIServiceInstance failed");
143 } catch (Exception e) {
144 logger.error("getAAIServiceInstance exception: ", e);
150 * This method constructs the VFC request.
152 * @param onset onset object
153 * @param operation operation object
154 * @param policy policy object
155 * @param aaiCqResponse response from aai custom query
158 public static VfcRequest constructRequestCq(VirtualControlLoopEvent onset, ControlLoopOperation operation,
159 Policy policy, AaiCqResponse aaiCqResponse) {
161 // Construct an VFC request
162 VfcRequest request = new VfcRequest();
163 String serviceInstance = onset.getAai().get("service-instance.service-instance-id");
164 if (serviceInstance == null || "".equals(serviceInstance)) {
165 // get service isntance from AaiCqResponse
166 if (aaiCqResponse == null) {
169 serviceInstance = aaiCqResponse.getServiceInstance().getServiceInstanceId();
170 // If the serviceInstanceId returned is null then return null
171 if (serviceInstance == null) {
176 request.setNsInstanceId(serviceInstance);
177 request.setRequestId(onset.getRequestId());
178 request.setHealRequest(new VfcHealRequest());
179 request.getHealRequest().setVnfInstanceId(onset.getAai().get("generic-vnf.vnf-id"));
180 request.getHealRequest().setCause(operation.getMessage());
181 request.getHealRequest().setAdditionalParams(new VfcHealAdditionalParams());
183 if (policy.getRecipe().toLowerCase().equalsIgnoreCase(RECIPE_RESTART)) {
184 request.getHealRequest().getAdditionalParams().setAction("restartvm");
185 request.getHealRequest().getAdditionalParams().setActionInfo(new VfcHealActionVmInfo());
186 request.getHealRequest().getAdditionalParams().getActionInfo()
187 .setVmid(onset.getAai().get("vserver.vserver-id"));
188 request.getHealRequest().getAdditionalParams().getActionInfo()
189 .setVmname(onset.getAai().get("vserver.vserver-name"));