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