add61543b09792e3660ca3dafa32f2f0a11696f8
[appc.git] / appc-inbound / appc-artifact-handler / provider / src / main / java / org / onap / appc / artifact / handler / utils / ArtifactHandlerProviderUtil.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.artifact.handler.utils;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import java.io.IOException;
30 import java.security.SecureRandom;
31 import java.util.HashMap;
32 import org.apache.commons.lang3.StringUtils;
33 import org.json.JSONObject;
34 import org.onap.appc.artifact.handler.node.ArtifactHandlerNode;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
36 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.UploadartifactInput;
37
38 public class ArtifactHandlerProviderUtil {
39
40     private static final EELFLogger log = EELFManager.getInstance().getLogger(ArtifactHandlerProviderUtil.class);
41     private UploadartifactInput templateData;
42     private SvcLogicContext context = null;
43
44     public ArtifactHandlerProviderUtil() { /*default constructor*/}
45
46     public ArtifactHandlerProviderUtil(UploadartifactInput input) {
47         this.templateData = input;
48         log.info("templateData " + this.templateData);
49     }
50
51     public static void loadProperties() {
52         // TODO Auto-generated method stub
53     }
54
55     public void processTemplate(String requestInfo) throws Exception {
56         if (context == null) {
57             context = new SvcLogicContext();
58         }
59
60         ArtifactHandlerNode node = new ArtifactHandlerNode();
61         try {
62
63             HashMap<String, String> processdata = new HashMap<>();
64             processdata.put("postData", requestInfo);
65             log.info("Post data = " + requestInfo);
66             node.processArtifact(processdata, context);
67         } catch (Exception e) {
68             log.error("Failed to process template", e);
69             throw e;
70         }
71     }
72
73     public String createDummyRequestData() throws IOException {
74
75         JSONObject info = new JSONObject(this.templateData);
76         log.info("INFO = " + info);
77
78         JSONObject json = new JSONObject();
79         JSONObject requestInfo = new JSONObject();
80         String random = getRandom();
81
82         requestInfo.put(SdcArtifactHandlerConstants.REQUEST_ID, "TLRID-" + random);
83         requestInfo.put(SdcArtifactHandlerConstants.REQUEST_ACTION, "StoreSdcDocumentRequest");
84         requestInfo.put(SdcArtifactHandlerConstants.SOURCE, "TemplateLoader");
85
86         JSONObject docParams = new JSONObject();
87         docParams.put(SdcArtifactHandlerConstants.SERVICE_UUID,
88             "TLSUUID" + templateData.getRequestInformation().getRequestId());
89         docParams.put(SdcArtifactHandlerConstants.DISTRIBUTION_ID, "TLDID" + random);
90         docParams.put(SdcArtifactHandlerConstants.SERVICE_NAME, "TLServiceName");
91         docParams.put(SdcArtifactHandlerConstants.SERVICE_DESCRIPTION, "Template Loader Test");
92         docParams.put(SdcArtifactHandlerConstants.SERVICE_ARTIFACTS, "[]");
93         docParams.put(SdcArtifactHandlerConstants.RESOURCE_UUID, "TLRUID" + random);
94         docParams.put(SdcArtifactHandlerConstants.RESOURCE_INSTANCE_NAME, "TLRIName");
95         docParams.put(SdcArtifactHandlerConstants.REOURCE_NAME, "TLResourceName");
96         docParams.put(SdcArtifactHandlerConstants.RESOURCE_VERSION, "TLResourceVersion");
97         docParams.put(SdcArtifactHandlerConstants.RESOURCE_TYPE, "TLResourceType");
98         docParams.put(SdcArtifactHandlerConstants.ARTIFACT_UUID, "TLAUUID" + random);
99         docParams
100             .put(SdcArtifactHandlerConstants.ARTIFACT_NAME, templateData.getDocumentParameters().getArtifactName());
101         docParams.put(SdcArtifactHandlerConstants.ARTIFACT_TYPE, "APPC-CONFIG");
102         docParams.put(SdcArtifactHandlerConstants.ARTIFACT_VERSION,
103             templateData.getDocumentParameters().getArtifactVersion());
104         docParams.put(SdcArtifactHandlerConstants.ARTIFACT_DESRIPTION, "SdcTestDescription");
105         docParams.put("artifact-contents", templateData.getDocumentParameters().getArtifactContents());
106         json.put(SdcArtifactHandlerConstants.REQUEST_INFORMATION, requestInfo);
107         json.put(SdcArtifactHandlerConstants.DOCUMENT_PARAMETERS, docParams);
108
109         log.info("Final data =" + this.templateData);
110         return String.format("{\"input\": %s}", json.toString());
111     }
112
113     private String getRandom() {
114         SecureRandom random = new SecureRandom();
115         int num = random.nextInt(100000);
116         return String.format("%05d", num);
117     }
118
119     public String escapeSql(String str) {
120         if (str == null) {
121             return null;
122         }
123         String[] searchList = new String[]{"'", "\\"};
124         String[] replacementList = new String[]{"''", "\\\\"};
125         return StringUtils.replaceEach(str, searchList, replacementList);
126     }
127
128     public String createRequestData() throws IOException {
129
130         JSONObject info = new JSONObject(templateData);
131         log.info("INFO = " + info);
132         JSONObject json = new JSONObject();
133         JSONObject requestInfo = new JSONObject();
134
135         requestInfo.put(SdcArtifactHandlerConstants.REQUEST_ID, templateData.getRequestInformation().getRequestId());
136         requestInfo.put(SdcArtifactHandlerConstants.REQUEST_ACTION, "StoreSdcDocumentRequest");
137         requestInfo.put(SdcArtifactHandlerConstants.SOURCE, templateData.getRequestInformation().getSource());
138
139         String serviceDescription = serviceDescriptionData(
140             templateData.getDocumentParameters().getServiceDescription());
141
142         JSONObject docParams = new JSONObject();
143         docParams.put(SdcArtifactHandlerConstants.SERVICE_UUID, templateData.getDocumentParameters().getResourceUuid());
144         docParams
145             .put(SdcArtifactHandlerConstants.DISTRIBUTION_ID, templateData.getDocumentParameters().getDistributionId());
146         docParams.put(SdcArtifactHandlerConstants.SERVICE_NAME, templateData.getDocumentParameters().getServiceName());
147         docParams.put(SdcArtifactHandlerConstants.SERVICE_DESCRIPTION, serviceDescription);
148         docParams.put(SdcArtifactHandlerConstants.SERVICE_ARTIFACTS,
149             templateData.getDocumentParameters().getServiceArtifacts());
150         docParams
151             .put(SdcArtifactHandlerConstants.RESOURCE_UUID, templateData.getDocumentParameters().getResourceUuid());
152         docParams.put(SdcArtifactHandlerConstants.RESOURCE_INSTANCE_NAME,
153             templateData.getDocumentParameters().getResourceInstanceName());
154         docParams.put(SdcArtifactHandlerConstants.REOURCE_NAME, templateData.getDocumentParameters().getResourceName());
155         docParams.put(SdcArtifactHandlerConstants.RESOURCE_VERSION,
156             templateData.getDocumentParameters().getResourceVersion());
157         docParams
158             .put(SdcArtifactHandlerConstants.RESOURCE_TYPE, templateData.getDocumentParameters().getResourceType());
159         docParams
160             .put(SdcArtifactHandlerConstants.ARTIFACT_UUID, templateData.getDocumentParameters().getArtifactUuid());
161         docParams
162             .put(SdcArtifactHandlerConstants.ARTIFACT_NAME, templateData.getDocumentParameters().getArtifactName());
163         docParams
164             .put(SdcArtifactHandlerConstants.ARTIFACT_TYPE, templateData.getDocumentParameters().getArtifactType());
165         docParams.put(SdcArtifactHandlerConstants.ARTIFACT_VERSION,
166             templateData.getDocumentParameters().getArtifactVersion());
167         docParams.put(SdcArtifactHandlerConstants.ARTIFACT_DESRIPTION,
168             templateData.getDocumentParameters().getArtifactDescription());
169
170         docParams.put("artifact-contents", templateData.getDocumentParameters().getArtifactContents());
171
172         json.put(SdcArtifactHandlerConstants.REQUEST_INFORMATION, requestInfo);
173         json.put(SdcArtifactHandlerConstants.DOCUMENT_PARAMETERS, docParams);
174         log.info("Final data =" + this.templateData);
175         return String.format("{\"input\": %s}", json.toString());
176     }
177
178     private String serviceDescriptionData(String serviceDescription) {
179         if (!StringUtils.isBlank(serviceDescription) && serviceDescription.length() > 255) {
180             return serviceDescription.substring(0, 255);
181         }
182         return serviceDescription;
183     }
184
185     public enum DistributionStatusEnum {
186         DOWNLOAD_OK,
187         DOWNLOAD_ERROR,
188         ALREADY_DOWNLOADED,
189         DEPLOY_OK,
190         DEPLOY_ERROR,
191         ALREADY_DEPLOYED;
192     }
193 }