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 ()) {
61 if (vnfRole != null) {
62 vnfResources = db.getVnfResourcesByRole (vnfRole);
64 vnfResources = db.getAllVnfResources ();
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 ();
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 ();
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);
90 StringWriter stringWriter = new StringWriter ();
92 JAXBContext jaxbContext = JAXBContext.newInstance (VnfTypes.class);
93 Marshaller jaxbMarshaller = jaxbContext.createMarshaller ();
95 jaxbMarshaller.setProperty (Marshaller.JAXB_FORMATTED_OUTPUT, true);
96 jaxbMarshaller.marshal (vnfTypes, stringWriter);
98 } catch (JAXBException e) {
99 msoLogger.debug ("Error marshalling", e);
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 ();