038e3f88b2d5a9755739e659f00ad5160c106a42
[policy/drools-applications.git] / controlloop / common / model-impl / aai / src / main / java / org / onap / policy / aai / AaiNqResponseWrapper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * aai
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.aai;
22
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;
32
33 public class AaiNqResponseWrapper implements Serializable {
34     private static final long serialVersionUID = 8411407444051746101L;
35     
36     private static final Logger logger = LoggerFactory.getLogger(AaiNqResponseWrapper.class);
37
38     private static final Pattern VF_MODULE_NAME_PAT = Pattern.compile("(.*_)(\\d+)");
39
40     private UUID requestId;
41     private AaiNqResponse aaiNqResponse;
42
43     public AaiNqResponseWrapper() {}
44
45     public AaiNqResponseWrapper(UUID requestId, AaiNqResponse aaiNqResponse) {
46         this.requestId = requestId;
47         this.aaiNqResponse = aaiNqResponse;
48     }
49
50     public UUID getRequestId() {
51         return requestId;
52     }
53
54     public void setRequestId(UUID requestId) {
55         this.requestId = requestId;
56     }
57
58     public AaiNqResponse getAaiNqResponse() {
59         return aaiNqResponse;
60     }
61
62     public void setAaiNqResponse(AaiNqResponse aaiNqResponse) {
63         this.aaiNqResponse = aaiNqResponse;
64     }
65
66     /**
67      * Counts the number of VF modules, if any, in the response.
68      *
69      * @return the number of VF modules, or {@code 0} if there are none
70      */
71     public int countVfModules() {
72         return getVfModuleItems(false).size();
73     }
74
75     /**
76      * Generates the name for the next VF module.
77      *
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
80      *         which to model it)
81      */
82     public String genVfModuleName() {
83         /*
84          * Loop through the VF modules, extracting the name prefix and the largest number
85          * suffix
86          */
87         String prefix = null;
88         int maxSuffix = -1;
89
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) {
96                     maxSuffix = suffix;
97                     prefix = matcher.group(1);
98                 }
99             }
100         }
101
102         ++maxSuffix;
103
104         return (prefix == null ? null : prefix + maxSuffix);
105     }
106
107     /**
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
110      * returned.
111      *
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
115      */
116     public List<AaiNqInventoryResponseItem> getVfModuleItems(boolean wantBaseModule) {
117         // get the list of items
118         List<AaiNqInventoryResponseItem> itemList;
119         try {
120             itemList = aaiNqResponse.getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0)
121                             .getItems().getInventoryResponseItems();
122
123         } catch (NullPointerException | IndexOutOfBoundsException e) {
124             logger.debug("no VF modules in AAI response", e);
125             return Collections.emptyList();
126         }
127
128         if (itemList == null) {
129             return Collections.emptyList();
130         }
131
132         /*
133          * Walk the items looking for VF modules, allocating the list only when an item is
134          * found.
135          */
136         List<AaiNqInventoryResponseItem> vfModuleItems = new ArrayList<>(itemList.size());
137
138         for (AaiNqInventoryResponseItem inventoryResponseItem : itemList) {
139             AaiNqVfModule vfmod = inventoryResponseItem.getVfModule();
140             if (vfmod == null) {
141                 continue;
142             }
143
144             if (vfmod.getIsBaseVfModule() == wantBaseModule
145                             && (wantBaseModule || VF_MODULE_NAME_PAT.matcher(vfmod.getVfModuleName()).matches())) {
146                 vfModuleItems.add(inventoryResponseItem);
147             }
148         }
149
150         return vfModuleItems;
151     }
152 }