Changes for design service
[appc.git] / appc-inbound / appc-design-services / provider / src / main / java / org / onap / appc / design / dbervices / DesignDBService.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  * Modifications (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.design.dbervices;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import com.fasterxml.jackson.core.JsonParser;
30 import com.fasterxml.jackson.databind.JsonNode;
31 import com.fasterxml.jackson.databind.ObjectMapper;
32 import java.sql.ResultSet;
33 import java.sql.SQLException;
34 import java.util.ArrayList;
35 import java.util.List;
36
37 import org.json.JSONObject;
38 import org.onap.appc.design.data.ArtifactInfo;
39 import org.onap.appc.design.data.DesignInfo;
40 import org.onap.appc.design.data.DesignResponse;
41 import org.onap.appc.design.data.StatusInfo;
42 import org.onap.appc.design.services.util.ArtifactHandlerClient;
43 import org.onap.appc.design.services.util.DesignServiceConstants;
44 import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;
45 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
46 import org.apache.commons.lang.StringUtils;
47
48 public class DesignDBService {
49
50     private static final EELFLogger log = EELFManager.getInstance().getLogger(DesignDBService.class);
51     private static DesignDBService dgGeneralDBService;
52
53     private static final String SUCCESS_JSON = "{\"update\" : \"success\" } ";
54     private static final String STATUS = "STATUS";
55     private static final String INFO_STR = "Info : ";
56     private static final String DB_OPERATION_ERROR = "Error while DB operation : ";
57     private static final String VNFC_TYPE = "vnfc-type";
58     private static final String QUERY_STR = "Query String :";
59     private static final String USER_ID = "userID";
60
61     private SvcLogicResource serviceLogic;
62     private DbService dbservice;
63     private static ArtifactHandlerFactory artifactHandlerFactory = new ArtifactHandlerFactory();
64
65     public static DesignDBService initialise() {
66         if (dgGeneralDBService == null) {
67             dgGeneralDBService = new DesignDBService();
68         }
69         return dgGeneralDBService;
70     }
71
72     private DesignDBService() {
73         if (serviceLogic == null) {
74             serviceLogic = new SqlResource();
75         }
76     }
77
78     public String execute(String action, String payload, String requestID) throws Exception {
79
80         log.info("Received execute request for action : " + action + "  with Payload : " + payload);
81         RequestValidator.validate(action, payload);
82         String response;
83         dbservice = new DbService();
84         switch (action) {
85             case DesignServiceConstants.GETDESIGNS:
86                 response = getDesigns(payload, requestID);
87                 break;
88             case DesignServiceConstants.GETAPPCTIMESTAMPUTC:
89                 response =  getAppcTimestampUTC( requestID );
90                 break;
91             case DesignServiceConstants.ADDINCART:
92                 response = setInCart(payload, requestID);
93                 break;
94             case DesignServiceConstants.GETARTIFACTREFERENCE:
95                 response = getArtifactReference(payload, requestID);
96                 break;
97             case DesignServiceConstants.GETARTIFACT:
98                 response = getArtifact(payload, requestID);
99                 break;
100             case DesignServiceConstants.GETGUIREFERENCE:
101                 response = getGuiReference(payload, requestID);
102                 break;
103             case DesignServiceConstants.GETSTATUS:
104                 response = getStatus(payload, requestID);
105                 break;
106             case DesignServiceConstants.SETSTATUS:
107                 response = setStatus(payload, requestID);
108                 break;
109             case DesignServiceConstants.UPLOADARTIFACT:
110                 response = uploadArtifact(payload, requestID);
111                 break;
112             case DesignServiceConstants.SETPROTOCOLREFERENCE:
113                 response = setProtocolReference(payload, requestID);
114                 break;
115             case DesignServiceConstants.UPLOADADMINARTIFACT:
116                 response = uploadAdminArtifact(payload, requestID);
117                 break;
118             case DesignServiceConstants.CHECKVNF:
119                 response = checkVNF(payload, requestID);
120                 break;
121             default:
122                 throw new DBException(" Action " + action + " not found while processing request ");
123
124         }
125         return response;
126     }
127
128     private String checkVNF(String payload, String requestId) throws Exception {
129
130         log.info("Got into Check VNF Request with payload: " + payload);
131         if (StringUtils.isBlank(payload))
132             throw new DBException("Payload in CheckVNF request is null or Blank");
133         if (StringUtils.isBlank(requestId))
134             throw new DBException("requestId in CheckVNF request is null or Blank");
135         ObjectMapper objectMapper = new ObjectMapper();
136         JsonNode payloadObject = objectMapper.readTree(payload);
137         String vnfType = payloadObject.get("vnf-type").textValue();
138         log.info("Check VNF Request with VNF TYPE: " + vnfType);
139         ArrayList<String> argList = new ArrayList<>();
140         argList.add(vnfType);
141         String queryString = "SELECT DT_ACTION_STATUS_ID,USER FROM sdnctl.DT_ACTION_STATUS WHERE VNF_TYPE = ? ORDER BY DT_ACTION_STATUS_ID DESC LIMIT 1 ; ";
142         log.info(QUERY_STR + queryString);
143         ResultSet data = dbservice.getDBData(queryString, argList);
144         int rowCount = 0;
145         String user = null;
146         String dtActionStatusId = null;
147         while (data.next()) {
148             rowCount++;
149             user = data.getString("USER");
150             dtActionStatusId = data.getString("DT_ACTION_STATUS_ID");
151         }
152         log.debug("DT_ACTION_STATUS_ID-> " + dtActionStatusId + " user-> " + user);
153         JSONObject jObject = new JSONObject();
154         if (rowCount == 0) {
155             log.debug("vnf-type does not present in APPC DB, row Count:" + rowCount);
156             jObject.put("result", "No");
157         } else {
158             log.debug("vnf-type present in APPC DB, row Count:" + rowCount);
159             jObject.put("result", "Yes");
160             jObject.put("user", user);
161         }
162         log.info("Check VNF result: " + jObject.toString());
163
164         return jObject.toString();
165
166     }
167
168     private String uploadAdminArtifact(String payload, String requestId) throws Exception {
169
170         ObjectMapper objectMapper = new ObjectMapper();
171         objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
172         JsonNode payloadObject = objectMapper.readTree(payload);
173         log.info("Got upload Admin Aritfact with  requestId : " + requestId + " & Payload" + payloadObject.asText());
174
175         if (StringUtils.isBlank(requestId)) {
176             throw new DBException("Request-id is missing in the uploadAdminArtifact payload . ");
177         }
178
179         ArtifactHandlerClient ac = new ArtifactHandlerClient();
180         String requestString = ac.createArtifactData(payload, requestId);
181         ac.execute(requestString, "POST");
182
183         int sdcArtifactId = getSDCArtifactIDbyRequestID(requestId);
184         if (sdcArtifactId == 0)
185             throw new DBException(
186                     "Error occured while validating/Saving the artifact to SDC_ARTIFACTS or getting SDC_ARTIFACTS_ID .");
187         JsonNode json = payloadObject.get(DesignServiceConstants.USER_ID);
188         if (json == null || json.asText().trim().isEmpty()) {
189             log.info("UserId in Admin Aritfact is null or blank, User Id : " + json.asText());
190             throw new DBException("User Id is null or blank");
191         }
192
193         int sdcReferenceId = 0;
194         createArtifactTrackingRecord(payload, requestId, sdcArtifactId, sdcReferenceId);
195
196         return SUCCESS_JSON;
197     }
198
199     private String getAppcTimestampUTC( String requestID) throws Exception
200     {
201       log.info("Starting getAppcTimestampUTC: requestID:" + requestID );
202       java.util.TimeZone gmtTZ= java.util.TimeZone.getTimeZone("GMT");
203       java.text.SimpleDateFormat formatter =
204         new java.text.SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" );
205       formatter.setTimeZone( gmtTZ );
206       java.util.Date dateVal= new java.util.Date();
207       log.info("getAppcTimestampUTC: current local Date:[" + dateVal+ "]");
208       String timeStr= formatter.format( dateVal );
209       log.info("getAppcTimestampUTC: returning:[" + timeStr + "]");
210       return timeStr;
211     }
212
213     private String setInCart(String payload, String requestID) throws Exception {
214
215         ObjectMapper objectMapper = new ObjectMapper();
216         JsonNode payloadObject = objectMapper.readTree(payload);
217         ArrayList<String> argList = new ArrayList<>();
218         argList.add(payloadObject.get(DesignServiceConstants.INCART).textValue());
219         argList.add(payloadObject.get(DesignServiceConstants.VNF_TYPE).textValue());
220
221         String queryString = "UPDATE DT_ARTIFACT_TRACKING SET INCART= ? WHERE ASDC_REFERENCE_ID  IN "
222             + " (SELECT ASDC_REFERENCE_ID FROM ASDC_REFERENCE_ID WHERE VNF_TYPE = ? ";
223
224         if (payloadObject.get(DesignServiceConstants.VNF_TYPE) != null && !payloadObject
225             .get(DesignServiceConstants.VNF_TYPE).textValue().isEmpty()) {
226             queryString = queryString + "  AND VNFC_TYPE = ? ) AND USER = ? ";
227             argList.add(payloadObject.get(DesignServiceConstants.VNFC_TYPE).textValue());
228         } else {
229             queryString = queryString + "  ) AND USER = ? ";
230         }
231         argList.add(payloadObject.get(DesignServiceConstants.USER_ID).textValue());
232         log.info(QUERY_STR + queryString);
233         boolean data = dbservice.updateDBData(queryString, argList);
234
235         if (!data) {
236             throw new DBException("Error while updating ProtocolReference");
237         }
238         return SUCCESS_JSON;
239     }
240
241     private String setProtocolReference(String payload, String requestID) throws Exception {
242
243         ObjectMapper objectMapper = new ObjectMapper();
244         JsonNode payloadObject = objectMapper.readTree(payload);
245         ArrayList<String> argList = new ArrayList<>();
246
247         argList.add(payloadObject.get(DesignServiceConstants.ACTION).textValue());
248         argList.add(payloadObject.get(DesignServiceConstants.ACTION_LEVEL).textValue());
249         argList.add(payloadObject.get(DesignServiceConstants.VNF_TYPE).textValue());
250         argList.add(payloadObject.get(DesignServiceConstants.PROTOCOL).textValue());
251
252         String queryString = " DELETE FROM PROTOCOL_REFERENCE WHERE ACTION = ? AND ACTION_LEVEL AND VNF_TYPE= ?  AND PROTOCOL = ? ";
253
254         log.info("Delete Query String :" + queryString);
255         boolean data;
256
257         log.info("Record Deleted");
258
259         if (payloadObject.get(DesignServiceConstants.TEMPLATE) != null &&
260             !payloadObject.get(DesignServiceConstants.TEMPLATE).textValue().isEmpty()) {
261
262             argList.add(payloadObject.get(DesignServiceConstants.TEMPLATE).textValue());
263         } else {
264             argList.add("NO");
265         }
266
267         if (payloadObject.get(DesignServiceConstants.VNFC_TYPE) != null &&
268             !payloadObject.get(DesignServiceConstants.VNFC_TYPE).textValue().isEmpty()) {
269
270             queryString = queryString + " AND  VNFC_TYPE =  ? )";
271         } else {
272             queryString = queryString + " ) ";
273         }
274         log.info(QUERY_STR + queryString);
275         data = dbservice.updateDBData(queryString, argList);
276
277         if (!data) {
278             throw new DBException("Error while updating ProtocolReference");
279         }
280         return SUCCESS_JSON;
281     }
282
283     private String uploadArtifact(String payload, String requestID) throws Exception {
284
285         ObjectMapper objectMapper = new ObjectMapper();
286         objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
287         JsonNode payloadObject = objectMapper.readTree(payload);
288         log.info("Got upload Aritfact with Payload : " + payloadObject.asText());
289         try {
290             ArtifactHandlerClient ac = artifactHandlerFactory.ahi();
291             String requestString = ac.createArtifactData(payload, requestID);
292             ac.execute(requestString, "POST");
293             int sdcArtifactId = getSDCArtifactIDbyRequestID(requestID);
294             int sdcReferenceId = getSDCReferenceID(payload);
295             createArtifactTrackingRecord(payload, requestID, sdcArtifactId, sdcReferenceId);
296             String status = getDataFromActionStatus(payload, STATUS);
297             if (status == null || status.isEmpty()) {
298               log.info("Action Status is: "+ status);
299               setActionStatus(payload, "Not Tested");
300             }
301             linkstatusRelationShip(sdcArtifactId, sdcReferenceId, payload);
302
303         } catch (Exception e) {
304             log.error("An error occured in uploadArtifact", e);
305             throw e;
306         }
307         return SUCCESS_JSON;
308
309     }
310
311     private void linkstatusRelationShip(int sdcArtifactId, int sdcReferenceId, String payload) throws Exception {
312
313         ObjectMapper objectMapper = new ObjectMapper();
314         JsonNode payloadObject = objectMapper.readTree(payload);
315         ArrayList<String> argList = new ArrayList<>();
316         argList.add(String.valueOf(sdcArtifactId));
317         argList.add(String.valueOf(sdcReferenceId));
318         argList.add(payloadObject.get(DesignServiceConstants.VNF_TYPE).textValue());
319         argList.add(payloadObject.get(DesignServiceConstants.ACTION).textValue());
320         argList.add(payloadObject.get(DesignServiceConstants.USER_ID).textValue());
321
322         String queryString =
323             "INSERT INTO DT_STATUS_RELATIONSHIP (DT_ARTIFACT_TRACKING_ID,DT_ACTION_STATUS_ID) VALUES " +
324                 "(( SELECT DT_ARTIFACT_TRACKING_ID FROM DT_ARTIFACT_TRACKING WHERE ASDC_ARTIFACTS_ID = ? AND ASDC_REFERENCE_ID = ? ) , "
325                 + "( SELECT DT_ACTION_STATUS_ID FROM DT_ACTION_STATUS WHERE  VNF_TYPE = ? AND ACTION = ?  AND USER = ? ";
326
327         if (payloadObject.get(DesignServiceConstants.VNFC_TYPE) != null && !payloadObject
328             .get(DesignServiceConstants.VNFC_TYPE).textValue().isEmpty()) {
329             queryString = queryString + " AND  VNFC_TYPE =  ? GROUP BY VNF_TYPE HAVING COUNT(VNF_TYPE)>=1 ) )";
330         } else {
331             queryString = queryString + " GROUP BY VNF_TYPE HAVING COUNT(VNF_TYPE)>=1 ) ) ";
332         }
333         log.info(QUERY_STR + queryString);
334         boolean data = dbservice.updateDBData(queryString, argList);
335
336         if (!data) {
337             throw new DBException("Error while updating RelationShip table");
338         }
339
340     }
341
342     private int getSDCReferenceID(String payload) throws Exception {
343
344         ObjectMapper objectMapper = new ObjectMapper();
345         JsonNode payloadObject = objectMapper.readTree(payload);
346         ArrayList<String> argList = new ArrayList<>();
347         argList.add(payloadObject.get(DesignServiceConstants.VNF_TYPE).textValue());
348
349         argList.add(payloadObject.get(DesignServiceConstants.ARTIFACT_TYPE).textValue());
350         argList.add(payloadObject.get(DesignServiceConstants.ARTIFACT_NAME).textValue());
351
352         String queryString = " SELECT ASDC_REFERENCE_ID FROM ASDC_REFERENCE WHERE VNF_TYPE = ?  "
353             + " AND ARTIFACT_TYPE = ?  AND ARTIFACT_NAME = ? ";
354
355         if (payloadObject.get(DesignServiceConstants.ACTION) != null && !payloadObject
356             .get(DesignServiceConstants.ACTION).textValue().isEmpty()) {
357             argList.add(payloadObject.get(DesignServiceConstants.ACTION).textValue());
358             queryString = queryString + " AND ACTION = ? ";
359         }
360         if (payloadObject.get(DesignServiceConstants.VNFC_TYPE) != null && !payloadObject
361             .get(DesignServiceConstants.VNFC_TYPE).textValue().isEmpty()) {
362             argList.add(payloadObject.get(DesignServiceConstants.VNFC_TYPE).textValue());
363             queryString = queryString + " AND VNFC_TYPE = ? ";
364
365         }
366
367         log.info(QUERY_STR + queryString);
368         ResultSet data = dbservice.getDBData(queryString, argList);
369         int sdcReferenceId = 0;
370         while (data.next()) {
371             sdcReferenceId = data.getInt("ASDC_REFERENCE_ID");
372         }
373         log.info("Got sdcReferenceId= " + sdcReferenceId);
374         return sdcReferenceId;
375     }
376
377     private String getDataFromActionStatus(String payload, String dataValue) throws Exception {
378         String status = null;
379         ObjectMapper objectMapper = new ObjectMapper();
380         JsonNode payloadObject = objectMapper.readTree(payload);
381         ArrayList<String> argList = new ArrayList<>();
382         argList.add(payloadObject.get(DesignServiceConstants.VNF_TYPE).textValue());
383         argList.add(payloadObject.get(DesignServiceConstants.ACTION).textValue());
384         argList.add(payloadObject.get(DesignServiceConstants.USER_ID).textValue());
385         String queryString =
386             " SELECT " + dataValue + " FROM DT_ACTION_STATUS WHERE VNF_TYPE = ? AND ACTION = ? AND USER = ? ";
387         if (payloadObject.get(DesignServiceConstants.VNFC_TYPE) != null && !payloadObject
388             .get(DesignServiceConstants.VNFC_TYPE).textValue().isEmpty()) {
389             argList.add(payloadObject.get(DesignServiceConstants.VNFC_TYPE).textValue());
390             queryString = queryString + " AND VNFC_TYPE = ? ";
391         }
392         log.info(QUERY_STR + queryString);
393         ResultSet data = dbservice.getDBData(queryString, argList);
394         while (data.next()) {
395             status = data.getString(STATUS);
396         }
397         log.info("DT_ACTION_STATUS Status = " + status);
398         return status;
399     }
400
401     private void setActionStatus(String payload, String status) throws Exception {
402         ObjectMapper objectMapper = new ObjectMapper();
403         JsonNode payloadObject = objectMapper.readTree(payload);
404         ArrayList<String> argList = new ArrayList<>();
405         argList.add(payloadObject.get(DesignServiceConstants.ACTION).textValue());
406         argList.add(payloadObject.get(DesignServiceConstants.VNF_TYPE).textValue());
407
408         String insertQuery = " INSERT INTO DT_ACTION_STATUS (ACTION, VNF_TYPE, VNFC_TYPE, USER, TECHNOLOGY, UPDATED_DATE, STATUS) VALUES (?,?,?,?,?,sysdate() , ?); ";
409         if (payloadObject.get(DesignServiceConstants.VNFC_TYPE) != null && !payloadObject
410             .get(DesignServiceConstants.VNFC_TYPE).textValue().isEmpty()) {
411             argList.add(payloadObject.get(DesignServiceConstants.VNFC_TYPE).textValue());
412             log.info("Vnfc-Type: " + payloadObject.get(DesignServiceConstants.VNFC_TYPE).textValue());
413         } else {
414             argList.add(null);
415         }
416         argList.add(payloadObject.get(DesignServiceConstants.USER_ID).textValue());
417         if (payloadObject.get(DesignServiceConstants.TECHNOLOGY) != null && !payloadObject
418             .get(DesignServiceConstants.TECHNOLOGY).textValue().isEmpty()) {
419             argList.add(payloadObject.get(DesignServiceConstants.TECHNOLOGY).textValue());
420         } else {
421             argList.add(null);
422         }
423         argList.add(status);
424
425         log.info("QueryString: " + insertQuery);
426         log.info("Arguments List: " + argList);
427         boolean updateStatus = dbservice.updateDBData(insertQuery, argList);
428         if (!updateStatus)
429             throw new DBException("Error while updating Action Status");
430     }
431
432     private void createArtifactTrackingRecord(String payload, String requestID, int sdcArtifactId, int sdcReferenceId)
433         throws Exception {
434
435         ObjectMapper objectMapper = new ObjectMapper();
436         JsonNode payloadObject = objectMapper.readTree(payload);
437
438         ArrayList<String> argList = new ArrayList<>();
439         argList.add(String.valueOf(sdcArtifactId));
440         argList.add(String.valueOf(sdcReferenceId));
441         argList.add(payloadObject.get(DesignServiceConstants.USER_ID).textValue());
442         if (payloadObject.get(DesignServiceConstants.TECHNOLOGY) != null && !payloadObject
443             .get(DesignServiceConstants.TECHNOLOGY).textValue().isEmpty()) {
444             argList.add(payloadObject.get(DesignServiceConstants.TECHNOLOGY).textValue());
445         } else {
446             argList.add("");
447         }
448
449         if (payloadObject.get(DesignServiceConstants.PROTOCOL) != null && !payloadObject
450             .get(DesignServiceConstants.PROTOCOL).textValue().isEmpty()) {
451             argList.add(payloadObject.get(DesignServiceConstants.PROTOCOL).textValue());
452         } else {
453             argList.add("");
454         }
455
456         String queryString = "INSERT INTO DT_ARTIFACT_TRACKING (ASDC_ARTIFACTS_ID, ASDC_REFERENCE_ID, USER, TECHNOLOGY, CREATION_DATE, UPDATED_DATE, ARTIFACT_STATUS, PROTOCOL, IN_CART) VALUES (? , ? , ?, ?, sysdate() , sysdate(), 'Created',  ? ,'N' )";
457
458         log.info(QUERY_STR + queryString);
459         boolean data = dbservice.updateDBData(queryString, argList);
460         if (!data) {
461             throw new DBException("Error Updating DT_ARTIFACT_TRACKING ");
462         }
463     }
464
465     private int getSDCArtifactIDbyRequestID(String requestID) throws Exception {
466         log.info("Starting getArtifactIDbyRequestID DB Operation");
467         int artifactId = 0;
468         try {
469             ArrayList<String> argList = new ArrayList<>();
470             argList.add("TLSUUID" + requestID);
471             String queryString = " SELECT ASDC_ARTIFACTS_ID FROM ASDC_ARTIFACTS where SERVICE_UUID = ? ";
472             log.info(QUERY_STR + queryString+ " & UUID or" + "TLSUUID :" + requestID);
473             ResultSet data = dbservice.getDBData(queryString, argList);
474             while (data.next()) {
475                 artifactId = data.getInt("ASDC_ARTIFACTS_ID");
476             }
477         } catch (Exception e) {
478             log.error("An error occurred in getSDCArtifactIDbyRequestID", e);
479             throw e;
480         }
481         log.info("Got SDC_ARTIFACTS_ID As :" + artifactId);
482         return artifactId;
483     }
484
485
486     private String getArtifact(String payload, String requestID) throws Exception {
487         log.info("Starting getArtifact DB Operation");
488         try {
489             ObjectMapper objectMapper = new ObjectMapper();
490             JsonNode payloadObject = objectMapper.readTree(payload);
491             String artifactName = payloadObject.get("artifact-name").textValue();
492             ArrayList<String> argList = new ArrayList<>();
493             argList.add(artifactName);
494             argList.add(payloadObject.get("artifact-type").textValue());
495
496             String queryString = "SELECT INTERNAL_VERSION, ARTIFACT_CONTENT FROM ASDC_ARTIFACTS where "
497                     + " ARTIFACT_NAME = ? AND ARTIFACT_TYPE = ?  ";
498             log.info(QUERY_STR + queryString);
499             ResultSet data = dbservice.getDBData(queryString, argList);
500             String artifactContent = null;
501             int rowCount = 0;
502             int hightestVerion = -1;
503             while (data.next()) {
504                 rowCount++;
505                 int version = data.getInt("INTERNAL_VERSION");
506                 if (hightestVerion < version) {
507                     artifactContent = data.getString("ARTIFACT_CONTENT");
508                     hightestVerion = version;
509                 }
510             }
511             log.debug("No of rows: " + rowCount + " highest Inetrnal Version" + hightestVerion);
512             if (rowCount == 0) {
513                 throw new DBException("Sorry !!!APPC DB doesn't have any artifact Named : " + artifactName);
514             }
515             if (artifactContent == null || artifactContent.isEmpty()) {
516                 throw new DBException("Sorry !!! Artifact Content is stored blank in APPC DB for " + artifactName
517                         + " and Internal version " + hightestVerion);
518             }
519             DesignResponse designResponse = new DesignResponse();
520             List<ArtifactInfo> artifactInfoList = new ArrayList<>();
521             ArtifactInfo artifactInfo = new ArtifactInfo();
522             artifactInfo.setArtifact_content(artifactContent);
523             artifactInfoList.add(artifactInfo);
524             designResponse.setArtifactInfo(artifactInfoList);
525             ObjectMapper mapper = new ObjectMapper();
526             String jsonString = mapper.writeValueAsString(designResponse);
527             log.debug("End of getArtifact:" + INFO_STR + jsonString);
528             return jsonString;
529         } catch (Exception e) {
530             log.error(DB_OPERATION_ERROR, e);
531             throw e;
532         }
533     }
534
535     private String setStatus(String payload, String requestID) throws Exception {
536
537         log.info("Starting getStatus DB Operation");
538         try {
539             ObjectMapper objectMapper = new ObjectMapper();
540             JsonNode payloadObject = objectMapper.readTree(payload);
541
542             ArrayList<String> argList = new ArrayList<>();
543             argList.add(payloadObject.get("artifact_status").textValue());
544             argList.add(payloadObject.get("action_status").textValue());
545
546             argList.add(payloadObject.get(USER_ID).textValue());
547             argList.add(payloadObject.get("vnf-type").textValue());
548
549             String queryString =
550                 " UPDATE DT_ARTIFACT_TRACKING DAT, DT_STATUS_RELATIONSHIP DSR  SET DAT.ARTIFACT_STATUS = ? , DAS.DT_ACTION_STATUS = ? "
551                     + " where  DAT.USER = DAS.USER and DSR.DT_ARTIFACT_TRACKING_ID = DAT.DT_ARTIFACT_TRACKING_ID "
552                     + " and DSR.DT_ACTION_STATUS_ID = DAS.DT_ACTION_STATUS_ID and DAT.USER = ? "
553                     + " and  DAS.VNF_TYPE = ? ";
554
555             if (payloadObject.get(VNFC_TYPE) != null && !payloadObject.get(VNFC_TYPE).textValue().isEmpty()) {
556                 argList.add(payloadObject.get(VNFC_TYPE).textValue());
557                 queryString = queryString + " and DAS.VNFC_TYPE = ? ";
558             }
559
560             log.info(QUERY_STR + queryString);
561
562             DesignResponse designResponse = new DesignResponse();
563             designResponse.setUserId(payloadObject.get(USER_ID).textValue());
564             boolean update = dbservice.updateDBData(queryString, argList);
565             if (!update) {
566                 throw new DBException("Sorry .....Something went wrong while updating the Status");
567             }
568
569             ObjectMapper mapper = new ObjectMapper();
570             String jsonString = mapper.writeValueAsString(designResponse);
571             log.info(INFO_STR + jsonString);
572             return jsonString;
573         } catch (Exception e) {
574             log.error(DB_OPERATION_ERROR, e);
575             throw e;
576         }
577     }
578
579     private String getStatus(String payload, String requestID) throws Exception {
580         log.info("Starting getStatus DB Operation");
581         try {
582             String vnfcType = null;
583             ObjectMapper objectMapper = new ObjectMapper();
584             JsonNode payloadObject = objectMapper.readTree(payload);
585             String userID = payloadObject.get(USER_ID).textValue();
586             String vnfType = payloadObject.get("vnf-type").textValue();
587             if (payloadObject.get(VNFC_TYPE) != null) {
588                 vnfcType = payloadObject.get(VNFC_TYPE).textValue();
589             }
590             ArrayList<String> argList = new ArrayList<>();
591
592             argList.add(userID);
593             argList.add(vnfType);
594
595             String queryString = "SELECT DAS.VNF_TYPE, DAS.VNFC_TYPE,  DAS.STATUS, DAS.ACTION, DAT.ARTIFACT_STATUS "
596                 + "from  DT_ACTION_STATUS DAS , DT_ARTIFACT_TRACKING DAT, DT_STATUS_RELATIONSHIP DSR " +
597                 " where  DAT.USER = DAS.USER and DSR.DT_ARTIFACT_TRACKING_ID = DAT.DT_ARTIFACT_TRACKING_ID "
598                 + " and DSR.DT_ACTION_STATUS_ID = DAS.DT_ACTION_STATUS_ID and DAT.USER = ? "
599                 + " and  DAS.VNF_TYPE = ? ";
600
601             if (vnfcType != null && !vnfcType.isEmpty()) {
602                 argList.add(vnfcType);
603                 queryString = queryString + " and DAS.VNFC_TYPE = ? ";
604             }
605
606             log.info(QUERY_STR + queryString);
607
608             DesignResponse designResponse = new DesignResponse();
609             designResponse.setUserId(userID);
610             List<StatusInfo> statusInfoList = new ArrayList<>();
611             ResultSet data = dbservice.getDBData(queryString, argList);
612             while (data.next()) {
613                 StatusInfo statusInfo = new StatusInfo();
614                 statusInfo.setAction(data.getString("ACTION"));
615                 statusInfo.setAction_status(data.getString(STATUS));
616                 statusInfo.setArtifact_status(data.getString("ARTIFACT_STATUS"));
617                 statusInfo.setVnf_type(data.getString("VNF_TYPE"));
618                 statusInfo.setVnfc_type(data.getString("VNFC_TYPE"));
619                 statusInfoList.add(statusInfo);
620             }
621
622             if (statusInfoList.isEmpty()) {
623                 throw new DBException(
624                     "OOPS !!!! No VNF information available for VNF-TYPE : " + vnfType + " for User : " + userID);
625             }
626             designResponse.setStatusInfoList(statusInfoList);
627             ObjectMapper mapper = new ObjectMapper();
628             String jsonString = mapper.writeValueAsString(designResponse);
629             log.info(INFO_STR + jsonString);
630             return jsonString;
631         } catch (SQLException e) {
632             log.error(DB_OPERATION_ERROR, e);
633             throw e;
634         } catch (Exception e) {
635             log.error(DB_OPERATION_ERROR + e.getMessage());
636             log.error("Exception : ", e);
637             throw e;
638         }
639     }
640
641     private String getGuiReference(String payload, String requestID) {
642         // TODO Auto-generated method stub
643         return null;
644     }
645
646     private String getArtifactReference(String payload, String requestID) {
647         // TODO Auto-generated method stub
648         return null;
649     }
650
651     private String getDesigns(String payload, String requestID) throws Exception {
652
653         String queryString;
654         log.info("Starting getDesigns DB Operation");
655
656         try {
657             ObjectMapper objectMapper = new ObjectMapper();
658             JsonNode payloadObject = objectMapper.readTree(payload);
659             String userID = payloadObject.get(USER_ID).textValue();
660             String filterKey = null;
661             if (payloadObject.hasNonNull("filter")) {
662                 filterKey = payloadObject.get("filter").textValue();
663             }
664             ArrayList<String> argList = new ArrayList<>();
665             argList.add(userID);
666
667             if (filterKey != null) {
668                 queryString =
669                     "SELECT AR.VNF_TYPE, AR.VNFC_TYPE,  DAT.PROTOCOL, DAT.IN_CART, AR.ACTION, AR.ARTIFACT_NAME, AR.ARTIFACT_TYPE from  "
670                         +
671                         DesignServiceConstants.DB_DT_ARTIFACT_TRACKING + " DAT , "
672                         + DesignServiceConstants.DB_SDC_REFERENCE +
673                         " AR where DAT.ASDC_REFERENCE_ID= AR.ASDC_REFERENCE_ID  and DAT.USER = ? and AR.ARTIFACT_NAME like '%"
674                         + filterKey + "%' GROUP BY AR.VNF_TYPE,AR.ARTIFACT_NAME";
675             } else {
676                 queryString =
677                     "SELECT AR.VNF_TYPE, AR.VNFC_TYPE,  DAT.PROTOCOL, DAT.IN_CART, AR.ACTION, AR.ARTIFACT_NAME, AR.ARTIFACT_TYPE from  "
678                         +
679                         DesignServiceConstants.DB_DT_ARTIFACT_TRACKING + " DAT , "
680                         + DesignServiceConstants.DB_SDC_REFERENCE +
681                         " AR where DAT.ASDC_REFERENCE_ID= AR.ASDC_REFERENCE_ID  and DAT.USER = ? GROUP BY AR.VNF_TYPE,AR.ARTIFACT_NAME";
682             }
683             DesignResponse designResponse = new DesignResponse();
684             designResponse.setUserId(userID);
685             List<DesignInfo> designInfoList = new ArrayList<>();
686             ResultSet data = dbservice.getDBData(queryString, argList);
687             while (data.next()) {
688                 DesignInfo designInfo = new DesignInfo();
689                 designInfo.setInCart(data.getString("IN_CART"));
690                 designInfo.setProtocol(data.getString("PROTOCOL"));
691                 designInfo.setVnf_type(data.getString("VNF_TYPE"));
692                 designInfo.setVnfc_type(data.getString("VNFC_TYPE"));
693                 designInfo.setAction(data.getString("ACTION"));
694                 designInfo.setArtifact_type(data.getString("ARTIFACT_TYPE"));
695                 designInfo.setArtifact_name(data.getString("ARTIFACT_NAME"));
696                 designInfoList.add(designInfo);
697             }
698             if (designInfoList.isEmpty()) {
699                 throw new DBException(
700                     " Welcome to CDT, Looks like you dont have Design Yet... Lets create some....");
701             }
702             designResponse.setDesignInfoList(designInfoList);
703             ObjectMapper mapper = new ObjectMapper();
704             String jsonString = mapper.writeValueAsString(designResponse);
705             log.info(INFO_STR + jsonString);
706             return jsonString;
707         } catch (Exception e) {
708             log.error("Error while Starting getDesgins DB operation : ", e);
709             throw e;
710         }
711     }
712
713     public static class ArtifactHandlerFactory {
714
715         public ArtifactHandlerClient ahi() throws Exception{
716             return new ArtifactHandlerClient();
717         }
718     }
719 }