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