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