2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2017 Intel Corp. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END=========================================================
19 package org.onap.policy.controlloop.actor.vfc;
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.UUID;
24 import org.onap.policy.controlloop.VirtualControlLoopEvent;
25 import org.onap.policy.vfc.VFCRequest;
26 import org.onap.policy.vfc.VFCHealRequest;
27 import org.onap.policy.vfc.VFCHealAdditionalParams;
28 import org.onap.policy.vfc.VFCHealActionVmInfo;
29 import org.onap.policy.controlloop.ControlLoopOperation;
30 import org.onap.policy.controlloop.policy.Policy;
31 import org.onap.policy.drools.system.PolicyEngine;
32 import org.onap.policy.rest.RESTManager;
33 import org.onap.policy.controlloop.actorServiceProvider.spi.Actor;
35 import com.google.common.collect.ImmutableList;
36 import com.google.common.collect.ImmutableMap;
37 import org.onap.policy.aai.AAIManager;
38 import org.onap.policy.aai.AAIGETVnfResponse;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 public class VFCActorServiceProvider implements Actor {
44 private static final Logger logger = LoggerFactory.getLogger(VFCActorServiceProvider.class);
45 private static final ImmutableList<String> recipes = ImmutableList.of("Restart");
46 private static final ImmutableMap<String, List<String>> targets = new ImmutableMap.Builder<String, List<String>>()
47 .put("Restart", ImmutableList.of("VM")).build();
50 public String actor() {
55 public List<String> recipes() {
56 return ImmutableList.copyOf(recipes);
60 public List<String> recipeTargets(String recipe) {
61 return ImmutableList.copyOf(targets.getOrDefault(recipe, Collections.emptyList()));
65 public List<String> recipePayloads(String recipe) {
66 return Collections.emptyList();
69 public static VFCRequest constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation,
70 Policy policy, AAIGETVnfResponse vnfResponse) {
71 // Construct an VFC request
72 VFCRequest request = new VFCRequest();
73 String serviceInstance = onset.getAAI().get("service-instance.service-instance-id");
74 if (serviceInstance == null || "".equals(serviceInstance))
76 AAIGETVnfResponse tempVnfResp = vnfResponse;
77 if(tempVnfResp == null) //if the response is null, we haven't queried
79 tempVnfResp = getAAIServiceInstance(onset); //This does the AAI query since we haven't already
80 if (tempVnfResp == null)
83 serviceInstance = tempVnfResp.getServiceId();
85 request.nsInstanceId = serviceInstance;
86 request.requestId = onset.getRequestID();
87 request.healRequest = new VFCHealRequest();
88 request.healRequest.vnfInstanceId = onset.getAAI().get("generic-vnf.vnf-id");
89 request.healRequest.cause = operation.getMessage();
90 request.healRequest.additionalParams = new VFCHealAdditionalParams();
92 switch (policy.getRecipe().toLowerCase()) {
94 request.healRequest.additionalParams.action = "restartvm";
95 request.healRequest.additionalParams.actionInfo = new VFCHealActionVmInfo();
96 request.healRequest.additionalParams.actionInfo.vmid = onset.getAAI().get("vserver.vserver-id");
97 request.healRequest.additionalParams.actionInfo.vmname = onset.getAAI().get("vserver.vserver-name");
106 private static AAIGETVnfResponse getAAIServiceInstance(VirtualControlLoopEvent event) {
107 AAIGETVnfResponse response = null;
108 UUID requestID = event.getRequestID();
109 String vnfName = event.getAAI().get("generic-vnf.vnf-name");
110 String vnfID = event.getAAI().get("generic-vnf.vnf-id");
111 String aaiUrl = PolicyEngine.manager.getEnvironmentProperty("aai.url");
112 String aaiUsername = PolicyEngine.manager.getEnvironmentProperty("aai.username");
113 String aaiPassword = PolicyEngine.manager.getEnvironmentProperty("aai.password");
115 if (vnfName != null) {
116 String url = aaiUrl + "/aai/v11/network/generic-vnfs/generic-vnf?vnf-name=";
117 response = new AAIManager(new RESTManager()).getQueryByVnfName(url, aaiUsername, aaiPassword, requestID, vnfName);
118 } else if (vnfID != null) {
119 String url = aaiUrl + "/aai/v11/network/generic-vnfs/generic-vnf/";
120 response = new AAIManager(new RESTManager()).getQueryByVnfID(url, aaiUsername, aaiPassword, requestID, vnfID);
122 logger.error("getAAIServiceInstance failed");
124 } catch (Exception e) {
125 logger.error("getAAIServiceInstance exception: ", e);