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