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.adapter.impl;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
25 import org.apache.commons.lang3.StringUtils;
26 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.EntityUtils;
27 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.FileUtils;
28 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.JujuConfigUtil;
29 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.YamlUtil;
30 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.EntityUtils.ExeRes;
31 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.servicetoken.VnfmRestfulUtil;
32 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.adapter.inf.IJujuClientManager;
33 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.constant.Constant;
34 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.constant.UrlConstant;
35 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.juju.JujuHelper;
36 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
37 import org.openo.baseservice.util.impl.SystemEnvVariablesFactory;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import net.sf.json.JSON;
42 import net.sf.json.JSONObject;
51 * @version NFVO 0.5 Sep 7, 2016
53 public class JujuClientManager implements IJujuClientManager {
54 private static final Logger LOG = LoggerFactory.getLogger(JujuClientManager.class);
56 public static final String ADDRESOURCE="addResource";
57 public static final String REMOVERESOURCE = "removeResource";
68 public JSONObject deploy(String charmPath, String appName) {
69 JSONObject result = new JSONObject();
70 if(charmPath == null || appName == null){
71 String msg = "the 'charmPath' or 'appName' can not be null";
72 result.put(EntityUtils.RESULT_CODE_KEY, -1);
73 result.put(EntityUtils.MSG_KEY, msg);
77 String modelName = JujuHelper.getModelName(appName);
79 this.addModel(modelName);//use appName as modelName
81 this.switchModel(modelName);
83 List<String> commands = new ArrayList<>();
85 commands.add("deploy");
86 if(StringUtils.isNotBlank(charmPath)){
87 String fullPath = charmPath+appName;
88 commands.add(fullPath);
90 commands.add(appName);
93 commands.add(modelName);
94 ExeRes exeRes = EntityUtils.execute(charmPath,commands);
95 if(exeRes.getCode() == ExeRes.SUCCESS){
96 LOG.info("deploy success. command:"+EntityUtils.formatCommand(commands));
97 result.put(EntityUtils.RESULT_CODE_KEY, 0);
98 result.put(EntityUtils.DATA_KEY, exeRes.getBody());
100 LOG.error("deploy failed. command:"+EntityUtils.formatCommand(commands)+"\n"+exeRes);
101 result.put(EntityUtils.RESULT_CODE_KEY, -1);
102 result.put(EntityUtils.MSG_KEY, "deploy failed:"+exeRes.getBody());
116 private JSONObject addModel(String modelName) {
117 JSONObject result = new JSONObject();
118 List<String> commands = new ArrayList<>();
119 commands.add("juju");
120 commands.add("add-model");
121 commands.add(modelName);
122 getExtraParam(commands);
123 ExeRes exeRes = EntityUtils.execute(null,commands);
124 if(exeRes.getCode() == ExeRes.SUCCESS){
125 LOG.info("addModel success. command:"+EntityUtils.formatCommand(commands));
126 result.put(EntityUtils.RESULT_CODE_KEY, 0);
127 result.put(EntityUtils.DATA_KEY, exeRes.getBody());
129 LOG.error("addModel failed. command:"+EntityUtils.formatCommand(commands)+"\n"+exeRes);
130 result.put(EntityUtils.RESULT_CODE_KEY, -1);
131 result.put(EntityUtils.MSG_KEY, "addModel failed:"+exeRes.getBody());
140 juju add-model <model-name> --config image-metadata-url=http://192.168.20.106/images --config network=demo-net --config use-floating-ip=True --config use-default-secgroup=True
143 private void getExtraParam(List<String> commands){
145 String configInfo = readJujuConfigInfo();
146 if(configInfo != null){
147 JSONObject json = JSONObject.fromObject(configInfo);
148 commands.add("--config");
149 commands.add("image-metadata-url="+json.getString("image-metadata-url"));
150 commands.add("--config");
151 commands.add("network="+json.getString("network"));
152 commands.add("--config");
153 commands.add("use-floating-ip="+json.getString("use-floating-ip"));
154 commands.add("--config");
155 commands.add("use-default-secgroup="+json.getString("use-default-secgroup"));
157 } catch (Exception e) {
158 LOG.error("read juju command config error:",e);
163 * Get csar package information.<br>
166 * @throws IOException
169 public static String readJujuConfigInfo() {
170 InputStream ins = null;
172 BufferedInputStream bins = null;
173 String fileContent = null;
174 String fileName = SystemEnvVariablesFactory.getInstance().getAppRoot() + System.getProperty("file.separator")
175 + "etc" + System.getProperty("file.separator") + "conf" + System.getProperty("file.separator")
178 ins = new FileInputStream(fileName);
179 bins = new BufferedInputStream(ins);
181 byte[] contentByte = new byte[ins.available()];
182 int num = bins.read(contentByte);
185 fileContent = new String(contentByte);
187 } catch (Exception e) {
188 LOG.error(fileName + "is not found!", e);
197 } catch (IOException e) {
202 private JSONObject changeDir(String charmPath) {
203 JSONObject result = new JSONObject();
204 List<String> commands = new ArrayList<>();
206 commands.add(charmPath);
207 ExeRes exeRes = EntityUtils.execute(null,commands);
208 if(exeRes.getCode() == ExeRes.SUCCESS){
209 LOG.info("changeDir success. command:"+EntityUtils.formatCommand(commands));
210 result.put(EntityUtils.RESULT_CODE_KEY, 0);
211 result.put(EntityUtils.DATA_KEY, exeRes.getBody());
213 LOG.error("changeDir failed. command:"+EntityUtils.formatCommand(commands)+"\n"+exeRes);
214 result.put(EntityUtils.RESULT_CODE_KEY, -1);
215 result.put(EntityUtils.MSG_KEY, "addModel failed:"+exeRes.getBody());
229 private JSONObject switchModel(String modelName) {
230 JSONObject result = new JSONObject();
231 List<String> commands = new ArrayList<>();
232 commands.add("juju");
233 commands.add("switch");
234 commands.add(modelName);
235 ExeRes exeRes = EntityUtils.execute(null,commands);
236 if(exeRes.getCode() == ExeRes.SUCCESS){
237 LOG.info("switchModel success. command:"+EntityUtils.formatCommand(commands));
238 result.put(EntityUtils.RESULT_CODE_KEY, 0);
239 result.put(EntityUtils.DATA_KEY, exeRes.getBody());
241 LOG.error("switchModel failed. command:"+EntityUtils.formatCommand(commands)+"\n"+exeRes);
242 result.put(EntityUtils.RESULT_CODE_KEY, -1);
243 result.put(EntityUtils.MSG_KEY, "addModel failed:"+exeRes.getBody());
257 public JSONObject destroy(String appName) {
258 String modelName = JujuHelper.getModelName(appName);
259 JSONObject result = new JSONObject();
260 List<String> commands = new ArrayList<>();
261 commands.add("juju");
262 commands.add("destroy-model");
264 commands.add(modelName);
266 ExeRes exeRes = EntityUtils.execute(null,commands);
267 if(exeRes.getCode() == ExeRes.SUCCESS){
268 LOG.info("remove success. command:"+EntityUtils.formatCommand(commands));
269 result.put(EntityUtils.RESULT_CODE_KEY, 0);
270 result.put(EntityUtils.DATA_KEY, exeRes.getBody());
272 LOG.error("remove failed. command:"+EntityUtils.formatCommand(commands)+"\n"+exeRes);
273 result.put(EntityUtils.RESULT_CODE_KEY, -1);
274 result.put(EntityUtils.MSG_KEY, "remove failed:"+exeRes.getBody());
289 public JSONObject getStatus(String appName) {
290 String modelName = JujuHelper.getModelName(appName);
291 JSONObject result = new JSONObject();
292 List<String> commands = new ArrayList<>();
293 commands.add("juju");
294 commands.add("status");
295 if(StringUtils.isNotBlank(modelName)){
297 commands.add(modelName);
299 commands.add("--format=json");
301 ExeRes exeRes = EntityUtils.execute(null,commands);
302 if(exeRes.getCode() == ExeRes.SUCCESS){
303 LOG.info("getStatus success. command:"+EntityUtils.formatCommand(commands));
304 result.put(EntityUtils.RESULT_CODE_KEY, 0);
305 JSONObject dataObj = buildDataObj(exeRes);
306 result.put(EntityUtils.DATA_KEY, dataObj);
308 LOG.error("getStatus failed. command:"+EntityUtils.formatCommand(commands)+"\n"+exeRes);
309 result.put(EntityUtils.RESULT_CODE_KEY, -1);
310 result.put(EntityUtils.MSG_KEY, "getStatus failed:"+exeRes.getBody());
323 private JSONObject buildDataObj(ExeRes exeRes) {
324 JSONObject dataObj = null;
325 if(StringUtils.isNotBlank(exeRes.getBody())){
326 dataObj = JSONObject.fromObject(exeRes.getBody());
327 //according to appName to select
334 * call the juju vnfm to grant resource(disk,mem,cpu)
336 * (fields:cpu,mem,disk,action(addResource/removeResource))
344 public boolean grantResource(String charmPath, String appName,String action , String vnfId){
348 params = this.parseYaml(charmPath, appName, action);
349 } catch(Exception e) {
350 LOG.error("ParseYaml error,please check it.",e);
354 LOG.error("ParseYaml fail,please check it.");
358 //2. call grant service
359 String url = JujuConfigUtil.getValue("grant_jujuvnfm_url");
360 Map<String, String> paramsMap = new HashMap<>(6);
361 paramsMap.put("url", url);
362 paramsMap.put(Constant.METHOD_TYPE, Constant.PUT);
363 paramsMap.put("path", String.format(UrlConstant.REST_JUJU_VNFM_GRANT_URL,vnfId));
364 paramsMap.put(Constant.AUTH_MODE, Constant.AuthenticationMode.ANONYMOUS);
365 RestfulResponse rsp = VnfmRestfulUtil.getRemoteResponse(paramsMap, params.toString(), null);
366 if(rsp == null || rsp.getStatus() != Constant.HTTP_OK) {
367 LOG.error("function=grantResource, msg=send grantResource msg to juju-vnfm get wrong results");
371 String response = rsp.getResponseContent();
372 LOG.info("grant resource result:"+response);
379 * "constraints":"arch=amd64 cpu-cores=1 cpu-power=100 mem=1740 root-disk=8192"
386 public JSONObject parseYaml(String charmPath, String appName,String action){
387 JSONObject compute = new JSONObject();
388 compute.put("action", action);
389 if(StringUtils.isBlank(charmPath)){
390 LOG.error("the charmPath can't be null! [in unzipFileAndParseYaml]");
393 //set default values for non 'yaml' type
394 if(!appName.endsWith(".yaml")){
395 compute.put("cpu", 4);
396 compute.put("mem", 2);
397 compute.put("disk", 40);
400 String yaml = FileUtils.getFriendlyPath(charmPath)+appName;
401 File yamlFile = new File(yaml);
402 if(yamlFile.exists()){
403 JSON json = YamlUtil.yamlToJson(yamlFile.getAbsolutePath());
404 LOG.info(yaml+":\n"+json);
406 LOG.error("the yaml file has error format,please check it!"+yamlFile);
409 JSONObject jsonObj = (JSONObject)json;
410 JSONObject services = jsonObj.getJSONObject("services");
414 for(Object key: services.keySet()){
415 JSONObject app = services.getJSONObject(key.toString());
416 if(app.containsKey("constraints")){
417 String constraints = app.getString("constraints");
418 String[] vals = constraints.split("\\s+");
419 LOG.info(key+"="+constraints);
420 for(String val : vals){
421 String[] kv = val.split("=");
422 if("cpu-cores".equals(kv[0]) && StringUtils.isNotBlank(kv[1])){
423 cpu+=Integer.valueOf(kv[1]);
424 }else if("mem".equals(kv[0]) && StringUtils.isNotBlank(kv[1])){
425 mem+=Integer.valueOf(kv[1]);
426 }else if("root-disk".equals(kv[0]) && StringUtils.isNotBlank(kv[1])){
427 disk+=Integer.valueOf(kv[1]);
432 compute.put("cpu", cpu);
433 compute.put("mem", mem);
434 compute.put("disk", disk);
436 LOG.error("the yaml file not exist!file="+yamlFile);
439 LOG.info("parse yaml result-->"+compute);
444 public static void main(String[] args) {
445 JujuClientManager jujuClientManager = new JujuClientManager();
446 jujuClientManager.parseYaml("E:/workspace/openo-common-utils/src/org/openo/common/yaml", "test.yaml", "addResource");