aboutsummaryrefslogtreecommitdiffstats
path: root/models-interactions/model-impl/aai/src/main/java/org/onap/policy/aai/AaiCqResponse.java
blob: 7a6eb6859b1496377e535193a02e164ed5984f7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*-
 * ============LICENSE_START=======================================================
 *
 * ================================================================================
 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2023 Nordix Foundation.
 * ================================================================================
 * 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.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.aai;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import org.onap.aai.domain.yang.CloudRegion;
import org.onap.aai.domain.yang.GenericVnf;
import org.onap.aai.domain.yang.ModelVer;
import org.onap.aai.domain.yang.Relationship;
import org.onap.aai.domain.yang.RelationshipData;
import org.onap.aai.domain.yang.ServiceInstance;
import org.onap.aai.domain.yang.Tenant;
import org.onap.aai.domain.yang.VfModule;
import org.onap.aai.domain.yang.Vserver;

public class AaiCqResponse implements Serializable {
    private static final long serialVersionUID = 1L;
    public static final String CONTEXT_KEY = AaiConstants.CONTEXT_PREFIX + "AaiCqResponse";
    public static final String OPERATION = "CustomQuery";
    private static final String GENERIC_VNF = "generic-vnf";
    private static final String VF_MODULE = "vf-module";

    @SerializedName("results")
    private List<Serializable> inventoryResponseItems = new LinkedList<>();

    private final Gson gson;

    /**
     * Constructor creates a custom query response from a valid json string.
     *
     * @param jsonString A&AI Custom Query response JSON string
     */
    public AaiCqResponse(String jsonString) {
        gson = new GsonBuilder()
            .setFieldNamingStrategy(new XmlElementFieldNamingStrategy())
            .create();

        // Read JSON String and add all AaiObjects
        var responseObj = new JSONObject(jsonString);
        var resultsArray = new JSONArray();
        if (responseObj.has("results")) {
            resultsArray = (JSONArray) responseObj.get("results");
        }
        for (var i = 0; i < resultsArray.length(); i++) {
            final var resultObject = resultsArray.getJSONObject(i);

            extractVserver(resultObject);
            extractGenericVnf(resultObject);
            extractServiceInstance(resultObject);
            extractVfModule(resultObject);
            extractCloudRegion(resultObject);
            extractTenant(resultObject);
            extractModelVer(resultObject);
        }
    }

    private void extractVserver(final JSONObject resultObject) {
        if (resultObject.has("vserver")) {

            // Create the StreamSource by creating StringReader using the
            // JSON input
            var json = resultObject.getJSONObject("vserver").toString();

            // Getting the vserver pojo again from the json
            var vserver = gson.fromJson(json, Vserver.class);
            this.inventoryResponseItems.add(vserver);
        }
    }

    private void extractGenericVnf(final JSONObject resultObject) {
        if (resultObject.has(GENERIC_VNF)) {
            // Create the StreamSource by creating StringReader using the
            // JSON input
            var json = resultObject.getJSONObject(GENERIC_VNF).toString();

            // Getting the generic vnf pojo again from the json
            var genericVnf = gson.fromJson(json, GenericVnf.class);
            this.inventoryResponseItems.add(genericVnf);
        }
    }

    private void extractServiceInstance(final JSONObject resultObject) {
        if (resultObject.has("service-instance")) {

            // Create the StreamSource by creating StringReader using the
            // JSON input
            var json = resultObject.getJSONObject("service-instance").toString();

            // Getting the employee pojo again from the json
            var serviceInstance = gson.fromJson(json, ServiceInstance.class);
            this.inventoryResponseItems.add(serviceInstance);
        }
    }

    private void extractVfModule(final JSONObject resultObject) {
        if (resultObject.has(VF_MODULE)) {
            // Create the StreamSource by creating StringReader using the
            // JSON input
            var json = resultObject.getJSONObject(VF_MODULE).toString();

            // Getting the vf module pojo again from the json
            var vfModule = gson.fromJson(json, VfModule.class);
            this.inventoryResponseItems.add(vfModule);
        }
    }

    private void extractCloudRegion(final JSONObject resultObject) {
        if (resultObject.has("cloud-region")) {
            // Create the StreamSource by creating StringReader using the
            // JSON input
            var json = resultObject.getJSONObject("cloud-region").toString();

            // Getting the cloud region pojo again from the json
            var cloudRegion = gson.fromJson(json, CloudRegion.class);
            this.inventoryResponseItems.add(cloudRegion);
        }
    }

    private void extractTenant(final JSONObject resultObject) {
        if (resultObject.has("tenant")) {
            // Create the StreamSource by creating StringReader using the
            // JSON input
            var json = resultObject.getJSONObject("tenant").toString();

            // Getting the tenant pojo again from the json
            var tenant = gson.fromJson(json, Tenant.class);
            this.inventoryResponseItems.add(tenant);
        }
    }

    private void extractModelVer(final JSONObject resultObject) {
        if (resultObject.has("model-ver")) {
            // Create the StreamSource by creating StringReader using the
            // JSON input
            var json = resultObject.getJSONObject("model-ver").toString();

            // Getting the ModelVer pojo again from the json
            var modelVer = gson.fromJson(json, ModelVer.class);
            this.inventoryResponseItems.add(modelVer);
        }
    }

    public List<Serializable> getInventoryResponseItems() {
        return inventoryResponseItems;
    }

    public void setInventoryResponseItems(List<Serializable> inventoryResponseItems) {
        this.inventoryResponseItems = inventoryResponseItems;
    }

    /**
     * Get list of A&AI objects in the custom query.
     *
     * @param classOfResponse Class of the type of A&AI objects to be returned
     * @return List A&AI objects matching the class
     */
    @SuppressWarnings("unchecked")
    public <T> List<T> getItemListByType(Class<T> classOfResponse) {
        List<T> returnItemList = new ArrayList<>();
        for (Serializable i : this.inventoryResponseItems) {
            if (i.getClass() == classOfResponse) {
                returnItemList.add((T) i);
            }
        }
        return returnItemList;

    }

    /**
     * Get Service Instance.
     *
     * @return Service Instance
     */
    public ServiceInstance getServiceInstance() {
        ServiceInstance serviceInstance = null;
        for (Serializable i : this.inventoryResponseItems) {
            if (i.getClass() == ServiceInstance.class) {
                serviceInstance = (ServiceInstance) i;
            }
        }
        return serviceInstance;

    }

    /**
     * Get Tenant.
     *
     * @return Tenant
     */
    public Tenant getDefaultTenant() {
        Tenant tenant = null;
        for (Serializable i : this.inventoryResponseItems) {
            if (i.getClass() == Tenant.class) {
                tenant = (Tenant) i;
            }
        }
        return tenant;

    }

    /**
     * Get Cloud Region.
     *
     * @return Cloud Region
     */
    public CloudRegion getDefaultCloudRegion() {
        CloudRegion cloudRegion = null;
        for (Serializable i : this.inventoryResponseItems) {
            if (i.getClass() == CloudRegion.class) {
                cloudRegion = (CloudRegion) i;
            }
        }
        return cloudRegion;

    }

    /**
     * Get Generic Vnfs in the custom query.
     *
     * @return List of generic Vnf
     */
    public List<GenericVnf> getGenericVnfs() {
        List<GenericVnf> genericVnfList = new ArrayList<>();
        for (Serializable i : this.inventoryResponseItems) {
            if (i.getClass() == GenericVnf.class) {
                genericVnfList.add((GenericVnf) i);
            }
        }
        return genericVnfList;

    }

    /**
     * Returns a generic Vnf matching vnf name.
     *
     * @param vnfName Name of the vnf to match
     * @return generic Vnf
     */
    public GenericVnf getGenericVnfByVnfName(String vnfName) {
        List<GenericVnf> genericVnfList = new ArrayList<>();
        GenericVnf genericVnf = null;
        for (Serializable i : this.inventoryResponseItems) {
            if (i.getClass() == GenericVnf.class) {
                genericVnfList.add((GenericVnf) i);
            }
        }

        for (GenericVnf genVnf : genericVnfList) {
            if (vnfName.equals(genVnf.getVnfName())) {
                genericVnf = genVnf;
            }

        }
        return genericVnf;

    }

    /**
     * Returns a generic Vnf matching model invariant ID.
     *
     * @param modelInvariantId Name of the vnf to match
     * @return generic Vnf
     */
    public GenericVnf getGenericVnfByModelInvariantId(String modelInvariantId) {
        List<GenericVnf> genericVnfList = new ArrayList<>();
        GenericVnf genericVnf = null;
        for (Serializable i : this.inventoryResponseItems) {
            if (i.getClass() == GenericVnf.class) {
                genericVnfList.add((GenericVnf) i);
            }
        }

        for (GenericVnf genVnf : genericVnfList) {
            if (modelInvariantId.equals(genVnf.getModelInvariantId())) {
                genericVnf = genVnf;
            }

        }
        return genericVnf;

    }

    /**
     * Returns a generic Vnf of a given VF Module ID.
     *
     * @param vfModuleModelInvariantId of the vf module for which vnf is to be returned
     * @return generic Vnf
     */
    public GenericVnf getGenericVnfByVfModuleModelInvariantId(String vfModuleModelInvariantId) {
        List<GenericVnf> genericVnfList = this.getGenericVnfs();

        for (GenericVnf genVnf : genericVnfList) {
            // Iterate through all the vfModules of that generic Vnf
            for (VfModule vfMod : genVnf.getVfModules().getVfModule()) {
                if (vfMod.getModelInvariantId() != null
                    && vfMod.getModelInvariantId().equals(vfModuleModelInvariantId)) {
                    return genVnf;
                }
            }
        }
        return null;
    }

    /**
     * Returns the VNF given the vnf-id.
     *
     * @param vnfId The vnf-id
     * @return generic Vnf
     */
    public GenericVnf getGenericVnfByVnfId(String vnfId) {
        List<GenericVnf> genericVnfList = this.getGenericVnfs();

        for (GenericVnf genVnf : genericVnfList) {
            if (vnfId.equals(genVnf.getVnfId())) {
                return genVnf;
            }
        }

        return null;
    }

    /**
     * Get the generic vnf associated with the vserver in the custom query.
     *
     * @return Generic VNF
     */
    public GenericVnf getDefaultGenericVnf() {
        GenericVnf genericVnf = null;

        // Get the vserver associated with the query
        var vserver = this.getVserver();

        // Get the relationships of the vserver
        List<Relationship> relations = vserver.getRelationshipList().getRelationship();

        // Find the relationship of the genericVNF
        var genericVnfId = "";
        List<RelationshipData> relationshipData = null;

        // Iterate through the list of relationships and get generic vnf
        // relationship data
        for (Relationship r : relations) {
            // Get the name of generic-vnf related to this server
            if (GENERIC_VNF.equals(r.getRelatedTo())) {
                relationshipData = r.getRelationshipData();
            }
        }

        // Iterate through relationship data, and get vnf-id
        for (RelationshipData rd : relationshipData) {
            // Get the id of the generic-vnf
            if ("generic-vnf.vnf-id".equals(rd.getRelationshipKey())) {
                genericVnfId = rd.getRelationshipValue();
            }
        }

        // Get the list of generic vnfs
        List<GenericVnf> genericVnfList = this.getGenericVnfs();

        for (GenericVnf genVnf : genericVnfList) {
            if (genericVnfId.equals(genVnf.getVnfId())) {
                genericVnf = genVnf;
            }
        }

        return genericVnf;
    }

    /**
     * Get Vf Module associated with the vserver in the custom query.
     *
     * @return Vf Module
     */
    public VfModule getDefaultVfModule() {
        GenericVnf genericVnf = null;
        VfModule vfModule = null;

        // Get the vserver associated with the query
        var vserver = this.getVserver();

        // Get the relationships of the vserver
        List<Relationship> relations = vserver.getRelationshipList().getRelationship();

        // Find the relationship of VfModule
        var vfModuleId = "";
        List<RelationshipData> relationshipData = null;

        // Iterate through the list of relationships and get vf module
        // relationship data
        for (Relationship r : relations) {
            // Get relationship data of vfmodule related to this server
            if (VF_MODULE.equals(r.getRelatedTo())) {
                relationshipData = r.getRelationshipData();
            }
        }

        // Iterate through relationship data, and get vf-module-id
        for (RelationshipData rd : relationshipData) {
            // Get the id of the vf-module
            if ("vf-module.vf-module-id".equals(rd.getRelationshipKey())) {
                vfModuleId = rd.getRelationshipValue();
            }
        }

        // Get the generic VNF associated with this vserver query
        genericVnf = this.getDefaultGenericVnf();

        // Get the list of VFmodules associated with this generic Vnf
        List<VfModule> vfModuleList = genericVnf.getVfModules().getVfModule();

        for (VfModule vfMod : vfModuleList) {
            if (vfModuleId.equals(vfMod.getVfModuleId())) {
                vfModule = vfMod;
            }
        }

        return vfModule;
    }

    /**
     * Get vf modules in the custom query.
     *
     * @return List of VfModule
     */
    public List<VfModule> getAllVfModules() {
        List<VfModule> vfModuleList = new ArrayList<>();

        for (GenericVnf genVnf : this.getGenericVnfs()) {
            vfModuleList.addAll(genVnf.getVfModules().getVfModule());
        }
        return vfModuleList;

    }

    /**
     * Get Vf Module matching a specific VF module name.
     *
     * @return VfModule
     */
    public VfModule getVfModuleByVfModuleName(String vfModuleName) {
        VfModule vfModule = null;

        for (VfModule vfMod : this.getAllVfModules()) {
            if (vfModuleName.equals(vfMod.getVfModuleName())) {
                vfModule = vfMod;
            }

        }
        return vfModule;
    }

    /**
     * Get Vf Module matching a specific VF model invariant ID.
     *
     * @return VfModule
     */
    public VfModule getVfModuleByVfModelInvariantId(String vfModelInvariantId) {
        VfModule vfModule = null;

        for (VfModule vfMod : this.getAllVfModules()) {
            if (vfMod.getModelInvariantId() != null && vfModelInvariantId.equals(vfMod.getModelInvariantId())) {
                vfModule = vfMod;
            }

        }
        return vfModule;
    }

    /**
     * Get verver in the custom query.
     *
     * @return Vserver
     */
    public Vserver getVserver() {
        Vserver vserver = null;
        var index = 0;
        while (this.inventoryResponseItems.get(index).getClass() != Vserver.class) {
            index = index + 1;
        }
        vserver = (Vserver) this.inventoryResponseItems.get(index);
        return vserver;

    }

    /**
     * Get Model Versions in the custom query.
     *
     * @return List of model Versions
     */
    public List<ModelVer> getAllModelVer() {
        List<ModelVer> modelVerList = new ArrayList<>();
        for (Serializable i : this.inventoryResponseItems) {
            if (i.getClass() == ModelVer.class) {
                modelVerList.add((ModelVer) i);
            }
        }
        return modelVerList;
    }

    /**
     * Get ModelVer matching a specific version id.
     *
     * @return VfModule
     */
    public ModelVer getModelVerByVersionId(String versionId) {
        ModelVer modelVer = null;

        for (ModelVer modVersion : this.getAllModelVer()) {
            if (versionId.equals(modVersion.getModelVersionId())) {
                modelVer = modVersion;
            }

        }
        return modelVer;
    }

    /**
     * Get the count of vfModules matching customizationId, InvariantId and VersionId.
     *
     * @param custId ModelCustomizationId
     * @param invId ModelInvariantId
     * @param verId ModelVersionId
     * @return Returns the count of vf modules
     */
    public int getVfModuleCount(String custId, String invId, String verId) {
        List<VfModule> vfModuleList = this.getAllVfModules();
        var count = 0;
        for (VfModule vfModule : vfModuleList) {
            if (vfModule.getModelCustomizationId() == null || vfModule.getModelInvariantId() == null
                || vfModule.getModelVersionId() == null) {
                continue;
            }

            if (vfModule.getModelCustomizationId().equals(custId) && vfModule.getModelInvariantId().equals(invId)
                && vfModule.getModelVersionId().equals(verId)) {
                count = count + 1;
            }
        }
        return count;
    }

}