2 * Copyright 2016 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.rest;
19 import java.util.UUID;
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
23 import javax.ws.rs.Consumes;
24 import javax.ws.rs.GET;
25 import javax.ws.rs.POST;
26 import javax.ws.rs.Path;
27 import javax.ws.rs.Produces;
28 import javax.ws.rs.QueryParam;
29 import javax.ws.rs.core.Context;
30 import javax.ws.rs.core.MediaType;
32 import org.apache.commons.lang3.StringUtils;
33 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.EntityUtils;
34 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.JujuConfigUtil;
35 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.StringUtil;
36 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.adapter.impl.JujuClientManager;
37 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.adapter.inf.IJujuClientManager;
38 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.constant.Constant;
39 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.process.VnfMgr;
40 import org.openo.baseservice.remoteservice.exception.ServiceException;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 import net.sf.json.JSONObject;
52 * @version NFVO 0.5 Aug 18, 2016
55 @Consumes(MediaType.APPLICATION_JSON)
56 @Produces(MediaType.APPLICATION_JSON)
57 public class JujuClientRoa {
59 private static final Logger LOG = LoggerFactory.getLogger(JujuClientRoa.class);
61 private IJujuClientManager jujuClientManager;
62 private VnfMgr vnfMgr;
65 * @return Returns the jujuClientManager.
67 public IJujuClientManager getJujuClientManager() {
68 return jujuClientManager;
72 * @param jujuClientManager The jujuClientManager to set.
74 public void setJujuClientManager(IJujuClientManager jujuClientManager) {
75 this.jujuClientManager = jujuClientManager;
82 public VnfMgr getVnfMgr() {
90 public void setVnfMgr(VnfMgr vnfMgr) {
95 * Set Charm url for juju deployment
100 * parameter : charmUrl
101 * @return "{"charmUrl":"http://dld_url"}
105 @Path("/setCharmUrl")
106 public String setCharmUrl(@Context HttpServletRequest context, @Context HttpServletResponse resp)
107 throws ServiceException {
108 JSONObject result = new JSONObject();
109 result.put("retCode", Constant.REST_FAIL);
110 JSONObject reqJsonObject = StringUtil.getJsonFromContexts(context);
112 LOG.debug(reqJsonObject + ":");
113 return result.toString();
118 * parameter: vnfInstanceId
124 * @return Depends on juju's return
125 * @throws ServiceException
130 public String getVnfStatus(@QueryParam("modelName") String modelName, @Context HttpServletRequest context,
131 @Context HttpServletResponse resp) throws ServiceException {
132 String appName = processAppName(modelName);
133 JSONObject result = jujuClientManager.getStatus(appName);
134 LOG.debug("status json str:"+result.toString());
135 return result.toString();
141 * Instance VNF to juju-client
143 * deployParam: depend on juju require
147 * @return status: deplay result <br>
148 * the return data must be include "{
149 * app_info:{"vnfId":123344}
151 * @throws ServiceException
156 public String deploySerivce(@Context HttpServletRequest context, @Context HttpServletResponse resp)
157 throws ServiceException {
158 JSONObject result = new JSONObject();
161 result.put(EntityUtils.RESULT_CODE_KEY, EntityUtils.ExeRes.FAILURE);
162 JSONObject reqJsonObject = StringUtil.getJsonFromContexts(context);
163 LOG.info("deploySerivce request data-->"+reqJsonObject);
164 if(reqJsonObject == null || reqJsonObject.get(Constant.APP_NAME) == null){
165 result.put(EntityUtils.MSG_KEY, "the param 'appName' can't be null");
166 resp.setStatus(Constant.HTTP_INNERERROR);
167 return result.toString();
169 String csarId = (String)reqJsonObject.get("csarId");
171 String appName = reqJsonObject.getString(Constant.APP_NAME);
172 appName = processAppName(appName);
173 //1、download the catalog,unzip file and get the charmPath
174 String charmPath = vnfMgr.getCharmPath(csarId);
175 if(StringUtils.isBlank(charmPath)) {
176 charmPath = JujuConfigUtil.getValue("charmPath");
178 String vnfId = UUID.randomUUID().toString();
180 boolean grantRes = jujuClientManager.grantResource(charmPath, appName, JujuClientManager.ADDRESOURCE, vnfId);
181 LOG.info("grantResource result:"+grantRes);
184 result = jujuClientManager.deploy(charmPath,appName);
185 if(result.getInt(EntityUtils.RESULT_CODE_KEY) == EntityUtils.ExeRes.SUCCESS){
186 resp.setStatus(Constant.HTTP_CREATED);
188 result.put("vnfId", vnfId);//return vnfId
189 return result.toString();
191 msg = "Grant resource fail:"+vnfId;
193 } catch(Exception e) {
194 msg = e.getMessage();
195 LOG.error("deploy fail in method deployService",e);
197 resp.setStatus(Constant.HTTP_INNERERROR);
198 result.put(EntityUtils.MSG_KEY, msg);
199 return result.toString();
204 * here appName equals modelName
208 * @throws ServiceException
213 public String destroySerivce(@Context HttpServletRequest context, @Context HttpServletResponse resp)
214 throws ServiceException {
215 JSONObject result = new JSONObject();
216 result.put(EntityUtils.RESULT_CODE_KEY, EntityUtils.ExeRes.FAILURE);
219 JSONObject reqJsonObject = StringUtil.getJsonFromContexts(context);
220 if(reqJsonObject == null || reqJsonObject.get(Constant.APP_NAME) == null){
221 result.put(EntityUtils.MSG_KEY, "the param 'appName' can't be null");
222 resp.setStatus(Constant.HTTP_INNERERROR);
223 return result.toString();
225 String appName = reqJsonObject.getString(Constant.APP_NAME);
226 appName = processAppName(appName);
228 if(reqJsonObject.containsKey("vnfId")) {
229 vnfId = reqJsonObject.getString("vnfId");
231 result = jujuClientManager.destroy(appName);
232 resp.setStatus(Constant.UNREG_SUCCESS);
233 LOG.info("destroy service success!!!"+appName+vnfId);
234 return result.toString();
235 } catch(Exception e) {
236 msg = e.getMessage();
237 LOG.error("destory fail in method destroyService",e);
240 resp.setStatus(Constant.HTTP_INNERERROR);
241 result.put(EntityUtils.MSG_KEY, msg);
242 return result.toString();
245 private static String processAppName(String appName){
246 if(appName != null && appName.indexOf(".yaml") > 0){//remove zte's attach
247 return appName.substring(0,appName.indexOf(".yaml"))+".yaml";