a00d25b4a2b4cbb0abf41e195c1d11501e86fcda
[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.PUT;
29 import javax.ws.rs.Path;
30 import javax.ws.rs.PathParam;
31 import javax.ws.rs.Produces;
32 import javax.ws.rs.QueryParam;
33 import javax.ws.rs.core.Context;
34 import javax.ws.rs.core.MediaType;
35
36 import org.onap.vfc.nfvo.resmanagement.common.constant.HttpConstant;
37 import org.onap.vfc.nfvo.resmanagement.common.constant.ParamConstant;
38 import org.onap.vfc.nfvo.resmanagement.common.constant.UrlConstant;
39 import org.onap.vfc.nfvo.resmanagement.common.util.request.RequestUtil;
40 import org.onap.vfc.nfvo.resmanagement.common.util.response.ResponseUtil;
41 import org.onap.vfc.nfvo.resmanagement.common.util.response.RoaResponseUtil;
42 import org.onap.vfc.nfvo.resmanagement.service.base.openstack.inf.Host;
43 import org.onap.vfc.nfvo.resmanagement.service.entity.HostEntity;
44 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 import net.sf.json.JSONObject;
49
50 /**
51  *
52  * Host ROA Class.<br>
53  * <p>
54  * </p>
55  *
56  * @author
57  * @version     NFVO 0.5  Sep 10, 2016
58  */
59 @Path(UrlConstant.HOST_URL)
60 @Produces(MediaType.APPLICATION_JSON)
61 @Consumes(MediaType.APPLICATION_JSON)
62 public class HostRoa {
63
64     private static final Logger LOGGER = LoggerFactory.getLogger(HostRoa.class);
65
66     private Host host;
67
68     /**
69      *
70      * Get hosts.<br>
71      *
72      * @param context
73      * @return
74      * @throws ServiceException
75      * @since  NFVO 0.5
76      */
77     @GET
78     public JSONObject getHosts(@Context HttpServletRequest context) throws ServiceException {
79         Map<String, Object> map = new HashMap<>(10);
80         List<HostEntity> hosts = host.getList(map);
81
82         JSONObject result = new JSONObject();
83         result.put("hosts", hosts);
84         return result;
85     }
86
87     /**
88      *
89      * Get host.<br>
90      *
91      * @param context
92      * @param id
93      * @return
94      * @throws ServiceException
95      * @since  NFVO 0.5
96      */
97     @GET
98     @Path("/{hostId}")
99     public JSONObject getHost(@Context HttpServletRequest context, @PathParam("hostId") String id)
100             throws ServiceException {
101         LOGGER.info("HostRoa::getHost id:{}", id);
102         Map<String, Object> map = new HashMap<>(10);
103         map.put(ParamConstant.PARAM_ID, id);
104         List<HostEntity> hosts = host.getList(map);
105
106         JSONObject result = new JSONObject();
107         result.put("hosts", hosts);
108         return result;
109     }
110
111     /**
112      *
113      * Add host.<br>
114      *
115      * @param context
116      * @return
117      * @throws ServiceException
118      * @since  NFVO 0.5
119      */
120     @POST
121     public JSONObject addHost(@Context HttpServletRequest context) throws ServiceException {
122         JSONObject object = RequestUtil.getJsonRequestBody(context);
123
124         LOGGER.info("HostRoa::addHost:{}", object.toString());
125         try {
126             int result = host.add(object);
127             return RoaResponseUtil.add(result);
128         } catch(ServiceException se) {
129             LOGGER.error("HostRoa::addHost error:{}" + se);
130             return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE, se.getMessage());
131         }
132     }
133
134     /**
135      *
136      * Delete host.<br>
137      *
138      * @param context
139      * @param id
140      * @return
141      * @throws ServiceException
142      * @since  NFVO 0.5
143      */
144     @DELETE
145     public JSONObject deleteHost(@Context HttpServletRequest context, @QueryParam(ParamConstant.PARAM_ID) String id)
146             throws ServiceException {
147         LOGGER.info("HostRoa::deleteHost id:{}", id);
148         try {
149             int result = host.delete(id);
150             return RoaResponseUtil.delete(result);
151         } catch(ServiceException se) {
152             LOGGER.error("HostRoa::deleteHost error:{}" + se);
153             return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE, se.getMessage());
154         }
155     }
156
157     /**
158      *
159      * Update host.<br>
160      *
161      * @param context
162      * @return
163      * @throws ServiceException
164      * @since  NFVO 0.5
165      */
166     @PUT
167     public JSONObject updateHost(@Context HttpServletRequest context) throws ServiceException {
168         JSONObject object = RequestUtil.getJsonRequestBody(context);
169
170         LOGGER.info("HostRoa::updateHost:{}", object.toString());
171         try {
172             int result = host.update(object);
173             return RoaResponseUtil.update(result);
174         } catch(ServiceException se) {
175             LOGGER.error("HostRoa::updateHost error:{}" + se);
176             return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE, se.getMessage());
177         }
178     }
179
180     public void setHost(Host host) {
181         this.host = host;
182     }
183 }