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