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