aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/onap')
-rw-r--r--src/main/java/org/onap/crud/logging/CrudServiceMsgs.java8
-rw-r--r--src/main/java/org/onap/crud/parser/CrudResponseBuilder.java6
-rw-r--r--src/main/java/org/onap/crud/service/AaiResourceService.java12
-rw-r--r--src/main/java/org/onap/crud/util/CrudServiceUtil.java4
-rw-r--r--src/main/java/org/onap/schema/EdgeRulesLoader.java231
-rw-r--r--src/main/java/org/onap/schema/OxmModelLoader.java34
-rw-r--r--src/main/java/org/onap/schema/RelationshipSchema.java24
-rw-r--r--src/main/java/org/onap/schema/RelationshipSchemaLoader.java272
-rw-r--r--src/main/java/org/onap/schema/RelationshipSchemaValidator.java12
-rw-r--r--src/main/java/org/onap/schema/util/SchemaIngestPropertyReader.java32
10 files changed, 325 insertions, 310 deletions
diff --git a/src/main/java/org/onap/crud/logging/CrudServiceMsgs.java b/src/main/java/org/onap/crud/logging/CrudServiceMsgs.java
index 72883a9..2b669ae 100644
--- a/src/main/java/org/onap/crud/logging/CrudServiceMsgs.java
+++ b/src/main/java/org/onap/crud/logging/CrudServiceMsgs.java
@@ -46,6 +46,14 @@ public enum CrudServiceMsgs implements LogMessageEnum {
LOADED_OXM_FILE,
/**
+ * Successfully loaded Edge Properties Files: {0}
+ *
+ * <p>
+ * Arguments: {0} = oxm filename
+ */
+ LOADED_DB_RULE_FILE,
+
+ /**
* Unable to load OXM schema: {0}
*
* <p>
diff --git a/src/main/java/org/onap/crud/parser/CrudResponseBuilder.java b/src/main/java/org/onap/crud/parser/CrudResponseBuilder.java
index e28f7a7..0c66d81 100644
--- a/src/main/java/org/onap/crud/parser/CrudResponseBuilder.java
+++ b/src/main/java/org/onap/crud/parser/CrudResponseBuilder.java
@@ -34,7 +34,7 @@ import org.onap.crud.exception.CrudException;
import org.onap.crud.service.BulkPayload;
import org.onap.crud.service.EdgePayload;
import org.onap.crud.service.VertexPayload;
-import org.onap.schema.RelationshipSchemaLoader;
+import org.onap.schema.EdgeRulesLoader;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@@ -120,7 +120,7 @@ public class CrudResponseBuilder {
EdgePayload inEdge = new EdgePayload();
inEdge.setId(e.getId().get());
inEdge.setType(e.getType());
- inEdge.setUrl(URL_BASE + "relationships/" + RelationshipSchemaLoader.getLatestSchemaVersion() + "/"
+ inEdge.setUrl(URL_BASE + "relationships/" + EdgeRulesLoader.getLatestSchemaVersion() + "/"
+ e.getType() + "/" + e.getId().get());
inEdge.setSource(URL_BASE + version + "/" + e.getSource().getType() + "/" + e.getSource().getId().get());
@@ -129,7 +129,7 @@ public class CrudResponseBuilder {
EdgePayload outEdge = new EdgePayload();
outEdge.setId(e.getId().get());
outEdge.setType(e.getType());
- outEdge.setUrl(URL_BASE + "relationships/" + RelationshipSchemaLoader.getLatestSchemaVersion() + "/"
+ outEdge.setUrl(URL_BASE + "relationships/" + EdgeRulesLoader.getLatestSchemaVersion() + "/"
+ e.getType() + "/" + e.getId().get());
outEdge.setTarget(URL_BASE + version + "/" + e.getTarget().getType() + "/" + e.getTarget().getId().get());
outEdges.add(outEdge);
diff --git a/src/main/java/org/onap/crud/service/AaiResourceService.java b/src/main/java/org/onap/crud/service/AaiResourceService.java
index 6dfbab7..c9a5805 100644
--- a/src/main/java/org/onap/crud/service/AaiResourceService.java
+++ b/src/main/java/org/onap/crud/service/AaiResourceService.java
@@ -56,7 +56,7 @@ import org.onap.crud.logging.CrudServiceMsgs;
import org.onap.crud.logging.LoggingUtil;
import org.onap.crud.service.CrudRestService.Action;
import org.onap.crud.util.CrudServiceConstants;
-import org.onap.schema.RelationshipSchemaLoader;
+import org.onap.schema.EdgeRulesLoader;
import org.onap.schema.RelationshipSchemaValidator;
import org.slf4j.MDC;
@@ -161,11 +161,11 @@ public class AaiResourceService {
payload = applyEdgeRulesToPayload(payload);
if(logger.isDebugEnabled()) {
- logger.debug("Creating AAI edge using version " + RelationshipSchemaLoader.getLatestSchemaVersion() );
+ logger.debug("Creating AAI edge using version " + EdgeRulesLoader.getLatestSchemaVersion() );
}
// Now, create our edge in the graph store.
- String result = graphDataService.addEdge(RelationshipSchemaLoader.getLatestSchemaVersion(), type, payload);
+ String result = graphDataService.addEdge(EdgeRulesLoader.getLatestSchemaVersion(), type, payload);
response = Response.status(Status.CREATED).entity(result).type(mediaType).build();
} catch (CrudException e) {
@@ -229,7 +229,7 @@ public class AaiResourceService {
payload = applyEdgeRulesToPayload(payload);
// Now, create our edge in the graph store.
- String result = graphDataService.addEdge(RelationshipSchemaLoader.getLatestSchemaVersion(), payload.getType(), payload);
+ String result = graphDataService.addEdge(EdgeRulesLoader.getLatestSchemaVersion(), payload.getType(), payload);
response = Response.status(Status.CREATED).entity(result).type(mediaType).build();
} catch (CrudException ce) {
@@ -304,10 +304,10 @@ public class AaiResourceService {
String result;
if (headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE) != null &&
headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE).equalsIgnoreCase("PATCH")) {
- result = graphDataService.patchEdge(RelationshipSchemaLoader.getLatestSchemaVersion(), id, type, payload);
+ result = graphDataService.patchEdge(EdgeRulesLoader.getLatestSchemaVersion(), id, type, payload);
} else {
- result = graphDataService.updateEdge(RelationshipSchemaLoader.getLatestSchemaVersion(), id, type, payload);
+ result = graphDataService.updateEdge(EdgeRulesLoader.getLatestSchemaVersion(), id, type, payload);
}
response = Response.status(Status.OK).entity(result).type(mediaType).build();
diff --git a/src/main/java/org/onap/crud/util/CrudServiceUtil.java b/src/main/java/org/onap/crud/util/CrudServiceUtil.java
index 4d108f2..6c251bc 100644
--- a/src/main/java/org/onap/crud/util/CrudServiceUtil.java
+++ b/src/main/java/org/onap/crud/util/CrudServiceUtil.java
@@ -23,7 +23,7 @@ package org.onap.crud.util;
import org.onap.aai.db.props.AAIProperties;
import org.onap.crud.exception.CrudException;
import org.onap.schema.OxmModelLoader;
-import org.onap.schema.RelationshipSchemaLoader;
+import org.onap.schema.EdgeRulesLoader;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
@@ -77,10 +77,10 @@ public class CrudServiceUtil {
// load the schemas
try {
OxmModelLoader.loadModels();
+ EdgeRulesLoader.loadModels ();
} catch (Exception e) {
throw new CrudException(e);
}
- RelationshipSchemaLoader.loadModels();
}
/**
diff --git a/src/main/java/org/onap/schema/EdgeRulesLoader.java b/src/main/java/org/onap/schema/EdgeRulesLoader.java
new file mode 100644
index 0000000..a990ae5
--- /dev/null
+++ b/src/main/java/org/onap/schema/EdgeRulesLoader.java
@@ -0,0 +1,231 @@
+/**
+ * ============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.schema;
+
+import com.google.common.collect.Multimap;
+import org.apache.commons.io.IOUtils;
+import org.onap.aai.cl.eelf.LoggerFactory;
+import org.onap.aai.edges.EdgeRule;
+import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
+import org.onap.aai.edges.EdgeIngestor;
+import org.onap.aai.setup.ConfigTranslator;
+import org.onap.aai.setup.SchemaLocationsBean;
+import org.onap.aai.setup.Version;
+import org.onap.crud.exception.CrudException;
+import org.onap.crud.logging.CrudServiceMsgs;
+import org.onap.schema.util.SchemaIngestPropertyReader;
+import org.springframework.core.io.UrlResource;
+
+import javax.ws.rs.core.Response.Status;
+import java.io.*;
+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;
+
+public class EdgeRulesLoader {
+
+ private static Map<String, RelationshipSchema> versionContextMap =
+ new ConcurrentHashMap<> ();
+
+ static final Pattern versionPattern = Pattern.compile ( "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 ( "v\\d*" );
+
+ private static org.onap.aai.cl.api.Logger logger =
+ LoggerFactory.getInstance ().getLogger ( EdgeRulesLoader.class.getName () );
+
+ private EdgeRulesLoader () {
+ }
+
+ /**
+ * Finds all DB Edge Rules and Edge Properties files for all OXM models.
+ *
+ * @throws CrudException
+ */
+ public static synchronized void loadModels () throws CrudException {
+ SchemaIngestPropertyReader schemaIngestPropertyReader = new SchemaIngestPropertyReader ();
+ SchemaLocationsBean schemaLocationsBean = new SchemaLocationsBean ();
+ schemaLocationsBean.setEdgeDirectory ( schemaIngestPropertyReader.getEdgeDir () );
+ ConfigTranslator configTranslator = new OxmModelConfigTranslator ( schemaLocationsBean );
+ EdgeIngestor edgeIngestor = new EdgeIngestor ( configTranslator );
+ Map<String, File> propFiles = edgePropertyFiles(schemaIngestPropertyReader);
+
+ if (logger.isDebugEnabled ()) {
+ logger.debug ( "Loading DB Edge Rules" );
+ }
+
+ for (String version : OxmModelLoader.getLoadedOXMVersions ()) {
+ try {
+ loadModel ( Version.valueOf ( version ), edgeIngestor, propFiles );
+ } catch (IOException | EdgeRuleNotFoundException e) {
+ throw new CrudException(e.getMessage (), e);
+ }
+ }
+ }
+
+ /**
+ * Loads DB Edge Rules and Edge Properties for a given version.
+ *
+ * @throws CrudException
+ */
+
+ public static synchronized void loadModels ( String v ) throws CrudException {
+ SchemaIngestPropertyReader schemaIngestPropertyReader = new SchemaIngestPropertyReader ();
+ SchemaLocationsBean schemaLocationsBean = new SchemaLocationsBean ();
+ schemaLocationsBean.setEdgeDirectory ( schemaIngestPropertyReader.getEdgeDir () );
+ ConfigTranslator configTranslator = new OxmModelConfigTranslator ( schemaLocationsBean );
+ EdgeIngestor edgeIngestor = new EdgeIngestor ( configTranslator );
+ String version = v.toUpperCase ();
+ Map<String, File> propFiles = edgePropertyFiles(schemaIngestPropertyReader);
+
+ if (logger.isDebugEnabled ()) {
+ logger.debug ( "Loading DB Edge Rules " );
+ }
+
+ try {
+ loadModel ( Version.valueOf ( version ), edgeIngestor, propFiles );
+ } catch (IOException | EdgeRuleNotFoundException e) {
+ throw new CrudException(e.getMessage (), Status.INTERNAL_SERVER_ERROR);
+ }
+ }
+
+ /**
+ * 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 CrudException
+ */
+ public static RelationshipSchema getSchemaForVersion ( String version ) throws CrudException {
+
+ // 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 ( CrudServiceMsgs.OXM_LOAD_ERROR, "Error loading DB Edge Rules for: " + version );
+ throw new CrudException ( "Error loading DB Edge Rules for: " + version, Status.NOT_FOUND );
+ }
+
+ 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 CrudException
+ */
+ public static Map<String, RelationshipSchema> getSchemas () throws CrudException {
+
+ // 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 CrudException
+ */
+ public static String getLatestSchemaVersion () throws CrudException {
+
+ // 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 ( CrudServiceMsgs.OXM_LOAD_ERROR, "No available DB Edge Rules to get latest version for." );
+ throw new CrudException ( "No available DB Edge Rules to get latest version for.",
+ Status.INTERNAL_SERVER_ERROR );
+ }
+
+ // 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.toUpperCase () );
+ 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 ( Version version, EdgeIngestor edgeIngestor, Map<String, File> props)
+ throws IOException, CrudException, 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 ( CrudServiceMsgs.LOADED_DB_RULE_FILE, version.toString () );
+ }
+ }
+
+ private static Map<String, File> edgePropertyFiles ( SchemaIngestPropertyReader dir ) throws CrudException {
+ Map<String, File> propsFiles = Arrays.stream ( new File ( dir.getEdgePropsDir () )
+ .listFiles ( ( d, name ) -> propsFilePattern.matcher ( name ).matches () ) )
+ .collect ( Collectors.toMap ( new Function<File, String> () {
+ public String apply ( File f ) {
+ Matcher m1 = propsVersionPattern.matcher ( f.getName () );
+ m1.find ();
+ return m1.group ( 0 );
+ }
+ }, f -> f ) );
+ return propsFiles;
+ }
+
+}
diff --git a/src/main/java/org/onap/schema/OxmModelLoader.java b/src/main/java/org/onap/schema/OxmModelLoader.java
index 869df8f..8f55602 100644
--- a/src/main/java/org/onap/schema/OxmModelLoader.java
+++ b/src/main/java/org/onap/schema/OxmModelLoader.java
@@ -21,6 +21,8 @@
package org.onap.schema;
import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
@@ -141,6 +143,38 @@ public class OxmModelLoader {
}
/**
+ * Retrieves the list of all Loaded OXM versions.
+ *
+ * @return - A List of Strings of all loaded OXM versions.
+ *
+ * @throws CrudException
+ */
+ public static List<String> getLoadedOXMVersions() throws CrudException {
+
+ // 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(CrudServiceMsgs.OXM_LOAD_ERROR, "No available OXM schemas to get versions for.");
+ throw new CrudException("No available OXM schemas to get versions for.",
+ Status.INTERNAL_SERVER_ERROR);
+ }
+
+ List<String> versions = new ArrayList<String> ();
+ for (String versionKey : versionContextMap.keySet()) {
+
+ Matcher matcher = versionPattern.matcher(versionKey.toUpperCase());
+ if (matcher.find()) {
+ versions.add ( "V" + matcher.group ( 1 ) );
+ }
+ }
+ return versions;
+ }
+
+ /**
* Retrieves the map of all JAXB context objects that have been created by importing the
* available OXM model schemas.
*
diff --git a/src/main/java/org/onap/schema/RelationshipSchema.java b/src/main/java/org/onap/schema/RelationshipSchema.java
index 557c374..fc91662 100644
--- a/src/main/java/org/onap/schema/RelationshipSchema.java
+++ b/src/main/java/org/onap/schema/RelationshipSchema.java
@@ -20,10 +20,9 @@
*/
package org.onap.schema;
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
-
+import com.google.common.collect.Multimap;
import org.codehaus.jackson.map.ObjectMapper;
+import org.onap.aai.edges.EdgeRule;
import org.onap.crud.exception.CrudException;
import java.io.IOException;
@@ -33,12 +32,8 @@ import javax.ws.rs.core.Response.Status;
public class RelationshipSchema {
- private static final Gson gson = new GsonBuilder().create();
- public static final String SCHEMA_SOURCE_NODE_TYPE = "from";
- public static final String SCHEMA_TARGET_NODE_TYPE = "to";
public static final String SCHEMA_RELATIONSHIP_TYPE = "label";
- public static final String SCHEMA_RULES_ARRAY = "rules";
private Map<String, Map<String, Class<?>>> relations = new HashMap<>();
@@ -47,12 +42,7 @@ public class RelationshipSchema {
*/
private Map<String, Map<String, Class<?>>> relationTypes = new HashMap<>();
-
- public RelationshipSchema(List<String> jsonStrings) throws CrudException, IOException {
- String edgeRules = jsonStrings.get(0);
- String props = jsonStrings.get(1);
-
- HashMap<String, ArrayList<LinkedHashMap<String, String>>> rules = new ObjectMapper().readValue(edgeRules, HashMap.class);
+ public RelationshipSchema( Multimap<String, EdgeRule> rules, String props) throws CrudException, IOException {
HashMap<String, String> properties = new ObjectMapper().readValue(props, HashMap.class);
Map<String, Class<?>> edgeProps = properties.entrySet().stream().collect(Collectors.toMap(p -> p.getKey(), p -> {
try {
@@ -63,14 +53,12 @@ public class RelationshipSchema {
return null;
}));
- rules.get(SCHEMA_RULES_ARRAY).forEach(l -> {
- relationTypes.put(l.get(SCHEMA_RELATIONSHIP_TYPE), edgeProps);
- relations.put(buildRelation(l.get(SCHEMA_SOURCE_NODE_TYPE), l.get(SCHEMA_TARGET_NODE_TYPE), l.get(SCHEMA_RELATIONSHIP_TYPE)), edgeProps);
+ rules.entries ().forEach ( (kv) -> {
+ relationTypes.put(kv.getValue ().getLabel (), edgeProps);
+ relations.put (buildRelation ( kv.getValue ().getFrom (), kv.getValue ().getTo (), kv.getValue ().getLabel ()), edgeProps);
});
}
-
-
public Map<String, Class<?>> lookupRelation(String key) {
return this.relations.get(key);
}
diff --git a/src/main/java/org/onap/schema/RelationshipSchemaLoader.java b/src/main/java/org/onap/schema/RelationshipSchemaLoader.java
deleted file mode 100644
index 9a9a37c..0000000
--- a/src/main/java/org/onap/schema/RelationshipSchemaLoader.java
+++ /dev/null
@@ -1,272 +0,0 @@
-/**
- * ============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.schema;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Comparator;
-import java.util.Date;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.SortedSet;
-import java.util.Timer;
-import java.util.TimerTask;
-import java.util.TreeSet;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-import java.util.stream.Collectors;
-import javax.ws.rs.core.Response.Status;
-import org.apache.commons.io.IOUtils;
-import org.onap.aai.cl.eelf.LoggerFactory;
-import org.onap.crud.exception.CrudException;
-import org.onap.crud.logging.CrudServiceMsgs;
-import org.onap.crud.util.CrudServiceConstants;
-import org.onap.crud.util.FileWatcher;
-import org.springframework.core.io.Resource;
-import org.springframework.core.io.UrlResource;
-import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
-import org.springframework.core.io.support.ResourcePatternResolver;
-
-public class RelationshipSchemaLoader {
-
- private static Map<String, RelationshipSchema> versionContextMap = new ConcurrentHashMap<>();
- private static SortedSet<Integer> versions = new TreeSet<Integer>();
- private static Map<String, Timer> timers = new ConcurrentHashMap<String, Timer>();
- final static String edgePropsFiles = "edge_properties_";
- final static String fileExt = ".json";
- final static Pattern rulesFilePattern = Pattern.compile("DbEdgeRules(.*)" + fileExt);
- final static Pattern propsFilePattern = Pattern.compile(edgePropsFiles + "(.*)" + fileExt);
- final static Pattern versionPattern = Pattern.compile(".*(v\\d+)" + fileExt);
-
- private static org.onap.aai.cl.api.Logger logger = LoggerFactory.getInstance()
- .getLogger(RelationshipSchemaLoader.class.getName());
-
- public synchronized static void loadModels() throws CrudException {
- load(rulesFilePattern, propsFilePattern);
- }
-
- public synchronized static void loadModels(String version) throws CrudException {
- String pattern = String.format("DbEdgeRules.*(%s)" + fileExt, version);
- load(Pattern.compile(pattern), Pattern.compile(edgePropsFiles + version + fileExt));
- }
-
- public static RelationshipSchema getSchemaForVersion(String version) throws CrudException {
- if (versionContextMap == null || versionContextMap.isEmpty()) {
- loadModels();
- } else if (!versionContextMap.containsKey(version)) {
- try {
- loadModels(version);
- } catch (Exception e) {
- throw new CrudException("", Status.NOT_FOUND);
- }
- }
- RelationshipSchema schema = versionContextMap.get(version);
- if (schema == null) {
- throw new CrudException("", Status.NOT_FOUND);
- } else
- return schema;
- }
-
- public static String getLatestSchemaVersion() throws CrudException {
- return "v" + versions.last();
- }
-
- public static Map<String, RelationshipSchema> getVersionContextMap() {
- return versionContextMap;
- }
-
- public static void setVersionContextMap(Map<String, RelationshipSchema> versionContextMap) {
- RelationshipSchemaLoader.versionContextMap = versionContextMap;
- }
-
- public static void resetVersionContextMap() {
- RelationshipSchemaLoader.versionContextMap = new ConcurrentHashMap<>();
- }
-
- private static void load(Pattern rulesPattern, Pattern edgePropsPattern) throws CrudException {
- ClassLoader cl = RelationshipSchemaLoader.class.getClassLoader();
- ResourcePatternResolver rulesResolver = new PathMatchingResourcePatternResolver(cl);
- List<Object> rulesFiles = new ArrayList<Object>();
- Set<String> existingFiles = new HashSet<String>();
- String rulesDir = CrudServiceConstants.CRD_HOME_MODEL;
- try {
-
- // Allow additional DBEdgeRule files to be dropped in manually (in addition to those found on the classpath)
- File[] edgeRuleFileList = new File(rulesDir).listFiles((d, name) -> rulesPattern.matcher(name).matches());
- for (File file : edgeRuleFileList) {
- rulesFiles.add(file);
- existingFiles.add(filename(file));
- }
-
- // Get DBEdgeRules from the jar on the classpath. Don't include any that conflict with files which
- // were dropped manually.
- Resource[] rawResourceList = rulesResolver.getResources("classpath*:/dbedgerules/DbEdgeRules*" + fileExt);
- List<Resource> prunedResourceList = new ArrayList<Resource>();
- for (Resource resource : rawResourceList) {
- if (!existingFiles.contains(filename(resource))) {
- prunedResourceList.add(resource);
- }
- }
-
- rulesFiles.addAll(Arrays.stream(prunedResourceList.toArray(new Resource[prunedResourceList.size()]))
- .filter(r -> !myMatcher(rulesPattern, r.getFilename()).isEmpty()).collect(Collectors.toList()));
-
- // This gets all the objects of type "File" from external directory (not
- // on the classpath)
- // 1. From an external directory (one not on the classpath) we get all the
- // objects of type "File"
- // 2. We only return the files whose names matched the supplied pattern
- // "p2".
- // 3. We then collect all the objects in a list and add the contents of
- // this list
- // to the previous collection (rulesFiles)
- rulesFiles
- .addAll(Arrays.stream(new File(rulesDir).listFiles( (d, name) -> edgePropsPattern.matcher(name).matches() ))
- .collect(Collectors.toList()));
-
- if (rulesFiles.isEmpty()) {
- logger.error(CrudServiceMsgs.INVALID_OXM_DIR, rulesDir);
- throw new FileNotFoundException("DbEdgeRules and edge_properties files were not found.");
- }
-
- // Sort and then group the files with their versions, convert them to the
- // schema, and add them to versionContextMap
- // 1. Sort the files. We need the DbEdgeRules files to be before the
- // edgeProperties files.
- // 2. Group the files with their versions. ie. v11 ->
- // ["DbEdgeRule_v11.json", "edgeProperties_v11.json"].
- // The "group method" returns a HashMap whose key is the version and the
- // value is a list of objects.
- // 3. Go through each version and map the files into one schema using the
- // "jsonFilesLoader" method.
- // Also update the "versionContextMap" with the version and it's schema.
- rulesFiles.stream().sorted(Comparator.comparing(RelationshipSchemaLoader::filename))
- .collect(Collectors.groupingBy(f -> myMatcher(versionPattern, filename(f))))
- .forEach((version, resourceAndFile) -> {
- if (resourceAndFile.size() == 2) {
- versionContextMap.put(version, jsonFilesLoader(version, resourceAndFile));
- } else {
- String filenames = resourceAndFile.stream().map(f -> filename(f)).collect(Collectors.toList()).toString();
- String errorMsg = "Expecting a rules and a edge_properties files for " + version + ". Found: "
- + filenames;
- logger.warn(CrudServiceMsgs.INVALID_OXM_FILE, errorMsg);
- }
- });
- logger.info(CrudServiceMsgs.LOADED_OXM_FILE, "Relationship Schema and Properties files: "
- + rulesFiles.stream().map(f -> filename(f)).collect(Collectors.toList()));
- } catch (IOException e) {
- logger.error(CrudServiceMsgs.INVALID_OXM_DIR, rulesDir);
- throw new CrudException("DbEdgeRules or edge_properties files were not found.", new FileNotFoundException());
- }
- }
-
- private static String filename(Object k) throws ClassCastException {
- if (k instanceof UrlResource) {
- return ((UrlResource) k).getFilename();
- } else if (k instanceof File) {
- return ((File) k).getName();
- } else {
- throw new ClassCastException();
- }
- }
-
- private static RelationshipSchema jsonFilesLoader(String version, List<Object> files) {
- List<String> fileContents = new ArrayList<>();
- RelationshipSchema rsSchema = null;
- if (files.size() == 2) {
- for (Object file : files) {
- fileContents.add(jsonToRelationSchema(version, file));
- versions.add(Integer.parseInt(version.substring(1)));
- }
-
- try {
- rsSchema = new RelationshipSchema(fileContents);
- } catch (CrudException | IOException e) {
- e.printStackTrace();
- logger.error(CrudServiceMsgs.INVALID_OXM_FILE,
- files.stream().map(f -> filename(f)).collect(Collectors.toList()).toString(), e.getMessage());
- }
- return rsSchema;
- }
- return rsSchema;
- }
-
- private synchronized static void updateVersionContext(String version, RelationshipSchema rs) {
- versionContextMap.put(version, rs);
- }
-
- private synchronized static String jsonToRelationSchema(String version, Object file) {
- InputStream inputStream = null;
- String content = null;
-
- try {
- if (file instanceof UrlResource) {
- inputStream = ((UrlResource) file).getInputStream();
- } else {
- inputStream = new FileInputStream((File) file);
- addtimer(version, file);
- }
- content = IOUtils.toString(inputStream, "UTF-8");
- } catch (IOException e) {
- e.printStackTrace();
- }
- return content;
- }
-
- private static void addtimer(String version, Object file) {
- TimerTask task = null;
- task = new FileWatcher((File) file) {
- protected void onChange(File file) {
- // here we implement the onChange
- logger.info(CrudServiceMsgs.OXM_FILE_CHANGED, file.getName());
-
- try {
- // Cannot use the file object here because we also have to get the
- // edge properties associated with that version.
- // The properties are stored in a different file.
- RelationshipSchemaLoader.loadModels(version);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- };
-
- if (!timers.containsKey(version)) {
- Timer timer = new Timer("db_edge_rules_" + version);
- timer.schedule(task, new Date(), 10000);
- timers.put(version, timer);
-
- }
- }
-
- private static String myMatcher(Pattern p, String s) {
- Matcher m = p.matcher(s);
- return m.matches() ? m.group(1) : "";
- }
-} \ No newline at end of file
diff --git a/src/main/java/org/onap/schema/RelationshipSchemaValidator.java b/src/main/java/org/onap/schema/RelationshipSchemaValidator.java
index 4b05c6f..15b1762 100644
--- a/src/main/java/org/onap/schema/RelationshipSchemaValidator.java
+++ b/src/main/java/org/onap/schema/RelationshipSchemaValidator.java
@@ -49,7 +49,7 @@ public class RelationshipSchemaValidator {
Map<String, String> filter)
throws CrudException {
- RelationshipSchema schema = RelationshipSchemaLoader.getSchemaForVersion(version);
+ RelationshipSchema schema = EdgeRulesLoader.getSchemaForVersion(version);
if (schema == null) {
throw new CrudException("", Status.NOT_FOUND);
}
@@ -77,7 +77,7 @@ public class RelationshipSchemaValidator {
public static void validateType(String version, String type) throws CrudException {
- RelationshipSchema schema = RelationshipSchemaLoader.getSchemaForVersion(version);
+ RelationshipSchema schema = EdgeRulesLoader.getSchemaForVersion(version);
if (!schema.isValidType(type)) {
throw new CrudException("Invalid " + RelationshipSchema.SCHEMA_RELATIONSHIP_TYPE
+ ": " + type,
@@ -101,7 +101,7 @@ public class RelationshipSchemaValidator {
public static Edge validateIncomingAddPayload(String version, String type, EdgePayload payload)
throws CrudException {
- RelationshipSchema schema = RelationshipSchemaLoader.getSchemaForVersion(version);
+ RelationshipSchema schema = EdgeRulesLoader.getSchemaForVersion(version);
try {
@@ -151,7 +151,7 @@ public class RelationshipSchemaValidator {
public static Edge validateIncomingPatchPayload(Edge edge, String version, EdgePayload payload)
throws CrudException {
- RelationshipSchema schema = RelationshipSchemaLoader.getSchemaForVersion(version);
+ RelationshipSchema schema = EdgeRulesLoader.getSchemaForVersion(version);
try {
if (payload.getSource() != null) {
@@ -243,7 +243,7 @@ public class RelationshipSchemaValidator {
public static Edge validateIncomingUpdatePayload(Edge edge, String version, EdgePayload payload)
throws CrudException {
- RelationshipSchema schema = RelationshipSchemaLoader.getSchemaForVersion(version);
+ RelationshipSchema schema = EdgeRulesLoader.getSchemaForVersion(version);
try {
@@ -328,7 +328,7 @@ public class RelationshipSchemaValidator {
.get()).source(edge.getSource())
.target(edge.getTarget());
- RelationshipSchema schema = RelationshipSchemaLoader.getSchemaForVersion(version);
+ RelationshipSchema schema = EdgeRulesLoader.getSchemaForVersion(version);
String key = edge.getSource().getType() + ":" + edge.getTarget().getType()
+ ":" + edge.getType();
diff --git a/src/main/java/org/onap/schema/util/SchemaIngestPropertyReader.java b/src/main/java/org/onap/schema/util/SchemaIngestPropertyReader.java
index 71ea47e..ca5fa6b 100644
--- a/src/main/java/org/onap/schema/util/SchemaIngestPropertyReader.java
+++ b/src/main/java/org/onap/schema/util/SchemaIngestPropertyReader.java
@@ -46,11 +46,37 @@ public class SchemaIngestPropertyReader {
* Gets the location of the OXM
*
* @return
- * @throws SpikeException
- * @throws IOException
+ * @throws CrudException
*/
public String getNodeDir() throws CrudException {
+ return getProps ().getProperty("nodeDir");
+ }
+
+ /**
+ * Gets the location of the Edge Rules
+ *
+ * @return
+ * @throws CrudException
+ */
+ public String getEdgeDir() throws CrudException {
+
+ return getProps ().getProperty("edgeDir");
+ }
+
+ /**
+ * Gets the location of the Edge Properties
+ *
+ * @return
+ * @throws CrudException
+ */
+ public String getEdgePropsDir() throws CrudException {
+
+ return getProps ().getProperty("edgePropsDir");
+ }
+
+ private Properties getProps() throws CrudException {
+
Properties prop = new Properties();
try {
prop = loadFromFile(SCHEMA_INGEST_PROPERTIES_LOCATION);
@@ -66,7 +92,7 @@ public class SchemaIngestPropertyReader {
logger.error(CrudServiceMsgs.SCHEMA_INGEST_LOAD_ERROR, e.getMessage());
throw new CrudException("Failed to load schemaIngest.properties", e);
}
- return prop.getProperty("nodeDir");
+ return prop;
}
private Properties loadFromFile(String filename) throws IOException {