2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.sdc.listener;
26 import org.openecomp.sdc.utils.DistributionStatusEnum;
28 import org.json.JSONException;
29 import org.json.JSONObject;
30 import org.onap.appc.exceptions.APPCException;
31 import org.openecomp.sdc.api.IDistributionClient;
32 import org.openecomp.sdc.api.consumer.IDistributionStatusMessage;
33 import org.openecomp.sdc.api.notification.IArtifactInfo;
34 import org.openecomp.sdc.api.notification.INotificationData;
35 import org.openecomp.sdc.api.notification.IResourceInstance;
36 import org.openecomp.sdc.utils.DistributionStatusEnum;
40 // TODO - Use the yang builder instead
41 public static String toSdcStoreDocumentInput(INotificationData notification, IResourceInstance resource,
42 IArtifactInfo artifact, String data) {
43 JSONObject json = new JSONObject();
45 JSONObject requestInfo = new JSONObject();
46 requestInfo.put("request-id", notification.getServiceUUID());
47 requestInfo.put("request-action", "StoreSdcDocumentRequest");
48 requestInfo.put("source", "SDC");
50 JSONObject docParams = new JSONObject();
51 docParams.put("service-uuid", notification.getServiceUUID());
52 docParams.put("distribution-id", notification.getDistributionID());
53 docParams.put("service-name", notification.getServiceName());
54 docParams.put("service-description", notification.getServiceDescription());
55 docParams.put("service-artifacts", "[]");
56 docParams.put("resource-uuid", resource.getResourceUUID());
57 docParams.put("resource-instance-name", resource.getResourceInstanceName());
58 docParams.put("resource-name", resource.getResourceName());
59 docParams.put("resource-version", resource.getResourceVersion());
60 docParams.put("resource-type", resource.getResourceType());
61 docParams.put("artifact-uuid", artifact.getArtifactUUID());
62 docParams.put("artifact-name", artifact.getArtifactName());
63 docParams.put("artifact-type", artifact.getArtifactType());
64 docParams.put("artifact-version", artifact.getArtifactVersion());
65 docParams.put("artifact-description", artifact.getArtifactDescription());
66 docParams.put("artifact-contents", data);
68 json.put("request-information", requestInfo);
69 json.put("document-parameters", docParams);
71 return String.format("{\"input\": %s}", json.toString());
74 public static boolean parseResponse(String input) throws APPCException {
75 JSONObject result, output, response;
77 result = new JSONObject(input);
78 output = result.getJSONObject("output");
79 response = output.getJSONObject("config-document-response");
80 String id = response.getString("request-id");
81 String status = response.getString("status");
82 if (status.equals(DistributionStatusEnum.DEPLOY_OK.toString())) {
85 String error = response.optString("error-reason");
86 String msg = error.isEmpty() ? "No Reason Provided" : error;
87 throw new APPCException(msg);
89 } catch (JSONException jse) {
90 throw new APPCException("Did not get valid json from provider.", jse);
94 public static IDistributionStatusMessage buildDistributionStatusMessage(final IDistributionClient client,
95 final INotificationData data, final IArtifactInfo relevantArtifact, final DistributionStatusEnum status) {
96 IDistributionStatusMessage statusMessage = new IDistributionStatusMessage() {
99 public long getTimestamp() {
100 long currentTimeMillis = System.currentTimeMillis();
101 return currentTimeMillis;
105 public DistributionStatusEnum getStatus() {
110 public String getDistributionID() {
111 return data.getDistributionID();
115 public String getConsumerID() {
116 return client.getConfiguration().getConsumerID();
120 public String getArtifactURL() {
121 return relevantArtifact.getArtifactURL();
124 return statusMessage;