406870208f7480a99a04af2a75acd38b4c3d1d2d
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SdnrActorServiceProvider
4  * ================================================================================
5  * Copyright (C) 2018 Wipro Limited 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.sdnr;
22
23 import com.google.common.collect.ImmutableList;
24 import com.google.common.collect.ImmutableMap;
25
26 import java.util.Collections;
27 import java.util.List;
28
29 import org.onap.policy.controlloop.ControlLoopOperation;
30 import org.onap.policy.controlloop.VirtualControlLoopEvent;
31 import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
32 import org.onap.policy.controlloop.policy.Policy;
33 import org.onap.policy.controlloop.policy.PolicyResult;
34 import org.onap.policy.sdnr.PciCommonHeader;
35 import org.onap.policy.sdnr.PciRequest;
36 import org.onap.policy.sdnr.PciRequestWrapper;
37 import org.onap.policy.sdnr.PciResponse;
38 import org.onap.policy.sdnr.PciResponseCode;
39 import org.onap.policy.sdnr.PciResponseWrapper;
40
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 public class SdnrActorServiceProvider implements Actor {
45
46     public static class Pair<A, B> {
47         public final A result;
48         public final B message;
49
50         public Pair(A result, B message) {
51             this.result = result;
52             this.message = message;
53         }
54
55         public A getResult() {
56             return this.result;
57         }
58
59         public B getMessage() {
60             return this.message;
61         }
62     }
63
64     private static final Logger logger = LoggerFactory.getLogger(SdnrActorServiceProvider.class);
65
66     // Strings for targets
67     private static final String TARGET_VNF = "VNF";
68
69     // Strings for recipes
70     private static final String RECIPE_MODIFY = "ModifyConfig";
71
72     /* To be used in future releases when pci ModifyConfig is used */
73     private static final String SDNR_REQUEST_PARAMS = "request-parameters";
74     private static final String SDNR_CONFIG_PARAMS = "configuration-parameters";
75
76     private static final ImmutableList<String> recipes = ImmutableList.of(RECIPE_MODIFY);
77     private static final ImmutableMap<String, List<String>> targets = new ImmutableMap.Builder<String, List<String>>()
78             .put(RECIPE_MODIFY, ImmutableList.of(TARGET_VNF)).build();
79     private static final ImmutableMap<String, List<String>> payloads = new ImmutableMap.Builder<String, List<String>>()
80             .put(RECIPE_MODIFY, ImmutableList.of(SDNR_REQUEST_PARAMS, SDNR_CONFIG_PARAMS)).build();
81
82     @Override
83     public String actor() {
84         return "SDNR";
85     }
86
87     @Override
88     public List<String> recipes() {
89         return ImmutableList.copyOf(recipes);
90     }
91
92     @Override
93     public List<String> recipeTargets(String recipe) {
94         return ImmutableList.copyOf(targets.getOrDefault(recipe, Collections.emptyList()));
95     }
96
97     @Override
98     public List<String> recipePayloads(String recipe) {
99         return ImmutableList.copyOf(payloads.getOrDefault(recipe, Collections.emptyList()));
100     }
101
102     /**
103      * Constructs an SDNR request conforming to the pci API. The actual request is
104      * constructed and then placed in a wrapper object used to send through DMAAP.
105      * 
106      * @param onset
107      *            the event that is reporting the alert for policy to perform an
108      *            action
109      * @param operation
110      *            the control loop operation specifying the actor, operation,
111      *            target, etc.
112      * @param policy
113      *            the policy the was specified from the yaml generated by CLAMP or
114      *            through the Policy GUI/API
115      * @return an SDNR request conforming to the pci API using the DMAAP wrapper
116      */
117
118     public static PciRequestWrapper constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation,
119             Policy policy) {
120
121         /* Construct an SDNR request using pci Model */
122
123         /*
124          * The actual pci request is placed in a wrapper used to send through dmaap. The
125          * current version is 2.0 as of R1.
126          */
127         PciRequestWrapper dmaapRequest = new PciRequestWrapper();
128         dmaapRequest.setVersion("1.0");
129         dmaapRequest.setCorrelationId(onset.getRequestId() + "-" + operation.getSubRequestId());
130         dmaapRequest.setRpcName(policy.getRecipe().toLowerCase());
131         dmaapRequest.setType("request");
132
133         /* This is the actual request that is placed in the dmaap wrapper. */
134         final PciRequest sdnrRequest = new PciRequest();
135
136         /* The common header is a required field for all SDNR requests. */
137         PciCommonHeader requestCommonHeader = new PciCommonHeader();
138         requestCommonHeader.setRequestId(onset.getRequestId());
139         requestCommonHeader.setSubRequestId(operation.getSubRequestId());
140
141         sdnrRequest.setCommonHeader(requestCommonHeader);
142         sdnrRequest.setPayload(onset.getPayload());
143
144         /*
145          * An action is required for all SDNR requests, this will be the recipe
146          * specified in the policy.
147          */
148         sdnrRequest.setAction(policy.getRecipe());
149
150         /*
151          * Once the pci request is constructed, add it into the body of the dmaap
152          * wrapper.
153          */
154         dmaapRequest.setBody(sdnrRequest);
155         logger.info("SDNR Request to be sent is {}", dmaapRequest);
156
157         /* Return the request to be sent through dmaap. */
158         return dmaapRequest;
159     }
160
161     /**
162      * Parses the operation attempt using the subRequestId of SDNR response.
163      * 
164      * @param subRequestId
165      *            the sub id used to send to SDNR, Policy sets this using the
166      *            operation attempt
167      * 
168      * @return the current operation attempt
169      */
170     public static Integer parseOperationAttempt(String subRequestId) {
171         Integer operationAttempt;
172         try {
173             operationAttempt = Integer.parseInt(subRequestId);
174         } catch (NumberFormatException e) {
175             logger.debug("A NumberFormatException was thrown in parsing the operation attempt {}", subRequestId);
176             return null;
177         }
178         return operationAttempt;
179     }
180
181     /**
182      * Processes the SDNR pci response sent from SDNR. Determines if the SDNR
183      * operation was successful/unsuccessful and maps this to the corresponding
184      * Policy result.
185      * 
186      * @param dmaapResponse
187      *            the dmaap wrapper message that contains the actual SDNR reponse
188      *            inside the body field
189      * 
190      * @return an key-value pair that contains the Policy result and SDNR response
191      *         message
192      */
193     public static SdnrActorServiceProvider.Pair<PolicyResult, String> processResponse(
194             PciResponseWrapper dmaapResponse) {
195
196         logger.info("SDNR processResponse called : {}", dmaapResponse);
197
198         /* The actual SDNR response is inside the wrapper's body field. */
199         PciResponse sdnrResponse = dmaapResponse.getBody();
200
201         /* The message returned in the SDNR response. */
202         String message;
203
204         /* The Policy result determined from the SDNR Response. */
205         PolicyResult result;
206
207         /*
208          * If there is no status, Policy cannot determine if the request was successful.
209          */
210         if (sdnrResponse.getStatus() == null) {
211             message = "Policy was unable to parse SDN-R response status field (it was null).";
212             return new SdnrActorServiceProvider.Pair<>(PolicyResult.FAILURE_EXCEPTION, message);
213         }
214
215         /*
216          * If there is no code, Policy cannot determine if the request was successful.
217          */
218         String responseValue = PciResponseCode.toResponseValue(sdnrResponse.getStatus().getCode());
219         if (responseValue == null) {
220             message = "Policy was unable to parse SDN-R response status code field.";
221             return new SdnrActorServiceProvider.Pair<>(PolicyResult.FAILURE_EXCEPTION, message);
222         }
223         logger.info("SDNR Response Code is {}", responseValue);
224
225         /* Save the SDNR response's message for Policy notification message. */
226         message = sdnrResponse.getStatus().getValue();
227         logger.info("SDNR Response Message is {}", message);
228
229         /*
230          * Response and Payload are just printed and no further action needed in
231          * casablanca release
232          */
233         String rspPayload = sdnrResponse.getPayload();
234         logger.info("SDNR Response Payload is {}", rspPayload);
235
236         /* Maps the SDNR response result to a Policy result. */
237         switch (responseValue) {
238             case PciResponseCode.ACCEPTED:
239                 /* Nothing to do if code is accept, continue processing */
240                 result = null;
241                 break;
242             case PciResponseCode.SUCCESS:
243                 result = PolicyResult.SUCCESS;
244                 break;
245             case PciResponseCode.FAILURE:
246                 result = PolicyResult.FAILURE;
247                 break;
248             case PciResponseCode.REJECT:
249             case PciResponseCode.ERROR:
250             default:
251                 result = PolicyResult.FAILURE_EXCEPTION;
252         }
253         return new SdnrActorServiceProvider.Pair<>(result, message);
254     }
255 }