Change nexus values to properties
[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
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;
33
34 public class Util {
35
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();
40
41         JSONObject requestInfo = new JSONObject();
42         requestInfo.put("request-id", notification.getServiceUUID());
43         requestInfo.put("request-action", "StoreAsdcDocumentRequest");
44         requestInfo.put("source", "ASDC");
45
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);
63
64         json.put("request-information", requestInfo);
65         json.put("document-parameters", docParams);
66
67         return String.format("{\"input\": %s}", json.toString());
68     }
69
70     public static boolean parseResponse(String input) throws APPCException {
71         JSONObject result, output, response;
72         try {
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())) {
79                 return true;
80             } else {
81                 String error = response.optString("error-reason");
82                 String msg = error.isEmpty() ? "No Reason Provided" : error;
83                 throw new APPCException(msg);
84             }
85         } catch (JSONException jse) {
86             throw new APPCException("Did not get valid json from provider.", jse);
87         }
88     }
89
90     public static IDistributionStatusMessage buildDistributionStatusMessage(final IDistributionClient client,
91         final INotificationData data, final IArtifactInfo relevantArtifact, final DistributionStatusEnum status) {
92         IDistributionStatusMessage statusMessage = new IDistributionStatusMessage() {
93
94             @Override
95             public long getTimestamp() {
96                 long currentTimeMillis = System.currentTimeMillis();
97                 return currentTimeMillis;
98             }
99
100             @Override
101             public DistributionStatusEnum getStatus() {
102                 return status;
103             }
104
105             @Override
106             public String getDistributionID() {
107                 return data.getDistributionID();
108             }
109
110             @Override
111             public String getConsumerID() {
112                 return client.getConfiguration().getConsumerID();
113             }
114
115             @Override
116             public String getArtifactURL() {
117                 return relevantArtifact.getArtifactURL();
118             }
119         };
120         return statusMessage;
121     }
122 }