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.core.Response;
30 import javax.xml.bind.JAXBContext;
31 import javax.xml.bind.JAXBException;
32 import javax.xml.bind.Marshaller;
34 import org.apache.http.HttpStatus;
36 import org.openecomp.mso.apihandlerinfra.networkbeans.NetworkType;
37 import org.openecomp.mso.apihandlerinfra.networkbeans.NetworkTypes;
38 import org.openecomp.mso.apihandlerinfra.networkbeans.ObjectFactory;
39 import org.openecomp.mso.db.catalog.CatalogDatabase;
40 import org.openecomp.mso.db.catalog.beans.NetworkResource;
41 import org.openecomp.mso.logger.MsoLogger;
42 import org.openecomp.mso.utils.UUIDChecker;
44 @Path(Constants.NETWORK_TYPES_PATH)
45 public class NetworkTypesHandler {
47 private static MsoLogger msoLogger = MsoLogger.getMsoLogger (MsoLogger.Catalog.APIH);
50 public Response getNetworkTypes () {
51 long startTime = System.currentTimeMillis ();
52 MsoLogger.setServiceName ("getNetworkTypes");
53 // Generate a Request Id
54 UUIDChecker.generateUUID(msoLogger);
55 msoLogger.debug ("Incoming request received for getNetworkTypes");
57 List <NetworkResource> networkResources = null;
58 try (CatalogDatabase db = new CatalogDatabase()){
59 networkResources = db.getAllNetworkResources ();
60 } catch (Exception e) {
61 msoLogger.debug ("No connection to catalog DB", e);
62 msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.DBAccessError, "no connection to catalog DB");
63 msoLogger.debug ("End of the transaction, the final response is: " + e.toString ());
64 return Response.status (HttpStatus.SC_NOT_FOUND).entity (e.toString ()).build ();
67 if (networkResources == null) {
68 msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.DataNotFound, "NetworkType not found");
69 msoLogger.debug ("End of the transaction. NetworkType not found the final response status: " + HttpStatus.SC_NOT_FOUND);
70 return Response.status (HttpStatus.SC_NOT_FOUND).entity ("").build ();
73 ObjectFactory beansObjectFactory = new ObjectFactory ();
74 NetworkTypes networkTypes = beansObjectFactory.createNetworkTypes ();
75 for (int i = 0; i < networkResources.size (); i++) {
76 NetworkType networkType = beansObjectFactory.createNetworkType ();
77 NetworkResource vr = networkResources.get (i);
78 networkType.setType (vr.getNetworkType ());
79 networkType.setDescription (vr.getDescription ());
80 networkType.setId (String.valueOf (vr.getId ()));
81 networkTypes.getNetworkType ().add (networkType);
84 StringWriter stringWriter = new StringWriter ();
86 JAXBContext jaxbContext = JAXBContext.newInstance (NetworkTypes.class);
87 Marshaller jaxbMarshaller = jaxbContext.createMarshaller ();
89 jaxbMarshaller.setProperty (Marshaller.JAXB_FORMATTED_OUTPUT, true);
90 jaxbMarshaller.marshal (networkTypes, stringWriter);
92 } catch (JAXBException e) {
93 msoLogger.debug ("Error marshalling", e);
96 String response = stringWriter.toString ();
97 msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successful");
98 msoLogger.debug ("End of the transaction, the final response is: " + response);
99 return Response.status (HttpStatus.SC_OK).entity (response).build ();