2 * ============LICENSE_START=======================================================
3 * SdnrActorServiceProvider
4 * ================================================================================
5 * Copyright (C) 2018 Wipro Limited Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.controlloop.actor.sdnr;
24 import com.google.common.collect.ImmutableList;
25 import com.google.common.collect.ImmutableMap;
27 import java.util.Collections;
28 import java.util.List;
30 import org.onap.policy.controlloop.ControlLoopOperation;
31 import org.onap.policy.controlloop.VirtualControlLoopEvent;
32 import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
33 import org.onap.policy.controlloop.policy.Policy;
34 import org.onap.policy.controlloop.policy.PolicyResult;
35 import org.onap.policy.sdnr.PciCommonHeader;
36 import org.onap.policy.sdnr.PciRequest;
37 import org.onap.policy.sdnr.PciRequestWrapper;
38 import org.onap.policy.sdnr.PciResponse;
39 import org.onap.policy.sdnr.PciResponseCode;
40 import org.onap.policy.sdnr.PciResponseWrapper;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 public class SdnrActorServiceProvider implements Actor {
47 public static class Pair<A, B> {
48 public final A result;
49 public final B message;
51 public Pair(A result, B message) {
53 this.message = message;
56 public A getResult() {
60 public B getMessage() {
65 private static final Logger logger = LoggerFactory.getLogger(SdnrActorServiceProvider.class);
67 // Strings for targets
68 private static final String TARGET_VNF = "VNF";
70 // Strings for recipes
71 private static final String RECIPE_MODIFY = "ModifyConfig";
73 /* To be used in future releases when pci ModifyConfig is used */
74 private static final String SDNR_REQUEST_PARAMS = "request-parameters";
75 private static final String SDNR_CONFIG_PARAMS = "configuration-parameters";
77 private static final ImmutableList<String> recipes = ImmutableList.of(RECIPE_MODIFY);
78 private static final ImmutableMap<String, List<String>> targets = new ImmutableMap.Builder<String, List<String>>()
79 .put(RECIPE_MODIFY, ImmutableList.of(TARGET_VNF)).build();
80 private static final ImmutableMap<String, List<String>> payloads = new ImmutableMap.Builder<String, List<String>>()
81 .put(RECIPE_MODIFY, ImmutableList.of(SDNR_REQUEST_PARAMS, SDNR_CONFIG_PARAMS)).build();
84 public String actor() {
89 public List<String> recipes() {
90 return ImmutableList.copyOf(recipes);
94 public List<String> recipeTargets(String recipe) {
95 return ImmutableList.copyOf(targets.getOrDefault(recipe, Collections.emptyList()));
99 public List<String> recipePayloads(String recipe) {
100 return ImmutableList.copyOf(payloads.getOrDefault(recipe, Collections.emptyList()));
104 * Constructs an SDNR request conforming to the pci API. The actual request is
105 * constructed and then placed in a wrapper object used to send through DMAAP.
108 * the event that is reporting the alert for policy to perform an
111 * the control loop operation specifying the actor, operation,
114 * the policy the was specified from the yaml generated by CLAMP or
115 * through the Policy GUI/API
116 * @return an SDNR request conforming to the pci API using the DMAAP wrapper
119 public static PciRequestWrapper constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation,
122 /* Construct an SDNR request using pci Model */
125 * The actual pci request is placed in a wrapper used to send through dmaap. The
126 * current version is 2.0 as of R1.
128 PciRequestWrapper dmaapRequest = new PciRequestWrapper();
129 dmaapRequest.setVersion("1.0");
130 dmaapRequest.setCorrelationId(onset.getRequestId() + "-" + operation.getSubRequestId());
131 dmaapRequest.setRpcName(policy.getRecipe().toLowerCase());
132 dmaapRequest.setType("request");
134 /* This is the actual request that is placed in the dmaap wrapper. */
135 final PciRequest sdnrRequest = new PciRequest();
137 /* The common header is a required field for all SDNR requests. */
138 PciCommonHeader requestCommonHeader = new PciCommonHeader();
139 requestCommonHeader.setRequestId(onset.getRequestId());
140 requestCommonHeader.setSubRequestId(operation.getSubRequestId());
142 sdnrRequest.setCommonHeader(requestCommonHeader);
143 sdnrRequest.setPayload(onset.getPayload());
146 * An action is required for all SDNR requests, this will be the recipe
147 * specified in the policy.
149 sdnrRequest.setAction(policy.getRecipe());
152 * Once the pci request is constructed, add it into the body of the dmaap
155 dmaapRequest.setBody(sdnrRequest);
156 logger.info("SDNR Request to be sent is {}", dmaapRequest);
158 /* Return the request to be sent through dmaap. */
163 * Parses the operation attempt using the subRequestId of SDNR response.
165 * @param subRequestId
166 * the sub id used to send to SDNR, Policy sets this using the
169 * @return the current operation attempt
171 public static Integer parseOperationAttempt(String subRequestId) {
172 Integer operationAttempt;
174 operationAttempt = Integer.parseInt(subRequestId);
175 } catch (NumberFormatException e) {
176 logger.debug("A NumberFormatException was thrown in parsing the operation attempt {}", subRequestId);
179 return operationAttempt;
183 * Processes the SDNR pci response sent from SDNR. Determines if the SDNR
184 * operation was successful/unsuccessful and maps this to the corresponding
187 * @param dmaapResponse
188 * the dmaap wrapper message that contains the actual SDNR reponse
189 * inside the body field
191 * @return an key-value pair that contains the Policy result and SDNR response
194 public static SdnrActorServiceProvider.Pair<PolicyResult, String> processResponse(
195 PciResponseWrapper dmaapResponse) {
197 logger.info("SDNR processResponse called : {}", dmaapResponse);
199 /* The actual SDNR response is inside the wrapper's body field. */
200 PciResponse sdnrResponse = dmaapResponse.getBody();
202 /* The message returned in the SDNR response. */
205 /* The Policy result determined from the SDNR Response. */
209 * If there is no status, Policy cannot determine if the request was successful.
211 if (sdnrResponse.getStatus() == null) {
212 message = "Policy was unable to parse SDN-R response status field (it was null).";
213 return new SdnrActorServiceProvider.Pair<>(PolicyResult.FAILURE_EXCEPTION, message);
217 * If there is no code, Policy cannot determine if the request was successful.
219 String responseValue = PciResponseCode.toResponseValue(sdnrResponse.getStatus().getCode());
220 if (responseValue == null) {
221 message = "Policy was unable to parse SDN-R response status code field.";
222 return new SdnrActorServiceProvider.Pair<>(PolicyResult.FAILURE_EXCEPTION, message);
224 logger.info("SDNR Response Code is {}", responseValue);
226 /* Save the SDNR response's message for Policy notification message. */
227 message = sdnrResponse.getStatus().getValue();
228 logger.info("SDNR Response Message is {}", message);
231 * Response and Payload are just printed and no further action needed in
234 String rspPayload = sdnrResponse.getPayload();
235 logger.info("SDNR Response Payload is {}", rspPayload);
237 /* Maps the SDNR response result to a Policy result. */
238 switch (responseValue) {
239 case PciResponseCode.ACCEPTED:
240 /* Nothing to do if code is accept, continue processing */
243 case PciResponseCode.SUCCESS:
244 result = PolicyResult.SUCCESS;
246 case PciResponseCode.FAILURE:
247 result = PolicyResult.FAILURE;
249 case PciResponseCode.REJECT:
250 case PciResponseCode.ERROR:
252 result = PolicyResult.FAILURE_EXCEPTION;
254 return new SdnrActorServiceProvider.Pair<>(result, message);