summaryrefslogtreecommitdiffstats
path: root/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/scriptmgr/ScriptManager.java
blob: 85531108a45349f281d1aca82b047bb069568722 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * Copyright (c) 2018 Intel Corporation Intellectual Property
 *
 * 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.vnfsdk.functest.scriptmgr;

import com.google.gson.Gson;
import org.onap.vnfsdk.functest.FileUtil;
import org.onap.vnfsdk.functest.TaskExecution;
import org.onap.vnfsdk.functest.constants.ApplicationConstants;
import org.onap.vnfsdk.functest.db.TaskMgrCaseTblDAO;
import org.onap.vnfsdk.functest.db.TaskMgrTaskTblDAO;
import org.onap.vnfsdk.functest.externalservice.entity.Environment;
import org.onap.vnfsdk.functest.externalservice.entity.EnvironmentMap;
import org.onap.vnfsdk.functest.externalservice.entity.OperationStatus;
import org.onap.vnfsdk.functest.externalservice.entity.OperationStatusHandler;
import org.onap.vnfsdk.functest.models.CaseRecord;
import org.onap.vnfsdk.functest.models.TaskRecord;
import org.onap.vnfsdk.functest.responsehandler.TestResult;
import org.onap.vnfsdk.functest.responsehandler.TestResultMap;
import org.onap.vnfsdk.functest.responsehandler.VnfFuncTestResponseHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.core.Response;
import java.io.*;
import java.net.URL;
import java.util.List;
import java.util.StringTokenizer;
import java.util.UUID;
import java.util.concurrent.*;

public class ScriptManager {

    private static final Logger LOGGER = LoggerFactory.getLogger(ScriptManager.class);
    private final TaskMgrTaskTblDAO taskMgrTaskTblDAO;
    private final TaskMgrCaseTblDAO taskMgrCaseTblDAO;

    public ScriptManager(TaskMgrTaskTblDAO taskMgrTaskTblDAO, TaskMgrCaseTblDAO taskMgrCaseTblDAO) {
        this.taskMgrTaskTblDAO = taskMgrTaskTblDAO;
        this.taskMgrCaseTblDAO = taskMgrCaseTblDAO;
    }

    /**
     * Convert the stream to File Name<br/>
     *
     * @param dirName             - Directory name
     * @param fileName            - FileName
     * @param uploadedInputStream - Input Stream
     * @return - File Path
     * @throws IOException - Exception while writing file
     * @since VNFSDK
     */
    public static String storeChunkFileInLocal(String dirName, String fileName, InputStream uploadedInputStream)
            throws IOException {
        File tmpDir = new File(dirName);
        LOGGER.info("tmpdir=" + dirName);
        if (!tmpDir.exists()) {
            tmpDir.mkdirs();
        }
        StringTokenizer st = new StringTokenizer(fileName, "/");
        String actualFile = null;
        while (st.hasMoreTokens()) {
            actualFile = st.nextToken();
        }
        File file = new File(tmpDir + File.separator + actualFile);
        try (OutputStream os = new FileOutputStream(file, true)) {
            int read = 0;
            byte[] bytes = new byte[1024];

            while ((read = uploadedInputStream.read(bytes)) != -1) {
                os.write(bytes, 0, read);
            }
            os.flush();
            return file.getAbsolutePath();
        }
    }

    public UUID setEnvironment(String env) {
        LOGGER.info("[Scrip Manager] Set Environment.");

        try {

            // Generate UUID for each environment
            final UUID envID = UUID.randomUUID();

            // Convert input string to Environment class
            Environment envObj = new Gson().fromJson(env, Environment.class);
            if (null == envObj) {
                // Converting input to Env object failed
                return null;
            }

            // Set to the environment map
            EnvironmentMap.getInstance().addEnv(envID, envObj);
            LOGGER.info(EnvironmentMap.getInstance().getEnv(envID).getPassword());

            // Send the envID back
            return envID;

        } catch (Exception e) {
            LOGGER.error("Setting the Environment Fail", e);
        }

        return null;
    }

    public UUID uploadFuncTestPackage(UUID taskID, UUID envID, String url) {
        LOGGER.info("[Scrip Manager] Upload Function Test Package.");

        try {
            URL oracle = new URL(url);

            InputStream fis = new BufferedInputStream(oracle.openStream());

            // Convert the stream to script folder
            String nl = File.separator;
            String filePath = storeChunkFileInLocal("temp", "TempFile.zip", fis);

            // Unzip the folder
            String tempDir = System.getProperty("user.dir") + nl + "temp";
            List<String> list = FileUtil.unzip(filePath, tempDir);
            LOGGER.info("File path=" + filePath);

            String[] directories = FileUtil.getDirectory(tempDir);
            LOGGER.info("tempdir=" + tempDir);
            if (null != directories && 0 != directories.length) {
                filePath = tempDir + File.separator + directories[0];
            } else {
                filePath = tempDir;
            }

            // generate UUID for the upload
            final UUID uploadID = UUID.randomUUID();
            List<TaskRecord> taskRecordList = taskMgrTaskTblDAO.findByTaskID(taskID.toString());
            TaskRecord taskRecord = taskRecordList.get(0);
            taskRecord.setUploadID(uploadID.toString());
            taskRecord.setOperID(uploadID.toString());
            taskMgrTaskTblDAO.saveOrUpdate(taskRecord);

            final String finalPath = filePath;

            ExecutorService es = Executors.newFixedThreadPool(3);
            Future<Integer> future = es.submit(new Callable<Integer>() {

                @Override
                public Integer call() throws Exception {

                    new TaskExecution().uploadScript(finalPath, envID, uploadID);
                    return 0;
                }
            });

            try {
                if (0 == future.get(5, TimeUnit.SECONDS)) {
                    LOGGER.info("ExecutorService Done");

                    OperationStatus operStatus = OperationStatusHandler.getInstance().getOperStatusMap().get(uploadID);
                    taskRecord.setOperFinished(operStatus.isOperFinished() ? "True" : "False");
                    taskRecord.setOperResult(operStatus.getoResultCode().toString());
                    taskRecord.setOperResultMessage(operStatus.getOperResultMessage());
                    taskMgrTaskTblDAO.saveOrUpdate(taskRecord);
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                LOGGER.error("uploadFuncTestPackage InterruptedException", e);
            } catch (ExecutionException e) {
                LOGGER.error("uploadFuncTestPackage ExecutionException", e);
            } catch (TimeoutException e) {
                LOGGER.error("uploadFuncTestPackage TimeoutException", e);
            }

            return uploadID;

        } catch (IOException e) {
            LOGGER.error(ApplicationConstants.RUN_SCRIPT_EXECUTE_CMD, e);
        }

        return null;
    }

    public Response getOperationResult(UUID operID) {
        LOGGER.info("[Script Manager] Query functest Status by ID." + operID);

        return OperationStatusHandler.getInstance().getOperationStatus(operID);
    }

    public Response downloadResults(UUID taskID) {
        LOGGER.info("[Script Manager] Download functest Result by ID: " + taskID);

        Response resp = VnfFuncTestResponseHandler.getInstance().downloadResults(taskID.toString());

        try {
            TestResult testResult = TestResultMap.getInstance().getTestResultMap().get(taskID).get(0);

            CaseRecord caseRecord = taskMgrCaseTblDAO.findByTaskID(taskID.toString());
            caseRecord.setTestID(testResult.getName());
            caseRecord.setTestResult(testResult.getStatus());
            caseRecord.setTestDescription(testResult.getDescription());
            taskMgrCaseTblDAO.saveOrUpdate(caseRecord);
        } catch (Exception e) {
            LOGGER.error("Collect Functest Result by ID Failed.", e);
        }
        return resp;
    }


}