bc4b4a896e9643f4eb9c5f78911d75011ee7a9c0
[appc.git] / appc-sdc-listener / appc-sdc-listener-bundle / src / main / java / org / onap / appc / sdc / listener / Util.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.sdc.listener;
26 import org.openecomp.sdc.utils.DistributionStatusEnum;
27
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;
37
38 public class Util {
39
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();
44
45         JSONObject requestInfo = new JSONObject();
46         requestInfo.put("request-id", notification.getServiceUUID());
47         requestInfo.put("request-action", "StoreSdcDocumentRequest");
48         requestInfo.put("source", "SDC");
49
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);
67
68         json.put("request-information", requestInfo);
69         json.put("document-parameters", docParams);
70
71         return String.format("{\"input\": %s}", json.toString());
72     }
73
74     public static boolean parseResponse(String input) throws APPCException {
75         JSONObject result, output, response;
76         try {
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())) {
83                 return true;
84             } else {
85                 String error = response.optString("error-reason");
86                 String msg = error.isEmpty() ? "No Reason Provided" : error;
87                 throw new APPCException(msg);
88             }
89         } catch (JSONException jse) {
90             throw new APPCException("Did not get valid json from provider.", jse);
91         }
92     }
93
94     public static IDistributionStatusMessage buildDistributionStatusMessage(final IDistributionClient client,
95         final INotificationData data, final IArtifactInfo relevantArtifact, final DistributionStatusEnum status) {
96         IDistributionStatusMessage statusMessage = new IDistributionStatusMessage() {
97
98             @Override
99             public long getTimestamp() {
100                 long currentTimeMillis = System.currentTimeMillis();
101                 return currentTimeMillis;
102             }
103
104             @Override
105             public DistributionStatusEnum getStatus() {
106                 return status;
107             }
108
109             @Override
110             public String getDistributionID() {
111                 return data.getDistributionID();
112             }
113
114             @Override
115             public String getConsumerID() {
116                 return client.getConfiguration().getConsumerID();
117             }
118
119             @Override
120             public String getArtifactURL() {
121                 return relevantArtifact.getArtifactURL();
122             }
123         };
124         return statusMessage;
125     }
126 }