Update gvnfm-driver .gitreview file
[vfc/nfvo/driver/vnfm/gvnfm.git] / juju / juju-vnfmadapter / Juju-vnfmadapterService / service / src / main / java / org / onap / vfc / nfvo / vnfm / gvnfm / jujuvnfmadapter / service / rest / JujuClientRoa.java
1 /*
2  * Copyright 2016 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.vnfm.gvnfm.jujuvnfmadapter.service.rest;
18
19 import java.util.UUID;
20
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;
31
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;
43
44 import net.sf.json.JSONObject;
45
46 /**
47  * <br/>
48  * <p>
49  * </p>
50  * 
51  * @author 
52  * @version NFVO 0.5 Aug 18, 2016
53  */
54 @Path("/v1/vnfms")
55 @Consumes(MediaType.APPLICATION_JSON)
56 @Produces(MediaType.APPLICATION_JSON)
57 public class JujuClientRoa {
58
59     private static final Logger LOG = LoggerFactory.getLogger(JujuClientRoa.class);
60
61     private IJujuClientManager jujuClientManager;
62     private VnfMgr vnfMgr;
63
64     /**
65      * @return Returns the jujuClientManager.
66      */
67     public IJujuClientManager getJujuClientManager() {
68         return jujuClientManager;
69     }
70
71     /**
72      * @param jujuClientManager The jujuClientManager to set.
73      */
74     public void setJujuClientManager(IJujuClientManager jujuClientManager) {
75         this.jujuClientManager = jujuClientManager;
76     }
77
78     /**
79      *
80      * @return
81      */
82     public VnfMgr getVnfMgr() {
83         return vnfMgr;
84     }
85
86     /**
87      *
88      * @param vnfMgr
89      */
90     public void setVnfMgr(VnfMgr vnfMgr) {
91         this.vnfMgr = vnfMgr;
92     }
93
94     /**
95      * Set Charm url for juju deployment
96      * <br/>
97      * 
98      * @param resp
99      * @param context
100      *            parameter : charmUrl
101      * @return "{"charmUrl":"http://dld_url"}
102      * @since NFVO 0.5
103      */
104     @POST
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);
111         
112         LOG.debug(reqJsonObject + ":");
113         return result.toString();
114     }
115
116     /**
117      * Get VNF status
118      * parameter: vnfInstanceId
119      * <br/>
120      * 
121      * @param modelName
122      * @param resp
123      * @param context
124      * @return Depends on juju's return
125      * @throws ServiceException
126      * @since NFVO 0.5
127      */
128     @GET
129     @Path("/status")
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();
136
137
138     }
139
140     /**
141      * Instance VNF to juju-client
142      * <br/>
143      * deployParam: depend on juju require
144      * 
145      * @param resp
146      * @param context
147      * @return status: deplay result <br>
148      *         the return data must be include "{
149      *         app_info:{"vnfId":123344}
150      *         }"
151      * @throws ServiceException
152      * @since NFVO 0.5
153      */
154     @POST
155     @Path("/deploy")
156     public String deploySerivce(@Context HttpServletRequest context, @Context HttpServletResponse resp)
157             throws ServiceException {
158         JSONObject result = new JSONObject();
159         String msg = null;
160         try {
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();
168             }
169             String csarId = (String)reqJsonObject.get("csarId");
170           
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");
177             }
178             String vnfId = UUID.randomUUID().toString();
179             //2、grant resource
180             boolean grantRes = jujuClientManager.grantResource(charmPath, appName, JujuClientManager.ADDRESOURCE, vnfId);
181             LOG.info("grantResource result:"+grantRes);
182             //3、deploy service
183             if(grantRes){
184                 result = jujuClientManager.deploy(charmPath,appName);
185                 if(result.getInt(EntityUtils.RESULT_CODE_KEY) == EntityUtils.ExeRes.SUCCESS){
186                     resp.setStatus(Constant.HTTP_CREATED); 
187                 }
188                 result.put("vnfId", vnfId);//return vnfId
189                 return result.toString();
190             }else{
191                 msg = "Grant resource fail:"+vnfId;
192             }
193         } catch(Exception e) {
194             msg = e.getMessage();
195            LOG.error("deploy fail in method deployService",e);
196         }
197         resp.setStatus(Constant.HTTP_INNERERROR); 
198         result.put(EntityUtils.MSG_KEY, msg);
199         return result.toString();
200     }
201
202     /**
203      * <br/>
204      * here appName equals modelName
205      * @param resp
206      * @param context
207      * @return
208      * @throws ServiceException
209      * @since NFVO 0.5
210      */
211     @POST
212     @Path("/destroy")
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);
217         String msg;
218         try {
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();
224             }
225             String appName = reqJsonObject.getString(Constant.APP_NAME);
226             appName = processAppName(appName);
227             String vnfId="";
228             if(reqJsonObject.containsKey("vnfId")) {
229                 vnfId = reqJsonObject.getString("vnfId");
230             }
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);
238          
239         }
240         resp.setStatus(Constant.HTTP_INNERERROR); 
241         result.put(EntityUtils.MSG_KEY, msg);
242         return result.toString();
243     }
244
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";
248         }
249         return appName;
250     }
251
252 }