b720b4ffec12172d80a8fbaf8770bf0014ba9a61
[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.core.Context;
33 import javax.ws.rs.core.MediaType;
34
35 import org.apache.logging.log4j.LogManager;
36 import org.apache.logging.log4j.Logger;
37 import org.onap.vfc.nfvo.resmanagement.common.VimUtil;
38 import org.onap.vfc.nfvo.resmanagement.common.constant.HttpConstant;
39 import org.onap.vfc.nfvo.resmanagement.common.constant.ParamConstant;
40 import org.onap.vfc.nfvo.resmanagement.common.constant.UrlConstant;
41 import org.onap.vfc.nfvo.resmanagement.common.util.request.RequestUtil;
42 import org.onap.vfc.nfvo.resmanagement.common.util.response.ResponseUtil;
43 import org.onap.vfc.nfvo.resmanagement.common.util.response.RoaResponseUtil;
44 import org.onap.vfc.nfvo.resmanagement.service.base.openstack.inf.Sites;
45 import org.onap.vfc.nfvo.resmanagement.service.entity.SitesEntity;
46 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
47
48 import net.sf.json.JSONArray;
49 import net.sf.json.JSONObject;
50
51 /**
52  * Sites ROA method<br/>
53  * <p>
54  * </p>
55  *
56  * @author
57  * @version VFC 1.0 Aug 24, 2016
58  */
59 @Path(UrlConstant.SITES_URL)
60 @Consumes(MediaType.APPLICATION_JSON)
61 @Produces(MediaType.APPLICATION_JSON)
62 public class SitesRoa {
63
64     private static final Logger LOGGER = LogManager.getLogger(SitesRoa.class);
65
66     private Sites sites;
67
68     /**
69      * getSites ROA method<br/>
70      *
71      * @param context
72      * @return the get result
73      * @throws ServiceException When get failed.
74      * @since VFC 1.0
75      */
76     @GET
77     public JSONObject getSites(@Context HttpServletRequest context) throws ServiceException {
78         Map<String, Object> map = new HashMap<>(10);
79         List<SitesEntity> datacenters = sites.getList(map);
80
81         JSONObject result = new JSONObject();
82         result.put("datacenters", datacenters);
83         return result;
84     }
85
86     /**
87      * getSite ROA method<br/>
88      *
89      * @param context
90      * @param id
91      * @return the get result
92      * @throws ServiceException When get failed.
93      * @since VFC 1.0
94      */
95     @GET
96     @Path("/{datacenterId}")
97     public JSONObject getSite(@Context HttpServletRequest context, @PathParam("datacenterId") String id)
98             throws ServiceException {
99         LOGGER.warn("SitesRoa::getSitesById id:{}", id);
100         Map<String, Object> map = new HashMap<>(10);
101         map.put(ParamConstant.PARAM_ID, id);
102         List<SitesEntity> datacenters = sites.getList(map);
103
104         JSONObject result = new JSONObject();
105         result.put("datacenters", datacenters);
106         return result;
107     }
108
109     /**
110      * addSites ROA method<br/>
111      *
112      * @param context
113      * @param id
114      * @return the add result
115      * @throws ServiceException When add failed.
116      * @since VFC 1.0
117      */
118     @POST
119     public JSONObject addSites(@Context HttpServletRequest context) throws ServiceException {
120         JSONObject json = RequestUtil.getAllJsonRequestBody(context);
121
122         LOGGER.warn("SitesRoa:: start add Sites");
123         try {
124             int result = sites.add(json);
125             sites.sendToMonitor(json);
126             return RoaResponseUtil.add(result);
127         } catch(ServiceException se) {
128             LOGGER.error("SitesRoa::addSites error:{}" + se);
129             return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE, se.getMessage());
130         }
131     }
132
133     /**
134      * deleteSites ROA method<br/>
135      *
136      * @param context
137      * @param id
138      * @return the delete result
139      * @throws ServiceException When delete failed.
140      * @since VFC 1.0
141      */
142     @DELETE
143     @Path("/{datacenterId}")
144     public JSONObject deleteSites(@Context HttpServletRequest context, @PathParam("datacenterId") String id)
145             throws ServiceException {
146         LOGGER.warn("SitesRoa::deleteSites siteId:{}", id);
147         try {
148             int result = sites.delete(id);
149             return RoaResponseUtil.delete(result);
150         } catch(ServiceException se) {
151             LOGGER.error("SitesRoa::deleteSites error: " + se);
152             return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE, se.getMessage());
153         }
154     }
155
156     /**
157      * updateSites ROA method<br/>
158      *
159      * @param context
160      * @param id
161      * @return the update result
162      * @throws ServiceException When update failed.
163      * @since VFC 1.0
164      */
165     @PUT
166     public JSONObject updateSites(@Context HttpServletRequest context) throws ServiceException {
167         JSONObject json = RequestUtil.getAllJsonRequestBody(context);
168
169         LOGGER.warn("SitesRoa::start update Sites");
170         try {
171             int result = sites.update(SitesEntity.toEntity(json));
172             return RoaResponseUtil.update(result);
173         } catch(ServiceException se) {
174             LOGGER.error("SitesRoa::updateSites error:" + se);
175             return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE, se.getMessage());
176         }
177     }
178
179     /**
180      * grant resource method
181      * <br>
182      * 
183      * @param context
184      * @return
185      * @throws ServiceException
186      * @since VFC 1.0
187      */
188     @PUT
189     @Path("/grant")
190     public JSONObject grantResource(@Context HttpServletRequest context) throws ServiceException {
191         JSONObject json = RequestUtil.getAllJsonRequestBody(context);
192
193         LOGGER.warn("SitesRoa::grant resource");
194         try {
195             int result = sites.update(json);
196             return RoaResponseUtil.update(result);
197         } catch(ServiceException se) {
198             LOGGER.error("SitesRoa::grant resource:" + se);
199             return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE, se.getMessage());
200         }
201     }
202
203     @GET
204     @Path("/vims")
205     public String getVims(@Context HttpServletRequest context) throws ServiceException {
206         LOGGER.info("SitesRoa::get vims");
207         JSONArray vims = VimUtil.getVims();
208         JSONObject result = new JSONObject();
209         result.put("data", vims);
210         return result.toString();
211     }
212
213     @GET
214     @Path("/vims/{vimId}")
215     public String getVim(@Context HttpServletRequest context, @PathParam("vimId") String vimId)
216             throws ServiceException {
217         LOGGER.info("SitesRoa::get vim by id: {}", vimId);
218         return VimUtil.getVimById(vimId).toString();
219     }
220
221     public void setSites(Sites sites) {
222         this.sites = sites;
223     }
224 }