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