add some new function
[vfc/nfvo/driver/vnfm/svnfm.git] / huawei / vnfmadapter / VnfmadapterService / service / src / main / java / org / onap / vfc / nfvo / vnfm / svnfm / vnfmadapter / service / csm / vnf / ScaleManager.java
1 /*
2  * Copyright 2018 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.svnfm.vnfmadapter.service.csm.vnf;
18
19 import java.io.BufferedInputStream;
20 import java.io.File;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24
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;
32
33 import net.sf.json.JSONArray;
34 import net.sf.json.JSONException;
35 import net.sf.json.JSONObject;
36
37 /**
38  * <br>
39  * <p>
40  * </p>
41  * 
42  * @author
43  * @version     VFC 1.0  2018年5月17日
44  */
45 public abstract class ScaleManager {
46
47     private static final Logger LOG = LoggerFactory.getLogger(ScaleManager.class);
48
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);
53
54     public static void beforeScaleOut(JSONObject queryVms, String vnfId) {
55         try {
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");
60         }
61     }
62
63     public static JSONArray beforeScaleIn(JSONObject queryVms, String vnfId) {
64         JSONArray vmList = new JSONArray();
65         try {
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);
75                         vmList.add(vmIdObj);
76                     }
77                 }
78             }
79         } catch(JSONException e) {
80             LOG.error("function=beforeScaleIn, msg=recode current vms JSONException");
81         }
82         return vmList;
83     }
84
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)) {
90                 return false;
91             }
92         }
93         return true;
94     }
95
96     private static String getVmIdsFilePath(String vnfId) {
97         return VM_IDS_PATH + vnfId + ".json";
98     }
99
100     private static void writeVmIdsToFile(String vnfId, JSONArray vms) {
101         String filePath = getVmIdsFilePath(vnfId);
102         FileOutputStream outStream = null;
103         try {
104             File destFile = FileUtils.getFile(filePath);
105
106             File parentFile = destFile.getParentFile();
107             if(parentFile != null) {
108                 if(!parentFile.mkdirs() && !parentFile.isDirectory()) {
109                     throw new IOException("Destination '" + parentFile + "' directory cannot be created");
110                 }
111             }
112             if(!destFile.exists()) {
113                 destFile.createNewFile();
114             }
115             outStream = new FileOutputStream(destFile);
116             outStream.write(vms.toString().getBytes());
117         } catch(IOException e) {
118             LOG.error("function=writeVmIdsToFile, msg=write vms to file ioexception, e : {}", e);
119         } finally {
120             IOUtils.closeQuietly(outStream);
121         }
122     }
123
124     private static JSONArray readVmIdsFile(String vnfId) {
125         String filePath = getVmIdsFilePath(vnfId);
126         InputStream ins = null;
127         BufferedInputStream bins = null;
128         String fileContent = "";
129         try {
130             ins = FileUtils.openInputStream(FileUtils.getFile(filePath));
131             bins = new BufferedInputStream(ins);
132
133             byte[] contentByte = new byte[ins.available()];
134             int num = bins.read(contentByte);
135
136
137             if(num > 0) {
138                 fileContent = new String(contentByte);
139             }
140
141             if(StringUtils.isNotEmpty(fileContent)) {
142                 return JSONArray.fromObject(fileContent);
143             }
144         } catch(IOException e) {
145             LOG.error("function=readVmIdsFile, msg=read vms from file IOException, filePath : {}", filePath);
146         } catch(JSONException e) {
147             LOG.error("function=readVmIdsFile, msg=read vms from file JSONException, fileContent : {}", fileContent);
148         } finally {
149             IOUtils.closeQuietly(bins);
150             IOUtils.closeQuietly(ins);
151         }
152         return new JSONArray();
153     }
154 }