2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.mso.apihandlerinfra;
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;
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;
43 @Path(Constants.VNF_TYPES_PATH)
44 public class VnfTypesHandler {
46 private static MsoLogger msoLogger = MsoLogger.getMsoLogger (MsoLogger.Catalog.APIH);
50 public Response getVnfTypes (@QueryParam("vnf-role") String vnfRole) {
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);
58 List <VnfResource> vnfResources = null;
59 try(CatalogDatabase db = new CatalogDatabase ()) {
60 if (vnfRole != null) {
61 vnfResources = db.getVnfResourcesByRole (vnfRole);
63 vnfResources = db.getAllVnfResources ();
65 } catch (Exception e) {
66 msoLogger.debug ("No connection to catalog DB", e);
67 msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.DBAccessError, "No connection to catalog DB");
68 msoLogger.debug ("End of the transaction, the final response is: " + e.toString ());
69 return Response.status (HttpStatus.SC_NOT_FOUND).entity (e.toString ()).build ();
72 if (vnfResources == null) {
73 msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.DataNotFound, "Error:no vnf types found");
74 msoLogger.debug ("End of the transaction. No VNF Types found. The final response status is: " + HttpStatus.SC_NOT_FOUND);
75 return Response.status (HttpStatus.SC_NOT_FOUND).entity ("").build ();
78 ObjectFactory beansObjectFactory = new ObjectFactory ();
79 VnfTypes vnfTypes = beansObjectFactory.createVnfTypes ();
80 for (int i = 0; i < vnfResources.size (); i++) {
81 VnfType vnfType = beansObjectFactory.createVnfType ();
82 VnfResource vr = vnfResources.get (i);
83 vnfType.setType (vr.getVnfType ());
84 vnfType.setDescription (vr.getDescription ());
85 vnfType.setId (String.valueOf (vr.getId ()));
86 vnfTypes.getVnfType ().add (vnfType);
89 StringWriter stringWriter = new StringWriter ();
91 JAXBContext jaxbContext = JAXBContext.newInstance (VnfTypes.class);
92 Marshaller jaxbMarshaller = jaxbContext.createMarshaller ();
94 jaxbMarshaller.setProperty (Marshaller.JAXB_FORMATTED_OUTPUT, true);
95 jaxbMarshaller.marshal (vnfTypes, stringWriter);
97 } catch (JAXBException e) {
98 msoLogger.debug ("Error marshalling", e);
101 String response = stringWriter.toString ();
102 msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successful");
103 msoLogger.debug ("End of the transaction, the final response is: " + response);
104 return Response.status (HttpStatus.SC_OK).entity (response).build ();