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