Upload the ESR server seed code.
[aai/esr-server.git] / esr-core / esr-mgr / src / main / java / org / onap / aai / esr / resource / SdncManager.java
1 /**
2  * Copyright 2016 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.aai.esr.resource;
17
18 import com.codahale.metrics.annotation.Timed;
19 import io.swagger.annotations.Api;
20 import io.swagger.annotations.ApiOperation;
21 import io.swagger.annotations.ApiParam;
22 import io.swagger.annotations.ApiResponse;
23 import io.swagger.annotations.ApiResponses;
24
25 import org.eclipse.jetty.http.HttpStatus;
26 import org.onap.aai.esr.entity.db.SdncData;
27 import org.onap.aai.esr.entity.rest.SdncRestData;
28 import org.onap.aai.esr.exception.ExtsysException;
29 import org.onap.aai.esr.handle.SdncHandler;
30 import org.onap.aai.esr.util.ExtsysDbUtil;
31 import org.onap.aai.esr.util.RestResponseUtil;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import java.util.ArrayList;
36 import java.util.List;
37 import javax.ws.rs.Consumes;
38 import javax.ws.rs.DELETE;
39 import javax.ws.rs.GET;
40 import javax.ws.rs.POST;
41 import javax.ws.rs.PUT;
42 import javax.ws.rs.Path;
43 import javax.ws.rs.PathParam;
44 import javax.ws.rs.Produces;
45 import javax.ws.rs.core.MediaType;
46 import javax.ws.rs.core.Response;
47
48 @Path("/sdncontrollers")
49 @Api(tags = {" sdnc Management     "})
50 public class SdncManager {
51
52   SdncHandler handler = new SdncHandler();
53   private static final Logger LOGGER = LoggerFactory.getLogger(SdncManager.class);
54
55   /**
56    *query all sdnc.
57    */
58   @Path("")
59   @GET
60   @ApiOperation(value = "get  all sdnc ")
61   @Produces(MediaType.APPLICATION_JSON)
62   @ApiResponses(value = {
63       @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
64           response = String.class),
65       @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
66           message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
67       @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "internal server error",
68           response = String.class)})
69   @Timed
70   public Response querysdncList() {
71     LOGGER.info("start query all sdnc!");
72     List<SdncData> list;
73     try {
74       list = handler.getAll();
75     } catch (ExtsysException error) {
76       LOGGER.error("query all sdnc failed.errorMsg:" + error.getErrorMsg());
77       return RestResponseUtil.getErrorResponse(error);
78     }
79     if (list == null || list.size() <= 0) {
80       LOGGER.info("query all sdnc end.no match condition record");
81       return RestResponseUtil.getSuccessResponse(new ArrayList<SdncRestData>());
82     } else {
83       LOGGER.info("query all sdnc end.size:" + list.size());
84       ArrayList<SdncRestData> restList = new ArrayList<SdncRestData>();
85       for (int i = 0; i < list.size(); i++) {
86         restList.add(new SdncRestData(list.get(i)));
87       }
88       return RestResponseUtil.getSuccessResponse(restList);
89     }
90
91   }
92   
93   /**
94    *query  sdnc by id.
95    */
96   @Path("/{sdncId}")
97   @GET
98   @ApiOperation(value = "get sdnc by id")
99   @Produces(MediaType.APPLICATION_JSON)
100   @ApiResponses(value = {
101       @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
102           response = String.class),
103       @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
104           message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
105       @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "internal server error",
106           response = String.class)})
107   @Timed
108   public Response querysdncById(@ApiParam(value = "sdnc id") @PathParam("sdncId") String sdncId) {
109     LOGGER.info("start query  sdnc by id." + sdncId);
110     List<SdncData> list;
111     try {
112       list = handler.getSdncById(sdncId);
113     } catch (ExtsysException error) {
114       LOGGER.error("query  sdnc failed.errorMsg:" + error.getErrorMsg());
115       return RestResponseUtil.getErrorResponse(error);
116     }
117     if (list == null || list.size() <= 0) {
118       LOGGER.info("query  sdnc end.no match condition record");
119       return RestResponseUtil.getSuccessResponse(null);
120     } else {
121       LOGGER.info("query  sdnc end.info:" + ExtsysDbUtil.objectToString(list));
122       return RestResponseUtil.getSuccessResponse(new SdncRestData(list.get(0)));
123     }
124   }
125   
126   /**
127    *delete  sdnc by id.
128    */
129   @Path("/{sdncId}")
130   @DELETE
131   @ApiOperation(value = "delete a sdnc")
132   @ApiResponses(value = {
133       @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
134           response = String.class),
135       @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
136           message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
137       @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "internal server error",
138           response = String.class)})
139   @Timed
140   public Response delsdnc(@ApiParam(value = "sdnc id") @PathParam("sdncId") String sdncId) {
141     LOGGER.info("start delete sdnc .id:" + sdncId);
142     try {
143       handler.delete(sdncId);
144     } catch (ExtsysException error) {
145       LOGGER.error("delete sdnc failed.errorMsg:" + error.getErrorMsg());
146       return RestResponseUtil.getErrorResponse(error);
147     }
148     LOGGER.info(" delete sdnc end !");
149     return Response.noContent().build();
150   }
151   
152   /**
153    *update sdnc by id.
154    */
155   @PUT
156   @Path("/{sdncId}")
157   @Consumes(MediaType.APPLICATION_JSON)
158   @Produces({MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON})
159   @ApiOperation(value = "update a sdnc")
160   @ApiResponses(value = {
161       @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
162           response = String.class),
163       @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
164           message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
165       @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "internal server error",
166           response = String.class)})
167   @Timed
168   public Response updatesdncs(@ApiParam(value = "sdnc", required = true) SdncData sdnc,
169       @ApiParam(value = "sdnc id", required = true) @PathParam("sdncId") String sdncId) {
170     LOGGER.info("start update sdnc .id:" + sdncId + " info:" + ExtsysDbUtil.objectToString(sdnc));
171     SdncData newData;
172     try {
173       newData = handler.update(sdnc, sdncId);
174     } catch (ExtsysException error) {
175       LOGGER.error("update sdnc failed.errorMsg:" + error.getErrorMsg());
176       return RestResponseUtil.getErrorResponse(error);
177     }
178     LOGGER.info(" update sdnc end !");
179     return RestResponseUtil.getSuccessResponse(new SdncRestData(newData));
180   }
181   
182   /**
183    *add  sdnc.
184    */
185   @POST
186   @Path("")
187   @Consumes(MediaType.APPLICATION_JSON)
188   @Produces({MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON})
189   @ApiOperation(value = "create a sdnc")
190   @ApiResponses(value = {
191       @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
192           response = String.class),
193       @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
194           message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
195       @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "internal server error",
196           response = String.class)})
197   @Timed
198   public Response addsdncs(@ApiParam(value = "sdnc", required = true) SdncData sdnc) {
199     LOGGER.info("start add sdnc" + " info:" + ExtsysDbUtil.objectToString(sdnc));
200     SdncData sdncData;
201     try {
202       sdncData = handler.add(sdnc);
203     } catch (ExtsysException error) {
204       LOGGER.error("add sdnc failed.errorMsg:" + error.getErrorMsg());
205       return RestResponseUtil.getErrorResponse(error);
206     }
207     LOGGER.info(" add sdnc end !");
208     return RestResponseUtil.getCreateSussceeResponse(new SdncRestData(sdncData));
209   }
210 }