aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/tools/compile-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/BuildState.java
blob: 89adf203b5816c5bb6ce8cd4138924e281679fe3 (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
 * Copyright © 2018 European Support Limited
 *
 * 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 a "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.openecomp.sdc.onboarding;

import static org.openecomp.sdc.onboarding.BuildHelper.readState;
import static org.openecomp.sdc.onboarding.Constants.ANSI_COLOR_RESET;
import static org.openecomp.sdc.onboarding.Constants.ANSI_YELLOW;
import static org.openecomp.sdc.onboarding.Constants.FULL_BUILD_DATA;
import static org.openecomp.sdc.onboarding.Constants.FULL_RESOURCE_BUILD_DATA;
import static org.openecomp.sdc.onboarding.Constants.JAR;
import static org.openecomp.sdc.onboarding.Constants.RESOURCES_CHANGED;
import static org.openecomp.sdc.onboarding.Constants.SKIP_MAIN_SOURCE_COMPILE;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.project.MavenProject;

public class BuildState {

    private static final String SHUTDOWN_TIME = "shutdownTime";
    private static final String VERSION = "version";

    private static HashMap<String, Map> compileDataStore = new HashMap<>();
    private static final Map<String, Object> MODULE_BUILD_DATA = new HashMap<>();
    private static final Map<String, Object> RESOURCE_BUILD_DATA = new HashMap<>();
    private static final Map<String, Artifact> ARTIFACTS = new HashMap<>();
    private static final Set<String> EXECUTE_TESTS_IF_DEPENDS_ON_STORE = new HashSet<>();
    private static final Set<String> PMD_EXECUTED_IN_RUN = new HashSet<>();
    private static File stateFileLocation =
            new File(Paths.get(System.getProperties().getProperty("java.io.tmpdir")).toFile(), "compileState.dat");

    private static File compileStateFile;
    private static final Logger LOG = Logger.getAnonymousLogger();
    private MavenProject project;
    private String compileStateFilePath;

    static {
        initializeStore();
        Optional<HashMap> masterStore = readState("compile.dat", HashMap.class);
        //noinspection unchecked
        compileDataStore = (HashMap) masterStore.orElseGet(() -> compileDataStore);
        String version = String.class.cast(compileDataStore.get(VERSION));
        if (version != null) {
            stateFileLocation = new File(Paths.get(System.getProperties().getProperty("java.io.tmpdir")).toFile(),
                    "compileState.dat-" + version);
        }
        if (stateFileLocation.exists()) {
            HashMap dat = loadState(stateFileLocation);
            if (swapStates(compileDataStore, dat)) {
                compileDataStore = dat;
            }
        }
    }


    void init() {
        ARTIFACTS.clear();
        for (Artifact artifact : project.getArtifacts()) {
            if (artifact.isSnapshot() && JAR.equals(artifact.getType())) {
                ARTIFACTS.put(artifact.getGroupId() + ":" + artifact.getArtifactId(), artifact);
            }
        }
        if (compileStateFile == null) {
            setCompileStateFile(
                    getCompileStateFile(compileStateFilePath.substring(0, compileStateFilePath.indexOf('/')), project));
        }
    }

    private static void setCompileStateFile(File file) {
        compileStateFile = file;
    }

    static void initializeStore() {
        compileDataStore.put(Constants.FULL_BUILD_DATA, new HashMap<>());
        compileDataStore.put(Constants.FULL_RESOURCE_BUILD_DATA, new HashMap<>());
        compileDataStore.put(Constants.MODULE_BUILD_DATA, new HashMap<>());
        compileDataStore.put(Constants.RESOURCE_BUILD_DATA, new HashMap<>());
    }


    static void recordPMDRun(String moduleCoordinates) {
        PMD_EXECUTED_IN_RUN.add(moduleCoordinates);
    }

    static boolean isPMDRun(String moduleCoordinates) {
        return PMD_EXECUTED_IN_RUN.contains(moduleCoordinates);
    }

    private void writeCompileState() throws IOException {
        writeState(compileStateFile, compileDataStore);
    }

    private void writeState(File file, HashMap store) throws IOException {
        try (FileOutputStream fos = new FileOutputStream(file); ObjectOutputStream oos = new ObjectOutputStream(fos)) {
            oos.writeObject(store);
        }
    }


    private File getCompileStateFile(String moduleCoordinate, MavenProject mavenProject) {
        return getStateFile(moduleCoordinate, mavenProject, compileStateFilePath);
    }


    private File getStateFile(String moduleCoordinate, MavenProject mavenProject, String filePath) {
        return new File(getTopParentProject(moduleCoordinate, mavenProject).getBasedir(),
                filePath.substring(filePath.indexOf('/') + 1));
    }

    MavenProject getTopParentProject(String moduleCoordinate, MavenProject mavenProject) {
        if (getModuleCoordinate(mavenProject).equals(moduleCoordinate) || mavenProject.getParent() == null) {
            return mavenProject;
        } else {
            return getTopParentProject(moduleCoordinate, mavenProject.getParent());
        }
    }

    private String getModuleCoordinate(MavenProject project) {
        return project.getGroupId() + ":" + project.getArtifactId();
    }

    void addModuleBuildTime(String moduleCoordinates, Long buildTime) {
        Map fullBuildData = compileDataStore.get(FULL_BUILD_DATA);
        @SuppressWarnings("unchecked") Long lastTime = (Long) fullBuildData.put(moduleCoordinates, buildTime);
        try {
            if (lastTime == null || !lastTime.equals(buildTime)) {
                boolean skipMainCompile = project.getProperties().containsKey(SKIP_MAIN_SOURCE_COMPILE);
                if (!skipMainCompile) {
                    writeCompileState();
                }
            }
        } catch (IOException e) {
            LOG.log(Level.FINE, e.getMessage(), e);
        }
    }

    void addResourceBuildTime(String moduleCoordinates, Long buildTime) {
        Map fullResourceBuildData = compileDataStore.get(FULL_RESOURCE_BUILD_DATA);
        if (project.getProperties().containsKey(RESOURCES_CHANGED)
                    || fullResourceBuildData.get(moduleCoordinates) == null) {
            try {
                //noinspection unchecked
                fullResourceBuildData.put(moduleCoordinates, buildTime);
                writeCompileState();
            } catch (IOException e) {
                LOG.log(Level.FINE, e.getMessage(), e);
            }
        }
    }

    void addModuleBuildData(String moduleCoordinates, Map moduleBuildDependencies) {
        MODULE_BUILD_DATA.put(moduleCoordinates, moduleBuildDependencies);
    }

    HashMap readModuleBuildData() {
        return (HashMap) compileDataStore.get(Constants.MODULE_BUILD_DATA).get(getModuleCoordinate(project));
    }

    void saveModuleBuildData(String moduleCoordinate) {
        if (MODULE_BUILD_DATA.get(moduleCoordinate) != null) {
            Map buildData = compileDataStore.get(Constants.MODULE_BUILD_DATA);
            //noinspection unchecked
            buildData.put(moduleCoordinate, MODULE_BUILD_DATA.get(moduleCoordinate));
        }
        saveCompileData();
    }

    void saveResourceBuildData(String moduleCoordinate) {
        if (RESOURCE_BUILD_DATA.get(moduleCoordinate) != null) {
            Map buildData = compileDataStore.get(Constants.RESOURCE_BUILD_DATA);
            //noinspection unchecked
            buildData.put(moduleCoordinate, RESOURCE_BUILD_DATA.get(moduleCoordinate));
        }
        saveCompileData();
    }

    void saveCompileData() {
        saveBuildData(compileStateFile, compileDataStore);
    }

    void markTestsMandatoryModule(String moduleCoordinates) {
        EXECUTE_TESTS_IF_DEPENDS_ON_STORE.add(moduleCoordinates);
    }

    private void saveBuildData(File file, Object dataToSave) {
        file.getParentFile().mkdirs();
        if (dataToSave != null) {
            try (FileOutputStream fos = new FileOutputStream(file);
                    ObjectOutputStream ois = new ObjectOutputStream(fos)) {
                ois.writeObject(dataToSave);
            } catch (IOException e) {
                LOG.log(Level.FINE, e.getMessage(), e);
            }
        }
    }

    HashMap readResourceBuildData() {
        return (HashMap) compileDataStore.get(Constants.RESOURCE_BUILD_DATA).get(getModuleCoordinate(project));
    }


    void addResourceBuildData(String moduleCoordinates, Map currentModuleResourceBuildData) {
        RESOURCE_BUILD_DATA.put(moduleCoordinates, currentModuleResourceBuildData);
    }

    Long getBuildTime(String moduleCoordinates) {
        Long buildTime = (Long) compileDataStore.get(FULL_BUILD_DATA).get(moduleCoordinates);
        return buildTime == null ? 0 : buildTime;
    }

    Long getResourceBuildTime(String moduleCoordinates) {
        Long resourceBuildTime = (Long) compileDataStore.get(FULL_RESOURCE_BUILD_DATA).get(moduleCoordinates);
        return resourceBuildTime == null ? 0 : resourceBuildTime;
    }

    boolean isCompileMust(String moduleCoordinates, Collection<String> dependencies) {
        for (String d : dependencies) {
            if (ARTIFACTS.containsKey(d) && JAR.equals(ARTIFACTS.get(d).getType())) {
                boolean versionEqual = ARTIFACTS.get(d).getVersion().equals(project.getVersion());
                if (versionEqual && getBuildTime(d) == 0) {
                    LOG.warning(ANSI_YELLOW + "[WARNING:]" + "You have module[" + d
                                        + "] not locally compiled even once, please compile your project once daily "
                                        + "from root to have reliable build results."
                                        + ANSI_COLOR_RESET);
                    return true;
                }
            }
        }
        return isMust(this::getBuildTime, moduleCoordinates, dependencies);
    }

    boolean isTestExecutionMandatory() {
        for (String d : ARTIFACTS.keySet()) {
            if (EXECUTE_TESTS_IF_DEPENDS_ON_STORE.contains(d)) {
                return true;
            }
        }
        return false;
    }

    boolean isTestMust(String moduleCoordinates) {
        return getBuildTime(moduleCoordinates) > getResourceBuildTime(moduleCoordinates) || isMust(
                this::getResourceBuildTime, moduleCoordinates, ARTIFACTS.keySet());
    }

    private boolean isMust(Function<String, Long> function, String moduleCoordinates, Collection<String> dependencies) {
        Long time = function.apply(moduleCoordinates);
        if (time == null || time == 0) {
            return true;
        }
        for (String module : dependencies) {
            Long buildTime = function.apply(module);
            if (buildTime >= time) {
                return true;
            }
        }
        return false;
    }

    private static HashMap loadState(File file) {
        try (InputStream is = new FileInputStream(file); ObjectInputStream ois = new ObjectInputStream(is)) {
            return (HashMap) ois.readObject();
        } catch (Exception e) {
            LOG.log(Level.FINE, e.getMessage(), e);
            return new HashMap<>();
        }
    }

    private static boolean swapStates(HashMap repo, HashMap last) {
        long repoTime = repo.get(SHUTDOWN_TIME) == null ? 0 : (Long) repo.get(SHUTDOWN_TIME);
        long lastTime = last.get(SHUTDOWN_TIME) == null ? 0 : (Long) last.get(SHUTDOWN_TIME);
        String repoVersion = repo.get(VERSION) == null ? "" : (String) repo.get(VERSION);
        String lastVersion = last.get(VERSION) == null ? "" : (String) last.get(VERSION);
        long repoBuildNumber = repoTime % 1000;
        long lastBuildNumber = lastTime % 1000;
        if (repoBuildNumber != lastBuildNumber) {
            return false;
        }
        if (!repoVersion.equals(lastVersion)) {
            return false;
        }
        return repoTime < lastTime;
    }


}