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 java.io.StringWriter;
25 import java.util.List;
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;
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;
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 {
49 private static MsoLogger msoLogger = MsoLogger.getMsoLogger (MsoLogger.Catalog.APIH);
50 private static final String LOG_SERVICE_NAME = "InfrastructurePortal:MSO-APIH.";
53 @ApiOperation(value="Finds Vf Module Model Names",response=Response.class)
54 public Response getVfModuleModelNames (@PathParam("version") String version) {
55 long startTime = System.currentTimeMillis ();
56 String methodName = "getVfModuleModelNames";
57 MsoLogger.setServiceName (LOG_SERVICE_NAME + methodName);
58 msoLogger.debug ("Incoming request received for vfModuleModelNames");
59 List <VfModule> vfModules = null;
60 try (CatalogDatabase db = CatalogDatabase.getInstance()){
61 vfModules = db.getAllVfModules ();
62 } catch (Exception e) {
63 msoLogger.debug ("No connection to catalog DB", e);
64 msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.DBAccessError, "no connection to catalog DB");
65 msoLogger.debug ("End of the transaction, the final response is: " + e.toString ());
66 return Response.status (HttpStatus.SC_NOT_FOUND).entity (e.toString ()).build ();
69 if (vfModules == null) {
70 msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.DataNotFound, "VfModule not found");
71 msoLogger.debug ("End of the transaction. VfModuleModelName not found the final response status: " + HttpStatus.SC_NOT_FOUND);
72 return Response.status (HttpStatus.SC_NOT_FOUND).entity ("").build ();
75 ObjectFactory beansObjectFactory = new ObjectFactory ();
76 VfModuleModelNames vfModuleModelNames = beansObjectFactory.createVfModuleModelNames ();
77 for (int i = 0; i < vfModules.size (); i++) {
78 VfModuleModelName vfModuleModelName = beansObjectFactory.createVfModuleModelName ();
79 VfModule vm = vfModules.get (i);
80 vfModuleModelName.setModelName (vm.getModelName ());
81 vfModuleModelName.setModelVersion (vm.getVersion ());
82 vfModuleModelName.setModelInvariantUuid (vm.getModelInvariantUuid ());
83 vfModuleModelName.setIsBase(vm.isBase());
84 vfModuleModelName.setDescription (vm.getDescription ());
85 vfModuleModelName.setId (String.valueOf (vm.getModelUUID()));
86 vfModuleModelName.setAsdcServiceModelVersion(vm.getVersion ());
87 vfModuleModelNames.getVfModuleModelName ().add (vfModuleModelName);
90 StringWriter stringWriter = new StringWriter ();
92 JAXBContext jaxbContext = JAXBContext.newInstance (VfModuleModelNames.class);
93 Marshaller jaxbMarshaller = jaxbContext.createMarshaller ();
95 jaxbMarshaller.setProperty (Marshaller.JAXB_FORMATTED_OUTPUT, true);
96 jaxbMarshaller.marshal (vfModuleModelNames, 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 ();