7cdf99625e3c26fb002feeadf338a9bb4b006e51
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPCActorServiceProvider
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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.appc;
22
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 import org.onap.policy.controlloop.VirtualControlLoopEvent;
29 import org.onap.policy.appc.CommonHeader;
30 import org.onap.policy.appc.Request;
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 APPCActorServiceProvider implements Actor {
40
41         private static final ImmutableList<String> recipes = ImmutableList.of("Restart", "Rebuild", "Migrate", "ModifyConfig");
42         private static final ImmutableMap<String, List<String>> targets = new ImmutableMap.Builder<String, List<String>>()
43                                                                                 .put("Restart", ImmutableList.of("VM"))
44                                                                                 .put("Rebuild", ImmutableList.of("VM"))
45                                                                                 .put("Migrate", ImmutableList.of("VM"))
46                                                                                 .put("ModifyConfig", ImmutableList.of("VFC"))
47                                                                                 .build();
48         private static final ImmutableMap<String, List<String>> payloads = new ImmutableMap.Builder<String, List<String>>()
49                                                                                 .put("ModifyConfig", ImmutableList.of("generic-vnf.vnf-id"))
50                                                                                 .build();
51         
52         @Override
53         public String actor() {
54                 return "APPC";
55         }
56
57         @Override
58         public List<String> recipes() {
59                 return ImmutableList.copyOf(recipes);
60         }
61
62         @Override
63         public List<String> recipeTargets(String recipe) {
64                 return ImmutableList.copyOf(targets.getOrDefault(recipe, Collections.emptyList()));
65         }
66
67         @Override
68         public List<String> recipePayloads(String recipe) {
69                 return ImmutableList.copyOf(payloads.getOrDefault(recipe, Collections.emptyList()));
70         }
71
72         
73         public static Request constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation, Policy policy) {
74                 //
75                 // Construct an APPC request
76                 //
77                 Request request = new Request();
78                 request.CommonHeader = new CommonHeader();
79                 request.CommonHeader.RequestID = onset.requestID;
80                 request.CommonHeader.SubRequestID = operation.subRequestId;
81                 request.Action = policy.getRecipe();
82                 
83                 //
84                 // TODO: do we need to take care of the target
85                 //
86                 
87                 //
88                 // Handle the payload
89                 //
90                 if (policy.getPayload() != null && !policy.getPayload().isEmpty()) {
91                         request.Payload = new HashMap<String, Object>();        
92                         //
93                         // Add each payload entry
94                         //
95                         for (Map.Entry<String, String> entry : policy.getPayload().entrySet()) {
96                         //
97                         // TODO: entry key has ref$, value has {xxxx}
98                         //
99                                 request.Payload.put(entry.getKey(), entry.getValue());  
100                         }
101                 }
102                 
103                 
104                 request.Payload.put("AICVServerSelfLink", onset.AAI.get("vserver.selflink"));//.AICVServerSelfLink);
105                 request.Payload.put("AICIdentity", onset.AAI.get("cloud-region.identity-url"));//AICIdentity);
106                 //
107                 // Return the request
108                 //
109                 return request;
110         }
111         
112         
113 }