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