a41d9e82d18f1372c4cb01ec4b316711ae7aaf50
[policy/models.git] / models-interactions / model-actors / actor.vfc / src / main / java / org / onap / policy / controlloop / actor / vfc / VfcActorServiceProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2017-2018 Intel Corp. All rights reserved.
4  * Modifications Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved.
5  * Modifications Copyright (C) 2019 Nordix Foundation.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.controlloop.actor.vfc;
22
23 import com.google.common.collect.ImmutableList;
24 import com.google.common.collect.ImmutableMap;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.UUID;
28 import org.onap.policy.aai.AaiCqResponse;
29 import org.onap.policy.aai.AaiGetVnfResponse;
30 import org.onap.policy.aai.AaiManager;
31 import org.onap.policy.controlloop.ControlLoopOperation;
32 import org.onap.policy.controlloop.VirtualControlLoopEvent;
33 import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
34 import org.onap.policy.controlloop.policy.Policy;
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 String GENERIC_VNF_ID = "generic-vnf.vnf-id";
45
46     private static final Logger logger = LoggerFactory.getLogger(VfcActorServiceProvider.class);
47
48     // Strings for VFC Actor
49     private static final String VFC_ACTOR = "VFC";
50
51     // Strings for targets
52     private static final String TARGET_VM = "VM";
53
54     // Strings for recipes
55     private static final String RECIPE_RESTART = "Restart";
56
57     private static final ImmutableList<String> recipes = ImmutableList.of(RECIPE_RESTART);
58     private static final ImmutableMap<String, List<String>> targets =
59             new ImmutableMap.Builder<String, List<String>>().put(RECIPE_RESTART, ImmutableList.of(TARGET_VM)).build();
60
61     @Override
62     public String actor() {
63         return VFC_ACTOR;
64     }
65
66     @Override
67     public List<String> recipes() {
68         return ImmutableList.copyOf(recipes);
69     }
70
71     @Override
72     public List<String> recipeTargets(String recipe) {
73         return ImmutableList.copyOf(targets.getOrDefault(recipe, Collections.emptyList()));
74     }
75
76     @Override
77     public List<String> recipePayloads(String recipe) {
78         return Collections.emptyList();
79     }
80
81     /**
82      * Construct a request.
83      *
84      * @param onset the onset event
85      * @param operation the control loop operation
86      * @param policy the policy
87      * @param vnfResponse the VNF response
88      * @return the constructed request
89      */
90     public static VfcRequest constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation,
91             Policy policy, AaiGetVnfResponse vnfResponse, String aaiUrl, String aaiUsername, String aaiPassword) {
92
93         // Construct an VFC request
94         VfcRequest request = new VfcRequest();
95         String serviceInstance = onset.getAai().get("service-instance.service-instance-id");
96         if (serviceInstance == null || "".equals(serviceInstance)) {
97             AaiGetVnfResponse tempVnfResp = vnfResponse;
98             if (tempVnfResp == null) { // if the response is null, we haven't queried
99                 // This does the AAI query since we haven't already
100                 tempVnfResp = getAaiServiceInstance(onset, aaiUrl, aaiUsername, aaiPassword);
101                 if (tempVnfResp == null) {
102                     return null;
103                 }
104             }
105             serviceInstance = tempVnfResp.getServiceId();
106         }
107         request.setNsInstanceId(serviceInstance);
108         request.setRequestId(onset.getRequestId());
109         request.setHealRequest(new VfcHealRequest());
110         request.getHealRequest().setVnfInstanceId(onset.getAai().get(GENERIC_VNF_ID));
111         request.getHealRequest().setCause(operation.getMessage());
112         request.getHealRequest().setAdditionalParams(new VfcHealAdditionalParams());
113
114         if (policy.getRecipe().toLowerCase().equalsIgnoreCase(RECIPE_RESTART)) {
115             request.getHealRequest().getAdditionalParams().setAction("restartvm");
116             request.getHealRequest().getAdditionalParams().setActionInfo(new VfcHealActionVmInfo());
117             request.getHealRequest().getAdditionalParams().getActionInfo()
118                     .setVmid(onset.getAai().get("vserver.vserver-id"));
119             request.getHealRequest().getAdditionalParams().getActionInfo()
120                     .setVmname(onset.getAai().get("vserver.vserver-name"));
121         } else {
122             return null;
123         }
124         return request;
125     }
126
127     private static AaiGetVnfResponse getAaiServiceInstance(VirtualControlLoopEvent event, String aaiUrl,
128             String aaiUsername, String aaiPassword) {
129         AaiGetVnfResponse response = null;
130         UUID requestId = event.getRequestId();
131         String vnfName = event.getAai().get("generic-vnf.vnf-name");
132         String vnfId = event.getAai().get(GENERIC_VNF_ID);
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
151     /**
152      * This method constructs the VFC request.
153      *
154      * @param onset onset object
155      * @param operation operation object
156      * @param policy policy object
157      * @param aaiCqResponse response from aai custom query
158      * @return VfcRequest
159      */
160     public static VfcRequest constructRequestCq(VirtualControlLoopEvent onset, ControlLoopOperation operation,
161             Policy policy, AaiCqResponse aaiCqResponse) {
162
163         // Construct an VFC request
164         VfcRequest request = new VfcRequest();
165         String serviceInstance = onset.getAai().get("service-instance.service-instance-id");
166         if (serviceInstance == null || "".equals(serviceInstance)) {
167             // get service isntance from AaiCqResponse
168             if (aaiCqResponse == null) {
169                 return null;
170             }
171             serviceInstance = aaiCqResponse.getServiceInstance().getServiceInstanceId();
172             // If the serviceInstanceId returned is null then return null
173             if (serviceInstance == null) {
174                 return null;
175             }
176
177         }
178         request.setNsInstanceId(serviceInstance);
179         request.setRequestId(onset.getRequestId());
180         request.setHealRequest(new VfcHealRequest());
181         request.getHealRequest().setVnfInstanceId(onset.getAai().get(GENERIC_VNF_ID));
182         request.getHealRequest().setCause(operation.getMessage());
183         request.getHealRequest().setAdditionalParams(new VfcHealAdditionalParams());
184
185         if (policy.getRecipe().toLowerCase().equalsIgnoreCase(RECIPE_RESTART)) {
186             request.getHealRequest().getAdditionalParams().setAction("restartvm");
187             request.getHealRequest().getAdditionalParams().setActionInfo(new VfcHealActionVmInfo());
188             request.getHealRequest().getAdditionalParams().getActionInfo()
189                     .setVmid(onset.getAai().get("vserver.vserver-id"));
190             request.getHealRequest().getAdditionalParams().getActionInfo()
191                     .setVmname(onset.getAai().get("vserver.vserver-name"));
192         } else {
193             return null;
194         }
195         return request;
196     }
197 }