summaryrefslogtreecommitdiffstats
path: root/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/service/csm/vnf/ScaleManager.java
blob: 3290ef3c9bcfa68b328f7e4469f2702fe5ba458e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * Copyright 2018 Huawei Technologies Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.vnf;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient.SystemEnvVariablesFactory;
import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

/**
 * <br>
 * <p>
 * </p>
 * 
 * @author
 * @version     VFC 1.0  2018年5月17日
 */
public abstract class ScaleManager {

    private static final Logger LOG = LoggerFactory.getLogger(ScaleManager.class);

    private static String VM_IDS_PATH =
            SystemEnvVariablesFactory.getInstance().getAppRoot() + System.getProperty(Constant.FILE_SEPARATOR) + "etc"
                    + System.getProperty(Constant.FILE_SEPARATOR) + "vnfpkginfo"
                    + System.getProperty(Constant.FILE_SEPARATOR) + "vms" + System.getProperty(Constant.FILE_SEPARATOR);

    public static void beforeScaleOut(JSONObject queryVms, String vnfId) {
        try {
            JSONArray vms = queryVms.getJSONObject("data").getJSONArray("vms");
            writeVmIdsToFile(vnfId, vms);
        } catch(JSONException e) {
            LOG.error("function=beforeScaleOut, msg=recode current vms JSONException", e);
        }
    }

    public static JSONArray beforeScaleIn(JSONObject queryVms, String vnfId) {
        JSONArray vmList = new JSONArray();
        try {
            JSONArray vms = queryVms.getJSONObject("data").getJSONArray("vms");
            JSONArray recodeVms = readVmIdsFile(vnfId);
            if(!recodeVms.isEmpty()) {
                for(int i = 0; i < vms.size(); i++) {
                    JSONObject obj = vms.getJSONObject(i);
                    String vmId = obj.getString("id");
                    if(isScaleOutVm(recodeVms, vmId)) {
                        JSONObject vmIdObj = new JSONObject();
                        vmIdObj.put("vm_id", vmId);
                        vmList.add(vmIdObj);
                    }
                }
            }
        } catch(JSONException e) {
            LOG.error("function=beforeScaleIn, msg=recode current vms JSONException", e);
        }
        return vmList;
    }

    private static boolean isScaleOutVm(JSONArray recodeVms, String vmId) {
        for(int i = 0; i < recodeVms.size(); i++) {
            JSONObject obj = recodeVms.getJSONObject(i);
            String oldVmId = obj.getString("id");
            if(oldVmId.equalsIgnoreCase(vmId)) {
                return false;
            }
        }
        return true;
    }

    private static String getVmIdsFilePath(String vnfId) {
        return VM_IDS_PATH + vnfId + ".json";
    }

    private static void writeVmIdsToFile(String vnfId, JSONArray vms) {
        String filePath = getVmIdsFilePath(vnfId);
        try {
            File destFile = FileUtils.getFile(filePath);

            File parentFile = destFile.getParentFile();
            if(parentFile != null) {
                if(!parentFile.mkdirs() && !parentFile.isDirectory()) {
                    throw new IOException("Destination '" + parentFile + "' directory cannot be created");
                }
            }
            if(!destFile.exists()) {
                destFile.createNewFile();
            }
            try(FileOutputStream outStream = new FileOutputStream(destFile)) {
                outStream.write(vms.toString().getBytes());
            }
        } catch(IOException e) {
            LOG.error("function=writeVmIdsToFile, msg=write vms to file ioexception, e : {}", e);
        }
    }

    private static JSONArray readVmIdsFile(String vnfId) {
        String filePath = getVmIdsFilePath(vnfId);

        String fileContent = "";
        try(InputStream ins = FileUtils.openInputStream(FileUtils.getFile(filePath));
            BufferedInputStream bins = new BufferedInputStream(ins);) {
            byte[] contentByte = new byte[ins.available()];
            int num = bins.read(contentByte);

            if(num > 0) {
                fileContent = new String(contentByte);
            }

            if(StringUtils.isNotEmpty(fileContent)) {
                return JSONArray.fromObject(fileContent);
            }
        } catch(IOException e) {
            LOG.error("function=readVmIdsFile, msg=read vms from file IOException, filePath : {}", filePath);
        } catch(JSONException e) {
            LOG.error("function=readVmIdsFile, msg=read vms from file JSONException, fileContent : {}", fileContent);
        }
        return new JSONArray();
    }
}