Initial OpenECOMP MSO commit
[so.git] / mso-api-handlers / mso-api-handler-infra / src / main / java / org / openecomp / mso / apihandlerinfra / NetworkTypesHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.mso.apihandlerinfra;
22
23
24 import java.io.StringWriter;
25 import java.util.List;
26
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;
33
34 import org.apache.http.HttpStatus;
35
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;
43
44 @Path(Constants.NETWORK_TYPES_PATH)
45 public class NetworkTypesHandler {
46
47     private static MsoLogger msoLogger = MsoLogger.getMsoLogger (MsoLogger.Catalog.APIH);
48
49     @GET
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");
56
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 ();
65         }
66
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 ();
71         }
72
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);
82         }
83
84         StringWriter stringWriter = new StringWriter ();
85         try {
86             JAXBContext jaxbContext = JAXBContext.newInstance (NetworkTypes.class);
87             Marshaller jaxbMarshaller = jaxbContext.createMarshaller ();
88
89             jaxbMarshaller.setProperty (Marshaller.JAXB_FORMATTED_OUTPUT, true);
90             jaxbMarshaller.marshal (networkTypes, stringWriter);
91
92         } catch (JAXBException e) {
93             msoLogger.debug ("Error marshalling", e);
94         }
95
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 ();
100     }
101 }