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