[MSO-8] Update the maven dependency
[so.git] / mso-api-handlers / mso-api-handler-infra / src / main / java / org / openecomp / mso / apihandlerinfra / VfModuleModelNamesHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.mso.apihandlerinfra;
22
23
24 import java.io.StringWriter;
25 import java.util.List;
26
27 import javax.ws.rs.GET;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.core.Response;
30 import javax.xml.bind.JAXBContext;
31 import javax.xml.bind.JAXBException;
32 import javax.xml.bind.Marshaller;
33
34 import org.apache.http.HttpStatus;
35
36 import org.openecomp.mso.apihandlerinfra.vnfbeans.VfModuleModelName;
37 import org.openecomp.mso.apihandlerinfra.vnfbeans.VfModuleModelNames;
38 import org.openecomp.mso.apihandlerinfra.vnfbeans.ObjectFactory;
39 import org.openecomp.mso.db.catalog.CatalogDatabase;
40 import org.openecomp.mso.db.catalog.beans.VfModule;
41 import org.openecomp.mso.logger.MsoLogger;
42
43 @Path(Constants.VF_MODULE_MODEL_NAMES_PATH)
44 public class VfModuleModelNamesHandler {
45
46     private static MsoLogger msoLogger = MsoLogger.getMsoLogger (MsoLogger.Catalog.APIH);
47     private static final String LOG_SERVICE_NAME = "InfrastructurePortal:MSO-APIH.";
48     private static final String LOG_REPLY_NAME = "MSO-APIH:InfrastructurePortal.";
49
50     @GET
51     public Response getVfModuleModelNames () {
52         long startTime = System.currentTimeMillis ();
53         String methodName = "getVfModuleModelNames";
54         MsoLogger.setServiceName (LOG_SERVICE_NAME + methodName);
55         msoLogger.debug ("Incoming request received for vfModuleModelNames");
56         List <VfModule> vfModules = null;
57         try (CatalogDatabase db = new CatalogDatabase()){
58             vfModules = db.getAllVfModules ();
59         } catch (Exception e) {
60             msoLogger.debug ("No connection to catalog DB", e);
61             msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.DBAccessError, "no connection to catalog DB");
62             msoLogger.debug ("End of the transaction, the final response is: " + e.toString ());
63             return Response.status (HttpStatus.SC_NOT_FOUND).entity (e.toString ()).build ();
64         }
65
66         if (vfModules == null) {
67                 msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.DataNotFound, "VfModule not found");
68             msoLogger.debug ("End of the transaction. VfModuleModelName not found the final response status: " + HttpStatus.SC_NOT_FOUND);
69             return Response.status (HttpStatus.SC_NOT_FOUND).entity ("").build ();
70         }
71
72         ObjectFactory beansObjectFactory = new ObjectFactory ();
73         VfModuleModelNames vfModuleModelNames = beansObjectFactory.createVfModuleModelNames ();
74         for (int i = 0; i < vfModules.size (); i++) {
75             VfModuleModelName vfModuleModelName = beansObjectFactory.createVfModuleModelName ();
76             VfModule vm = vfModules.get (i);
77             vfModuleModelName.setModelName (vm.getModelName ());
78             vfModuleModelName.setModelVersion (vm.getVersion ());
79             vfModuleModelName.setModelInvariantUuid (vm.getModelInvariantUuid ());
80             vfModuleModelName.setIsBase(vm.isBase());
81             vfModuleModelName.setDescription (vm.getDescription ());
82             vfModuleModelName.setId (String.valueOf (vm.getId ()));
83             vfModuleModelName.setAsdcServiceModelVersion(vm.getVersion ());
84             vfModuleModelNames.getVfModuleModelName ().add (vfModuleModelName);
85         }
86
87         StringWriter stringWriter = new StringWriter ();
88         try {
89             JAXBContext jaxbContext = JAXBContext.newInstance (VfModuleModelNames.class);
90             Marshaller jaxbMarshaller = jaxbContext.createMarshaller ();
91
92             jaxbMarshaller.setProperty (Marshaller.JAXB_FORMATTED_OUTPUT, true);
93             jaxbMarshaller.marshal (vfModuleModelNames, stringWriter);
94
95         } catch (JAXBException e) {
96             msoLogger.debug ("Error marshalling", e);
97         }
98
99         String response = stringWriter.toString ();
100         msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successful");
101         msoLogger.debug ("End of the transaction, the final response is: " + response);
102         return Response.status (HttpStatus.SC_OK).entity (response).build ();
103     }
104 }