cf41731764456ff6bc37ee031b1079f1104706c7
[policy/models.git] / models-interactions / model-actors / actor.vfc / src / main / java / org / onap / policy / controlloop / actor / vfc / VfcActor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2017-2018 Intel Corp. All rights reserved.
4  * Modifications Copyright (C) 2018-2020 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.impl.HttpActor;
31 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpPollingOperator;
32 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingActorParams;
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 VfcActor extends HttpActor<HttpPollingActorParams> {
40     private static final String GENERIC_VNF_ID = "generic-vnf.vnf-id";
41
42     // TODO old code: remove lines down to **HERE**
43
44     // Strings for VFC Actor
45     private static final String VFC_ACTOR = "VFC";
46
47     public static final String NAME = VFC_ACTOR;
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     // **HERE**
60
61     /**
62      * Constructor.
63      */
64     public VfcActor() {
65         super(NAME, HttpPollingActorParams.class);
66
67         addOperator(new HttpPollingOperator(NAME, Restart.NAME, Restart::new));
68     }
69
70     // TODO old code: remove lines down to **HERE**
71
72     @Override
73     public String actor() {
74         return VFC_ACTOR;
75     }
76
77     @Override
78     public List<String> recipes() {
79         return ImmutableList.copyOf(recipes);
80     }
81
82     @Override
83     public List<String> recipeTargets(String recipe) {
84         return ImmutableList.copyOf(targets.getOrDefault(recipe, Collections.emptyList()));
85     }
86
87     @Override
88     public List<String> recipePayloads(String recipe) {
89         return Collections.emptyList();
90     }
91
92     /**
93      * This method constructs the VFC request.
94      *
95      * @param onset onset object
96      * @param operation operation object
97      * @param policy policy object
98      * @param aaiCqResponse response from aai custom query
99      * @return VfcRequest
100      */
101     public static VfcRequest constructRequestCq(VirtualControlLoopEvent onset, ControlLoopOperation operation,
102             Policy policy, AaiCqResponse aaiCqResponse) {
103
104         // Construct an VFC request
105         VfcRequest request = new VfcRequest();
106         String serviceInstance = onset.getAai().get("service-instance.service-instance-id");
107         if (serviceInstance == null || "".equals(serviceInstance)) {
108             // get service instance from AaiCqResponse
109             if (aaiCqResponse == null) {
110                 return null;
111             }
112             serviceInstance = aaiCqResponse.getServiceInstance().getServiceInstanceId();
113             // If the serviceInstanceId returned is null then return null
114             if (serviceInstance == null) {
115                 return null;
116             }
117
118         }
119         request.setNsInstanceId(serviceInstance);
120         request.setRequestId(onset.getRequestId());
121         request.setHealRequest(new VfcHealRequest());
122         request.getHealRequest().setVnfInstanceId(onset.getAai().get(GENERIC_VNF_ID));
123         request.getHealRequest().setCause(operation.getMessage());
124         request.getHealRequest().setAdditionalParams(new VfcHealAdditionalParams());
125
126         if (policy.getRecipe().toLowerCase().equalsIgnoreCase(RECIPE_RESTART)) {
127             request.getHealRequest().getAdditionalParams().setAction("restartvm");
128             request.getHealRequest().getAdditionalParams().setActionInfo(new VfcHealActionVmInfo());
129             request.getHealRequest().getAdditionalParams().getActionInfo()
130                     .setVmid(onset.getAai().get("vserver.vserver-id"));
131             request.getHealRequest().getAdditionalParams().getActionInfo()
132                     .setVmname(onset.getAai().get("vserver.vserver-name"));
133         } else {
134             return null;
135         }
136         return request;
137     }
138
139     // **HERE**
140
141 }