cf32abf38a46b9796c1e55ea5bfd7df941673c93
[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.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         private static final Logger logger = LoggerFactory.getLogger(VFCActorServiceProvider.class);
44
45         // Strings for VFC Actor
46         private static final String VFC_ACTOR  = "VFC";
47
48         // Strings for targets
49         private static final String TARGET_VM  = "VM";
50
51         // Strings for recipes
52         private static final String RECIPE_RESTART = "Restart";
53
54         private static final ImmutableList<String> recipes = ImmutableList.of(RECIPE_RESTART);
55         private static final ImmutableMap<String, List<String>> targets = new ImmutableMap.Builder<String, List<String>>()
56                         .put(RECIPE_RESTART, ImmutableList.of(TARGET_VM)).build();
57
58         @Override
59         public String actor() {
60                 return VFC_ACTOR;
61         }
62
63         @Override
64         public List<String> recipes() {
65                 return ImmutableList.copyOf(recipes);
66         }
67
68         @Override
69         public List<String> recipeTargets(String recipe) {
70                 return ImmutableList.copyOf(targets.getOrDefault(recipe, Collections.emptyList()));
71         }
72
73         @Override
74         public List<String> recipePayloads(String recipe) {
75                 return Collections.emptyList();
76         }
77
78         public static VFCRequest constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation,
79                         Policy policy, AAIGETVnfResponse vnfResponse) {
80                 
81                 // Construct an VFC request
82                 VFCRequest request = new VFCRequest();
83                 String serviceInstance = onset.getAAI().get("service-instance.service-instance-id");
84                 if (serviceInstance == null || "".equals(serviceInstance))
85                 {
86                         AAIGETVnfResponse tempVnfResp = vnfResponse;
87                         if(tempVnfResp == null) //if the response is null, we haven't queried
88                         {
89                                 tempVnfResp = getAAIServiceInstance(onset); //This does the AAI query since we haven't already
90                                 if (tempVnfResp == null)
91                                         return null;
92                         }
93                         serviceInstance = tempVnfResp.getServiceId();
94                 }
95                 request.setNSInstanceId(serviceInstance);
96                 request.setRequestId(onset.getRequestID());
97                 request.setHealRequest(new VFCHealRequest());
98                 request.getHealRequest().setVnfInstanceId(onset.getAAI().get("generic-vnf.vnf-id"));
99                 request.getHealRequest().setCause(operation.getMessage());
100                 request.getHealRequest().setAdditionalParams(new VFCHealAdditionalParams());
101
102                 if (policy.getRecipe().toLowerCase().equalsIgnoreCase(RECIPE_RESTART)) {
103                         request.getHealRequest().getAdditionalParams().setAction("restartvm");
104                         request.getHealRequest().getAdditionalParams().setActionInfo(new VFCHealActionVmInfo());
105                         request.getHealRequest().getAdditionalParams().getActionInfo().setVmid(onset.getAAI().get("vserver.vserver-id"));
106                         request.getHealRequest().getAdditionalParams().getActionInfo().setVmname(onset.getAAI().get("vserver.vserver-name"));
107                 }
108                 else {
109                         return null;
110                 }
111                 return request;
112         }
113
114         private static AAIGETVnfResponse getAAIServiceInstance(VirtualControlLoopEvent event) {
115                 AAIGETVnfResponse response = null;
116                 UUID requestID = event.getRequestID();
117                 String vnfName = event.getAAI().get("generic-vnf.vnf-name");
118                 String vnfID = event.getAAI().get("generic-vnf.vnf-id");
119                 String aaiUrl = PolicyEngine.manager.getEnvironmentProperty("aai.url");
120                 String aaiUsername = PolicyEngine.manager.getEnvironmentProperty("aai.username");
121                 String aaiPassword = PolicyEngine.manager.getEnvironmentProperty("aai.password");
122                 try {
123                         if (vnfName != null) {
124                                 String url = aaiUrl + "/aai/v11/network/generic-vnfs/generic-vnf?vnf-name=";
125                                 response = new AAIManager(new RESTManager()).getQueryByVnfName(url, aaiUsername, aaiPassword, requestID, vnfName);
126                         } else if (vnfID != null) {
127                                 String url = aaiUrl + "/aai/v11/network/generic-vnfs/generic-vnf/";
128                                 response = new AAIManager(new RESTManager()).getQueryByVnfID(url, aaiUsername, aaiPassword, requestID, vnfID);
129                         } else {
130                                 logger.error("getAAIServiceInstance failed");
131                         }
132                 } catch (Exception e) {
133                         logger.error("getAAIServiceInstance exception: ", e);
134                 }
135                 return response;
136         }
137 }