Use "coder" to serialize Actor requests
[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-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.ActorImpl;
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
37 public class VfcActorServiceProvider extends ActorImpl {
38     private static final String GENERIC_VNF_ID = "generic-vnf.vnf-id";
39
40     // TODO old code: remove lines down to **HERE**
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     // **HERE**
56
57     /**
58      * Constructor.
59      */
60     public VfcActorServiceProvider() {
61         super(VFC_ACTOR);
62
63         addOperator(new VfcOperator(VFC_ACTOR, Restart.NAME, Restart::new));
64     }
65
66     // TODO old code: remove lines down to **HERE**
67
68     @Override
69     public String actor() {
70         return VFC_ACTOR;
71     }
72
73     @Override
74     public List<String> recipes() {
75         return ImmutableList.copyOf(recipes);
76     }
77
78     @Override
79     public List<String> recipeTargets(String recipe) {
80         return ImmutableList.copyOf(targets.getOrDefault(recipe, Collections.emptyList()));
81     }
82
83     @Override
84     public List<String> recipePayloads(String recipe) {
85         return Collections.emptyList();
86     }
87
88     /**
89      * This method constructs the VFC request.
90      *
91      * @param onset onset object
92      * @param operation operation object
93      * @param policy policy object
94      * @param aaiCqResponse response from aai custom query
95      * @return VfcRequest
96      */
97     public static VfcRequest constructRequestCq(VirtualControlLoopEvent onset, ControlLoopOperation operation,
98             Policy policy, AaiCqResponse aaiCqResponse) {
99
100         // Construct an VFC request
101         VfcRequest request = new VfcRequest();
102         String serviceInstance = onset.getAai().get("service-instance.service-instance-id");
103         if (serviceInstance == null || "".equals(serviceInstance)) {
104             // get service instance from AaiCqResponse
105             if (aaiCqResponse == null) {
106                 return null;
107             }
108             serviceInstance = aaiCqResponse.getServiceInstance().getServiceInstanceId();
109             // If the serviceInstanceId returned is null then return null
110             if (serviceInstance == null) {
111                 return null;
112             }
113
114         }
115         request.setNsInstanceId(serviceInstance);
116         request.setRequestId(onset.getRequestId());
117         request.setHealRequest(new VfcHealRequest());
118         request.getHealRequest().setVnfInstanceId(onset.getAai().get(GENERIC_VNF_ID));
119         request.getHealRequest().setCause(operation.getMessage());
120         request.getHealRequest().setAdditionalParams(new VfcHealAdditionalParams());
121
122         if (policy.getRecipe().toLowerCase().equalsIgnoreCase(RECIPE_RESTART)) {
123             request.getHealRequest().getAdditionalParams().setAction("restartvm");
124             request.getHealRequest().getAdditionalParams().setActionInfo(new VfcHealActionVmInfo());
125             request.getHealRequest().getAdditionalParams().getActionInfo()
126                     .setVmid(onset.getAai().get("vserver.vserver-id"));
127             request.getHealRequest().getAdditionalParams().getActionInfo()
128                     .setVmname(onset.getAai().get("vserver.vserver-name"));
129         } else {
130             return null;
131         }
132         return request;
133     }
134
135     // **HERE**
136
137 }