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.HashMap;
23 import java.util.List;
26 import org.onap.policy.controlloop.VirtualControlLoopEvent;
27 import org.onap.policy.vfc.VFCRequest;
28 import org.onap.policy.vfc.VFCHealRequest;
29 import org.onap.policy.vfc.VFCHealAdditionalParams;
30 import org.onap.policy.vfc.VFCHealActionVmInfo;
31 import org.onap.policy.controlloop.ControlLoopOperation;
32 import org.onap.policy.controlloop.policy.Policy;
34 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;
40 import java.util.UUID;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 public class VFCActorServiceProvider implements Actor {
47 private static final Logger logger = LoggerFactory.getLogger(VFCActorServiceProvider.class);
48 private static final ImmutableList<String> recipes = ImmutableList.of("Restart");
49 private static final ImmutableMap<String, List<String>> targets = new ImmutableMap.Builder<String, List<String>>()
50 .put("Restart", ImmutableList.of("VM")).build();
53 public String actor() {
58 public List<String> recipes() {
59 return ImmutableList.copyOf(recipes);
63 public List<String> recipeTargets(String recipe) {
64 return ImmutableList.copyOf(targets.getOrDefault(recipe, Collections.emptyList()));
68 public List<String> recipePayloads(String recipe) {
69 return Collections.emptyList();
72 public static VFCRequest constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation,
75 // Construct an VFC request
76 VFCRequest request = new VFCRequest();
77 // TODO: Verify service-instance-id is part of onset event
78 request.nsInstanceId = getAAIServiceInstance(onset); // onset.AAI.get("service-instance.service-instance-id");
80 request.healRequest = new VFCHealRequest();
81 request.healRequest.vnfInstanceId = onset.AAI.get("generic-vnf.vnf-id");
82 request.healRequest.cause = operation.message;
84 request.healRequest.additionalParams = new VFCHealAdditionalParams();
85 switch (policy.getRecipe()) {
87 // TODO: check target??
88 request.healRequest.additionalParams.action = "restartvm";
89 request.healRequest.additionalParams.actionInfo = new VFCHealActionVmInfo();
90 // TODO: Verify vserver-id and vserver-name is part of onset event
91 request.healRequest.additionalParams.actionInfo.vmid = onset.AAI.get("vserver.vserver-id");
92 request.healRequest.additionalParams.actionInfo.vmname = onset.AAI.get("vserver.vserver-name");
101 private static String getAAIServiceInstance(VirtualControlLoopEvent event) {
102 AAIGETVnfResponse response = null;
103 UUID requestID = event.requestID;
104 String serviceInstance = event.AAI.get("service-instance.service-instance-id");
105 String vnfName = event.AAI.get("generic-vnf.vnf-name");
106 String vnfID = event.AAI.get("generic-vnf.vnf-id");
108 String urlBase = "http://localhost:6666";
109 String username = "testUser";
110 String password = "testPass";
111 if (serviceInstance == null) {
113 AAIManager manager = new AAIManager();
114 if (vnfName != null) {
115 String url = urlBase + "/aai/v11/network/generic-vnfs/generic-vnf?vnf-name=";
116 response = manager.getQueryByVnfName(url, username, password, requestID, vnfName);
117 serviceInstance = response.serviceId;
118 } else if (vnfID != null) {
119 String url = urlBase + "/aai/v11/network/generic-vnfs/generic-vnf/";
120 response = manager.getQueryByVnfID(url, username, password, requestID, vnfID);
121 serviceInstance = response.serviceId;
123 logger.error("getAAIServiceInstance failed");
126 } catch (Exception e) {
127 logger.error("getAAIServiceInstance exception: ", e);
130 return serviceInstance;