ffada0a8183e568b38d6d2f881bded000801c7d7
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2017 Intel Corp. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=========================================================
17  */
18
19 package org.onap.policy.controlloop.actor.vfc;
20
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.onap.policy.controlloop.VirtualControlLoopEvent;
27 import org.onap.policy.vfc.VFCRequest;
28 import org.onap.policy.vfc.VFCHealRequest;
29 import org.onap.policy.vfc.VFCHealAdditionalParams;
30 import org.onap.policy.vfc.VFCHealActionVmInfo;
31 import org.onap.policy.controlloop.ControlLoopOperation;
32 import org.onap.policy.controlloop.policy.Policy;
33
34 import org.onap.policy.controlloop.actorServiceProvider.spi.Actor;
35 import com.google.common.collect.ImmutableList;
36 import com.google.common.collect.ImmutableMap;
37
38
39 public class VFCActorServiceProvider implements Actor {
40
41     private static final ImmutableList<String> recipes = ImmutableList.of("Restart");
42     private static final ImmutableMap<String, List<String>> targets = new ImmutableMap.Builder<String, List<String>>()
43             .put("Restart", ImmutableList.of("VM"))
44             .build();
45
46     @Override
47     public String actor() {
48         return "VFC";
49     }
50
51     @Override
52     public List<String> recipes() {
53         return ImmutableList.copyOf(recipes);
54     }
55
56     @Override
57     public List<String> recipeTargets(String recipe) {
58         return ImmutableList.copyOf(targets.getOrDefault(recipe, Collections.emptyList()));
59     }
60
61     @Override
62     public List<String> recipePayloads(String recipe) {
63         return Collections.emptyList();
64     }
65
66     public static VFCRequest constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation, Policy policy) {
67
68         // Construct an VFC request
69         VFCRequest request = new VFCRequest();
70         // TODO: Verify service-instance-id is part of onset event
71         request.nsInstanceId = onset.AAI.get("service-instance.service-instance-id");
72         request.healRequest = new VFCHealRequest();
73         request.healRequest.vnfInstanceId = onset.AAI.get("generic-vnf.vnf-id");
74         request.healRequest.cause = operation.message;
75
76         request.healRequest.additionalParams = new VFCHealAdditionalParams();
77         switch (policy.getRecipe()) {
78             case "Restart":
79                 // TODO: check target??
80                 request.healRequest.additionalParams.action = "restartvm";
81                 request.healRequest.additionalParams.actionInfo = new VFCHealActionVmInfo();
82                 // TODO: Verify vserver-id and vserver-name is part of onset event
83                 request.healRequest.additionalParams.actionInfo.vmid = onset.AAI.get("vserver.vserver-id");
84                 request.healRequest.additionalParams.actionInfo.vmname = onset.AAI.get("vserver.vserver-name");
85                 break;
86             default:
87                 //TODO: default
88                 break;
89         }
90         return request;
91     }
92
93
94 }