2  * ============LICENSE_START=======================================================
 
   3  * SdnrActorServiceProvider
 
   4  * ================================================================================
 
   5  * Copyright (C) 2018 Wipro Limited Intellectual Property. All rights reserved.
 
   6  * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 
   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.ControlLoopResponse;
 
  32 import org.onap.policy.controlloop.VirtualControlLoopEvent;
 
  33 import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
 
  34 import org.onap.policy.controlloop.policy.Policy;
 
  35 import org.onap.policy.controlloop.policy.PolicyResult;
 
  36 import org.onap.policy.sdnr.PciCommonHeader;
 
  37 import org.onap.policy.sdnr.PciRequest;
 
  38 import org.onap.policy.sdnr.PciRequestWrapper;
 
  39 import org.onap.policy.sdnr.PciResponse;
 
  40 import org.onap.policy.sdnr.PciResponseCode;
 
  41 import org.onap.policy.sdnr.PciResponseWrapper;
 
  43 import org.slf4j.Logger;
 
  44 import org.slf4j.LoggerFactory;
 
  46 public class SdnrActorServiceProvider implements Actor {
 
  48     public static class Pair<A, B> {
 
  49         public final A result;
 
  50         public final B message;
 
  52         public Pair(A result, B message) {
 
  54             this.message = message;
 
  57         public A getResult() {
 
  61         public B getMessage() {
 
  66     private static final Logger logger = LoggerFactory.getLogger(SdnrActorServiceProvider.class);
 
  68     // Strings for targets
 
  69     private static final String TARGET_VNF = "VNF";
 
  71     // Strings for recipes
 
  72     private static final String RECIPE_MODIFY = "ModifyConfig";
 
  74     /* To be used in future releases when pci ModifyConfig is used */
 
  75     private static final String SDNR_REQUEST_PARAMS = "request-parameters";
 
  76     private static final String SDNR_CONFIG_PARAMS = "configuration-parameters";
 
  78     private static final ImmutableList<String> recipes = ImmutableList.of(RECIPE_MODIFY);
 
  79     private static final ImmutableMap<String, List<String>> targets = new ImmutableMap.Builder<String, List<String>>()
 
  80             .put(RECIPE_MODIFY, ImmutableList.of(TARGET_VNF)).build();
 
  81     private static final ImmutableMap<String, List<String>> payloads = new ImmutableMap.Builder<String, List<String>>()
 
  82             .put(RECIPE_MODIFY, ImmutableList.of(SDNR_REQUEST_PARAMS, SDNR_CONFIG_PARAMS)).build();
 
  85     public String actor() {
 
  90     public List<String> recipes() {
 
  91         return ImmutableList.copyOf(recipes);
 
  95     public List<String> recipeTargets(String recipe) {
 
  96         return ImmutableList.copyOf(targets.getOrDefault(recipe, Collections.emptyList()));
 
 100     public List<String> recipePayloads(String recipe) {
 
 101         return ImmutableList.copyOf(payloads.getOrDefault(recipe, Collections.emptyList()));
 
 105      * Constructs an SDNR request conforming to the pci API. The actual request is
 
 106      * constructed and then placed in a wrapper object used to send through DMAAP.
 
 109      *            the event that is reporting the alert for policy to perform an
 
 112      *            the control loop operation specifying the actor, operation,
 
 115      *            the policy the was specified from the yaml generated by CLAMP or
 
 116      *            through the Policy GUI/API
 
 117      * @return an SDNR request conforming to the pci API using the DMAAP wrapper
 
 120     public static PciRequestWrapper constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation,
 
 123         /* Construct an SDNR request using pci Model */
 
 126          * The actual pci request is placed in a wrapper used to send through dmaap. The
 
 127          * current version is 2.0 as of R1.
 
 129         PciRequestWrapper dmaapRequest = new PciRequestWrapper();
 
 130         dmaapRequest.setVersion("1.0");
 
 131         dmaapRequest.setCorrelationId(onset.getRequestId() + "-" + operation.getSubRequestId());
 
 132         dmaapRequest.setRpcName(policy.getRecipe().toLowerCase());
 
 133         dmaapRequest.setType("request");
 
 135         /* This is the actual request that is placed in the dmaap wrapper. */
 
 136         final PciRequest sdnrRequest = new PciRequest();
 
 138         /* The common header is a required field for all SDNR requests. */
 
 139         PciCommonHeader requestCommonHeader = new PciCommonHeader();
 
 140         requestCommonHeader.setRequestId(onset.getRequestId());
 
 141         requestCommonHeader.setSubRequestId(operation.getSubRequestId());
 
 143         sdnrRequest.setCommonHeader(requestCommonHeader);
 
 144         sdnrRequest.setPayload(onset.getPayload());
 
 147          * An action is required for all SDNR requests, this will be the recipe
 
 148          * specified in the policy.
 
 150         sdnrRequest.setAction(policy.getRecipe());
 
 153          * Once the pci request is constructed, add it into the body of the dmaap
 
 156         dmaapRequest.setBody(sdnrRequest);
 
 157         logger.info("SDNR Request to be sent is {}", dmaapRequest);
 
 159         /* Return the request to be sent through dmaap. */
 
 164      * Parses the operation attempt using the subRequestId of SDNR response.
 
 166      * @param subRequestId
 
 167      *            the sub id used to send to SDNR, Policy sets this using the
 
 170      * @return the current operation attempt
 
 172     public static Integer parseOperationAttempt(String subRequestId) {
 
 173         Integer operationAttempt;
 
 175             operationAttempt = Integer.parseInt(subRequestId);
 
 176         } catch (NumberFormatException e) {
 
 177             logger.debug("A NumberFormatException was thrown in parsing the operation attempt {}", subRequestId);
 
 180         return operationAttempt;
 
 184      * Processes the SDNR pci response sent from SDNR. Determines if the SDNR
 
 185      * operation was successful/unsuccessful and maps this to the corresponding
 
 188      * @param dmaapResponse
 
 189      *            the dmaap wrapper message that contains the actual SDNR reponse
 
 190      *            inside the body field
 
 192      * @return an key-value pair that contains the Policy result and SDNR response
 
 195     public static SdnrActorServiceProvider.Pair<PolicyResult, String> processResponse(
 
 196             PciResponseWrapper dmaapResponse) {
 
 198         logger.info("SDNR processResponse called : {}", dmaapResponse);
 
 200         /* The actual SDNR response is inside the wrapper's body field. */
 
 201         PciResponse sdnrResponse = dmaapResponse.getBody();
 
 203         /* The message returned in the SDNR response. */
 
 206         /* The Policy result determined from the SDNR Response. */
 
 210          * If there is no status, Policy cannot determine if the request was successful.
 
 212         if (sdnrResponse.getStatus() == null) {
 
 213             message = "Policy was unable to parse SDN-R response status field (it was null).";
 
 214             return new SdnrActorServiceProvider.Pair<>(PolicyResult.FAILURE_EXCEPTION, message);
 
 218          * If there is no code, Policy cannot determine if the request was successful.
 
 220         String responseValue = PciResponseCode.toResponseValue(sdnrResponse.getStatus().getCode());
 
 221         if (responseValue == null) {
 
 222             message = "Policy was unable to parse SDN-R response status code field.";
 
 223             return new SdnrActorServiceProvider.Pair<>(PolicyResult.FAILURE_EXCEPTION, message);
 
 225         logger.info("SDNR Response Code is {}", responseValue);
 
 227         /* Save the SDNR response's message for Policy notification message. */
 
 228         message = sdnrResponse.getStatus().getValue();
 
 229         logger.info("SDNR Response Message is {}", message);
 
 232          * Response and Payload are just printed and no further action needed in
 
 235         String rspPayload = sdnrResponse.getPayload();
 
 236         logger.info("SDNR Response Payload is {}", rspPayload);
 
 238         /* Maps the SDNR response result to a Policy result. */
 
 239         switch (responseValue) {
 
 240             case PciResponseCode.ACCEPTED:
 
 241                 /* Nothing to do if code is accept, continue processing */
 
 244             case PciResponseCode.SUCCESS:
 
 245                 result = PolicyResult.SUCCESS;
 
 247             case PciResponseCode.FAILURE:
 
 248                 result = PolicyResult.FAILURE;
 
 250             case PciResponseCode.REJECT:
 
 251             case PciResponseCode.ERROR:
 
 253                 result = PolicyResult.FAILURE_EXCEPTION;
 
 255         return new SdnrActorServiceProvider.Pair<>(result, message);
 
 259      * Converts the SDNR response to ControlLoopResponse object.
 
 261      * @param dmaapResponse
 
 262      *            the dmaap wrapper message that contains the actual SDNR reponse
 
 263      *            inside the body field
 
 265      * @return a ControlLoopResponse object to send to DCAE_CL_RSP topic
 
 267     public static ControlLoopResponse getControlLoopResponse(PciResponseWrapper dmaapResponse,
 
 268             VirtualControlLoopEvent event) {
 
 270         logger.info("SDNR getClosedLoopResponse called : {} {}", dmaapResponse, event);
 
 272         /* The actual SDNR response is inside the wrapper's body field. */
 
 273         PciResponse sdnrResponse = dmaapResponse.getBody();
 
 275         /* The ControlLoop response determined from the SDNR Response and input event. */
 
 276         ControlLoopResponse clRsp = new ControlLoopResponse();
 
 277         clRsp.setPayload(sdnrResponse.getPayload());
 
 278         clRsp.setFrom("SDNR");
 
 279         clRsp.setTarget("DCAE");
 
 280         clRsp.setClosedLoopControlName(event.getClosedLoopControlName());
 
 281         clRsp.setPolicyName(event.getPolicyName());
 
 282         clRsp.setPolicyVersion(event.getPolicyVersion());
 
 283         clRsp.setRequestId(event.getRequestId());
 
 284         clRsp.setVersion(event.getVersion());
 
 285         logger.info("SDNR getClosedLoopResponse clRsp : {}", clRsp);