Upload the ESR server seed code.
[aai/esr-server.git] / esr-core / esr-mgr / src / main / java / org / onap / aai / esr / resource / VnfmManager.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 import org.eclipse.jetty.http.HttpStatus;
25 import org.onap.aai.esr.entity.db.VnfmData;
26 import org.onap.aai.esr.entity.rest.VnfmRestData;
27 import org.onap.aai.esr.exception.ExtsysException;
28 import org.onap.aai.esr.handle.VnfmHandler;
29 import org.onap.aai.esr.util.ExtsysDbUtil;
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("/vnfms")
48 @Api(tags = {" vnfm Management "})
49 public class VnfmManager {
50
51   VnfmHandler handler = new VnfmHandler();
52   private static final Logger LOGGER = LoggerFactory.getLogger(VnfmManager.class);
53
54   /**
55    * query all vnfm.
56    */
57   @Path("")
58   @GET
59   @ApiOperation(value = "get  all vnfm ")
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 queryVnfmList() {
70     LOGGER.info("start query all vnfm!");
71     List<VnfmData> list;
72     try {
73       list = handler.getAll();
74     } catch (ExtsysException error) {
75       LOGGER.error("query all vnfm failed.errorMsg:" + error.getErrorMsg());
76       return RestResponseUtil.getErrorResponse(error);
77     }
78     if (list == null || list.size() <= 0) {
79       LOGGER.info("query all vnfm end.no match condition record");
80       return RestResponseUtil.getSuccessResponse(new ArrayList<VnfmRestData>());
81     } else {
82       LOGGER.info("query all vnfm end.size:" + list.size());
83       ArrayList<VnfmRestData> restList = new ArrayList<VnfmRestData>();
84       for (int i = 0; i < list.size(); i++) {
85         restList.add(new VnfmRestData(list.get(i)));
86       }
87       return RestResponseUtil.getSuccessResponse(restList);
88     }
89
90   }
91   
92   /**
93    * query  vnfm by id.
94    */
95   @Path("/{vnfmId}")
96   @GET
97   @ApiOperation(value = "get vnfm by id")
98   @Produces(MediaType.APPLICATION_JSON)
99   @ApiResponses(value = {
100       @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
101           response = String.class),
102       @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
103           message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
104       @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "internal server error",
105           response = String.class)})
106   @Timed
107   public Response queryVnfmById(@ApiParam(value = "vnfm id") @PathParam("vnfmId") String vnfmId) {
108     LOGGER.info("start query  vnfm by id." + vnfmId);
109     List<VnfmData> list;
110     try {
111       list = handler.getVnfmById(vnfmId);
112     } catch (ExtsysException error) {
113       LOGGER.error("query  vnfm failed.errorMsg:" + error.getErrorMsg());
114       return RestResponseUtil.getErrorResponse(error);
115     }
116     if (list == null || list.size() <= 0) {
117       LOGGER.info("query  vnfm end.no match condition record");
118       return RestResponseUtil.getSuccessResponse(null);
119     } else {
120       LOGGER.info("query  vnfm end.info:" + ExtsysDbUtil.objectToString(list));
121       return RestResponseUtil.getSuccessResponse(new VnfmRestData(list.get(0)));
122     }
123   }
124   
125   /**
126    * delete  vnfm by id.
127    */
128   @Path("/{vnfmId}")
129   @DELETE
130   @ApiOperation(value = "delete a vnfm")
131   @ApiResponses(value = {
132       @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
133           response = String.class),
134       @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
135           message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
136       @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "internal server error",
137           response = String.class)})
138   @Timed
139   public Response delVnfm(@ApiParam(value = "vnfm id") @PathParam("vnfmId") String vnfmId) {
140     LOGGER.info("start delete vnfm .id:" + vnfmId);
141     try {
142       handler.delete(vnfmId);
143     } catch (ExtsysException error) {
144       LOGGER.error("delete vnfm failed.errorMsg:" + error.getErrorMsg());
145       return RestResponseUtil.getErrorResponse(error);
146     }
147     LOGGER.info(" delete vnfm end !");
148     return Response.noContent().build();
149   }
150   
151   /**
152    * update  vnfm by id.
153    */
154   @PUT
155   @Path("/{vnfmId}")
156   @Consumes(MediaType.APPLICATION_JSON)
157   @Produces({MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON})
158   @ApiOperation(value = "update a vnfm")
159   @ApiResponses(value = {
160       @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
161           response = String.class),
162       @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
163           message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
164       @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "internal server error",
165           response = String.class)})
166   @Timed
167   public Response updateVnfms(@ApiParam(value = "vnfm", required = true) VnfmData vnfm,
168       @ApiParam(value = "vnfm id", required = true) @PathParam("vnfmId") String vnfmId) {
169     LOGGER.info("start update vnfm .id:" + vnfmId + " info:" + ExtsysDbUtil.objectToString(vnfm));
170     VnfmData newData;
171     try {
172       newData = handler.update(vnfm, vnfmId);
173     } catch (ExtsysException error) {
174       LOGGER.error("update vnfm failed.errorMsg:" + error.getErrorMsg());
175       return RestResponseUtil.getErrorResponse(error);
176     }
177     LOGGER.info(" update vnfm end !");
178     return RestResponseUtil.getSuccessResponse(new VnfmRestData(newData));
179   }
180   
181   /**
182    * add  vnfm .
183    */
184   @POST
185   @Path("")
186   @Consumes(MediaType.APPLICATION_JSON)
187   @Produces({MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON})
188   @ApiOperation(value = "create a vnfm")
189   @ApiResponses(value = {
190       @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
191           response = String.class),
192       @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
193           message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
194       @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "internal server error",
195           response = String.class)})
196   @Timed
197   public Response addVnfms(@ApiParam(value = "vnfm", required = true) VnfmData vnfm) {
198     LOGGER.info("start add vnfm" + " info:" + ExtsysDbUtil.objectToString(vnfm));
199     VnfmData vnfmData;
200     try {
201       vnfmData = handler.add(vnfm);
202     } catch (ExtsysException error) {
203       LOGGER.error("add vnfm failed.errorMsg:" + error.getErrorMsg());
204       return RestResponseUtil.getErrorResponse(error);
205     }
206     LOGGER.info(" add vnfm end !");
207     return RestResponseUtil.getCreateSussceeResponse(new VnfmRestData(vnfmData));
208   }
209 }