aboutsummaryrefslogtreecommitdiffstats
path: root/controlloop/common/model-impl/aai/src/main
diff options
context:
space:
mode:
authorjh7358 <jh7358@att.com>2018-08-23 17:00:50 -0400
committerJim Hahn <jrh3@att.com>2018-08-23 18:40:43 -0400
commit51e88a932274a3ff8a93996332bb4f1d55d4a773 (patch)
tree2328dbc3e54fbf29fffa6ebe10e5dad4837a83f4 /controlloop/common/model-impl/aai/src/main
parent5bb419efe574df172555263110f9492980f5d1e0 (diff)
walk VF module names
Added code to walk the VF modules to count the number of modules and to generate the new module instance name. Added comment to test method. Fix checkstyle errors. Read json files instead of resources. Removed checkstyle plugin from aai pom, as it continues to barf every time I use some automated feature of Eclipse. Use Files.readAllBytes() instead of IoUtils.toString(uri). Try concatenating file path. Turned out to be a problem with file name case sensitivity - renamed the file. Change-Id: I1ce98d846dfa1d29e109b161c869108425d29771 Issue-ID: POLICY-1037 Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'controlloop/common/model-impl/aai/src/main')
-rw-r--r--controlloop/common/model-impl/aai/src/main/java/org/onap/policy/aai/AaiNqResponseWrapper.java115
1 files changed, 112 insertions, 3 deletions
diff --git a/controlloop/common/model-impl/aai/src/main/java/org/onap/policy/aai/AaiNqResponseWrapper.java b/controlloop/common/model-impl/aai/src/main/java/org/onap/policy/aai/AaiNqResponseWrapper.java
index a04d6768a..9d10cfd26 100644
--- a/controlloop/common/model-impl/aai/src/main/java/org/onap/policy/aai/AaiNqResponseWrapper.java
+++ b/controlloop/common/model-impl/aai/src/main/java/org/onap/policy/aai/AaiNqResponseWrapper.java
@@ -7,9 +7,9 @@
* 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.
@@ -20,9 +20,21 @@
package org.onap.policy.aai;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
import java.util.UUID;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
-public class AaiNqResponseWrapper {
+public class AaiNqResponseWrapper implements Serializable {
+ private static final long serialVersionUID = 8411407444051746101L;
+
+ private static final Logger logger = LoggerFactory.getLogger(AaiNqResponseWrapper.class);
+
+ private static final Pattern VF_MODULE_NAME_PAT = Pattern.compile("(.*_)(\\d+)");
private UUID requestId;
private AaiNqResponse aaiNqResponse;
@@ -49,4 +61,101 @@ public class AaiNqResponseWrapper {
public void setAaiNqResponse(AaiNqResponse aaiNqResponse) {
this.aaiNqResponse = aaiNqResponse;
}
+
+ /**
+ * Counts the number of VF modules, if any, in the response.
+ *
+ * @return the number of VF modules, or {@code 0} if there are none
+ */
+ public int countVfModules() {
+ List<AaiNqVfModule> lst = getVfModules(false);
+ return (lst == null ? 0 : lst.size());
+ }
+
+ /**
+ * Generates the name for the next VF module.
+ *
+ * @return the name of the next VF module, or {@code null} if the name could not be
+ * generated (i.e., because the response has no matching VF module names on
+ * which to model it)
+ */
+ public String genVfModuleName() {
+ List<AaiNqVfModule> lst = getVfModules(false);
+ if (lst == null) {
+ return null;
+ }
+
+ /*
+ * Loop through the VF modules, extracting the name prefix and the largest number
+ * suffix
+ */
+ String prefix = null;
+ int maxSuffix = -1;
+
+ for (AaiNqVfModule vfmod : lst) {
+ String name = vfmod.getVfModuleName();
+ Matcher matcher = VF_MODULE_NAME_PAT.matcher(name);
+ if (matcher.matches()) {
+ int suffix = Integer.parseInt(matcher.group(2));
+ if (suffix > maxSuffix) {
+ maxSuffix = suffix;
+ prefix = matcher.group(1);
+ }
+ }
+ }
+
+ ++maxSuffix;
+
+ return (prefix == null ? null : prefix + maxSuffix);
+ }
+
+ /**
+ * Gets a list of VF modules. If the non-base VF modules are requested, then only
+ * those whose names match the name pattern, {@link #VF_MODULE_NAME_PAT}, are
+ * returned.
+ *
+ * @param wantBaseModule {@code true} if the the base VF module(s) is desired,
+ * {@code false} otherwise
+ * @return the list of VF modules, or {@code null} if there are no VF modules
+ */
+ public List<AaiNqVfModule> getVfModules(boolean wantBaseModule) {
+ // get the list of items
+ List<AaiNqInventoryResponseItem> itemList;
+ try {
+ itemList = aaiNqResponse.getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0)
+ .getItems().getInventoryResponseItems();
+
+ } catch (NullPointerException | IndexOutOfBoundsException e) {
+ logger.debug("no VF modules in AAI response", e);
+ return null;
+ }
+
+ if (itemList == null) {
+ return null;
+ }
+
+ /*
+ * Walk the items looking for VF modules, allocating the list only when an item is
+ * found.
+ */
+ List<AaiNqVfModule> vfModules = null;
+
+ for (AaiNqInventoryResponseItem inventoryResponseItem : itemList) {
+ AaiNqVfModule vfmod = inventoryResponseItem.getVfModule();
+ if (vfmod == null) {
+ continue;
+ }
+
+ if (vfModules == null) {
+ vfModules = new ArrayList<>(itemList.size());
+ }
+
+ if (vfmod.getIsBaseVfModule() == wantBaseModule
+ && (wantBaseModule || VF_MODULE_NAME_PAT.matcher(vfmod.getVfModuleName()).matches())) {
+ vfModules.add(vfmod);
+ }
+ }
+
+ return vfModules;
+ }
}