2  * Copyright 2018 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.svnfm.vnfmadapter.service.csm.vnf;
 
  19 import java.io.BufferedInputStream;
 
  21 import java.io.FileOutputStream;
 
  22 import java.io.IOException;
 
  23 import java.io.InputStream;
 
  25 import org.apache.commons.io.FileUtils;
 
  26 import org.apache.commons.io.IOUtils;
 
  27 import org.apache.commons.lang3.StringUtils;
 
  28 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient.SystemEnvVariablesFactory;
 
  29 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
 
  30 import org.slf4j.Logger;
 
  31 import org.slf4j.LoggerFactory;
 
  33 import net.sf.json.JSONArray;
 
  34 import net.sf.json.JSONException;
 
  35 import net.sf.json.JSONObject;
 
  43  * @version     VFC 1.0  2018年5月17日
 
  45 public abstract class ScaleManager {
 
  47     private static final Logger LOG = LoggerFactory.getLogger(ScaleManager.class);
 
  49     private static String VM_IDS_PATH =
 
  50             SystemEnvVariablesFactory.getInstance().getAppRoot() + System.getProperty(Constant.FILE_SEPARATOR) + "etc"
 
  51                     + System.getProperty(Constant.FILE_SEPARATOR) + "vnfpkginfo"
 
  52                     + System.getProperty(Constant.FILE_SEPARATOR) + "vms" + System.getProperty(Constant.FILE_SEPARATOR);
 
  54     public static void beforeScaleOut(JSONObject queryVms, String vnfId) {
 
  56             JSONArray vms = queryVms.getJSONObject("data").getJSONArray("vms");
 
  57             writeVmIdsToFile(vnfId, vms);
 
  58         } catch(JSONException e) {
 
  59             LOG.error("function=beforeScaleOut, msg=recode current vms JSONException", e);
 
  63     public static JSONArray beforeScaleIn(JSONObject queryVms, String vnfId) {
 
  64         JSONArray vmList = new JSONArray();
 
  66             JSONArray vms = queryVms.getJSONObject("data").getJSONArray("vms");
 
  67             JSONArray recodeVms = readVmIdsFile(vnfId);
 
  68             if(!recodeVms.isEmpty()) {
 
  69                 for(int i = 0; i < vms.size(); i++) {
 
  70                     JSONObject obj = vms.getJSONObject(i);
 
  71                     String vmId = obj.getString("id");
 
  72                     if(isScaleOutVm(recodeVms, vmId)) {
 
  73                         JSONObject vmIdObj = new JSONObject();
 
  74                         vmIdObj.put("vm_id", vmId);
 
  79         } catch(JSONException e) {
 
  80             LOG.error("function=beforeScaleIn, msg=recode current vms JSONException", e);
 
  85     private static boolean isScaleOutVm(JSONArray recodeVms, String vmId) {
 
  86         for(int i = 0; i < recodeVms.size(); i++) {
 
  87             JSONObject obj = recodeVms.getJSONObject(i);
 
  88             String oldVmId = obj.getString("id");
 
  89             if(oldVmId.equalsIgnoreCase(vmId)) {
 
  96     private static String getVmIdsFilePath(String vnfId) {
 
  97         return VM_IDS_PATH + vnfId + ".json";
 
 100     private static void writeVmIdsToFile(String vnfId, JSONArray vms) {
 
 101         String filePath = getVmIdsFilePath(vnfId);
 
 103             File destFile = FileUtils.getFile(filePath);
 
 105             File parentFile = destFile.getParentFile();
 
 106             if(parentFile != null) {
 
 107                 if(!parentFile.mkdirs() && !parentFile.isDirectory()) {
 
 108                     throw new IOException("Destination '" + parentFile + "' directory cannot be created");
 
 111             if(!destFile.exists()) {
 
 112                 destFile.createNewFile();
 
 114             try(FileOutputStream outStream = new FileOutputStream(destFile)) {
 
 115                 outStream.write(vms.toString().getBytes());
 
 117         } catch(IOException e) {
 
 118             LOG.error("function=writeVmIdsToFile, msg=write vms to file ioexception, e : {}", e);
 
 122     private static JSONArray readVmIdsFile(String vnfId) {
 
 123         String filePath = getVmIdsFilePath(vnfId);
 
 125         String fileContent = "";
 
 126         try(InputStream ins = FileUtils.openInputStream(FileUtils.getFile(filePath));
 
 127             BufferedInputStream bins = new BufferedInputStream(ins);) {
 
 128             byte[] contentByte = new byte[ins.available()];
 
 129             int num = bins.read(contentByte);
 
 132                 fileContent = new String(contentByte);
 
 135             if(StringUtils.isNotEmpty(fileContent)) {
 
 136                 return JSONArray.fromObject(fileContent);
 
 138         } catch(IOException e) {
 
 139             LOG.error("function=readVmIdsFile, msg=read vms from file IOException, filePath : {}", filePath);
 
 140         } catch(JSONException e) {
 
 141             LOG.error("function=readVmIdsFile, msg=read vms from file JSONException, fileContent : {}", fileContent);
 
 143         return new JSONArray();