2d299210627d4b2c85c10dfe51018c6952cb8aa7
[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.List;
25
26 import org.onap.policy.controlloop.VirtualControlLoopEvent;
27 import org.onap.policy.appc.CommonHeader;
28 import org.onap.policy.appc.Request;
29 import org.onap.policy.controlloop.ControlLoopOperation;
30 import org.onap.policy.controlloop.policy.Policy;
31 import org.onap.policy.vnf.trafficgenerator.PGRequest;
32 import org.onap.policy.vnf.trafficgenerator.PGStream;
33 import org.onap.policy.vnf.trafficgenerator.PGStreams;
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("VNF"))
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          * Constructs an APPC request conforming to the legacy API.
74          * The legacy API will be deprecated in future releases as
75          * all legacy functionality is moved into the LCM API.
76          * 
77          * @param onset
78          *         the event that is reporting the alert for policy
79      *            to perform an action
80          * @param operation
81          *         the control loop operation specifying the actor,
82      *         operation, target, etc.
83          * @param policy
84          *         the policy the was specified from the yaml generated
85      *         by CLAMP or through the Policy GUI/API
86          * @return an APPC request conforming to the legacy API
87          */
88         public static Request constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation, Policy policy) {
89                 /*
90                  * Construct an APPC request
91                  */
92                 Request request = new Request();
93                 request.CommonHeader = new CommonHeader();
94                 request.CommonHeader.RequestID = onset.requestID;
95                 request.CommonHeader.SubRequestID = operation.subRequestId;
96                 request.Action = policy.getRecipe();
97                 
98                 /*
99                  * The target vnf-id may not be the same as the source vnf-id
100                  * specified in the yaml, the target vnf-id is retrieved by
101                  * a named query to A&AI.
102                  */
103                 String vnfId = "test";
104                 
105                 /*
106                  * For now Policy generates the PG Streams as a demo, in the
107                  * future the payload can be provided by CLAMP
108                  */
109                 request.Payload.put("generic-vnf.vnf-id", vnfId);
110                 
111                 PGRequest pgRequest = new PGRequest();
112                 pgRequest.pgStreams = new PGStreams();
113                 
114                 PGStream pgStream;
115                 for (int i = 0; i < 5; i++) {
116                     pgStream = new PGStream();
117                     pgStream.streamId = "fw_udp"+(i+1);
118             pgStream.isEnabled = "true";
119             pgRequest.pgStreams.pgStream.add(pgStream);
120                 }
121                 request.Payload.put("pg-streams", pgRequest.pgStreams);
122                 
123                 /*
124                  * Return the request
125                  */
126                 
127                 return request;
128         }
129         
130         
131 }