Merge of new rebased code
[appc.git] / appc-asdc-listener / appc-asdc-listener-bundle / src / main / java / org / openecomp / appc / sdc / listener / Util.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  */
21
22 package org.openecomp.appc.sdc.listener;
23 import org.openecomp.sdc.utils.DistributionStatusEnum;
24
25 import org.json.JSONException;
26 import org.json.JSONObject;
27 import org.openecomp.appc.exceptions.APPCException;
28 import org.openecomp.sdc.api.IDistributionClient;
29 import org.openecomp.sdc.api.consumer.IDistributionStatusMessage;
30 import org.openecomp.sdc.api.notification.IArtifactInfo;
31 import org.openecomp.sdc.api.notification.INotificationData;
32 import org.openecomp.sdc.api.notification.IResourceInstance;
33 import org.openecomp.sdc.utils.DistributionStatusEnum;
34
35 public class Util {
36
37     // TODO - Use the yang builder instead
38     public static String toAsdcStoreDocumentInput(INotificationData notification, IResourceInstance resource,
39         IArtifactInfo artifact, String data) {
40         JSONObject json = new JSONObject();
41
42         JSONObject requestInfo = new JSONObject();
43         requestInfo.put("request-id", notification.getServiceUUID());
44         requestInfo.put("request-action", "StoreAsdcDocumentRequest");
45         requestInfo.put("source", "ASDC");
46
47         JSONObject docParams = new JSONObject();
48         docParams.put("service-uuid", notification.getServiceUUID());
49         docParams.put("distribution-id", notification.getDistributionID());
50         docParams.put("service-name", notification.getServiceName());
51         docParams.put("service-description", notification.getServiceDescription());
52         docParams.put("service-artifacts", "[]");
53         docParams.put("resource-uuid", resource.getResourceUUID());
54         docParams.put("resource-instance-name", resource.getResourceInstanceName());
55         docParams.put("resource-name", resource.getResourceName());
56         docParams.put("resource-version", resource.getResourceVersion());
57         docParams.put("resource-type", resource.getResourceType());
58         docParams.put("artifact-uuid", artifact.getArtifactUUID());
59         docParams.put("artifact-name", artifact.getArtifactName());
60         docParams.put("artifact-type", artifact.getArtifactType());
61         docParams.put("artifact-version", artifact.getArtifactVersion());
62         docParams.put("artifact-description", artifact.getArtifactDescription());
63         docParams.put("artifact-contents", data);
64
65         json.put("request-information", requestInfo);
66         json.put("document-parameters", docParams);
67
68         return String.format("{\"input\": %s}", json.toString());
69     }
70
71     public static boolean parseResponse(String input) throws APPCException {
72         JSONObject result, output, response;
73         try {
74             result = new JSONObject(input);
75             output = result.getJSONObject("output");
76             response = output.getJSONObject("config-document-response");
77             String id = response.getString("request-id");
78             String status = response.getString("status");
79             if (status.equals(DistributionStatusEnum.DEPLOY_OK.toString())) {
80                 return true;
81             } else {
82                 String error = response.optString("error-reason");
83                 String msg = error.isEmpty() ? "No Reason Provided" : error;
84                 throw new APPCException(msg);
85             }
86         } catch (JSONException jse) {
87             throw new APPCException("Did not get valid json from provider.", jse);
88         }
89     }
90
91     public static IDistributionStatusMessage buildDistributionStatusMessage(final IDistributionClient client,
92         final INotificationData data, final IArtifactInfo relevantArtifact, final DistributionStatusEnum status) {
93         IDistributionStatusMessage statusMessage = new IDistributionStatusMessage() {
94
95             @Override
96             public long getTimestamp() {
97                 long currentTimeMillis = System.currentTimeMillis();
98                 return currentTimeMillis;
99             }
100
101             @Override
102             public DistributionStatusEnum getStatus() {
103                 return status;
104             }
105
106             @Override
107             public String getDistributionID() {
108                 return data.getDistributionID();
109             }
110
111             @Override
112             public String getConsumerID() {
113                 return client.getConfiguration().getConsumerID();
114             }
115
116             @Override
117             public String getArtifactURL() {
118                 return relevantArtifact.getArtifactURL();
119             }
120         };
121         return statusMessage;
122     }
123 }