e1c2fa43e5ea5ceb0fae25cafa10127a1b1efafd
[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 javax.servlet.http.HttpServletRequest;
20 import javax.ws.rs.Consumes;
21 import javax.ws.rs.DELETE;
22 import javax.ws.rs.POST;
23 import javax.ws.rs.PUT;
24 import javax.ws.rs.Path;
25 import javax.ws.rs.Produces;
26 import javax.ws.rs.QueryParam;
27 import javax.ws.rs.core.Context;
28 import javax.ws.rs.core.MediaType;
29
30 import org.onap.vfc.nfvo.resmanagement.common.VimUtil;
31 import org.onap.vfc.nfvo.resmanagement.common.constant.HttpConstant;
32 import org.onap.vfc.nfvo.resmanagement.common.constant.ParamConstant;
33 import org.onap.vfc.nfvo.resmanagement.common.constant.UrlConstant;
34 import org.onap.vfc.nfvo.resmanagement.common.util.request.RequestUtil;
35 import org.onap.vfc.nfvo.resmanagement.common.util.response.ResponseUtil;
36 import org.onap.vfc.nfvo.resmanagement.common.util.response.RoaResponseUtil;
37 import org.onap.vfc.nfvo.resmanagement.service.group.inf.ResOperateService;
38 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 import net.sf.json.JSONObject;
43
44 /**
45  * Resource Operate ROA Class.<br>
46  * <p>
47  * </p>
48  *
49  * @author
50  * @version NFVO 0.5 Sep 10, 2016
51  */
52 @Path(UrlConstant.RESOPERATE_URL)
53 @Consumes(MediaType.APPLICATION_JSON)
54 @Produces(MediaType.APPLICATION_JSON)
55 public class ResOperateRoa {
56
57     private static final Logger LOGGER = LoggerFactory.getLogger(ResOperateRoa.class);
58
59     private ResOperateService resOperateService;
60
61     /**
62      * Update iResource pool.<br>
63      *
64      * @param context
65      * @param tenantId
66      * @param vimId
67      * @return
68      * @throws ServiceException
69      * @since NFVO 0.5
70      */
71     @PUT
72     @Path(UrlConstant.MODRES_URL)
73     @Consumes(MediaType.APPLICATION_JSON)
74     @Produces(MediaType.APPLICATION_JSON)
75     public JSONObject updateIResPool(@Context HttpServletRequest context,
76             @QueryParam(ParamConstant.PARAM_VIMID) String vimId) throws ServiceException {
77         JSONObject json = RequestUtil.getAllJsonRequestBody(context);
78         LOGGER.warn("ResPoolRoa::modVimId :{}", vimId);
79         JSONObject vimInfo = VimUtil.getVimById(vimId);
80         String tenant = vimInfo.getString("tenant");
81         String tenantId = VimUtil.getTenantIdByName(tenant, vimId);
82         try {
83             resOperateService.updateIRes(tenantId, vimId, json);
84             resOperateService.sendMsgMonitor("update", vimId);
85             return RoaResponseUtil.update(HttpConstant.OK_CODE);
86         } catch(ServiceException se) {
87             LOGGER.error("ResOperateRoa::updateIResPool error:{}" + se);
88             return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE, se.getMessage());
89         }
90     }
91
92     /**
93      * Add all resource pool.<br>
94      *
95      * @param context
96      * @param tenantId
97      * @param vimId
98      * @return
99      * @throws ServiceException
100      * @since NFVO 0.5
101      */
102     @POST
103     @Path(UrlConstant.ADDRES_URL)
104     @Consumes(UrlConstant.APPLICATION_TYPE)
105     @Produces(MediaType.APPLICATION_JSON)
106     public JSONObject addAllResPool(@Context HttpServletRequest context,
107             @QueryParam(ParamConstant.PARAM_TENANTID) String tenantId,
108             @QueryParam(ParamConstant.PARAM_VIMID) String vimId) throws ServiceException {
109         JSONObject json = RequestUtil.getAllJsonRequestBody(context);
110         LOGGER.warn("ResOperateRoa::vimId :{}", vimId);
111
112         try {
113             resOperateService.addIRes(tenantId, vimId, json);
114             resOperateService.sendMsgMonitor("create", vimId);
115             return RoaResponseUtil.add(HttpConstant.OK_CODE);
116         } catch(ServiceException se) {
117             LOGGER.error("ResOperateRoa::addAllResPool error:{}" + se);
118             return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE, se.getMessage());
119         }
120     }
121
122     /**
123      * Delete iResource.<br>
124      *
125      * @param context
126      * @param vimId
127      * @return
128      * @throws ServiceException
129      * @since NFVO 0.5
130      */
131     @DELETE
132     @Consumes(MediaType.APPLICATION_JSON)
133     @Produces(MediaType.APPLICATION_JSON)
134     public JSONObject deleteIRes(@Context HttpServletRequest context,
135             @QueryParam(ParamConstant.PARAM_VIMID) String vimId) throws ServiceException {
136         LOGGER.warn("ResOperateRoa::deleteIResource vimId:{}", vimId);
137
138         try {
139             resOperateService.sendMsgMonitor("delete", vimId);
140             int result = resOperateService.deleteIRes(vimId);
141             return RoaResponseUtil.delete(result);
142         } catch(ServiceException se) {
143             LOGGER.error("ResOperateRoa::deleteIRes error:{}" + se);
144             return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE, se.getMessage());
145         }
146     }
147
148     public void setResOperateService(ResOperateService resOperateService) {
149         this.resOperateService = resOperateService;
150     }
151 }