Change the header to SO
[so.git] / mso-api-handlers / mso-api-handler-infra / src / main / java / org / openecomp / mso / apihandlerinfra / VfModuleModelNamesHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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.PathParam;
30 import javax.ws.rs.core.Response;
31 import javax.xml.bind.JAXBContext;
32 import javax.xml.bind.JAXBException;
33 import javax.xml.bind.Marshaller;
34
35 import org.apache.http.HttpStatus;
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 import com.wordnik.swagger.annotations.Api;
43 import com.wordnik.swagger.annotations.ApiOperation;
44
45 @Path(Constants.VF_MODULE_MODEL_NAMES_PATH)
46 @Api(value="/{version: v2|v3}/vf-module-model-names",description="API Requests to find Vf Module model names")
47 public class VfModuleModelNamesHandler {
48
49     private static MsoLogger msoLogger = MsoLogger.getMsoLogger (MsoLogger.Catalog.APIH);
50     private static final String LOG_SERVICE_NAME = "InfrastructurePortal:MSO-APIH.";
51     private static final String LOG_REPLY_NAME = "MSO-APIH:InfrastructurePortal.";
52
53     @GET
54     @ApiOperation(value="Finds Vf Module Model Names",response=Response.class)
55     public Response getVfModuleModelNames (@PathParam("version") String version) {
56         long startTime = System.currentTimeMillis ();
57         String methodName = "getVfModuleModelNames";
58         MsoLogger.setServiceName (LOG_SERVICE_NAME + methodName);
59         msoLogger.debug ("Incoming request received for vfModuleModelNames");
60         List <VfModule> vfModules = null;
61         try (CatalogDatabase db = CatalogDatabase.getInstance()){
62             vfModules = db.getAllVfModules ();
63         } catch (Exception e) {
64             msoLogger.debug ("No connection to catalog DB", e);
65             msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.DBAccessError, "no connection to catalog DB");
66             msoLogger.debug ("End of the transaction, the final response is: " + e.toString ());
67             return Response.status (HttpStatus.SC_NOT_FOUND).entity (e.toString ()).build ();
68         }
69
70         if (vfModules == null) {
71                 msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.DataNotFound, "VfModule not found");
72             msoLogger.debug ("End of the transaction. VfModuleModelName not found the final response status: " + HttpStatus.SC_NOT_FOUND);
73             return Response.status (HttpStatus.SC_NOT_FOUND).entity ("").build ();
74         }
75
76         ObjectFactory beansObjectFactory = new ObjectFactory ();
77         VfModuleModelNames vfModuleModelNames = beansObjectFactory.createVfModuleModelNames ();
78         for (int i = 0; i < vfModules.size (); i++) {
79             VfModuleModelName vfModuleModelName = beansObjectFactory.createVfModuleModelName ();
80             VfModule vm = vfModules.get (i);
81             vfModuleModelName.setModelName (vm.getModelName ());
82             vfModuleModelName.setModelVersion (vm.getVersion ());
83             vfModuleModelName.setModelInvariantUuid (vm.getModelInvariantUuid ());
84             vfModuleModelName.setIsBase(vm.isBase());
85             vfModuleModelName.setDescription (vm.getDescription ());
86             vfModuleModelName.setId (String.valueOf (vm.getModelUUID()));
87             vfModuleModelName.setAsdcServiceModelVersion(vm.getVersion ());
88             vfModuleModelNames.getVfModuleModelName ().add (vfModuleModelName);
89         }
90
91         StringWriter stringWriter = new StringWriter ();
92         try {
93             JAXBContext jaxbContext = JAXBContext.newInstance (VfModuleModelNames.class);
94             Marshaller jaxbMarshaller = jaxbContext.createMarshaller ();
95
96             jaxbMarshaller.setProperty (Marshaller.JAXB_FORMATTED_OUTPUT, true);
97             jaxbMarshaller.marshal (vfModuleModelNames, stringWriter);
98
99         } catch (JAXBException e) {
100             msoLogger.debug ("Error marshalling", e);
101         }
102
103         String response = stringWriter.toString ();
104         msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successful");
105         msoLogger.debug ("End of the transaction, the final response is: " + response);
106         return Response.status (HttpStatus.SC_OK).entity (response).build ();
107     }
108 }