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.requestsdb.RequestsDatabase;
31 import org.openecomp.mso.utils.UUIDChecker;
32 import org.apache.http.HttpStatus;
34 import com.wordnik.swagger.annotations.Api;
35 import com.wordnik.swagger.annotations.ApiOperation;
37 import javax.ws.rs.GET;
38 import javax.ws.rs.Path;
39 import javax.ws.rs.PathParam;
40 import javax.ws.rs.QueryParam;
41 import javax.ws.rs.core.Response;
42 import javax.xml.bind.JAXBContext;
43 import javax.xml.bind.JAXBException;
44 import javax.xml.bind.Marshaller;
46 import java.io.StringWriter;
47 import java.util.List;
49 @Path(Constants.VNF_TYPES_PATH)
50 @Api(value="/{version: v1|v2|v3}/vnf-types",description="API Requests of vnfTypes")
51 public class VnfTypesHandler {
53 private static MsoLogger msoLogger = MsoLogger.getMsoLogger (MsoLogger.Catalog.APIH);
57 @ApiOperation(value="Finds Vnf Types",response=Response.class)
58 public Response getVnfTypes (@QueryParam("vnf-role") String vnfRole, @PathParam("version") String version) {
60 long startTime = System.currentTimeMillis ();
61 MsoLogger.setServiceName ("GetVnfTypes");
62 // Generate a Request Id
63 UUIDChecker.generateUUID(msoLogger);
64 msoLogger.debug ("Incoming request received for getVnfTypes with vnf-role:" + vnfRole);
66 List <VnfResource> vnfResources = null;
67 try(CatalogDatabase db = CatalogDatabase.getInstance()) {
68 if (vnfRole != null) {
69 vnfResources = db.getVnfResourcesByRole (vnfRole);
71 vnfResources = db.getAllVnfResources ();
73 } catch (Exception e) {
74 msoLogger.debug ("No connection to catalog DB", e);
75 msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.DBAccessError, "No connection to catalog DB");
76 msoLogger.debug ("End of the transaction, the final response is: " + e.toString ());
77 return Response.status (HttpStatus.SC_NOT_FOUND).entity (e.toString ()).build ();
80 if (vnfResources == null) {
81 msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.DataNotFound, "Error:no vnf types found");
82 msoLogger.debug ("End of the transaction. No VNF Types found. The final response status is: " + HttpStatus.SC_NOT_FOUND);
83 return Response.status (HttpStatus.SC_NOT_FOUND).entity ("").build ();
86 ObjectFactory beansObjectFactory = new ObjectFactory ();
87 VnfTypes vnfTypes = beansObjectFactory.createVnfTypes ();
88 for (int i = 0; i < vnfResources.size (); i++) {
89 VnfType vnfType = beansObjectFactory.createVnfType ();
90 VnfResource vr = vnfResources.get (i);
91 vnfType.setDescription (vr.getDescription ());
92 vnfType.setId (String.valueOf (vr.getModelUuid()));
93 vnfTypes.getVnfType ().add (vnfType);
96 StringWriter stringWriter = new StringWriter ();
98 JAXBContext jaxbContext = JAXBContext.newInstance (VnfTypes.class);
99 Marshaller jaxbMarshaller = jaxbContext.createMarshaller ();
101 jaxbMarshaller.setProperty (Marshaller.JAXB_FORMATTED_OUTPUT, true);
102 jaxbMarshaller.marshal (vnfTypes, stringWriter);
104 } catch (JAXBException e) {
105 msoLogger.debug ("Error marshalling", e);
108 String response = stringWriter.toString ();
109 msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successful");
110 msoLogger.debug ("End of the transaction, the final response is: " + response);
111 return Response.status (HttpStatus.SC_OK).entity (response).build ();