7bf5a282692b835570e48794da1f07009f72bad6
[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, AAIGETVnfResponse vnfResponse) {
70         // Construct an VFC request
71         VFCRequest request = new VFCRequest();
72         String serviceInstance = onset.AAI.get("service-instance.service-instance-id");
73         if (serviceInstance == null || "".equals(serviceInstance))
74         {
75                 AAIGETVnfResponse tempVnfResp = vnfResponse;
76                 if(tempVnfResp == null) //if the response is null, we haven't queried
77                 {
78                         tempVnfResp = getAAIServiceInstance(onset); //This does the AAI query since we haven't already
79                         if (tempVnfResp == null)
80                             return null;
81                 }
82                 serviceInstance = tempVnfResp.serviceId;
83         }
84         request.nsInstanceId = serviceInstance;
85         request.requestId = onset.requestID;
86         request.healRequest = new VFCHealRequest();
87         request.healRequest.vnfInstanceId = onset.AAI.get("generic-vnf.vnf-id");
88         request.healRequest.cause = operation.message;
89         request.healRequest.additionalParams = new VFCHealAdditionalParams();
90         
91         switch (policy.getRecipe().toLowerCase()) {
92             case "restart":
93                 request.healRequest.additionalParams.action = "restartvm";
94                 request.healRequest.additionalParams.actionInfo = new VFCHealActionVmInfo();
95                 request.healRequest.additionalParams.actionInfo.vmid = onset.AAI.get("vserver.vserver-id");
96                 request.healRequest.additionalParams.actionInfo.vmname = onset.AAI.get("vserver.vserver-name");
97                 break;
98             default:
99                 return null;
100         }
101         return request;
102     }
103
104
105     private static AAIGETVnfResponse getAAIServiceInstance(VirtualControlLoopEvent event) {
106         AAIGETVnfResponse response = null;
107         UUID requestID = event.requestID;
108         String vnfName = event.AAI.get("generic-vnf.vnf-name");
109         String vnfID = event.AAI.get("generic-vnf.vnf-id");
110         String aaiUrl = PolicyEngine.manager.getEnvironmentProperty("aai.url");
111         String aaiUsername = PolicyEngine.manager.getEnvironmentProperty("aai.username");
112         String aaiPassword = PolicyEngine.manager.getEnvironmentProperty("aai.password");
113         try {
114             if (vnfName != null) {
115                 String url = aaiUrl + "/aai/v11/network/generic-vnfs/generic-vnf?vnf-name=";
116                 response = AAIManager.getQueryByVnfName(url, aaiUsername, aaiPassword, requestID, vnfName);
117             } else if (vnfID != null) {
118                 String url = aaiUrl + "/aai/v11/network/generic-vnfs/generic-vnf/";
119                 response = AAIManager.getQueryByVnfID(url, aaiUsername, aaiPassword, requestID, vnfID);
120             } else {
121                 logger.error("getAAIServiceInstance failed");
122             }
123         } catch (Exception e) {
124             logger.error("getAAIServiceInstance exception: ", e);
125         }
126         return response;
127     }
128 }