Upload the ESR server seed code.
[aai/esr-server.git] / esr-core / esr-mgr / src / main / java / org / onap / aai / esr / resource / CommonManager.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.BaseData;
27 import org.onap.aai.esr.entity.rest.BaseRestData;
28 import org.onap.aai.esr.exception.ExtsysException;
29 import org.onap.aai.esr.handle.CommonHandler;
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.GET;
38 import javax.ws.rs.Path;
39 import javax.ws.rs.PathParam;
40 import javax.ws.rs.Produces;
41 import javax.ws.rs.core.MediaType;
42 import javax.ws.rs.core.Response;
43
44 @Path("/common")
45 @Api(tags = {" common Management "})
46 public class CommonManager {
47
48   CommonHandler handler = new CommonHandler();
49   private static final Logger LOGGER = LoggerFactory.getLogger(CommonManager.class);
50
51   /**
52    * query all  exr instance. 
53    */
54   @Path("")
55   @GET
56   @ApiOperation(value = "get  all instance ")
57   @Produces(MediaType.APPLICATION_JSON)
58   @ApiResponses(value = {
59       @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
60           response = String.class),
61       @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
62           message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
63       @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "internal server error",
64           response = String.class)})
65   @Timed
66   public Response queryInstanceList() {
67     LOGGER.info("start query all instance!");
68     List<BaseData> list;
69     try {
70       list = handler.getAll();
71     } catch (ExtsysException error) {
72       LOGGER.error("query all instance failed.errorMsg:" + error.getErrorMsg());
73       return RestResponseUtil.getErrorResponse(error);
74     }
75     if (list == null || list.size() <= 0) {
76       LOGGER.info("query all instance end.no match condition record");
77       return RestResponseUtil.getSuccessResponse(new ArrayList<BaseRestData>());
78     } else {
79       LOGGER.info("query all instance end.size:" + list.size());
80       ArrayList<BaseRestData> restList = new ArrayList<BaseRestData>();
81       for (int i = 0; i < list.size(); i++) {
82         restList.add(new BaseRestData(list.get(i)));
83       }
84       return RestResponseUtil.getSuccessResponse(restList);
85     }
86
87   }
88   
89   /**
90    * query  exr instance by id. 
91    */
92   @Path("/{instanceId}")
93   @GET
94   @ApiOperation(value = "get instance by id")
95   @Produces(MediaType.APPLICATION_JSON)
96   @ApiResponses(value = {
97       @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
98           response = String.class),
99       @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
100           message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
101       @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "internal server error",
102           response = String.class)})
103   @Timed
104   public Response queryInstanceById(
105       @ApiParam(value = "instanceId") @PathParam("instanceId") String instanceId) {
106     LOGGER.info("start query  instance by id." + instanceId);
107     List<BaseData> list;
108     try {
109       list = handler.getInstanceById(instanceId);
110     } catch (ExtsysException error) {
111       LOGGER.error("query  instance failed.errorMsg:" + error.getErrorMsg());
112       return RestResponseUtil.getErrorResponse(error);
113     }
114     if (list == null || list.size() <= 0) {
115       LOGGER.info("query  instance end.no match condition record");
116       return RestResponseUtil.getSuccessResponse(null);
117     } else {
118       LOGGER.info("query  ems end.info:" + ExtsysDbUtil.objectToString(list));
119       return RestResponseUtil.getSuccessResponse(new BaseRestData(list.get(0)));
120     }
121   }
122
123
124 }