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
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  21 package org.onap.policy.controlloop.actor.sdnr;
 
  23 import com.google.common.collect.ImmutableList;
 
  24 import com.google.common.collect.ImmutableMap;
 
  26 import java.util.Collections;
 
  27 import java.util.List;
 
  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;
 
  41 import org.slf4j.Logger;
 
  42 import org.slf4j.LoggerFactory;
 
  44 public class SdnrActorServiceProvider implements Actor {
 
  46     public static class Pair<A, B> {
 
  47         public final A result;
 
  48         public final B message;
 
  50         public Pair(A result, B message) {
 
  52             this.message = message;
 
  55         public A getResult() {
 
  59         public B getMessage() {
 
  64     private static final Logger logger = LoggerFactory.getLogger(SdnrActorServiceProvider.class);
 
  66     // Strings for targets
 
  67     private static final String TARGET_VNF = "VNF";
 
  69     // Strings for recipes
 
  70     private static final String RECIPE_MODIFY = "ModifyConfig";
 
  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";
 
  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();
 
  83     public String actor() {
 
  88     public List<String> recipes() {
 
  89         return ImmutableList.copyOf(recipes);
 
  93     public List<String> recipeTargets(String recipe) {
 
  94         return ImmutableList.copyOf(targets.getOrDefault(recipe, Collections.emptyList()));
 
  98     public List<String> recipePayloads(String recipe) {
 
  99         return ImmutableList.copyOf(payloads.getOrDefault(recipe, Collections.emptyList()));
 
 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.
 
 107      *            the event that is reporting the alert for policy to perform an
 
 110      *            the control loop operation specifying the actor, operation,
 
 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
 
 118     public static PciRequestWrapper constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation,
 
 121         /* Construct an SDNR request using pci Model */
 
 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.
 
 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");
 
 133         /* This is the actual request that is placed in the dmaap wrapper. */
 
 134         final PciRequest sdnrRequest = new PciRequest();
 
 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());
 
 141         sdnrRequest.setCommonHeader(requestCommonHeader);
 
 142         sdnrRequest.setPayload(onset.getPayload());
 
 145          * An action is required for all SDNR requests, this will be the recipe
 
 146          * specified in the policy.
 
 148         sdnrRequest.setAction(policy.getRecipe());
 
 151          * Once the pci request is constructed, add it into the body of the dmaap
 
 154         dmaapRequest.setBody(sdnrRequest);
 
 155         logger.info("SDNR Request to be sent is {}", dmaapRequest);
 
 157         /* Return the request to be sent through dmaap. */
 
 162      * Parses the operation attempt using the subRequestId of SDNR response.
 
 164      * @param subRequestId
 
 165      *            the sub id used to send to SDNR, Policy sets this using the
 
 168      * @return the current operation attempt
 
 170     public static Integer parseOperationAttempt(String subRequestId) {
 
 171         Integer operationAttempt;
 
 173             operationAttempt = Integer.parseInt(subRequestId);
 
 174         } catch (NumberFormatException e) {
 
 175             logger.debug("A NumberFormatException was thrown in parsing the operation attempt {}", subRequestId);
 
 178         return operationAttempt;
 
 182      * Processes the SDNR pci response sent from SDNR. Determines if the SDNR
 
 183      * operation was successful/unsuccessful and maps this to the corresponding
 
 186      * @param dmaapResponse
 
 187      *            the dmaap wrapper message that contains the actual SDNR reponse
 
 188      *            inside the body field
 
 190      * @return an key-value pair that contains the Policy result and SDNR response
 
 193     public static SdnrActorServiceProvider.Pair<PolicyResult, String> processResponse(
 
 194             PciResponseWrapper dmaapResponse) {
 
 196         logger.info("SDNR processResponse called : {}", dmaapResponse);
 
 198         /* The actual SDNR response is inside the wrapper's body field. */
 
 199         PciResponse sdnrResponse = dmaapResponse.getBody();
 
 201         /* The message returned in the SDNR response. */
 
 204         /* The Policy result determined from the SDNR Response. */
 
 208          * If there is no status, Policy cannot determine if the request was successful.
 
 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);
 
 216          * If there is no code, Policy cannot determine if the request was successful.
 
 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);
 
 223         logger.info("SDNR Response Code is {}", responseValue);
 
 225         /* Save the SDNR response's message for Policy notification message. */
 
 226         message = sdnrResponse.getStatus().getValue();
 
 227         logger.info("SDNR Response Message is {}", message);
 
 230          * Response and Payload are just printed and no further action needed in
 
 233         String rspPayload = sdnrResponse.getPayload();
 
 234         logger.info("SDNR Response Payload is {}", rspPayload);
 
 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 */
 
 242             case PciResponseCode.SUCCESS:
 
 243                 result = PolicyResult.SUCCESS;
 
 245             case PciResponseCode.FAILURE:
 
 246                 result = PolicyResult.FAILURE;
 
 248             case PciResponseCode.REJECT:
 
 249             case PciResponseCode.ERROR:
 
 251                 result = PolicyResult.FAILURE_EXCEPTION;
 
 253         return new SdnrActorServiceProvider.Pair<>(result, message);