469faa46ce8f24d46030bae01766f6e811819e34
[policy/drools-applications.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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=========================================================
17  */
18
19 package org.onap.policy.controlloop.actor.vfc;
20
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.controlloop.actorServiceProvider.spi.Actor;
33
34 import com.google.common.collect.ImmutableList;
35 import com.google.common.collect.ImmutableMap;
36 import org.onap.policy.aai.AAIManager;
37 import org.onap.policy.aai.AAIGETVnfResponse;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 public class VFCActorServiceProvider implements Actor {
42
43     private static final Logger logger = LoggerFactory.getLogger(VFCActorServiceProvider.class);
44     private static final ImmutableList<String> recipes = ImmutableList.of("Restart");
45     private static final ImmutableMap<String, List<String>> targets = new ImmutableMap.Builder<String, List<String>>()
46             .put("Restart", ImmutableList.of("VM")).build();
47
48     @Override
49     public String actor() {
50         return "VFC";
51     }
52
53     @Override
54     public List<String> recipes() {
55         return ImmutableList.copyOf(recipes);
56     }
57
58     @Override
59     public List<String> recipeTargets(String recipe) {
60         return ImmutableList.copyOf(targets.getOrDefault(recipe, Collections.emptyList()));
61     }
62
63     @Override
64     public List<String> recipePayloads(String recipe) {
65         return Collections.emptyList();
66     }
67
68     public static VFCRequest constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation,
69                                               Policy policy) {
70         // Construct an VFC request
71         VFCRequest request = new VFCRequest();
72         request.nsInstanceId = getAAIServiceInstance(onset);
73             request.requestId = onset.requestID;
74         request.healRequest = new VFCHealRequest();
75         request.healRequest.vnfInstanceId = onset.AAI.get("generic-vnf.vnf-id");
76         request.healRequest.cause = operation.message;
77
78         request.healRequest.additionalParams = new VFCHealAdditionalParams();
79         if ("Restart".equalsIgnoreCase(policy.getRecipe())) {
80             request.healRequest.additionalParams.action = "restartvm";
81             request.healRequest.additionalParams.actionInfo = new VFCHealActionVmInfo();
82             request.healRequest.additionalParams.actionInfo.vmid = onset.AAI.get("vserver.vserver-id");
83             request.healRequest.additionalParams.actionInfo.vmname = onset.AAI.get("vserver.vserver-name");
84         } else {
85             request = null;
86         }
87         return request;
88     }
89
90     private static String getAAIServiceInstance(VirtualControlLoopEvent event) {
91         AAIGETVnfResponse response;
92         UUID requestID = event.requestID;
93         String serviceInstance = event.AAI.get("service-instance.service-instance-id");
94         String vnfName = event.AAI.get("generic-vnf.vnf-name");
95         String vnfID = event.AAI.get("generic-vnf.vnf-id");
96
97         if (serviceInstance == null) {
98             String aaiUrl = PolicyEngine.manager.getEnvironmentProperty("aai.url");
99             String aaiUsername = PolicyEngine.manager.getEnvironmentProperty("aai.username");
100             String aaiPassword = PolicyEngine.manager.getEnvironmentProperty("aai.password");
101             try {
102                 if (vnfName != null) {
103                     String url = aaiUrl + "/aai/v11/network/generic-vnfs/generic-vnf?vnf-name=";
104                     response = AAIManager.getQueryByVnfName(url, aaiUsername, aaiPassword, requestID, vnfName);
105                     if (response != null) {
106                         serviceInstance = response.serviceId;
107                     }
108                 } else if (vnfID != null) {
109                     String url = aaiUrl + "/aai/v11/network/generic-vnfs/generic-vnf/";
110                     response = AAIManager.getQueryByVnfID(url, aaiUsername, aaiPassword, requestID, vnfID);
111                     if (response != null) {
112                         serviceInstance = response.serviceId;
113                     }
114                 } else {
115                     logger.error("getAAIServiceInstance failed");
116
117                 }
118             } catch (Exception e) {
119                 logger.error("getAAIServiceInstance exception: ", e);
120             }
121         }
122         return serviceInstance;
123     }
124 }