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