b8aaf854a29d77eb2450d10f10ba56d5232d24a7
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2017-2018 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 com.google.common.collect.ImmutableList;
22 import com.google.common.collect.ImmutableMap;
23
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.UUID;
27
28 import org.onap.policy.aai.AaiGetVnfResponse;
29 import org.onap.policy.aai.AaiManager;
30 import org.onap.policy.controlloop.ControlLoopOperation;
31 import org.onap.policy.controlloop.VirtualControlLoopEvent;
32 import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
33 import org.onap.policy.controlloop.policy.Policy;
34 import org.onap.policy.drools.system.PolicyEngine;
35 import org.onap.policy.rest.RESTManager;
36 import org.onap.policy.vfc.VFCHealActionVmInfo;
37 import org.onap.policy.vfc.VFCHealAdditionalParams;
38 import org.onap.policy.vfc.VFCHealRequest;
39 import org.onap.policy.vfc.VFCRequest;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 public class VFCActorServiceProvider implements Actor {
44     private static final Logger logger = LoggerFactory.getLogger(VFCActorServiceProvider.class);
45
46     // Strings for VFC Actor
47     private static final String VFC_ACTOR = "VFC";
48
49     // Strings for targets
50     private static final String TARGET_VM = "VM";
51
52     // Strings for recipes
53     private static final String RECIPE_RESTART = "Restart";
54
55     private static final ImmutableList<String> recipes = ImmutableList.of(RECIPE_RESTART);
56     private static final ImmutableMap<String, List<String>> targets =
57             new ImmutableMap.Builder<String, List<String>>().put(RECIPE_RESTART, ImmutableList.of(TARGET_VM)).build();
58
59     @Override
60     public String actor() {
61         return VFC_ACTOR;
62     }
63
64     @Override
65     public List<String> recipes() {
66         return ImmutableList.copyOf(recipes);
67     }
68
69     @Override
70     public List<String> recipeTargets(String recipe) {
71         return ImmutableList.copyOf(targets.getOrDefault(recipe, Collections.emptyList()));
72     }
73
74     @Override
75     public List<String> recipePayloads(String recipe) {
76         return Collections.emptyList();
77     }
78
79     /**
80      * Construct a request.
81      * 
82      * @param onset the onset event
83      * @param operation the control loop operation
84      * @param policy the policy
85      * @param vnfResponse the VNF response
86      * @return the constructed request
87      */
88     public static VFCRequest constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation,
89             Policy policy, AaiGetVnfResponse vnfResponse) {
90
91         // Construct an VFC request
92         VFCRequest request = new VFCRequest();
93         String serviceInstance = onset.getAai().get("service-instance.service-instance-id");
94         if (serviceInstance == null || "".equals(serviceInstance)) {
95             AaiGetVnfResponse tempVnfResp = vnfResponse;
96             if (tempVnfResp == null) { // if the response is null, we haven't queried
97                 // This does the AAI query since we haven't already
98                 tempVnfResp = getAaiServiceInstance(onset);
99                 if (tempVnfResp == null) {
100                     return null;
101                 }
102             }
103             serviceInstance = tempVnfResp.getServiceId();
104         }
105         request.setNSInstanceId(serviceInstance);
106         request.setRequestId(onset.getRequestId());
107         request.setHealRequest(new VFCHealRequest());
108         request.getHealRequest().setVnfInstanceId(onset.getAai().get("generic-vnf.vnf-id"));
109         request.getHealRequest().setCause(operation.getMessage());
110         request.getHealRequest().setAdditionalParams(new VFCHealAdditionalParams());
111
112         if (policy.getRecipe().toLowerCase().equalsIgnoreCase(RECIPE_RESTART)) {
113             request.getHealRequest().getAdditionalParams().setAction("restartvm");
114             request.getHealRequest().getAdditionalParams().setActionInfo(new VFCHealActionVmInfo());
115             request.getHealRequest().getAdditionalParams().getActionInfo()
116                     .setVmid(onset.getAai().get("vserver.vserver-id"));
117             request.getHealRequest().getAdditionalParams().getActionInfo()
118                     .setVmname(onset.getAai().get("vserver.vserver-name"));
119         } else {
120             return null;
121         }
122         return request;
123     }
124
125     private static AaiGetVnfResponse getAaiServiceInstance(VirtualControlLoopEvent event) {
126         AaiGetVnfResponse response = null;
127         UUID requestId = event.getRequestId();
128         String vnfName = event.getAai().get("generic-vnf.vnf-name");
129         String vnfId = event.getAai().get("generic-vnf.vnf-id");
130         String aaiUrl = PolicyEngine.manager.getEnvironmentProperty("aai.url");
131         String aaiUsername = PolicyEngine.manager.getEnvironmentProperty("aai.username");
132         String aaiPassword = PolicyEngine.manager.getEnvironmentProperty("aai.password");
133         try {
134             if (vnfName != null) {
135                 String url = aaiUrl + "/aai/v11/network/generic-vnfs/generic-vnf?vnf-name=";
136                 response = new AaiManager(new RESTManager()).getQueryByVnfName(url, aaiUsername, aaiPassword, requestId,
137                         vnfName);
138             } else if (vnfId != null) {
139                 String url = aaiUrl + "/aai/v11/network/generic-vnfs/generic-vnf/";
140                 response = new AaiManager(new RESTManager()).getQueryByVnfId(url, aaiUsername, aaiPassword, requestId,
141                         vnfId);
142             } else {
143                 logger.error("getAAIServiceInstance failed");
144             }
145         } catch (Exception e) {
146             logger.error("getAAIServiceInstance exception: ", e);
147         }
148         return response;
149     }
150 }