2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.aai;
23 import java.io.Serializable;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.UUID;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class AaiNqResponseWrapper implements Serializable {
34 private static final long serialVersionUID = 8411407444051746101L;
36 private static final Logger logger = LoggerFactory.getLogger(AaiNqResponseWrapper.class);
38 private static final Pattern VF_MODULE_NAME_PAT = Pattern.compile("(.*_)(\\d+)");
40 private UUID requestId;
41 private AaiNqResponse aaiNqResponse;
43 public AaiNqResponseWrapper() {}
45 public AaiNqResponseWrapper(UUID requestId, AaiNqResponse aaiNqResponse) {
46 this.requestId = requestId;
47 this.aaiNqResponse = aaiNqResponse;
50 public UUID getRequestId() {
54 public void setRequestId(UUID requestId) {
55 this.requestId = requestId;
58 public AaiNqResponse getAaiNqResponse() {
62 public void setAaiNqResponse(AaiNqResponse aaiNqResponse) {
63 this.aaiNqResponse = aaiNqResponse;
67 * Counts the number of VF modules, if any, in the response.
69 * @return the number of VF modules, or {@code 0} if there are none
71 public int countVfModules() {
72 return getVfModuleItems(false).size();
76 * Generates the name for the next VF module.
78 * @return the name of the next VF module, or {@code null} if the name could not be
79 * generated (i.e., because the response has no matching VF module names on
82 public String genVfModuleName() {
84 * Loop through the VF modules, extracting the name prefix and the largest number
90 for (AaiNqInventoryResponseItem item : getVfModuleItems(false)) {
91 String name = item.getVfModule().getVfModuleName();
92 Matcher matcher = VF_MODULE_NAME_PAT.matcher(name);
93 if (matcher.matches()) {
94 int suffix = Integer.parseInt(matcher.group(2));
95 if (suffix > maxSuffix) {
97 prefix = matcher.group(1);
104 return (prefix == null ? null : prefix + maxSuffix);
108 * Gets a list of VF modules. If the non-base VF modules are requested, then only
109 * those whose names match the name pattern, {@link #VF_MODULE_NAME_PAT}, are
112 * @param wantBaseModule {@code true} if the the base VF module(s) is desired,
113 * {@code false} otherwise
114 * @return the list of VF module items
116 public List<AaiNqInventoryResponseItem> getVfModuleItems(boolean wantBaseModule) {
117 // get the list of items
118 List<AaiNqInventoryResponseItem> itemList;
120 itemList = aaiNqResponse.getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0)
121 .getItems().getInventoryResponseItems();
123 } catch (NullPointerException | IndexOutOfBoundsException e) {
124 logger.debug("no VF modules in AAI response", e);
125 return Collections.emptyList();
128 if (itemList == null) {
129 return Collections.emptyList();
133 * Walk the items looking for VF modules, allocating the list only when an item is
136 List<AaiNqInventoryResponseItem> vfModuleItems = new ArrayList<>(itemList.size());
138 for (AaiNqInventoryResponseItem inventoryResponseItem : itemList) {
139 AaiNqVfModule vfmod = inventoryResponseItem.getVfModule();
144 if (vfmod.getIsBaseVfModule() == wantBaseModule
145 && (wantBaseModule || VF_MODULE_NAME_PAT.matcher(vfmod.getVfModuleName()).matches())) {
146 vfModuleItems.add(inventoryResponseItem);
150 return vfModuleItems;