Merge "Fix: Update OpenSSF Scorecard to RelEng reusable"
[cps.git] / integration-test / src / test / groovy / org / onap / cps / integration / performance / base / NcmpPerfTestBase.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2023-2025 OpenInfra Foundation Europe. All rights reserved.
4  *  Modifications Copyright (C) 2024 TechMahindra Ltd.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the 'License');
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an 'AS IS' BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.integration.performance.base
23
24 import org.onap.cps.integration.ResourceMeter
25 import org.onap.cps.api.parameters.FetchDescendantsOption
26 import org.onap.cps.utils.ContentType
27
28 import static org.onap.cps.ncmp.impl.inventory.NcmpPersistence.NCMP_DATASPACE_NAME
29 import static org.onap.cps.ncmp.impl.inventory.NcmpPersistence.NCMP_DMI_REGISTRY_ANCHOR
30 import static org.onap.cps.ncmp.impl.inventory.NcmpPersistence.NCMP_DMI_REGISTRY_PARENT
31
32 class NcmpPerfTestBase extends PerfTestBase {
33
34     def static NCMP_PERFORMANCE_TEST_DATASPACE = 'ncmpPerformance'
35     def static REGISTRY_ANCHOR = NCMP_DMI_REGISTRY_ANCHOR
36     def static REGISTRY_PARENT = NCMP_DMI_REGISTRY_PARENT
37     def static REGISTRY_SCHEMA_SET = 'registrySchemaSet'
38     def static TOTAL_CM_HANDLES = 20_000
39     def static CM_DATA_SUBSCRIPTIONS_ANCHOR = 'cm-data-subscriptions'
40     def static CM_DATA_SUBSCRIPTIONS_SCHEMA_SET = 'cmDataSubscriptionsSchemaSet'
41
42     def datastore1cmHandlePlaceHolder = '{"datastores":{"datastore":[{"name":"ds-1","cm-handles":{"cm-handle":[]}}]}}'
43     def xPathForDataStore1CmHandles = '/datastores/datastore[@name="ds-1"]/cm-handles'
44     def numberOfCmDataSubscribers = 200
45     def numberOfFiltersPerCmHandle = 10
46     def numberOfCmHandlesPerCmDataSubscription = 200
47
48     ResourceMeter resourceMeter = new ResourceMeter()
49
50     def subscriberIdPrefix = 'some really long subscriber id to see if this makes any difference to the performance'
51     def xpathPrefix = 'some really long xpath/with/loads/of/children/grandchildren/and/whatever/else/I/can/think/of to see if this makes any difference to the performance'
52     def cmHandlePrefix = 'some really long cm handle id to see if this makes any difference to the performance'
53
54     def printTitle() {
55         println('##                  N C M P   P E R F O R M A N C E   T E S T   R E S U L T S                   ##')
56     }
57
58     def isInitialised() {
59         return dataspaceExists(NCMP_PERFORMANCE_TEST_DATASPACE)
60     }
61
62     def setupPerformanceInfraStructure() {
63         cpsDataspaceService.createDataspace(NCMP_PERFORMANCE_TEST_DATASPACE)
64         createRegistrySchemaSet()
65         createCmDataSubscriptionsSchemaSet()
66     }
67
68     def createInitialData() {
69         addRegistryData()
70         addRegistryDataWithAlternateIdAsPath()
71         addCmSubscriptionData()
72     }
73
74     def createRegistrySchemaSet() {
75         def modelAsString = readResourceDataFile('inventory/dmi-registry@2024-02-23.yang')
76         cpsModuleService.createSchemaSet(NCMP_PERFORMANCE_TEST_DATASPACE, REGISTRY_SCHEMA_SET, [registry: modelAsString])
77     }
78
79     def addRegistryData() {
80         cpsAnchorService.createAnchor(NCMP_PERFORMANCE_TEST_DATASPACE, REGISTRY_SCHEMA_SET, REGISTRY_ANCHOR)
81         cpsDataService.saveData(NCMP_PERFORMANCE_TEST_DATASPACE, REGISTRY_ANCHOR, '{"dmi-registry": []}', now)
82         def cmHandleJsonTemplate = readResourceDataFile('inventory/cmHandleTemplate.json')
83         def batchSize = 100
84         for (def i = 0; i < TOTAL_CM_HANDLES; i += batchSize) {
85             def data = '{ "cm-handles": [' + (1..batchSize).collect { cmHandleJsonTemplate.replace('CM_HANDLE_ID_POSTFIX', (it + i).toString()) }.join(',') + ']}'
86             cpsDataService.saveListElements(NCMP_PERFORMANCE_TEST_DATASPACE, REGISTRY_ANCHOR, REGISTRY_PARENT, data, now, ContentType.JSON)
87         }
88     }
89
90     def addRegistryDataWithAlternateIdAsPath() {
91         def cmHandleWithAlternateIdTemplate = readResourceDataFile('inventory/cmHandleWithAlternateIdTemplate.json')
92         def batchSize = 10
93         for (def i = 0; i < TOTAL_CM_HANDLES; i += batchSize) {
94             def data = '{ "cm-handles": [' + (1..batchSize).collect {
95                 cmHandleWithAlternateIdTemplate.replace('CM_HANDLE_ID_POSTFIX', (it + i).toString())
96                         .replace('ALTERNATE_ID_POSTFIX', (it + i).toString())
97             }.join(',') + ']}'
98             cpsDataService.saveListElements(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, REGISTRY_PARENT, data, now, ContentType.JSON)
99         }
100     }
101
102     def createCmDataSubscriptionsSchemaSet() {
103         def modelAsString = readResourceDataFile('cm-data-subscriptions/cm-data-subscriptions@2023-09-21.yang')
104         cpsModuleService.createSchemaSet(NCMP_PERFORMANCE_TEST_DATASPACE, CM_DATA_SUBSCRIPTIONS_SCHEMA_SET, [registry: modelAsString])
105     }
106
107     def addCmSubscriptionData() {
108         cpsAnchorService.createAnchor(NCMP_PERFORMANCE_TEST_DATASPACE, CM_DATA_SUBSCRIPTIONS_SCHEMA_SET, CM_DATA_SUBSCRIPTIONS_ANCHOR)
109         cpsDataService.saveData(NCMP_PERFORMANCE_TEST_DATASPACE, CM_DATA_SUBSCRIPTIONS_ANCHOR, datastore1cmHandlePlaceHolder, now)
110         def subscribers = createLeafList('subscribers',numberOfCmDataSubscribers, subscriberIdPrefix)
111         def filters = '"filters":' + createJsonArray('filter',numberOfFiltersPerCmHandle,'xpath',xpathPrefix,subscribers)
112         def cmHandles = createJsonArray('cm-handle',numberOfCmHandlesPerCmDataSubscription,'id',cmHandlePrefix, filters)
113         cpsDataService.saveData(NCMP_PERFORMANCE_TEST_DATASPACE, CM_DATA_SUBSCRIPTIONS_ANCHOR, xPathForDataStore1CmHandles, cmHandles, now)
114     }
115
116     def 'NCMP pre-load test data'() {
117         when: 'dummy get data nodes runs so that populating the DB does not get included in other test timings'
118             resourceMeter.start()
119             def result = cpsDataService.getDataNodes(NCMP_PERFORMANCE_TEST_DATASPACE, REGISTRY_ANCHOR, '/', FetchDescendantsOption.OMIT_DESCENDANTS)
120             resourceMeter.stop()
121         then: 'expected data exists'
122             assert result.xpath == [REGISTRY_PARENT]
123     }
124
125 }