2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2020 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
 
  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.sdnc;
 
  23 import java.util.HashMap;
 
  25 import java.util.concurrent.CompletableFuture;
 
  26 import javax.ws.rs.client.Entity;
 
  27 import javax.ws.rs.core.MediaType;
 
  28 import javax.ws.rs.core.Response;
 
  29 import org.onap.policy.common.endpoints.http.client.HttpClient;
 
  30 import org.onap.policy.common.utils.coder.CoderException;
 
  31 import org.onap.policy.common.utils.coder.StandardCoder;
 
  32 import org.onap.policy.controlloop.actorserviceprovider.AsyncResponseHandler;
 
  33 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
 
  34 import org.onap.policy.controlloop.actorserviceprovider.Util;
 
  35 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
 
  36 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperator;
 
  37 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
 
  38 import org.onap.policy.controlloop.policy.PolicyResult;
 
  39 import org.onap.policy.sdnc.SdncRequest;
 
  40 import org.onap.policy.sdnc.SdncResponse;
 
  41 import org.slf4j.Logger;
 
  42 import org.slf4j.LoggerFactory;
 
  45  * Superclass for SDNC Operators.
 
  47 public abstract class SdncOperator extends HttpOperator {
 
  48     private static final Logger logger = LoggerFactory.getLogger(SdncOperator.class);
 
  51      * Constructs the object.
 
  53      * @param actorName name of the actor with which this operator is associated
 
  54      * @param name operation name
 
  56     public SdncOperator(String actorName, String name) {
 
  57         super(actorName, name);
 
  61     protected CompletableFuture<OperationOutcome> startOperationAsync(ControlLoopOperationParams params, int attempt,
 
  62                     OperationOutcome outcome) {
 
  64         SdncRequest request = constructRequest(params.getContext());
 
  65         return postRequest(params, outcome, request);
 
  69      * Constructs the request.
 
  71      * @param context associated event context
 
  72      * @return a new request
 
  74     protected abstract SdncRequest constructRequest(ControlLoopEventContext context);
 
  77      * Posts the request and and arranges to retrieve the response.
 
  79      * @param params operation parameters
 
  80      * @param outcome updated with the response
 
  81      * @param sdncRequest request to be posted
 
  82      * @return the result of the request
 
  84     private CompletableFuture<OperationOutcome> postRequest(ControlLoopOperationParams params, OperationOutcome outcome,
 
  85                     SdncRequest sdncRequest) {
 
  86         Map<String, Object> headers = new HashMap<>();
 
  88         headers.put("Accept", "application/json");
 
  89         String sdncUrl = getClient().getBaseUrl();
 
  91         Util.logRestRequest(sdncUrl, sdncRequest);
 
  93         Entity<SdncRequest> entity = Entity.entity(sdncRequest, MediaType.APPLICATION_JSON);
 
  95         ResponseHandler handler = new ResponseHandler(params, outcome, sdncUrl);
 
  96         return handler.handle(getClient().post(handler, getPath(), entity, headers));
 
  99     private class ResponseHandler extends AsyncResponseHandler<Response> {
 
 100         private final String sdncUrl;
 
 102         public ResponseHandler(ControlLoopOperationParams params, OperationOutcome outcome, String sdncUrl) {
 
 103             super(params, outcome);
 
 104             this.sdncUrl = sdncUrl;
 
 108          * Handles the response.
 
 111         protected OperationOutcome doComplete(Response rawResponse) {
 
 112             String strResponse = HttpClient.getBody(rawResponse, String.class);
 
 114             Util.logRestResponse(sdncUrl, strResponse);
 
 116             SdncResponse response;
 
 118                 response = makeDecoder().decode(strResponse, SdncResponse.class);
 
 119             } catch (CoderException e) {
 
 120                 logger.warn("Sdnc Heal cannot decode response with http error code {}", rawResponse.getStatus(), e);
 
 121                 return SdncOperator.this.setOutcome(getParams(), getOutcome(), PolicyResult.FAILURE_EXCEPTION);
 
 124             if (response.getResponseOutput() != null && "200".equals(response.getResponseOutput().getResponseCode())) {
 
 125                 return SdncOperator.this.setOutcome(getParams(), getOutcome(), PolicyResult.SUCCESS);
 
 128                 logger.info("Sdnc Heal Restcall failed with http error code {}", rawResponse.getStatus());
 
 129                 return SdncOperator.this.setOutcome(getParams(), getOutcome(), PolicyResult.FAILURE);
 
 134          * Handles exceptions.
 
 137         protected OperationOutcome doFailed(Throwable thrown) {
 
 138             logger.info("Sdnc Heal Restcall threw an exception", thrown);
 
 139             return SdncOperator.this.setOutcome(getParams(), getOutcome(), PolicyResult.FAILURE_EXCEPTION);
 
 143     // these may be overridden by junit tests
 
 145     protected StandardCoder makeDecoder() {
 
 146         return new StandardCoder();