aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilliam Reehil <william.reehil@att.com>2020-07-07 19:02:24 +0000
committerGerrit Code Review <gerrit@onap.org>2020-07-07 19:02:24 +0000
commit162d6cec96a154606b9e92c2766172c99c269ae9 (patch)
tree8ddae69cc75d4a884c17a972ca17057f5d063399
parent9352ec8b7dc6e4d9eb5ee599049ee6e3c6dfbfb4 (diff)
parent27b2add68b647b1a744c949398bd963d8e35c84d (diff)
Merge "AAIResourcesUriTemplates - Removed Sonar warnings"
-rw-r--r--src/main/java/org/onap/aai/cacher/exceptions/MissingTemplateException.java29
-rw-r--r--src/main/java/org/onap/aai/cacher/injestion/parser/strategy/aai/AAIResourcesUriTemplates.java76
2 files changed, 70 insertions, 35 deletions
diff --git a/src/main/java/org/onap/aai/cacher/exceptions/MissingTemplateException.java b/src/main/java/org/onap/aai/cacher/exceptions/MissingTemplateException.java
new file mode 100644
index 0000000..d47541b
--- /dev/null
+++ b/src/main/java/org/onap/aai/cacher/exceptions/MissingTemplateException.java
@@ -0,0 +1,29 @@
+/**
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright © 2020 Bell 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.cacher.exceptions;
+
+public class MissingTemplateException extends RuntimeException {
+
+ public static final String DEFAULT_EXCEPTION_MESSAGE = "Failed in uriToTemplates for truncatedUri '%s'";
+
+ public MissingTemplateException(String uri) {
+ super(String.format(DEFAULT_EXCEPTION_MESSAGE, uri));
+ }
+}
diff --git a/src/main/java/org/onap/aai/cacher/injestion/parser/strategy/aai/AAIResourcesUriTemplates.java b/src/main/java/org/onap/aai/cacher/injestion/parser/strategy/aai/AAIResourcesUriTemplates.java
index 887e4d3..819a7a6 100644
--- a/src/main/java/org/onap/aai/cacher/injestion/parser/strategy/aai/AAIResourcesUriTemplates.java
+++ b/src/main/java/org/onap/aai/cacher/injestion/parser/strategy/aai/AAIResourcesUriTemplates.java
@@ -24,6 +24,7 @@ import com.att.eelf.configuration.EELFManager;
import com.google.gson.JsonObject;
import org.apache.commons.lang3.StringUtils;
import org.onap.aai.annotations.Metadata;
+import org.onap.aai.cacher.exceptions.MissingTemplateException;
import org.onap.aai.cacher.util.AAIConstants;
import org.reflections.Reflections;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
@@ -44,7 +45,7 @@ import java.util.*;
@Scope(scopeName = ConfigurableBeanFactory.SCOPE_SINGLETON)
public class AAIResourcesUriTemplates {
- private final static EELFLogger LOGGER = EELFManager.getInstance().getLogger(AAIResourcesUriTemplates.class);
+ private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AAIResourcesUriTemplates.class);
private final Map<String, String> typeToUriTemplate;
@@ -63,13 +64,13 @@ public class AAIResourcesUriTemplates {
Reflections reflections = new Reflections("org.onap.aai.domain.yang");
reflections.getTypesAnnotatedWith(Metadata.class)
- .stream()
- .filter(aClass -> "org.onap.aai.domain.yang".equals(aClass.getPackage().getName()))
- .filter(aClass -> !aClass.getAnnotation(Metadata.class).uriTemplate().isEmpty())
- .forEach(aClass -> typeToUriTemplate.put(
- aClass.getAnnotation(XmlRootElement.class).name(),
- aClass.getAnnotation(Metadata.class).uriTemplate())
- );
+ .stream()
+ .filter(aClass -> "org.onap.aai.domain.yang".equals(aClass.getPackage().getName()))
+ .filter(aClass -> !aClass.getAnnotation(Metadata.class).uriTemplate().isEmpty())
+ .forEach(aClass -> typeToUriTemplate.put(
+ aClass.getAnnotation(XmlRootElement.class).name(),
+ aClass.getAnnotation(Metadata.class).uriTemplate())
+ );
LOGGER.info("AAI uri templates: " + typeToUriTemplate);
}
@@ -80,7 +81,7 @@ public class AAIResourcesUriTemplates {
/**
* Get templated aai uri segment by type.
- *
+ *
* @param type
* @return
*/
@@ -90,6 +91,7 @@ public class AAIResourcesUriTemplates {
/**
* For the given template and uri get the variable key value pairs
+ *
* @param uri
* @param template
* @return
@@ -106,7 +108,7 @@ public class AAIResourcesUriTemplates {
/**
* For a given uri get an ordered list of templates.
- *
+ *
* @param uri
* @return
*/
@@ -118,19 +120,20 @@ public class AAIResourcesUriTemplates {
while (truncatedUri.contains("/")) {
matchingStartingTemplate = this.getMatchingStartingTemplate(truncatedUri);
- if ( !matchingStartingTemplate.isPresent()) {
+ if (!matchingStartingTemplate.isPresent()) {
LOGGER.error("failed in uriToTemplates for truncatedUri " + truncatedUri);
- // exception expected for missing template
- }
- template = matchingStartingTemplate.get();
- uriTemplateList.add(template);
- int count = StringUtils.countMatches(template, "/");
- if (count < StringUtils.countMatches(truncatedUri, "/")) {
- truncatedUri = StringUtils.substring(
+ throw new MissingTemplateException(truncatedUri);
+ } else {
+ template = matchingStartingTemplate.get();
+ uriTemplateList.add(template);
+ int count = StringUtils.countMatches(template, "/");
+ if (count < StringUtils.countMatches(truncatedUri, "/")) {
+ truncatedUri = StringUtils.substring(
truncatedUri,
StringUtils.ordinalIndexOf(truncatedUri, "/", count + 1));
- } else {
- truncatedUri = "";
+ } else {
+ truncatedUri = "";
+ }
}
}
@@ -139,7 +142,7 @@ public class AAIResourcesUriTemplates {
/**
* For a given uri get an ordered list of templates.
- *
+ *
* @param uri
* @return
*/
@@ -149,14 +152,17 @@ public class AAIResourcesUriTemplates {
String truncatedUri = uri;
while (truncatedUri.contains("/")) {
- template = this.getMatchingStartingTemplate(truncatedUri).get();
- int count = StringUtils.countMatches(template, "/");
- int cutIndex = truncatedUri.length();
- if (count != StringUtils.countMatches(truncatedUri, "/")) {
- cutIndex = StringUtils.ordinalIndexOf(truncatedUri, "/", count + 1);
+ Optional<String> templateOpt = this.getMatchingStartingTemplate(truncatedUri);
+ if (templateOpt.isPresent()) {
+ template = templateOpt.get();
+ int count = StringUtils.countMatches(template, "/");
+ int cutIndex = truncatedUri.length();
+ if (count != StringUtils.countMatches(truncatedUri, "/")) {
+ cutIndex = StringUtils.ordinalIndexOf(truncatedUri, "/", count + 1);
+ }
+ uriList.add(StringUtils.substring(truncatedUri, 0, cutIndex));
+ truncatedUri = StringUtils.substring(truncatedUri, cutIndex);
}
- uriList.add(StringUtils.substring(truncatedUri, 0, cutIndex));
- truncatedUri = StringUtils.substring(truncatedUri, cutIndex);
}
return uriList;
@@ -164,7 +170,7 @@ public class AAIResourcesUriTemplates {
/**
* returns the template matching the start of the uri.
- *
+ *
* @param uri
* @return @see java.util.Optional
*/
@@ -174,7 +180,7 @@ public class AAIResourcesUriTemplates {
/**
* Given aai type and json object generate the uri for it.
- *
+ *
* @param type
* @param jo
* @return
@@ -190,7 +196,7 @@ public class AAIResourcesUriTemplates {
/**
* Get encoded values from json object for each key in keys
- *
+ *
* @param keys
* @param jo
* @return
@@ -204,7 +210,7 @@ public class AAIResourcesUriTemplates {
/**
* extract uri keys from the templated uri
- *
+ *
* @param template
* @return
*/
@@ -216,7 +222,7 @@ public class AAIResourcesUriTemplates {
/**
* UTF-8 encoding of @param string
- *
+ *
* @param string string to be encoded
* @return
*/
@@ -230,7 +236,7 @@ public class AAIResourcesUriTemplates {
/**
* UTF-8 decoding of @param string
- *
+ *
* @param string string to be encoded
* @return
*/
@@ -260,7 +266,7 @@ public class AAIResourcesUriTemplates {
for (int i = 0; i < uriSegments.size(); i++) {
aus = new AAIUriSegment(uriSegments.get(i), uriSegmentTemplates.get(i));
aus.setSegmentKeyValues(
- getUriTemplateMappings(aus.getSegment(), aus.getSegmentTemplate()));
+ getUriTemplateMappings(aus.getSegment(), aus.getSegmentTemplate()));
uriSegmentList.add(aus);
}
return uriSegmentList;