75810c01ed25c5a42e8082c17043a8a9c9ad6242
[policy/drools-applications.git] / controlloop / common / actors / actor.appc / src / main / java / org / onap / policy / controlloop / actor / appc / APPCActorServiceProvider.java
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.controlloop.actor.appclcm.AppcLcmActorServiceProvider;
28 import org.onap.policy.appc.CommonHeader;
29 import org.onap.policy.appc.Request;
30 import org.onap.policy.controlloop.ControlLoopOperation;
31 import org.onap.policy.controlloop.policy.Policy;
32 import org.onap.policy.vnf.trafficgenerator.PGRequest;
33 import org.onap.policy.vnf.trafficgenerator.PGStream;
34 import org.onap.policy.vnf.trafficgenerator.PGStreams;
35 import org.onap.policy.controlloop.actorServiceProvider.spi.Actor;
36 import com.google.common.collect.ImmutableList;
37 import com.google.common.collect.ImmutableMap;
38
39
40 public class APPCActorServiceProvider implements Actor {
41
42         private static final ImmutableList<String> recipes = ImmutableList.of("Restart", "Rebuild", "Migrate", "ModifyConfig");
43         private static final ImmutableMap<String, List<String>> targets = new ImmutableMap.Builder<String, List<String>>()
44                                                                                 .put("Restart", ImmutableList.of("VM"))
45                                                                                 .put("Rebuild", ImmutableList.of("VM"))
46                                                                                 .put("Migrate", ImmutableList.of("VM"))
47                                                                                 .put("ModifyConfig", ImmutableList.of("VNF"))
48                                                                                 .build();
49         private static final ImmutableMap<String, List<String>> payloads = new ImmutableMap.Builder<String, List<String>>()
50                                                                                 .put("ModifyConfig", ImmutableList.of("generic-vnf.vnf-id"))
51                                                                                 .build();
52         
53         @Override
54         public String actor() {
55                 return "APPC";
56         }
57
58         @Override
59         public List<String> recipes() {
60                 return ImmutableList.copyOf(recipes);
61         }
62
63         @Override
64         public List<String> recipeTargets(String recipe) {
65                 return ImmutableList.copyOf(targets.getOrDefault(recipe, Collections.emptyList()));
66         }
67
68         @Override
69         public List<String> recipePayloads(String recipe) {
70                 return ImmutableList.copyOf(payloads.getOrDefault(recipe, Collections.emptyList()));
71         }
72
73         /**
74          * Constructs an APPC request conforming to the legacy API.
75          * The legacy API will be deprecated in future releases as
76          * all legacy functionality is moved into the LCM API.
77          * 
78          * @param onset
79          *         the event that is reporting the alert for policy
80      *            to perform an action
81          * @param operation
82          *         the control loop operation specifying the actor,
83      *         operation, target, etc.
84          * @param policy
85          *         the policy the was specified from the yaml generated
86      *         by CLAMP or through the Policy GUI/API
87          * @return an APPC request conforming to the legacy API
88          */
89         public static Request constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation, Policy policy) {
90                 /*
91                  * Construct an APPC request
92                  */
93                 Request request = new Request();
94                 request.CommonHeader = new CommonHeader();
95                 request.CommonHeader.RequestID = onset.requestID;
96                 request.CommonHeader.SubRequestID = operation.subRequestId;
97                 request.Action = policy.getRecipe().substring(0, 1).toUpperCase() 
98                         + policy.getRecipe().substring(1);
99                 
100                 /*
101                  * The target vnf-id may not be the same as the source vnf-id
102                  * specified in the yaml, the target vnf-id is retrieved by
103                  * a named query to A&AI.
104                  */
105                 String vnfId;
106                 if (onset.AAI.get("generic-vnf.vnf-id").equalsIgnoreCase(policy.getTarget().getResourceID())) {
107                     vnfId = onset.AAI.get("generic-vnf.vnf-id");
108                 }
109                 else {
110                     vnfId = AppcLcmActorServiceProvider.vnfNamedQuery(
111                     policy.getTarget().getResourceID(), onset.AAI.get("generic-vnf.vnf-id"));
112                 }
113                 
114                 /*
115                  * For now Policy generates the PG Streams as a demo, in the
116                  * future the payload can be provided by CLAMP
117                  */
118                 request.Payload.put("generic-vnf.vnf-id", vnfId);
119                 
120                 PGRequest pgRequest = new PGRequest();
121                 pgRequest.pgStreams = new PGStreams();
122                 
123                 PGStream pgStream;
124                 for (int i = 0; i < 5; i++) {
125                     pgStream = new PGStream();
126                     pgStream.streamId = "fw_udp"+(i+1);
127             pgStream.isEnabled = "true";
128             pgRequest.pgStreams.pgStream.add(pgStream);
129                 }
130                 request.Payload.put("pg-streams", pgRequest.pgStreams);
131                 
132                 /*
133                  * Return the request
134                  */
135                 
136                 return request;
137         }
138         
139         
140 }