e92da07acc3af1fcad85b8ea8b725021e226bf54
[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         try {
103             File destFile = FileUtils.getFile(filePath);
104
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");
109                 }
110             }
111             if(!destFile.exists()) {
112                 destFile.createNewFile();
113             }
114             try(FileOutputStream outStream = new FileOutputStream(destFile)) {
115                 outStream.write(vms.toString().getBytes());
116             }
117         } catch(IOException e) {
118             LOG.error("function=writeVmIdsToFile, msg=write vms to file ioexception, e : {}", e);
119         }
120     }
121
122     private static JSONArray readVmIdsFile(String vnfId) {
123         String filePath = getVmIdsFilePath(vnfId);
124
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);
130
131             if(num > 0) {
132                 fileContent = new String(contentByte);
133             }
134
135             if(StringUtils.isNotEmpty(fileContent)) {
136                 return JSONArray.fromObject(fileContent);
137             }
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);
142         }
143         return new JSONArray();
144     }
145 }