32fd80d58a1d022f7ddb7d36da9c592454cfe72c
[so.git] / mso-api-handlers / mso-api-handler-infra / src / main / java / org / openecomp / mso / apihandlerinfra / VnfTypesHandler.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 org.openecomp.mso.apihandlerinfra.vnfbeans.ObjectFactory;
25 import org.openecomp.mso.apihandlerinfra.vnfbeans.VnfType;
26 import org.openecomp.mso.apihandlerinfra.vnfbeans.VnfTypes;
27 import org.openecomp.mso.db.catalog.CatalogDatabase;
28 import org.openecomp.mso.db.catalog.beans.VnfResource;
29 import org.openecomp.mso.logger.MsoLogger;
30 import org.openecomp.mso.utils.UUIDChecker;
31 import org.apache.http.HttpStatus;
32
33 import javax.ws.rs.GET;
34 import javax.ws.rs.Path;
35 import javax.ws.rs.QueryParam;
36 import javax.ws.rs.core.Response;
37 import javax.xml.bind.JAXBContext;
38 import javax.xml.bind.JAXBException;
39 import javax.xml.bind.Marshaller;
40 import java.io.StringWriter;
41 import java.util.List;
42
43 @Path(Constants.VNF_TYPES_PATH)
44 public class VnfTypesHandler {
45
46     private static MsoLogger msoLogger = MsoLogger.getMsoLogger (MsoLogger.Catalog.APIH);
47
48
49     @GET
50     public Response getVnfTypes (@QueryParam("vnf-role") String vnfRole) {
51
52         long startTime = System.currentTimeMillis ();
53         MsoLogger.setServiceName ("GetVnfTypes");
54         // Generate a Request Id
55         UUIDChecker.generateUUID(msoLogger);
56         msoLogger.debug ("Incoming request received for getVnfTypes with vnf-role:" + vnfRole);
57
58         List <VnfResource> vnfResources = null;
59         try(CatalogDatabase db = new CatalogDatabase ()) {
60             ;
61             if (vnfRole != null) {
62                 vnfResources = db.getVnfResourcesByRole (vnfRole);
63             } else {
64                 vnfResources = db.getAllVnfResources ();
65             }
66         } catch (Exception e) {
67             msoLogger.debug ("No connection to catalog DB", e);
68             msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.DBAccessError, "No connection to catalog DB");
69             msoLogger.debug ("End of the transaction, the final response is: " + e.toString ());
70             return Response.status (HttpStatus.SC_NOT_FOUND).entity (e.toString ()).build ();
71         }
72
73         if (vnfResources == null) {
74             msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.DataNotFound, "Error:no vnf types found");
75             msoLogger.debug ("End of the transaction. No VNF Types found. The final response status is: " + HttpStatus.SC_NOT_FOUND);
76             return Response.status (HttpStatus.SC_NOT_FOUND).entity ("").build ();
77         }
78
79         ObjectFactory beansObjectFactory = new ObjectFactory ();
80         VnfTypes vnfTypes = beansObjectFactory.createVnfTypes ();
81         for (int i = 0; i < vnfResources.size (); i++) {
82             VnfType vnfType = beansObjectFactory.createVnfType ();
83             VnfResource vr = vnfResources.get (i);
84             vnfType.setType (vr.getVnfType ());
85             vnfType.setDescription (vr.getDescription ());
86             vnfType.setId (String.valueOf (vr.getId ()));
87             vnfTypes.getVnfType ().add (vnfType);
88         }
89
90         StringWriter stringWriter = new StringWriter ();
91         try {
92             JAXBContext jaxbContext = JAXBContext.newInstance (VnfTypes.class);
93             Marshaller jaxbMarshaller = jaxbContext.createMarshaller ();
94
95             jaxbMarshaller.setProperty (Marshaller.JAXB_FORMATTED_OUTPUT, true);
96             jaxbMarshaller.marshal (vnfTypes, stringWriter);
97
98         } catch (JAXBException e) {
99             msoLogger.debug ("Error marshalling", e);
100         }
101
102         String response = stringWriter.toString ();
103         msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successful");
104         msoLogger.debug ("End of the transaction, the final response is: " + response);
105         return Response.status (HttpStatus.SC_OK).entity (response).build ();
106     }
107 }