91b6e88b0f324688ab2b908443da8ba41270c626
[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(AsdcArtifactHandlerConstants.REQUETS_ID, "TLRID-" + random);
98         requestInfo.put(AsdcArtifactHandlerConstants.REQUEST_ACTION, "StoreAsdcDocumentRequest");
99         requestInfo.put(AsdcArtifactHandlerConstants.SOURCE, "TemplateLoader");
100     
101         JSONObject docParams = new JSONObject();
102         docParams.put(AsdcArtifactHandlerConstants.SERVICE_UUID, "TLSUUID" + templateData.getRequestInformation().getRequestId());
103         docParams.put(AsdcArtifactHandlerConstants.DISTRIBUTION_ID, "TLDID" + random);
104         docParams.put(AsdcArtifactHandlerConstants.SERVICE_NAME, "TLServiceName");
105         docParams.put(AsdcArtifactHandlerConstants.SERVICE_DESCRIPTION, "Template Loader Test");
106         docParams.put(AsdcArtifactHandlerConstants.SERVICE_ARTIFACTS, "[]");
107         docParams.put(AsdcArtifactHandlerConstants.RESOURCE_UUID, "TLRUID" + random);
108         docParams.put(AsdcArtifactHandlerConstants.RESOURCE_INSTANCE_NAME, "TLRIName");
109         docParams.put(AsdcArtifactHandlerConstants.REOURCE_NAME, "TLResourceName");
110         docParams.put(AsdcArtifactHandlerConstants.RESOURCE_VERSOIN, "TLResourceVersion");
111         docParams.put(AsdcArtifactHandlerConstants.RESOURCE_TYPE, "TLResourceType");
112         docParams.put(AsdcArtifactHandlerConstants.ARTIFACT_UUID, "TLAUUID" +  random);
113         docParams.put(AsdcArtifactHandlerConstants.ARTIFACT_NAME, templateData.getDocumentParameters().getArtifactName());
114         docParams.put(AsdcArtifactHandlerConstants.ARTIFACT_TYPE, "APPC-CONFIG");
115         docParams.put(AsdcArtifactHandlerConstants.ARTIFACT_VERSOIN, templateData.getDocumentParameters().getArtifactVersion());
116         docParams.put(AsdcArtifactHandlerConstants.ARTIFACT_DESRIPTION, "AsdcTestDescription");
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(AsdcArtifactHandlerConstants.REQUEST_INFORMATION, requestInfo);
124         json.put(AsdcArtifactHandlerConstants.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(AsdcArtifactHandlerConstants.REQUETS_ID, templateData.getRequestInformation().getRequestId());
155         requestInfo.put(AsdcArtifactHandlerConstants.REQUEST_ACTION, "StoreAsdcDocumentRequest");
156         requestInfo.put(AsdcArtifactHandlerConstants.SOURCE, templateData.getRequestInformation().getSource());
157     
158         JSONObject docParams = new JSONObject();
159         docParams.put(AsdcArtifactHandlerConstants.SERVICE_UUID, templateData.getDocumentParameters().getResourceUuid());
160         docParams.put(AsdcArtifactHandlerConstants.DISTRIBUTION_ID, templateData.getDocumentParameters().getDistributionId());
161         docParams.put(AsdcArtifactHandlerConstants.SERVICE_NAME, templateData.getDocumentParameters().getServiceName());
162         docParams.put(AsdcArtifactHandlerConstants.SERVICE_DESCRIPTION, templateData.getDocumentParameters().getServiceDescription());
163         docParams.put(AsdcArtifactHandlerConstants.SERVICE_ARTIFACTS, templateData.getDocumentParameters().getServiceArtifacts());
164         docParams.put(AsdcArtifactHandlerConstants.RESOURCE_UUID, templateData.getDocumentParameters().getResourceUuid());
165         docParams.put(AsdcArtifactHandlerConstants.RESOURCE_INSTANCE_NAME, templateData.getDocumentParameters().getResourceInstanceName());
166         docParams.put(AsdcArtifactHandlerConstants.REOURCE_NAME, templateData.getDocumentParameters().getResourceName());
167         docParams.put(AsdcArtifactHandlerConstants.RESOURCE_VERSOIN, templateData.getDocumentParameters().getResourceVersion());
168         docParams.put(AsdcArtifactHandlerConstants.RESOURCE_TYPE, templateData.getDocumentParameters().getResourceType());
169         docParams.put(AsdcArtifactHandlerConstants.ARTIFACT_UUID, templateData.getDocumentParameters().getArtifactUuid());
170         docParams.put(AsdcArtifactHandlerConstants.ARTIFACT_NAME, templateData.getDocumentParameters().getArtifactName());
171         docParams.put(AsdcArtifactHandlerConstants.ARTIFACT_TYPE, templateData.getDocumentParameters().getArtifactType());
172         docParams.put(AsdcArtifactHandlerConstants.ARTIFACT_VERSOIN, templateData.getDocumentParameters().getArtifactVersion());
173         docParams.put(AsdcArtifactHandlerConstants.ARTIFACT_DESRIPTION, templateData.getDocumentParameters().getArtifactDescription());
174
175         docParams.put("artifact-contents", templateData.getDocumentParameters().getArtifactContents());
176     
177         json.put(AsdcArtifactHandlerConstants.REQUEST_INFORMATION, requestInfo);
178         json.put(AsdcArtifactHandlerConstants.DOCUMENT_PARAMETERS, docParams);
179         System.out.println("Final data ="  + this.templateData);
180         return String.format("{\"input\": %s}", json.toString());
181     }
182     
183
184 }