15e5198e3ab1e6b920813ec4b13b90eda9f84ab4
[aai/esr-server.git] / esr-core / esr-mgr / src / main / java / org / onap / aai / esr / resource / VimManager.java
1 /**
2  * Copyright 2016-2017 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 import org.eclipse.jetty.http.HttpStatus;
25 import org.onap.aai.esr.entity.aai.VimData;
26 import org.onap.aai.esr.entity.rest.VimRestData;
27 import org.onap.aai.esr.exception.ExtsysException;
28 import org.onap.aai.esr.handle.VimHandler;
29 import org.onap.aai.esr.util.ExtsysUtil;
30 import org.onap.aai.esr.util.RestResponseUtil;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import java.util.ArrayList;
35 import java.util.List;
36 import javax.ws.rs.Consumes;
37 import javax.ws.rs.DELETE;
38 import javax.ws.rs.GET;
39 import javax.ws.rs.POST;
40 import javax.ws.rs.PUT;
41 import javax.ws.rs.Path;
42 import javax.ws.rs.PathParam;
43 import javax.ws.rs.Produces;
44 import javax.ws.rs.core.MediaType;
45 import javax.ws.rs.core.Response;
46
47 @Path("/vims")
48 @Api(tags = {" vim Management "})
49 public class VimManager {
50
51   VimHandler handler = new VimHandler();
52   private static final Logger LOGGER = LoggerFactory.getLogger(VimManager.class);
53
54   /**
55    * query all VIM.
56    */
57   @Path("")
58   @GET
59   @ApiOperation(value = "get  all vim ")
60   @Produces(MediaType.APPLICATION_JSON)
61   @ApiResponses(value = {
62       @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
63           response = String.class),
64       @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
65           message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
66       @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "internal server error",
67           response = String.class)})
68   @Timed
69   public Response queryvimList() {
70     LOGGER.info("start query all vim!");
71     List<VimData> list;
72     try {
73       list = handler.getAll();
74     } catch (ExtsysException error) {
75       LOGGER.error("query all vim failed.errorMsg:" + error.getErrorMsg());
76       return RestResponseUtil.getErrorResponse(error);
77     }
78     if (list == null || list.size() <= 0) {
79       LOGGER.info("query all vim end.no match condition record");
80       return RestResponseUtil.getSuccessResponse(new ArrayList<VimRestData>());
81     } else {
82       LOGGER.info("query all vim end.size:" + list.size());
83       ArrayList<VimRestData> restList = new ArrayList<VimRestData>();
84       for (int i = 0; i < list.size(); i++) {
85 //        restList.add(new VimRestData(list.get(i)));
86         restList.add(new VimRestData());
87       }
88       return RestResponseUtil.getSuccessResponse(restList);
89     }
90
91   }
92
93   /**
94    * query vim by id.
95    */
96   @Path("/{vimId}")
97   @GET
98   @ApiOperation(value = "get vim 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 queryvimById(@ApiParam(value = "vim id") @PathParam("vimId") String vimId) {
109     LOGGER.info("start query  vim by id." + vimId);
110     List<VimData> list;
111     try {
112       list = handler.getVimById(vimId);
113     } catch (ExtsysException error) {
114       LOGGER.error("query  vim failed.errorMsg:" + error.getErrorMsg());
115       return RestResponseUtil.getErrorResponse(error);
116     }
117     if (list == null || list.size() <= 0) {
118       LOGGER.info("query  vim end.no match condition record");
119       return RestResponseUtil.getSuccessResponse(null);
120     } else {
121       LOGGER.info("query  vim end.info:" + ExtsysUtil.objectToString(list));
122 //      return RestResponseUtil.getSuccessResponse(new VimRestData(list.get(0)));
123       return RestResponseUtil.getSuccessResponse(new VimRestData());
124     }
125   }
126   
127   /**
128    * delete vim by id.
129    */
130   @Path("/{vimId}")
131   @DELETE
132   @ApiOperation(value = "delete a vim")
133   @ApiResponses(value = {
134       @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
135           response = String.class),
136       @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
137           message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
138       @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "internal server error",
139           response = String.class)})
140   @Timed
141   public Response delvim(@ApiParam(value = "vim id") @PathParam("vimId") String vimId) {
142     LOGGER.info("start delete vim .id:" + vimId);
143     try {
144       handler.delete(vimId);
145     } catch (ExtsysException error) {
146       LOGGER.error("delete vim failed.errorMsg:" + error.getErrorMsg());
147       return RestResponseUtil.getErrorResponse(error);
148     }
149     LOGGER.info(" delete vim end !");
150     return Response.noContent().build();
151   }
152   
153   /**
154    * update vim by id.
155    */
156   @PUT
157   @Path("/{vimId}")
158   @Consumes(MediaType.APPLICATION_JSON)
159   @Produces(MediaType.APPLICATION_JSON)
160   @ApiOperation(value = "update a vim")
161   @ApiResponses(value = {
162       @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
163           response = String.class),
164       @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
165           message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
166       @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "internal server error",
167           response = String.class)})
168   @Timed
169   public Response updatevims(@ApiParam(value = "vim", required = true) VimData vim,
170       @ApiParam(value = "vim id", required = true) @PathParam("vimId") String vimId) {
171     LOGGER.info("start update vim .id:" + vimId + " info:" + ExtsysUtil.objectToString(vim));
172     VimData newData;
173     try {
174       newData = handler.update(vim, vimId);
175     } catch (ExtsysException error) {
176       LOGGER.error("update vim failed.errorMsg:" + error.getErrorMsg());
177       return RestResponseUtil.getErrorResponse(error);
178     }
179     LOGGER.info(" update vim end !");
180 //    return RestResponseUtil.getSuccessResponse(new VimRestData(newData));
181     return RestResponseUtil.getSuccessResponse(new VimRestData());
182   }
183   
184   /**
185    * add vim .
186    */
187   @POST
188   @Path("")
189   @Consumes(MediaType.APPLICATION_JSON)
190   @Produces({MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON})
191   @ApiOperation(value = "create a vim")
192   @ApiResponses(value = {
193       @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
194           response = String.class),
195       @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
196           message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
197       @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "internal server error",
198           response = String.class)})
199   @Timed
200   public Response addvims(@ApiParam(value = "vim", required = true) VimData vim) {
201     LOGGER.info("start add vim" + " info:" + ExtsysUtil.objectToString(vim));
202     VimData vimData;
203     try {
204       vimData = handler.add(vim);
205     } catch (ExtsysException error) {
206       LOGGER.error("add vim failed.errorMsg:" + error.getErrorMsg());
207       return RestResponseUtil.getErrorResponse(error);
208     }
209     LOGGER.info(" add vim end !");
210 //    return RestResponseUtil.getCreateSussceeResponse(new VimRestData(vimData));
211     return RestResponseUtil.getCreateSussceeResponse(new VimRestData());
212   }
213 }