dcdf4947b2dc1b940e98f8f2091dd0852e3d0e19
[vfc/nfvo/resmanagement.git] /
1 /*
2  * Copyright 2016-2017 Huawei Technologies Co., Ltd.
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
17 package org.onap.vfc.nfvo.resmanagement.service.rest;
18
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 import javax.servlet.http.HttpServletRequest;
24 import javax.ws.rs.Consumes;
25 import javax.ws.rs.DELETE;
26 import javax.ws.rs.GET;
27 import javax.ws.rs.POST;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.PathParam;
30 import javax.ws.rs.Produces;
31 import javax.ws.rs.core.Context;
32 import javax.ws.rs.core.MediaType;
33
34 import org.apache.logging.log4j.LogManager;
35 import org.apache.logging.log4j.Logger;
36 import org.onap.vfc.nfvo.resmanagement.common.ResourceUtil;
37 import org.onap.vfc.nfvo.resmanagement.common.constant.HttpConstant;
38 import org.onap.vfc.nfvo.resmanagement.common.constant.ParamConstant;
39 import org.onap.vfc.nfvo.resmanagement.common.constant.UrlConstant;
40 import org.onap.vfc.nfvo.resmanagement.common.util.request.RequestUtil;
41 import org.onap.vfc.nfvo.resmanagement.common.util.response.ResponseUtil;
42 import org.onap.vfc.nfvo.resmanagement.common.util.response.RoaResponseUtil;
43 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
44 import org.onap.vfc.nfvo.resmanagement.service.entity.VnfStatusEntity;
45 import org.onap.vfc.nfvo.resmanagement.service.group.inf.VnfStatusService;
46
47 import net.sf.json.JSONObject;
48
49 /**
50  * <br>
51  * <p>
52  * </p>
53  * 
54  * @author
55  * @version VFC 1.0 Oct 29, 2016
56  */
57 @Path(UrlConstant.VNFSTATUS_URL)
58 @Produces(MediaType.APPLICATION_JSON)
59 @Consumes(MediaType.APPLICATION_JSON)
60 public class VnfStatusRoa {
61
62     private static final Logger LOGGER = LogManager.getLogger(VnfStatusRoa.class);
63
64     private VnfStatusService vnfStatusService;
65
66     @GET
67     public JSONObject getVnfStatuss(@Context HttpServletRequest context) throws ServiceException {
68         Map<String, Object> map = new HashMap<>(10);
69         List<VnfStatusEntity> vnfStatus = vnfStatusService.getList(map);
70         LOGGER.info("VnfStatusRoa::getVnfStatuss:{}", vnfStatus.toString(), context);
71         JSONObject result = new JSONObject();
72         result.put("vnfStatus", vnfStatus);
73         return result;
74     }
75
76     @GET
77     @Path("/{id}")
78     public JSONObject getVnfStatus(@Context HttpServletRequest context, @PathParam("id") String id)
79             throws ServiceException {
80         LOGGER.info("VnfStatusRoa::getVnfStatus id:{}", id, context);
81         Map<String, Object> map = new HashMap<>(10);
82         map.put(ParamConstant.PARAM_ID, id);
83         List<VnfStatusEntity> vnfStatus = vnfStatusService.getList(map);
84         LOGGER.info("VnfStatusRoa::getVnfStatus:{}", vnfStatus.toString());
85         JSONObject result = new JSONObject();
86         result.put("vnfStatus", vnfStatus);
87         return result;
88     }
89
90     @POST
91     public JSONObject addVnfStatus(@Context HttpServletRequest context) throws ServiceException {
92         JSONObject object = RequestUtil.getJsonRequestBody(context);
93         if(null == object) {
94             LOGGER.error("function=addVnfInfo; msg=add error, because vnfStatus is null.");
95             throw new ServiceException(ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.vnfStatus.add.null"));
96         }
97
98         LOGGER.info("VnfStatusRoa::addVnfStatus:{}", object.toString());
99         try {
100             return vnfStatusService.addVnfStatus(object);
101         } catch(ServiceException se) {
102             LOGGER.error("VnfStatusRoa::addVnfStatus error:{}" + se);
103             return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE, se.getMessage());
104         }
105     }
106
107     @DELETE
108     @Path("/{id}")
109     public JSONObject deleteVnfStatus(@Context HttpServletRequest context, @PathParam(ParamConstant.PARAM_ID) String id)
110             throws ServiceException {
111         if(id == null) {
112             LOGGER.error("function=deleteVnfStatus; msg=delete error, because id is null.");
113             throw new ServiceException(
114                     ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.vnfStatus.delete.id.null"));
115         }
116         LOGGER.info("VnfStatusRoa::deleteVnfStatus id:{}", id, context);
117         try {
118             int result = vnfStatusService.delete(id);
119             return RoaResponseUtil.delete(result);
120         } catch(ServiceException se) {
121             LOGGER.error("VnfStatusRoa::deleteVnfStatys error:{}" + se);
122             return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE, se.getMessage());
123         }
124     }
125
126     public void setVnfStatusService(VnfStatusService vnfStatusService) {
127         this.vnfStatusService = vnfStatusService;
128     }
129 }