2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
 
   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.openecomp.appc.listener.LCM.operation;
 
  24 import com.fasterxml.jackson.databind.JsonNode;
 
  25 import org.json.JSONObject;
 
  26 import org.openecomp.appc.exceptions.APPCException;
 
  27 import org.openecomp.appc.listener.LCM.model.InputBody;
 
  28 import org.openecomp.appc.listener.LCM.model.ResponseStatus;
 
  29 import org.openecomp.appc.listener.util.Mapper;
 
  31 import com.att.eelf.configuration.EELFLogger;
 
  32 import com.att.eelf.configuration.EELFManager;
 
  38 public class GenericProviderOperationRequestFormatter implements ProviderOperationRequestFormatter {
 
  40     private final EELFLogger LOG = EELFManager.getInstance().getLogger(GenericProviderOperationRequestFormatter.class);
 
  43     @SuppressWarnings("nls")
 
  44     private final static String TEMPLATE = "{\"input\": %s}";
 
  48     public String buildPath(URL url, String rpcName) {
 
  49         return url.getPath() + ":"+rpcName;
 
  52 //    private String convertActionToUrl(String action) {
 
  55 //            case "SoftwareUpload":
 
  56 //                res = "software-upload";
 
  58 //            case "LiveUpgrade":
 
  59 //                res = "live-upgrade";
 
  61 //            case "HealthCheck":
 
  62 //                res = "health-check";
 
  65 //                res = "check-lock";
 
  70 //        return res.toLowerCase();
 
  74     public String buildRequest(InputBody msg) {
 
  75         JSONObject jsonObject = Mapper.toJsonObject(msg);
 
  76         return String.format(TEMPLATE, jsonObject.toString());
 
  80     public OperationStatus getOperationStatus(JSONObject responseBody) throws APPCException {
 
  82             JSONObject status = responseBody.getJSONObject("output").getJSONObject("status");
 
  83             return new OperationStatus(String.valueOf(status.getInt("code")), status.getString("message"));
 
  84         } catch (Exception e) {
 
  85             LOG.error("Unknown error processing failed response from provider. Json not in expected format", e);
 
  86             throw new APPCException("APPC has an unknown RPC error");
 
  91     public ResponseStatus getResponseStatus(JsonNode responseBody)throws APPCException{
 
  93             JsonNode status = responseBody.get("output").get("status");
 
  94             return new ResponseStatus(status.get("code").asInt(), status.get("message").asText());
 
  95         } catch (Exception e) {
 
  96             LOG.error("Unknown error processing failed response from provider. Json not in expected format", e);
 
  97             throw new APPCException("APPC has an unknown RPC error");
 
 102     public String getLocked(JSONObject responseBody) throws APPCException {
 
 104             JSONObject outputObject=responseBody.getJSONObject("output");
 
 105             if(outputObject.has("locked")){
 
 106                 return outputObject.getString("locked");
 
 110         } catch (Exception e) {
 
 111             LOG.error("Unknown error processing failed response from provider. Json not in expected format", e);
 
 112             throw new APPCException("APPC has an unknown RPC error");