aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/spike/schema/EdgeRulesLoader.java
blob: be303f5bb45dad3287cc2537406c936bea542ffd (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
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017-2018 Amdocs
 * ================================================================================
 * 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.spike.schema;

import com.google.common.collect.Multimap;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.commons.io.IOUtils;
import org.onap.aai.cl.eelf.LoggerFactory;
import org.onap.aai.edges.EdgeIngestor;
import org.onap.aai.edges.EdgeRule;
import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
import org.onap.aai.setup.ConfigTranslator;
import org.onap.aai.setup.SchemaVersion;
import org.onap.aai.spike.exception.SpikeException;
import org.onap.aai.spike.logging.SpikeMsgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class EdgeRulesLoader {

    private static ConfigTranslator configTranslator;
    private static EdgeIngestor edgeIngestor;

    private static EdgePropsConfiguration edgePropsConfiguration;

    private static Map<String, RelationshipSchema> versionContextMap = new ConcurrentHashMap<>();

    static final Pattern versionPattern = Pattern.compile("(?i)v(\\d*)");
    static final String propsPrefix = "edge_properties_";
    static final String propsSuffix = ".json";
    final static Pattern propsFilePattern = Pattern.compile(propsPrefix + "(.*)" + propsSuffix);
    final static Pattern propsVersionPattern = Pattern.compile("(?i)v\\d*");

    private static org.onap.aai.cl.api.Logger logger =
            LoggerFactory.getInstance().getLogger(EdgeRulesLoader.class.getName());

    private EdgeRulesLoader() {}

    /**
     * This constructor presents an awkward marrying of Spring bean creation and static method use. This
     * is technical debt that will need fixing.
     *
     * @param configTranslator contains schema versions configuration
     * @param edgeIngestor provides edge rules
     * @param edgePropsConfiguration edge property validation configuration
     */
    @Autowired
    public EdgeRulesLoader(ConfigTranslator configTranslator, EdgeIngestor edgeIngestor,
            EdgePropsConfiguration edgePropsConfiguration) {
        EdgeRulesLoader.configTranslator = configTranslator;
        EdgeRulesLoader.edgeIngestor = edgeIngestor;
        EdgeRulesLoader.edgePropsConfiguration = edgePropsConfiguration;
    }

    /**
     * Finds all DB Edge Rules and Edge Properties files for all OXM models.
     *
     * @throws SpikeException
     */
    public static synchronized void loadModels() throws SpikeException {
        Map<String, File> propFiles = edgePropertyFiles(edgePropsConfiguration);

        if (logger.isDebugEnabled()) {
            logger.debug("Loading DB Edge Rules");
        }

        for (String version : OXMModelLoader.getLoadedOXMVersions()) {
            try {
                SchemaVersion schemaVersion = configTranslator.getSchemaVersions().getVersions().stream()
                        .filter(s -> s.toString().equalsIgnoreCase(version)).findAny().orElse(null);
                loadModel(schemaVersion, edgeIngestor, propFiles);
            } catch (IOException | EdgeRuleNotFoundException e) {
                throw new SpikeException(e.getMessage(), e);
            }
        }
    }

    /**
     * Loads DB Edge Rules and Edge Properties for a given version.
     *
     * @throws SpikeException
     */

    public static synchronized void loadModels(String v) throws SpikeException {
        Map<String, File> propFiles = edgePropertyFiles(edgePropsConfiguration);

        if (logger.isDebugEnabled()) {
            logger.debug("Loading DB Edge Rules ");
        }

        try {
            SchemaVersion schemaVersion = configTranslator.getSchemaVersions().getVersions().stream()
                    .filter(s -> s.toString().equalsIgnoreCase(v)).findAny().orElse(null);

            loadModel(schemaVersion, edgeIngestor, propFiles);
        } catch (IOException | EdgeRuleNotFoundException e) {
            throw new SpikeException(e.getMessage());
        }
    }

    /**
     * Retrieves the DB Edge Rule relationship schema for a given version.
     *
     * @param version - The OXM version that we want the DB Edge Rule for.
     * @return - A RelationshipSchema of the DB Edge Rule for the OXM version.
     * @throws SpikeException
     */
    public static RelationshipSchema getSchemaForVersion(String version) throws SpikeException {

        // If we haven't already loaded in the available OXM models, then do so now.
        if (versionContextMap == null || versionContextMap.isEmpty()) {
            loadModels();
        } else if (!versionContextMap.containsKey(version)) {
            logger.error(SpikeMsgs.OXM_LOAD_ERROR, "Error loading DB Edge Rules for: " + version);
            throw new SpikeException("Error loading DB Edge Rules for: " + version);
        }

        return versionContextMap.get(version);
    }

    /**
     * Retrieves the DB Edge Rule relationship schema for all loaded OXM versions.
     *
     * @return - A Map of the OXM version and it's corresponding RelationshipSchema of the DB Edge Rule.
     * @throws SpikeException
     */
    public static Map<String, RelationshipSchema> getSchemas() throws SpikeException {

        // If we haven't already loaded in the available OXM models, then do so now.
        if (versionContextMap == null || versionContextMap.isEmpty()) {
            loadModels();
        }
        return versionContextMap;
    }

    /**
     * Returns the latest available DB Edge Rule version.
     *
     * @return - A Map of the OXM version and it's corresponding RelationshipSchema of the DB Edge Rule.
     * @throws SpikeException
     */
    public static String getLatestSchemaVersion() throws SpikeException {

        // If we haven't already loaded in the available OXM models, then do so now.
        if (versionContextMap == null || versionContextMap.isEmpty()) {
            loadModels();
        }

        // If there are still no models available, then there's not much we can do...
        if (versionContextMap.isEmpty()) {
            logger.error(SpikeMsgs.OXM_LOAD_ERROR, "No available DB Edge Rules to get latest version for.");
            throw new SpikeException("No available DB Edge Rules to get latest version for.");
        }

        // Iterate over the available model versions to determine which is the most
        // recent.
        Integer latestVersion = null;
        String latestVersionStr = null;
        for (String versionKey : versionContextMap.keySet()) {

            Matcher matcher = versionPattern.matcher(versionKey);
            if (matcher.find()) {

                int currentVersion = Integer.parseInt(matcher.group(1));

                if ((latestVersion == null) || (currentVersion > latestVersion)) {
                    latestVersion = currentVersion;
                    latestVersionStr = versionKey;
                }
            }
        }

        return latestVersionStr;
    }

    /**
     * Reset the loaded DB Edge Rule schemas
     *
     */
    public static void resetSchemaVersionContext() {
        versionContextMap = new ConcurrentHashMap<>();
    }

    private static synchronized void loadModel(SchemaVersion version, EdgeIngestor edgeIngestor,
            Map<String, File> props) throws IOException, SpikeException, EdgeRuleNotFoundException {

        Multimap<String, EdgeRule> edges = edgeIngestor.getAllRules(version);
        String edgeProps;
        if (props.get(version.toString().toLowerCase()) != null) {
            edgeProps = IOUtils.toString(new FileInputStream(props.get(version.toString().toLowerCase())), "UTF-8");
        } else {
            throw new FileNotFoundException("The Edge Properties file for OXM version " + version + "was not found.");
        }
        if (edges != null) {
            RelationshipSchema rs = new RelationshipSchema(edges, edgeProps);
            versionContextMap.put(version.toString().toLowerCase(), rs);
            logger.info(SpikeMsgs.LOADED_DB_RULE_FILE, version.toString());
        }
    }

    private static Map<String, File> edgePropertyFiles(EdgePropsConfiguration edgePropsConfiguration)
            throws SpikeException {
        Map<String, File> propsFiles = Arrays
                .stream(new File(edgePropsConfiguration.getEdgePropsDir())
                        .listFiles((d, name) -> propsFilePattern.matcher(name).matches()))
                .collect(Collectors.toMap(new Function<File, String>() {
                    @Override
                    public String apply(File f) {
                        Matcher m1 = propsVersionPattern.matcher(f.getName());
                        m1.find();
                        return m1.group(0);
                    }
                }, f -> f));
        return propsFiles;
    }
}