aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openecomp/schema
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/openecomp/schema')
-rw-r--r--src/main/java/org/openecomp/schema/OxmModelValidator.java108
-rw-r--r--src/main/java/org/openecomp/schema/RelationshipSchemaLoader.java99
2 files changed, 95 insertions, 112 deletions
diff --git a/src/main/java/org/openecomp/schema/OxmModelValidator.java b/src/main/java/org/openecomp/schema/OxmModelValidator.java
index e51b23f..79136e4 100644
--- a/src/main/java/org/openecomp/schema/OxmModelValidator.java
+++ b/src/main/java/org/openecomp/schema/OxmModelValidator.java
@@ -44,11 +44,8 @@ import javax.ws.rs.core.Response.Status;
public class OxmModelValidator {
public enum Metadata {
- NODE_TYPE("aai-node-type"),
- URI("aai-uri"),
- CREATED_TS("aai-created-ts"),
- SOT("source-of-truth"),
- LAST_MOD_SOT("last-mod-source-of-truth");
+ NODE_TYPE("aai-node-type"), URI("aai-uri"), CREATED_TS("aai-created-ts"), SOT("source-of-truth"), LAST_MOD_SOT(
+ "last-mod-source-of-truth");
private final String propName;
@@ -70,9 +67,7 @@ public class OxmModelValidator {
}
}
-
- public static Map<String, Object> resolveCollectionfilter(String version, String type,
- Map<String, String> filter)
+ public static Map<String, Object> resolveCollectionfilter(String version, String type, Map<String, String> filter)
throws CrudException {
DynamicJAXBContext jaxbContext = null;
@@ -86,18 +81,15 @@ public class OxmModelValidator {
if (jaxbContext == null) {
throw new CrudException("", Status.NOT_FOUND);
}
- final DynamicType modelObjectType = jaxbContext.getDynamicType(CaseFormat.LOWER_CAMEL
- .to(CaseFormat.UPPER_CAMEL,
- CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, type)));
+ final DynamicType modelObjectType = jaxbContext.getDynamicType(
+ CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, type)));
for (String key : filter.keySet()) {
String keyJavaName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, key);
if (modelObjectType.getDescriptor().getMappingForAttributeName(keyJavaName) != null) {
try {
- DatabaseMapping mapping = modelObjectType.getDescriptor()
- .getMappingForAttributeName(keyJavaName);
- Object value = CrudServiceUtil.validateFieldType(filter.get(key),
- mapping.getField().getType());
+ DatabaseMapping mapping = modelObjectType.getDescriptor().getMappingForAttributeName(keyJavaName);
+ Object value = CrudServiceUtil.validateFieldType(filter.get(key), mapping.getField().getType());
result.put(key, value);
} catch (Exception ex) {
// Skip any exceptions thrown while validating the filter
@@ -125,9 +117,8 @@ public class OxmModelValidator {
}
// Determine if the Object part is a collection type in the model
// definition
- final DynamicType modelObjectType = jaxbContext.getDynamicType(CaseFormat.LOWER_CAMEL
- .to(CaseFormat.UPPER_CAMEL,
- CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, type)));
+ final DynamicType modelObjectType = jaxbContext.getDynamicType(
+ CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, type)));
if (modelObjectType == null) {
throw new CrudException("", Status.NOT_FOUND);
@@ -135,8 +126,7 @@ public class OxmModelValidator {
if (modelObjectType.getDescriptor().getMappings().size() == 1
&& modelObjectType.getDescriptor().getMappings().get(0).isCollectionMapping()) {
- String childJavaObjectName = modelObjectType.getDescriptor().getMappings()
- .get(0).getAttributeName();
+ String childJavaObjectName = modelObjectType.getDescriptor().getMappings().get(0).getAttributeName();
childJavaObjectName = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, childJavaObjectName);
final DynamicType childObjectType = jaxbContext.getDynamicType(childJavaObjectName);
if (childObjectType == null) {
@@ -151,9 +141,7 @@ public class OxmModelValidator {
}
-
- public static Vertex validateIncomingUpsertPayload(String id, String version, String type,
- JsonElement properties)
+ public static Vertex validateIncomingUpsertPayload(String id, String version, String type, JsonElement properties)
throws CrudException {
try {
@@ -165,18 +153,17 @@ public class OxmModelValidator {
final DynamicType modelObjectType = jaxbContext.getDynamicType(modelObjectClass);
final DynamicType reservedType = jaxbContext.getDynamicType("ReservedPropNames");
- Set<Map.Entry<String, JsonElement>> payloadEntriesSet = properties.getAsJsonObject()
- .entrySet();
+ Set<Map.Entry<String, JsonElement>> payloadEntriesSet = properties.getAsJsonObject().entrySet();
- //loop through input to validate against schema
+ // loop through input to validate against schema
for (Map.Entry<String, JsonElement> entry : payloadEntriesSet) {
String keyJavaName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, entry.getKey());
// check for valid field
if (modelObjectType.getDescriptor().getMappingForAttributeName(keyJavaName) == null) {
- if(reservedType.getDescriptor().getMappingForAttributeName(keyJavaName) == null){
- throw new CrudException("Invalid field: " + entry.getKey(), Status.BAD_REQUEST);
- }
+ if (reservedType.getDescriptor().getMappingForAttributeName(keyJavaName) == null) {
+ throw new CrudException("Invalid field: " + entry.getKey(), Status.BAD_REQUEST);
+ }
}
}
@@ -198,31 +185,26 @@ public class OxmModelValidator {
String keyName = field.getName().substring(0, field.getName().indexOf("/"));
- if (((XMLField) field).isRequired() && !entriesMap.containsKey(keyName)
- && !defaultValue.isEmpty()) {
- modelVertexBuilder.property(keyName,
- CrudServiceUtil.validateFieldType(defaultValue, field.getType()));
+ if (((XMLField) field).isRequired() && !entriesMap.containsKey(keyName) && !defaultValue.isEmpty()) {
+ modelVertexBuilder.property(keyName, CrudServiceUtil.validateFieldType(defaultValue, field.getType()));
}
// if schema field is required and not set then reject
- if (((XMLField) field).isRequired() && !entriesMap.containsKey(keyName)
- && defaultValue.isEmpty()) {
+ if (((XMLField) field).isRequired() && !entriesMap.containsKey(keyName) && defaultValue.isEmpty()) {
throw new CrudException("Missing required field: " + keyName, Status.BAD_REQUEST);
}
// If invalid field then reject
if (entriesMap.containsKey(keyName)) {
- Object value = CrudServiceUtil.validateFieldType(entriesMap.get(keyName)
- .getAsString(), field.getType());
+ Object value = CrudServiceUtil.validateFieldType(entriesMap.get(keyName).getAsString(), field.getType());
modelVertexBuilder.property(keyName, value);
}
// Set defaults
if (!defaultValue.isEmpty() && !entriesMap.containsKey(keyName)) {
- modelVertexBuilder.property(keyName,
- CrudServiceUtil.validateFieldType(defaultValue, field.getType()));
+ modelVertexBuilder.property(keyName, CrudServiceUtil.validateFieldType(defaultValue, field.getType()));
}
}
}
-
+
// Handle reserved properties
for (DatabaseMapping mapping : reservedType.getDescriptor().getMappings()) {
if (mapping.isAbstractDirectMapping()) {
@@ -230,8 +212,7 @@ public class OxmModelValidator {
String keyName = field.getName().substring(0, field.getName().indexOf("/"));
if (entriesMap.containsKey(keyName)) {
- Object value = CrudServiceUtil.validateFieldType(entriesMap.get(keyName)
- .getAsString(), field.getType());
+ Object value = CrudServiceUtil.validateFieldType(entriesMap.get(keyName).getAsString(), field.getType());
modelVertexBuilder.property(keyName, value);
}
}
@@ -243,9 +224,8 @@ public class OxmModelValidator {
}
}
- public static Vertex validateIncomingPatchPayload(String id, String version, String type,
- JsonElement properties, Vertex existingVertex)
- throws CrudException {
+ public static Vertex validateIncomingPatchPayload(String id, String version, String type, JsonElement properties,
+ Vertex existingVertex) throws CrudException {
try {
type = resolveCollectionType(version, type);
@@ -256,8 +236,7 @@ public class OxmModelValidator {
final DynamicType modelObjectType = jaxbContext.getDynamicType(modelObjectClass);
final DynamicType reservedType = jaxbContext.getDynamicType("ReservedPropNames");
- Set<Map.Entry<String, JsonElement>> payloadEntriesSet = properties.getAsJsonObject()
- .entrySet();
+ Set<Map.Entry<String, JsonElement>> payloadEntriesSet = properties.getAsJsonObject().entrySet();
// Loop through the payload properties and merge with existing
// vertex props
@@ -267,41 +246,34 @@ public class OxmModelValidator {
DatabaseField field = null;
String defaultValue = null;
-
+
if (modelObjectType.getDescriptor().getMappingForAttributeName(keyJavaName) != null) {
field = modelObjectType.getDescriptor().getMappingForAttributeName(keyJavaName).getField();
- defaultValue = modelObjectType.getDescriptor()
- .getMappingForAttributeName(keyJavaName)
- .getProperties().get("defaultValue") == null ? ""
- : modelObjectType.getDescriptor().getMappingForAttributeName(keyJavaName)
- .getProperties().get("defaultValue").toString();
- }
- else if (reservedType.getDescriptor().getMappingForAttributeName(keyJavaName) != null) {
+ defaultValue = modelObjectType.getDescriptor().getMappingForAttributeName(keyJavaName).getProperties()
+ .get("defaultValue") == null ? ""
+ : modelObjectType.getDescriptor().getMappingForAttributeName(keyJavaName).getProperties()
+ .get("defaultValue").toString();
+ } else if (reservedType.getDescriptor().getMappingForAttributeName(keyJavaName) != null) {
field = reservedType.getDescriptor().getMappingForAttributeName(keyJavaName).getField();
defaultValue = "";
}
-
+
if (field == null) {
throw new CrudException("Invalid field: " + entry.getKey(), Status.BAD_REQUEST);
}
// check if mandatory field is not set to null
- if (((XMLField) field).isRequired() && entry.getValue() instanceof JsonNull
- && !defaultValue.isEmpty()) {
+ if (((XMLField) field).isRequired() && entry.getValue() instanceof JsonNull && !defaultValue.isEmpty()) {
existingVertex.getProperties().put(entry.getKey(),
CrudServiceUtil.validateFieldType(defaultValue, field.getType()));
- } else if (((XMLField) field).isRequired() && entry.getValue() instanceof JsonNull
- && defaultValue.isEmpty()) {
- throw new CrudException("Mandatory field: " + entry.getKey()
- + " can't be set to null",
- Status.BAD_REQUEST);
+ } else if (((XMLField) field).isRequired() && entry.getValue() instanceof JsonNull && defaultValue.isEmpty()) {
+ throw new CrudException("Mandatory field: " + entry.getKey() + " can't be set to null", Status.BAD_REQUEST);
} else if (!((XMLField) field).isRequired() && entry.getValue() instanceof JsonNull
&& existingVertex.getProperties().containsKey(entry.getKey())) {
existingVertex.getProperties().remove(entry.getKey());
} else if (!(entry.getValue() instanceof JsonNull)) {
// add/update the value if found in existing vertex
- Object value = CrudServiceUtil.validateFieldType(entry.getValue().getAsString(),
- field.getType());
+ Object value = CrudServiceUtil.validateFieldType(entry.getValue().getAsString(), field.getType());
existingVertex.getProperties().put(entry.getKey(), value);
}
@@ -313,7 +285,7 @@ public class OxmModelValidator {
}
}
-
+
private static DatabaseField getDatabaseField(String fieldName, DynamicType modelObjectType) {
for (DatabaseField field : modelObjectType.getDescriptor().getAllFields()) {
int ix = field.getName().indexOf("/");
@@ -331,8 +303,7 @@ public class OxmModelValidator {
public static Vertex validateOutgoingPayload(String version, Vertex vertex) {
- Vertex.Builder modelVertexBuilder = new Vertex.Builder(vertex.getType())
- .id(vertex.getId().get());
+ Vertex.Builder modelVertexBuilder = new Vertex.Builder(vertex.getType()).id(vertex.getId().get());
try {
DynamicJAXBContext jaxbContext = OxmModelLoader.getContextForVersion(version);
@@ -357,5 +328,4 @@ public class OxmModelValidator {
}
-
}
diff --git a/src/main/java/org/openecomp/schema/RelationshipSchemaLoader.java b/src/main/java/org/openecomp/schema/RelationshipSchemaLoader.java
index 89a9e58..cc6315c 100644
--- a/src/main/java/org/openecomp/schema/RelationshipSchemaLoader.java
+++ b/src/main/java/org/openecomp/schema/RelationshipSchemaLoader.java
@@ -66,9 +66,8 @@ public class RelationshipSchemaLoader {
final static Pattern propsFilePattern = Pattern.compile(edgePropsFiles + "(.*)" + fileExt);
final static Pattern versionPattern = Pattern.compile(".*(v\\d+)" + fileExt);
-
private static org.openecomp.cl.api.Logger logger = LoggerFactory.getInstance()
- .getLogger(RelationshipSchemaLoader.class.getName());
+ .getLogger(RelationshipSchemaLoader.class.getName());
public synchronized static void loadModels() throws CrudException {
load(rulesFilePattern, propsFilePattern);
@@ -112,7 +111,6 @@ public class RelationshipSchemaLoader {
RelationshipSchemaLoader.versionContextMap = new ConcurrentHashMap<>();
}
-
private static void load(Pattern rulesPattern, Pattern edgePropsPattern) throws CrudException {
ClassLoader cl = RelationshipSchemaLoader.class.getClassLoader();
ResourcePatternResolver rulesResolver = new PathMatchingResourcePatternResolver(cl);
@@ -121,51 +119,66 @@ public class RelationshipSchemaLoader {
try {
// getResources method returns objects of type "Resource"
- // 1. We are getting all the objects from the classpath which has "DbEdgeRules" in the name.
- // 2. We run them through a filter and return only the objects which match the supplied pattern "p"
- // 3. We then collect the objects in a list. At this point we have a list of the kind of files we require.
+ // 1. We are getting all the objects from the classpath which has
+ // "DbEdgeRules" in the name.
+ // 2. We run them through a filter and return only the objects which match
+ // the supplied pattern "p"
+ // 3. We then collect the objects in a list. At this point we have a list
+ // of the kind of files we require.
rulesFiles = Arrays.stream(rulesResolver.getResources("classpath*:/dbedgerules/DbEdgeRules*" + fileExt))
- .filter(r -> !myMatcher(rulesPattern, r.getFilename()).isEmpty())
- .collect(Collectors.toList());
+ .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()));
+ // 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.
+ // 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()));
+ .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){
+ 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();
@@ -174,7 +187,7 @@ public class RelationshipSchemaLoader {
}
}
- private static RelationshipSchema jsonFilesLoader (String version, List<Object> files) {
+ private static RelationshipSchema jsonFilesLoader(String version, List<Object> files) {
List<String> fileContents = new ArrayList<>();
RelationshipSchema rsSchema = null;
if (files.size() == 2) {
@@ -188,29 +201,29 @@ public class RelationshipSchemaLoader {
} catch (CrudException | IOException e) {
e.printStackTrace();
logger.error(CrudServiceMsgs.INVALID_OXM_FILE,
- files.stream().map(f -> filename(f)).collect(Collectors.toList()).toString(), e.getMessage());
+ 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){
+ private synchronized static void updateVersionContext(String version, RelationshipSchema rs) {
versionContextMap.put(version, rs);
}
- private synchronized static String jsonToRelationSchema (String version, Object file) {
+ private synchronized static String jsonToRelationSchema(String version, Object file) {
InputStream inputStream = null;
String content = null;
try {
- if (file instanceof UrlResource) {
+ if (file instanceof UrlResource) {
inputStream = ((UrlResource) file).getInputStream();
} else {
inputStream = new FileInputStream((File) file);
addtimer(version, file);
}
- content = IOUtils.toString(inputStream, "UTF-8");
+ content = IOUtils.toString(inputStream, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
@@ -219,14 +232,14 @@ public class RelationshipSchemaLoader {
private static void addtimer(String version, Object file) {
TimerTask task = null;
- task = new FileWatcher(
- (File) file) {
+ 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.
+ // 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) {
@@ -243,7 +256,7 @@ public class RelationshipSchemaLoader {
}
}
- private static String myMatcher (Pattern p, String s) {
+ private static String myMatcher(Pattern p, String s) {
Matcher m = p.matcher(s);
return m.matches() ? m.group(1) : "";
}