Removing Named Query.
[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 org.onap.policy.aai.AaiCqResponse;
28 import org.onap.policy.controlloop.ControlLoopOperation;
29 import org.onap.policy.controlloop.VirtualControlLoopEvent;
30 import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
31 import org.onap.policy.controlloop.policy.Policy;
32 import org.onap.policy.vfc.VfcHealActionVmInfo;
33 import org.onap.policy.vfc.VfcHealAdditionalParams;
34 import org.onap.policy.vfc.VfcHealRequest;
35 import org.onap.policy.vfc.VfcRequest;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class VfcActorServiceProvider implements Actor {
40     private static final String GENERIC_VNF_ID = "generic-vnf.vnf-id";
41
42     private static final Logger logger = LoggerFactory.getLogger(VfcActorServiceProvider.class);
43
44     // Strings for VFC Actor
45     private static final String VFC_ACTOR = "VFC";
46
47     // Strings for targets
48     private static final String TARGET_VM = "VM";
49
50     // Strings for recipes
51     private static final String RECIPE_RESTART = "Restart";
52
53     private static final ImmutableList<String> recipes = ImmutableList.of(RECIPE_RESTART);
54     private static final ImmutableMap<String, List<String>> targets =
55             new ImmutableMap.Builder<String, List<String>>().put(RECIPE_RESTART, ImmutableList.of(TARGET_VM)).build();
56
57     @Override
58     public String actor() {
59         return VFC_ACTOR;
60     }
61
62     @Override
63     public List<String> recipes() {
64         return ImmutableList.copyOf(recipes);
65     }
66
67     @Override
68     public List<String> recipeTargets(String recipe) {
69         return ImmutableList.copyOf(targets.getOrDefault(recipe, Collections.emptyList()));
70     }
71
72     @Override
73     public List<String> recipePayloads(String recipe) {
74         return Collections.emptyList();
75     }
76
77     /**
78      * This method constructs the VFC request.
79      *
80      * @param onset onset object
81      * @param operation operation object
82      * @param policy policy object
83      * @param aaiCqResponse response from aai custom query
84      * @return VfcRequest
85      */
86     public static VfcRequest constructRequestCq(VirtualControlLoopEvent onset, ControlLoopOperation operation,
87             Policy policy, AaiCqResponse aaiCqResponse) {
88
89         // Construct an VFC request
90         VfcRequest request = new VfcRequest();
91         String serviceInstance = onset.getAai().get("service-instance.service-instance-id");
92         if (serviceInstance == null || "".equals(serviceInstance)) {
93             // get service isntance from AaiCqResponse
94             if (aaiCqResponse == null) {
95                 return null;
96             }
97             serviceInstance = aaiCqResponse.getServiceInstance().getServiceInstanceId();
98             // If the serviceInstanceId returned is null then return null
99             if (serviceInstance == null) {
100                 return null;
101             }
102
103         }
104         request.setNsInstanceId(serviceInstance);
105         request.setRequestId(onset.getRequestId());
106         request.setHealRequest(new VfcHealRequest());
107         request.getHealRequest().setVnfInstanceId(onset.getAai().get(GENERIC_VNF_ID));
108         request.getHealRequest().setCause(operation.getMessage());
109         request.getHealRequest().setAdditionalParams(new VfcHealAdditionalParams());
110
111         if (policy.getRecipe().toLowerCase().equalsIgnoreCase(RECIPE_RESTART)) {
112             request.getHealRequest().getAdditionalParams().setAction("restartvm");
113             request.getHealRequest().getAdditionalParams().setActionInfo(new VfcHealActionVmInfo());
114             request.getHealRequest().getAdditionalParams().getActionInfo()
115                     .setVmid(onset.getAai().get("vserver.vserver-id"));
116             request.getHealRequest().getAdditionalParams().getActionInfo()
117                     .setVmname(onset.getAai().get("vserver.vserver-name"));
118         } else {
119             return null;
120         }
121         return request;
122     }
123 }