Fix Tech Debt/JUnit on control loop event POJOs
[policy/drools-applications.git] / controlloop / common / actors / actor.vfc / src / main / java / org / onap / policy / controlloop / actor / vfc / VFCActorServiceProvider.java
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.rest.RESTManager;
33 import org.onap.policy.controlloop.actorServiceProvider.spi.Actor;
34
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;
41
42 public class VFCActorServiceProvider implements Actor {
43
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();
48
49     @Override
50     public String actor() {
51         return "VFC";
52     }
53
54     @Override
55     public List<String> recipes() {
56         return ImmutableList.copyOf(recipes);
57     }
58
59     @Override
60     public List<String> recipeTargets(String recipe) {
61         return ImmutableList.copyOf(targets.getOrDefault(recipe, Collections.emptyList()));
62     }
63
64     @Override
65     public List<String> recipePayloads(String recipe) {
66         return Collections.emptyList();
67     }
68
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))
75         {
76                 AAIGETVnfResponse tempVnfResp = vnfResponse;
77                 if(tempVnfResp == null) //if the response is null, we haven't queried
78                 {
79                         tempVnfResp = getAAIServiceInstance(onset); //This does the AAI query since we haven't already
80                         if (tempVnfResp == null)
81                             return null;
82                 }
83                 serviceInstance = tempVnfResp.getServiceId();
84         }
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();
91         
92         switch (policy.getRecipe().toLowerCase()) {
93             case "restart":
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");
98                 break;
99             default:
100                 return null;
101         }
102         return request;
103     }
104
105
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");
114         try {
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);
121             } else {
122                 logger.error("getAAIServiceInstance failed");
123             }
124         } catch (Exception e) {
125             logger.error("getAAIServiceInstance exception: ", e);
126         }
127         return response;
128     }
129 }