2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.aai;
24 import java.io.Serializable;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.UUID;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class AaiNqResponseWrapper implements Serializable {
35 private static final long serialVersionUID = 8411407444051746101L;
37 private static final Logger logger = LoggerFactory.getLogger(AaiNqResponseWrapper.class);
39 private static final Pattern VF_MODULE_NAME_PAT = Pattern.compile("(.*_)(\\d+)");
41 private UUID requestId;
42 private AaiNqResponse aaiNqResponse;
44 public AaiNqResponseWrapper() {}
46 public AaiNqResponseWrapper(UUID requestId, AaiNqResponse aaiNqResponse) {
47 this.requestId = requestId;
48 this.aaiNqResponse = aaiNqResponse;
51 public UUID getRequestId() {
55 public void setRequestId(UUID requestId) {
56 this.requestId = requestId;
59 public AaiNqResponse getAaiNqResponse() {
63 public void setAaiNqResponse(AaiNqResponse aaiNqResponse) {
64 this.aaiNqResponse = aaiNqResponse;
68 * Counts the number of VF modules, if any, in the response.
70 * @return the number of VF modules, or {@code 0} if there are none
72 public int countVfModules() {
73 return getVfModuleItems(false).size();
77 * Generates the name for the next VF module.
79 * @return the name of the next VF module, or {@code null} if the name could not be
80 * generated (i.e., because the response has no matching VF module names on
83 public String genVfModuleName() {
85 * Loop through the VF modules, extracting the name prefix and the largest number
91 for (AaiNqInventoryResponseItem item : getVfModuleItems(false)) {
92 String name = item.getVfModule().getVfModuleName();
93 Matcher matcher = VF_MODULE_NAME_PAT.matcher(name);
94 if (matcher.matches()) {
95 int suffix = Integer.parseInt(matcher.group(2));
96 if (suffix > maxSuffix) {
98 prefix = matcher.group(1);
105 return (prefix == null ? null : prefix + maxSuffix);
109 * Gets a list of VF modules. If the non-base VF modules are requested, then only
110 * those whose names match the name pattern, {@link #VF_MODULE_NAME_PAT}, are
113 * @param wantBaseModule {@code true} if the the base VF module(s) is desired,
114 * {@code false} otherwise
115 * @return the list of VF module items
117 public List<AaiNqInventoryResponseItem> getVfModuleItems(boolean wantBaseModule) {
118 // get the list of items
119 List<AaiNqInventoryResponseItem> itemList;
121 itemList = aaiNqResponse.getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0)
122 .getItems().getInventoryResponseItems();
124 } catch (NullPointerException | IndexOutOfBoundsException e) {
125 logger.debug("no VF modules in AAI response", e);
126 return Collections.emptyList();
129 if (itemList == null) {
130 return Collections.emptyList();
134 * Walk the items looking for VF modules, allocating the list only when an item is
137 List<AaiNqInventoryResponseItem> vfModuleItems = new ArrayList<>(itemList.size());
139 for (AaiNqInventoryResponseItem inventoryResponseItem : itemList) {
140 AaiNqVfModule vfmod = inventoryResponseItem.getVfModule();
145 if (vfmod.getIsBaseVfModule() == wantBaseModule
146 && (wantBaseModule || VF_MODULE_NAME_PAT.matcher(vfmod.getVfModuleName()).matches())) {
147 vfModuleItems.add(inventoryResponseItem);
151 return vfModuleItems;