aboutsummaryrefslogtreecommitdiffstats
path: root/aai-resources/src/main/java/org/onap/aai/TenantIsolation/DataImportTasks.java
blob: 098b715691b0394fc562835831b9f70fe65450ae (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.
 * ============LICENSE_END=========================================================
 */

package org.onap.aai.TenantIsolation;

import com.att.eelf.configuration.Configuration;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.comparator.LastModifiedFileComparator;
import org.apache.commons.io.filefilter.DirectoryFileFilter;
import org.apache.commons.io.filefilter.FileFileFilter;
import org.onap.aai.exceptions.AAIException;
import org.onap.aai.logging.ErrorLogHelper;
import org.onap.aai.util.AAIConfig;
import org.onap.aai.util.AAIConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * DataImportTasks
 *
 */
@Component
@PropertySource(value="file:${server.local.startpath}/etc/appprops/datatoolscrons.properties", ignoreResourceNotFound=true)
public class DataImportTasks {

    private static final Logger LOGGER;
    private SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    private static final List<String> EXTS = Arrays.asList("tar.gz", "tgz");

    static {
        Properties props = System.getProperties();
        props.setProperty(Configuration.PROPERTY_LOGGING_FILE_NAME, AAIConstants.AAI_LOGBACK_PROPS);
        props.setProperty(Configuration.PROPERTY_LOGGING_FILE_PATH, AAIConstants.AAI_HOME_BUNDLECONFIG);
        LOGGER = LoggerFactory.getLogger(DataImportTasks.class);
    }

    /**
     * Scheduled task to invoke importTask
     */
    @Scheduled(cron = "${dataimporttask.cron}")
    public void import1() {
        try {
            importTask();
        } catch (Exception e) {
        }
    }

    /**
     * The importTask method.
     *
     * @throws AAIException, Exception
     */
    public void importTask() throws AAIException, Exception {

        if (AAIConfig.get("aai.dataimport.enable").equalsIgnoreCase("false")) {
            LOGGER.info("Data Import is not enabled");
            return;
        }
        // Check if the process was started via command line
        if (isDataImportRunning()) {
            LOGGER.info("There is a dataImport process already running");
            return;
        }

        LOGGER.info("Started importTask: " + dateFormat.format(new Date()));

        String inputLocation = AAIConstants.AAI_HOME_BUNDLECONFIG + AAIConfig.get("aai.dataimport.input.location");

        // Check that the input location exist
        File targetDirFile = new File(inputLocation);
        if (targetDirFile.exists()) {
            // Delete any existing payload file directories
            deletePayload(targetDirFile);
        }

        File payloadFile = findExportedPayload();
        if (payloadFile == null)
            return; // already logged error in the findExportedPayload function

        if (unpackPayloadFile(payloadFile.getAbsolutePath())) {
            String[] command = new String[2];
            command[0] = AAIConstants.AAI_HOME + AAIConstants.AAI_FILESEP + "bin" + AAIConstants.AAI_FILESEP + "install"
                    + AAIConstants.AAI_FILESEP + "addManualData.sh";
            command[1] = "tenant_isolation";
            runAddManualDataScript(command);
        }

        // clean up
        payloadFile.delete();

    }

    /**
     * The isDataImportRunning method, checks if the data import task was started separately via command line
     * 
     * @return true if another process is running, false if not
     */
    private static boolean isDataImportRunning() {

        Process process = null;

        int count = 0;
        try {
            process = new ProcessBuilder().command("bash", "-c", "ps -ef | grep 'addManualData'").start();
            InputStream is = process.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);

            while (br.readLine() != null) {
                count++;
            }

            int exitVal = process.waitFor();
            LOGGER.info("Check if dataImport is running returned: " + exitVal);
        } catch (Exception e) {
            ErrorLogHelper.logError("AAI_8002",
                    "Exception while running the check to see if dataImport is running  " + e.getMessage());
            LOGGER.info("Exception while running the check to see if dataImport is running " + e.getMessage());
        }

        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * The findPayExportedPayload method tries to find the latest exported payload.
     * Also deletes the old files if any or any other file in this directory
     */
    private static File findExportedPayload() throws AAIException {
        String targetDir = AAIConstants.AAI_HOME_BUNDLECONFIG + AAIConfig.get("aai.dataimport.input.location");
        File targetDirFile = new File(targetDir);
        File payloadFile = null;

        File[] allFilesArr = targetDirFile.listFiles((FileFilter) FileFileFilter.FILE);
        if (allFilesArr == null || allFilesArr.length == 0) {
            ErrorLogHelper.logError("AAI_8001", "Unable to find payload file at " + targetDir);
            LOGGER.info("Unable to find payload at " + targetDir);
            return null;
        }
        if (allFilesArr.length > 1) {
            Arrays.sort(allFilesArr, LastModifiedFileComparator.LASTMODIFIED_REVERSE);
            // get the latest payload file
            boolean foundTheLatestPayload = false;
            for (File f : allFilesArr) {
                if (!foundTheLatestPayload && isTargzExtension(f.getAbsolutePath())) {
                    payloadFile = f;
                    foundTheLatestPayload = true;
                } else // delete all files except the latest payload file!
                    f.delete();
            }
        } else {
            if (isTargzExtension(allFilesArr[0].getAbsolutePath()))
                payloadFile = allFilesArr[0];
        }

        return payloadFile;
    }

    /**
     * The deletePayload method deletes all the payload files that it finds at targetDirectory
     * 
     * @param targetDirFile the directory that contains payload files
     * @throws AAIException
     */
    private static void deletePayload(File targetDirFile) throws AAIException {

        File[] allFilesArr = targetDirFile.listFiles((FileFilter) DirectoryFileFilter.DIRECTORY);
        if (allFilesArr == null || allFilesArr.length == 0) {
            LOGGER.info("No payload files found at " + targetDirFile.getPath());
            return;
        }
        for (File f : allFilesArr) {
            try {
                FileUtils.deleteDirectory(f);
            } catch (IOException e) {

                LOGGER.info("Unable to delete directory " + f.getAbsolutePath() + " " + e.getMessage());
            }

        }

    }

    /**
     * The isDataImportRunning method, checks if the data import task was started separately via command line
     * 
     * @return true if another process is running, false if not
     */
    private static boolean unpackPayloadFile(String payLoadFileName) {

        Process process = null;

        try {
            process =
                    new ProcessBuilder().command("bash", "-c", "gzip –d < " + payLoadFileName + " | tar xf -").start();
            int exitVal = process.waitFor();
            LOGGER.info("gzip -d returned: " + exitVal);
        } catch (Exception e) {
            ErrorLogHelper.logError("AAI_8002", "Exception while running the unzip  " + e.getMessage());
            LOGGER.info("Exception while running the unzip " + e.getMessage());
            return false;
        }
        /*
         * if (payLoadFileName.indexOf(".") > 0)
         * payLoadFileName = payLoadFileName.substring(0, payLoadFileName.lastIndexOf("."));
         * 
         * try {
         * process = new ProcessBuilder().command("bash", "-c", "tar xf " + payLoadFileName).start();
         * int exitVal = process.waitFor();
         * LOGGER.info("tar xf returned: " + exitVal);
         * } catch (Exception e) {
         * ErrorLogHelper.logError("AAI_8002", "Exception while running the tar xf  "+ e.getMessage());
         * LOGGER.info("Exception while running the tar xf "+ e.getMessage());
         * return false;
         * }
         */
        return true;
    }

    private static boolean isTargzExtension(String fileName) {
        boolean found = false;
        for (String ext : EXTS) {
            if (fileName.toLowerCase().endsWith("." + ext)) {
                found = true;
            }
        }
        return found;
    }

    /**
     * The runAddManualDataScript method runs a shell script/command with a variable number of arguments
     * 
     * @param script The script/command arguments
     */
    private static void runAddManualDataScript(String... script) {
        Process process = null;
        try {
            process = new ProcessBuilder().command(script).start();
            int exitVal = process.waitFor();
            LOGGER.info("addManualData.sh returned: " + exitVal);
        } catch (Exception e) {
            ErrorLogHelper.logError("AAI_8002", "Exception while running addManualData.sh " + e.getMessage());
            LOGGER.info("Exception while running addManualData.sh" + e.getMessage());
        }

    }
}