1ea65faa58feb678cabc0d0f1d637e2fd9f32ff4
[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.List;
23 import java.util.UUID;
24 import org.onap.policy.controlloop.VirtualControlLoopEvent;
25 import org.onap.policy.vfc.VFCRequest;
26 import org.onap.policy.vfc.VFCHealRequest;
27 import org.onap.policy.vfc.VFCHealAdditionalParams;
28 import org.onap.policy.vfc.VFCHealActionVmInfo;
29 import org.onap.policy.controlloop.ControlLoopOperation;
30 import org.onap.policy.controlloop.policy.Policy;
31 import org.onap.policy.controlloop.actorServiceProvider.spi.Actor;
32
33 import com.google.common.collect.ImmutableList;
34 import com.google.common.collect.ImmutableMap;
35 import org.onap.policy.aai.AAIManager;
36 import org.onap.policy.aai.AAIGETVnfResponse;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public class VFCActorServiceProvider implements Actor {
41
42     private static final Logger logger = LoggerFactory.getLogger(VFCActorServiceProvider.class);
43     private static final ImmutableList<String> recipes = ImmutableList.of("Restart");
44     private static final ImmutableMap<String, List<String>> targets = new ImmutableMap.Builder<String, List<String>>()
45             .put("Restart", ImmutableList.of("VM")).build();
46
47     @Override
48     public String actor() {
49         return "VFC";
50     }
51
52     @Override
53     public List<String> recipes() {
54         return ImmutableList.copyOf(recipes);
55     }
56
57     @Override
58     public List<String> recipeTargets(String recipe) {
59         return ImmutableList.copyOf(targets.getOrDefault(recipe, Collections.emptyList()));
60     }
61
62     @Override
63     public List<String> recipePayloads(String recipe) {
64         return Collections.emptyList();
65     }
66
67     public static VFCRequest constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation,
68                                               Policy policy) {
69
70         // Construct an VFC request
71         VFCRequest request = new VFCRequest();
72         // TODO: Verify service-instance-id is part of onset event
73         request.nsInstanceId = getAAIServiceInstance(onset); // onset.AAI.get("service-instance.service-instance-id");
74         request.requestId = onset.requestID;
75         request.healRequest = new VFCHealRequest();
76         request.healRequest.vnfInstanceId = onset.AAI.get("generic-vnf.vnf-id");
77         request.healRequest.cause = operation.message;
78
79         request.healRequest.additionalParams = new VFCHealAdditionalParams();
80         switch (policy.getRecipe()) {
81             case "Restart":
82                 // TODO: check target??
83                 request.healRequest.additionalParams.action = "restartvm";
84                 request.healRequest.additionalParams.actionInfo = new VFCHealActionVmInfo();
85                 // TODO: Verify vserver-id and vserver-name is part of onset event
86                 request.healRequest.additionalParams.actionInfo.vmid = onset.AAI.get("vserver.vserver-id");
87                 request.healRequest.additionalParams.actionInfo.vmname = onset.AAI.get("vserver.vserver-name");
88                 break;
89             default:
90                 // TODO: default
91                 break;
92         }
93         return request;
94     }
95
96     private static String getAAIServiceInstance(VirtualControlLoopEvent event) {
97         AAIGETVnfResponse response = null;
98         UUID requestID = event.requestID;
99         String serviceInstance = event.AAI.get("service-instance.service-instance-id");
100         String vnfName = event.AAI.get("generic-vnf.vnf-name");
101         String vnfID = event.AAI.get("generic-vnf.vnf-id");
102
103         String urlBase = "http://localhost:6666";
104         String username = "testUser";
105         String password = "testPass";
106         if (serviceInstance == null) {
107             try {
108                 if (vnfName != null) {
109                     String url = urlBase + "/aai/v11/network/generic-vnfs/generic-vnf?vnf-name=";
110                     response = AAIManager.getQueryByVnfName(url, username, password, requestID, vnfName);
111                     serviceInstance = response.serviceId;
112                 } else if (vnfID != null) {
113                     String url = urlBase + "/aai/v11/network/generic-vnfs/generic-vnf/";
114                     response = AAIManager.getQueryByVnfID(url, username, password, requestID, vnfID);
115                     serviceInstance = response.serviceId;
116                 } else {
117                     logger.error("getAAIServiceInstance failed");
118
119                 }
120             } catch (Exception e) {
121                 logger.error("getAAIServiceInstance exception: ", e);
122             }
123         }
124         return serviceInstance;
125     }
126 }