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.openo.nfvo.jujuvnfmadapter.service.adapter.impl;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
25 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
26 import org.openo.baseservice.util.impl.SystemEnvVariablesFactory;
27 import org.openo.nfvo.jujuvnfmadapter.common.DownloadCsarManager;
28 import org.openo.nfvo.jujuvnfmadapter.common.EntityUtils;
29 import org.openo.nfvo.jujuvnfmadapter.common.servicetoken.JujuVnfmRestfulUtil;
30 import org.openo.nfvo.jujuvnfmadapter.service.adapter.inf.IResourceManager;
31 import org.openo.nfvo.jujuvnfmadapter.service.constant.Constant;
32 import org.openo.nfvo.jujuvnfmadapter.service.constant.UrlConstant;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.stereotype.Service;
37 import net.sf.json.JSONObject;
41 * Adapter resource manager class.<br>
46 * @version NFVO 0.5 Sep 12, 2016
49 public class AdapterResourceManager implements IResourceManager {
51 private static final Logger LOG = LoggerFactory.getLogger(AdapterResourceManager.class);
55 public JSONObject getJujuVnfmInfo(Map<String, String> paramsMap) {
56 JSONObject resultObj = new JSONObject();
59 RestfulResponse rsp = JujuVnfmRestfulUtil.getRemoteResponse(paramsMap,"");
61 LOG.error("function=getJujuVnfmInfo, RestfulResponse is null");
62 resultObj.put(Constant.REASON, "RestfulResponse is null.");
63 resultObj.put(Constant.RETURN_CODE, Constant.ERROR_STATUS_CODE);
66 String resultCreate = rsp.getResponseContent();
68 if(rsp.getStatus() == Constant.HTTP_OK) {
69 LOG.warn("function=getJujuVnfmInfo, msg= status={}, result={}.", rsp.getStatus(), resultCreate);
70 resultObj = JSONObject.fromObject(resultCreate);
71 resultObj.put(Constant.RETURN_CODE, Constant.HTTP_OK);
74 LOG.error("function=getJujuVnfmInfo, msg=ESR return fail,status={}, result={}.", rsp.getStatus(),
76 resultObj.put(Constant.REASON, "ESR return fail.");
78 resultObj.put(Constant.RETURN_CODE, Constant.ERROR_STATUS_CODE);
83 public JSONObject getVnfdInfo(String csarId) {
84 JSONObject resultObj = new JSONObject();
85 JSONObject csarPkgInfoObj = new JSONObject();
87 if(null == csarId || "".equals(csarId)) {
88 resultObj.put("reason", "csarId is null.");
89 resultObj.put("retCode", Constant.REST_FAIL);
92 String downloadUri = this.fetchDownloadUrlFromCatalog(csarId);
93 if(downloadUri == null){
94 LOG.error("fetchDownloadUrlFromCatalog return null,csarId="+csarId);
95 resultObj.put(Constant.REASON, "fetchDownloadUrlFromCatalog is null.");
96 resultObj.put(Constant.RETURN_CODE, Constant.ERROR_STATUS_CODE);
101 csarPkgInfo = readCsarPkgInfo();
102 csarPkgInfoObj = JSONObject.fromObject(csarPkgInfo); //NOSONAR
103 } catch (IOException e) {
107 String csarfilepath = csarPkgInfoObj.getString("csar_file_path");
108 String csarfilename = csarPkgInfoObj.getString("csar_file_name");
110 // download csar package and save in location.
111 JSONObject downloadObject = downloadCsar(downloadUri, csarfilepath+ System.getProperty("file.separator") +csarfilename);
113 if (Integer.valueOf(downloadObject.get("retCode").toString()) != Constant.REST_SUCCESS) {
114 LOG.error("download CSAR fail.", downloadObject.get("retCode"));
115 resultObj.put("reason", downloadObject.get("reason").toString());
116 resultObj.put("retCode", downloadObject.get("retCode").toString());
119 LOG.info("download CSAR successful.", downloadObject.get("retCode"));
121 // unzip csar package to location.
122 JSONObject unzipObject = unzipCSAR(csarfilepath+ System.getProperty("file.separator") +csarfilename, csarfilepath);
124 if (Integer.valueOf(unzipObject.get("retCode").toString()) != Constant.REST_SUCCESS) {
125 LOG.error("unzip CSAR fail.", unzipObject.get("retCode"));
126 resultObj.put("reason", unzipObject.get("reason").toString());
127 resultObj.put("retCode", unzipObject.get("retCode").toString());
130 LOG.info("unzip CSAR successful.", unzipObject.get("retCode"));
132 resultObj.put(Constant.RETURN_CODE, Constant.HTTP_OK);
133 resultObj.put("csarFilePath", getImagesPath(csarfilepath));
137 private String getImagesPath(String csarfilepath){
138 File imageFile = new File(csarfilepath+"SoftwareImages");
139 if(imageFile.exists()){
140 File[] charmFiles = imageFile.listFiles();
141 for(File file : charmFiles){
142 if(!file.getName().endsWith(".zip")){
143 return file.getAbsolutePath()+"/";
150 public String fetchDownloadUrlFromCatalog(String csarId){
151 String downloadUri = null;
153 Map paramsMap = new HashMap();
155 paramsMap.put("url", String.format(UrlConstant.REST_CSARINFO_GET, csarId));
156 paramsMap.put("methodType", Constant.GET);
158 RestfulResponse rsp = JujuVnfmRestfulUtil.getRemoteResponse(paramsMap,"");
160 LOG.error("function=getVnfdInfo, RestfulResponse is null");
163 String resultCreate = rsp.getResponseContent();
165 if(rsp.getStatus() != Constant.HTTP_OK) {
166 LOG.error("function=getVnfdInfo, msg=catalog return fail,status={}, result={}.", rsp.getStatus(),
170 JSONObject csarObj = JSONObject.fromObject(resultCreate);
171 downloadUri = csarObj.getString("downloadUri");
172 } catch (Exception e) {
173 LOG.error("fetchDownloadUrlFromCatalog error",e);
179 * Get csar package information.<br>
182 * @throws IOException
185 public static String readCsarPkgInfo() throws IOException {
186 InputStream ins = null;
187 BufferedInputStream bins = null;
188 String fileContent = "";
190 String fileName = SystemEnvVariablesFactory.getInstance().getAppRoot() + System.getProperty("file.separator")
191 + "etc" + System.getProperty("file.separator") + "csarInfo" + System.getProperty("file.separator")
195 ins = new FileInputStream(fileName);
196 bins = new BufferedInputStream(ins);
198 byte[] contentByte = new byte[ins.available()];
199 int num = bins.read(contentByte);
202 fileContent = new String(contentByte);
204 } catch (FileNotFoundException e) {
205 LOG.error(fileName + "is not found!", e);
220 * @throws IOException
223 public JSONObject downloadCsar(String url, String filePath) {
224 JSONObject resultObj = new JSONObject();
226 if(url == null || "".equals(url)) {
227 resultObj.put("reason", "url is null.");
228 resultObj.put("retCode", Constant.REST_FAIL);
231 if(filePath == null || "".equals(filePath)) {
232 resultObj.put("reason", "downloadUrl filePath is null.");
233 resultObj.put("retCode", Constant.REST_FAIL);
237 String status = DownloadCsarManager.download(url, filePath);
239 if (Constant.DOWNLOADCSAR_SUCCESS.equals(status)) {
240 resultObj.put("reason", "download csar file successfully.");
241 resultObj.put("retCode", Constant.REST_SUCCESS);
243 resultObj.put("reason", "download csar file failed.");
244 resultObj.put("retCode", Constant.REST_FAIL);
251 * @param fileName filePath
254 public JSONObject unzipCSAR(String fileName,String filePath) {
255 JSONObject resultObj = new JSONObject();
257 if(fileName == null || "".equals(fileName)) {
258 resultObj.put("reason", "fileName is null.");
259 resultObj.put("retCode", Constant.REST_FAIL);
262 if(filePath == null || "".equals(filePath)) {
263 resultObj.put("reason", "unzipCSAR filePath is null.");
264 resultObj.put("retCode", Constant.REST_FAIL);
268 int status = DownloadCsarManager.unzipCSAR(fileName, filePath);
271 if (Constant.UNZIP_SUCCESS == status) {
272 resultObj.put("reason", "unzip csar file successfully.");
273 resultObj.put("retCode", Constant.REST_SUCCESS);
274 chmodToFiles(filePath);
276 resultObj.put("reason", "unzip csar file failed.");
277 resultObj.put("retCode", Constant.REST_FAIL);
284 private void chmodToFiles(String filePath) {
286 List<String> commands = new ArrayList<String>();
287 commands.add("chmod");
290 commands.add(filePath);
291 EntityUtils.ExeRes exeRes = EntityUtils.execute(null, commands);
292 if (exeRes.getCode() == EntityUtils.ExeRes.SUCCESS) {
293 LOG.info("chmod success:" + EntityUtils.formatCommand(commands));
295 LOG.error("dchmod fail" + EntityUtils.formatCommand(commands) + "\n" + exeRes);
297 } catch (Exception e) {
298 LOG.error("chmod error:",e);