ef897b9c9a4391b31e558056454fae16e1a362b3
[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
36 import com.google.common.collect.ImmutableList;
37 import com.google.common.collect.ImmutableMap;
38
39
40 public class APPCActorServiceProvider implements Actor {
41         // Strings for targets
42         private static final String TARGET_VM  = "VM";
43         private static final String TARGET_VNF = "VNF";
44
45         // Strings for recipes
46         private static final String RECIPE_RESTART = "Restart";
47         private static final String RECIPE_REBUILD = "Rebuild";
48         private static final String RECIPE_MIGRATE = "Migrate";
49         private static final String RECIPE_MODIFY  = "ModifyConfig";
50
51         private static final ImmutableList<String> recipes = ImmutableList.of(RECIPE_RESTART, RECIPE_REBUILD, RECIPE_MIGRATE, RECIPE_MODIFY);
52         private static final ImmutableMap<String, List<String>> targets = new ImmutableMap.Builder<String, List<String>>()
53                         .put(RECIPE_RESTART, ImmutableList.of(TARGET_VM))
54                         .put(RECIPE_REBUILD, ImmutableList.of(TARGET_VM))
55                         .put(RECIPE_MIGRATE, ImmutableList.of(TARGET_VM))
56                         .put(RECIPE_MODIFY, ImmutableList.of(TARGET_VNF))
57                         .build();
58         private static final ImmutableMap<String, List<String>> payloads = new ImmutableMap.Builder<String, List<String>>()
59                         .put(RECIPE_MODIFY, ImmutableList.of("generic-vnf.vnf-id"))
60                         .build();
61
62         @Override
63         public String actor() {
64                 return "APPC";
65         }
66
67         @Override
68         public List<String> recipes() {
69                 return ImmutableList.copyOf(recipes);
70         }
71
72         @Override
73         public List<String> recipeTargets(String recipe) {
74                 return ImmutableList.copyOf(targets.getOrDefault(recipe, Collections.emptyList()));
75         }
76
77         @Override
78         public List<String> recipePayloads(String recipe) {
79                 return ImmutableList.copyOf(payloads.getOrDefault(recipe, Collections.emptyList()));
80         }
81
82         /**
83          * Constructs an APPC request conforming to the legacy API.
84          * The legacy API will be deprecated in future releases as
85          * all legacy functionality is moved into the LCM API.
86          * 
87          * @param onset
88          *         the event that is reporting the alert for policy
89          *            to perform an action
90          * @param operation
91          *         the control loop operation specifying the actor,
92          *         operation, target, etc.
93          * @param policy
94          *         the policy the was specified from the yaml generated
95          *         by CLAMP or through the Policy GUI/API
96          * @return an APPC request conforming to the legacy API
97          * @throws AAIException 
98          */
99         public static Request constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation,
100                         Policy policy, String targetVnf) {
101                 /*
102                  * Construct an APPC request
103                  */
104                 Request request = new Request();
105                 request.setCommonHeader(new CommonHeader());
106                 request.getCommonHeader().setRequestID(onset.getRequestID());
107                 request.getCommonHeader().setSubRequestID(operation.getSubRequestId());
108                 request.setAction(policy.getRecipe().substring(0, 1).toUpperCase() 
109                                 + policy.getRecipe().substring(1));
110
111                 /*
112                  * For now Policy generates the PG Streams as a demo, in the
113                  * future the payload can be provided by CLAMP
114                  */
115                 request.getPayload().put("generic-vnf.vnf-id", targetVnf);
116
117                 PGRequest pgRequest = new PGRequest();
118                 pgRequest.pgStreams = new PGStreams();
119
120                 PGStream pgStream;
121                 for (int i = 0; i < 5; i++) {
122                         pgStream = new PGStream();
123                         pgStream.streamId = "fw_udp"+(i+1);
124                         pgStream.isEnabled = "true";
125                         pgRequest.pgStreams.pgStream.add(pgStream);
126                 }
127                 request.getPayload().put("pg-streams", pgRequest.pgStreams);
128
129                 /*
130                  * Return the request
131                  */
132
133                 return request;
134         }
135
136
137 }