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.sdc.listener;
 
  24 import org.json.JSONException;
 
  25 import org.json.JSONObject;
 
  26 import org.openecomp.appc.exceptions.APPCException;
 
  27 import org.openecomp.sdc.api.IDistributionClient;
 
  28 import org.openecomp.sdc.api.consumer.IDistributionStatusMessage;
 
  29 import org.openecomp.sdc.api.notification.IArtifactInfo;
 
  30 import org.openecomp.sdc.api.notification.INotificationData;
 
  31 import org.openecomp.sdc.api.notification.IResourceInstance;
 
  32 import org.openecomp.sdc.utils.DistributionStatusEnum;
 
  36     // TODO - Use the yang builder instead
 
  37     public static String toAsdcStoreDocumentInput(INotificationData notification, IResourceInstance resource,
 
  38         IArtifactInfo artifact, String data) {
 
  39         JSONObject json = new JSONObject();
 
  41         JSONObject requestInfo = new JSONObject();
 
  42         requestInfo.put("request-id", notification.getServiceUUID());
 
  43         requestInfo.put("request-action", "StoreAsdcDocumentRequest");
 
  44         requestInfo.put("source", "ASDC");
 
  46         JSONObject docParams = new JSONObject();
 
  47         docParams.put("service-uuid", notification.getServiceUUID());
 
  48         docParams.put("distribution-id", notification.getDistributionID());
 
  49         docParams.put("service-name", notification.getServiceName());
 
  50         docParams.put("service-description", notification.getServiceDescription());
 
  51         docParams.put("service-artifacts", "[]");
 
  52         docParams.put("resource-uuid", resource.getResourceUUID());
 
  53         docParams.put("resource-instance-name", resource.getResourceInstanceName());
 
  54         docParams.put("resource-name", resource.getResourceName());
 
  55         docParams.put("resource-version", resource.getResourceVersion());
 
  56         docParams.put("resource-type", resource.getResourceType());
 
  57         docParams.put("artifact-uuid", artifact.getArtifactUUID());
 
  58         docParams.put("artifact-name", artifact.getArtifactName());
 
  59         docParams.put("artifact-type", artifact.getArtifactType());
 
  60         docParams.put("artifact-version", artifact.getArtifactVersion());
 
  61         docParams.put("artifact-description", artifact.getArtifactDescription());
 
  62         docParams.put("artifact-contents", data);
 
  64         json.put("request-information", requestInfo);
 
  65         json.put("document-parameters", docParams);
 
  67         return String.format("{\"input\": %s}", json.toString());
 
  70     public static boolean parseResponse(String input) throws APPCException {
 
  71         JSONObject result, output, response;
 
  73             result = new JSONObject(input);
 
  74             output = result.getJSONObject("output");
 
  75             response = output.getJSONObject("config-document-response");
 
  76             String id = response.getString("request-id");
 
  77             String status = response.getString("status");
 
  78             if (status.equals(DistributionStatusEnum.DEPLOY_OK.toString())) {
 
  81                 String error = response.optString("error-reason");
 
  82                 String msg = error.isEmpty() ? "No Reason Provided" : error;
 
  83                 throw new APPCException(msg);
 
  85         } catch (JSONException jse) {
 
  86             throw new APPCException("Did not get valid json from provider.", jse);
 
  90     public static IDistributionStatusMessage buildDistributionStatusMessage(final IDistributionClient client,
 
  91         final INotificationData data, final IArtifactInfo relevantArtifact, final DistributionStatusEnum status) {
 
  92         IDistributionStatusMessage statusMessage = new IDistributionStatusMessage() {
 
  95             public long getTimestamp() {
 
  96                 long currentTimeMillis = System.currentTimeMillis();
 
  97                 return currentTimeMillis;
 
 101             public DistributionStatusEnum getStatus() {
 
 106             public String getDistributionID() {
 
 107                 return data.getDistributionID();
 
 111             public String getConsumerID() {
 
 112                 return client.getConfiguration().getConsumerID();
 
 116             public String getArtifactURL() {
 
 117                 return relevantArtifact.getArtifactURL();
 
 120         return statusMessage;