Change the header to SO
[so.git] / mso-api-handlers / mso-api-handler-infra / src / main / java / org / openecomp / mso / apihandlerinfra / NetworkTypesHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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.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;
34
35 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.requestsdb.RequestsDatabase;
43 import org.openecomp.mso.utils.UUIDChecker;
44
45 import com.wordnik.swagger.annotations.Api;
46 import com.wordnik.swagger.annotations.ApiOperation;
47
48 @Path(Constants.NETWORK_TYPES_PATH)
49 @Api(value="/{version: v1|v2|v3}/network-types",description="API Requests to find Network Types")
50 public class NetworkTypesHandler {
51
52     private static MsoLogger msoLogger = MsoLogger.getMsoLogger (MsoLogger.Catalog.APIH);
53
54     @GET
55     @ApiOperation(value="Finds Network Types",response=Response.class)
56     public Response getNetworkTypes (@PathParam("version") String version) {
57         long startTime = System.currentTimeMillis ();
58         MsoLogger.setServiceName ("getNetworkTypes");
59         // Generate a Request Id
60         UUIDChecker.generateUUID(msoLogger);
61         msoLogger.debug ("Incoming request received for getNetworkTypes");
62
63         List <NetworkResource> networkResources = null;
64         try (CatalogDatabase db = CatalogDatabase.getInstance()){
65             networkResources = db.getAllNetworkResources ();
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 ();
71         }
72
73         if (networkResources == null) {
74             msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.DataNotFound, "NetworkType not found");
75             msoLogger.debug ("End of the transaction. NetworkType not found the final response status: " + HttpStatus.SC_NOT_FOUND);
76             return Response.status (HttpStatus.SC_NOT_FOUND).entity ("").build ();
77         }
78
79         ObjectFactory beansObjectFactory = new ObjectFactory ();
80         NetworkTypes networkTypes = beansObjectFactory.createNetworkTypes ();
81         for (int i = 0; i < networkResources.size (); i++) {
82             NetworkType networkType = beansObjectFactory.createNetworkType ();
83             NetworkResource vr = networkResources.get (i);
84             networkType.setType (vr.getModelName());
85             networkType.setDescription (vr.getDescription ());
86             networkType.setId (String.valueOf (vr.getModelUUID()));
87             networkTypes.getNetworkType ().add (networkType);
88         }
89
90         StringWriter stringWriter = new StringWriter ();
91         try {
92             JAXBContext jaxbContext = JAXBContext.newInstance (NetworkTypes.class);
93             Marshaller jaxbMarshaller = jaxbContext.createMarshaller ();
94
95             jaxbMarshaller.setProperty (Marshaller.JAXB_FORMATTED_OUTPUT, true);
96             jaxbMarshaller.marshal (networkTypes, stringWriter);
97
98         } catch (JAXBException e) {
99             msoLogger.debug ("Error marshalling", e);
100         }
101
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 ();
106     }
107 }