blob: 88ecdb45b86563bebbdb94a7143bd9ad8b327234 [file] [log] [blame]
danielhanrahanf66694a2024-06-10 21:32:12 +01001/*
2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2024 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
19 */
20
21import http from 'k6/http';
danielhanrahan4e9f68c2024-06-28 13:43:35 +010022import { check, sleep } from 'k6';
emacleef11aad92024-08-14 14:25:33 +010023import { NCMP_BASE_URL, DMI_PLUGIN_URL, TOTAL_CM_HANDLES, MODULE_SET_TAGS, REGISTRATION_BATCH_SIZE, CONTENT_TYPE_JSON_PARAM, makeBatchOfCmHandleIds } from './utils.js';
danielhanrahan4e9f68c2024-06-28 13:43:35 +010024import { executeCmHandleIdSearch } from './search-base.js';
danielhanrahanf66694a2024-06-10 21:32:12 +010025
danielhanrahan4e9f68c2024-06-28 13:43:35 +010026export function registerAllCmHandles() {
27 forEachBatchOfCmHandles(createCmHandles);
28 waitForAllCmHandlesToBeReady();
29}
30
31export function deregisterAllCmHandles() {
32 forEachBatchOfCmHandles(deleteCmHandles);
33}
34
35function forEachBatchOfCmHandles(functionToExecute) {
36 const TOTAL_BATCHES = Math.ceil(TOTAL_CM_HANDLES / REGISTRATION_BATCH_SIZE);
37 for (let batchNumber = 0; batchNumber < TOTAL_BATCHES; batchNumber++) {
38 const nextBatchOfCmHandleIds = makeBatchOfCmHandleIds(REGISTRATION_BATCH_SIZE, batchNumber);
39 functionToExecute(nextBatchOfCmHandleIds);
40 }
41}
42
43function createCmHandles(cmHandleIds) {
danielhanrahanf66694a2024-06-10 21:32:12 +010044 const url = `${NCMP_BASE_URL}/ncmpInventory/v1/ch`;
45 const payload = {
46 "dmiPlugin": DMI_PLUGIN_URL,
danielhanrahanb12d0c82024-08-15 12:42:21 +010047 "createdCmHandles": cmHandleIds.map((cmHandleId, index) => ({
danielhanrahanf66694a2024-06-10 21:32:12 +010048 "cmHandle": cmHandleId,
seanbeirne28559522024-07-19 16:55:06 +010049 "alternateId": `alt-${cmHandleId}`,
emacleef11aad92024-08-14 14:25:33 +010050 "moduleSetTag": MODULE_SET_TAGS[index % MODULE_SET_TAGS.length],
danielhanrahanf66694a2024-06-10 21:32:12 +010051 "cmHandleProperties": {"neType": "RadioNode"},
52 "publicCmHandleProperties": {
53 "Color": "yellow",
54 "Size": "small",
55 "Shape": "cube"
56 }
57 })),
58 };
danielhanrahan4e9f68c2024-06-28 13:43:35 +010059 const response = http.post(url, JSON.stringify(payload), CONTENT_TYPE_JSON_PARAM);
60 check(response, { 'create CM-handles status equals 200': (r) => r.status === 200 });
danielhanrahanf66694a2024-06-10 21:32:12 +010061 return response;
62}
63
danielhanrahan4e9f68c2024-06-28 13:43:35 +010064function deleteCmHandles(cmHandleIds) {
danielhanrahanf66694a2024-06-10 21:32:12 +010065 const url = `${NCMP_BASE_URL}/ncmpInventory/v1/ch`;
66 const payload = {
67 "dmiPlugin": DMI_PLUGIN_URL,
68 "removedCmHandles": cmHandleIds,
69 };
danielhanrahan4e9f68c2024-06-28 13:43:35 +010070 const response = http.post(url, JSON.stringify(payload), CONTENT_TYPE_JSON_PARAM);
71 check(response, { 'delete CM-handles status equals 200': (r) => r.status === 200 });
danielhanrahanf66694a2024-06-10 21:32:12 +010072 return response;
73}
74
danielhanrahan4e9f68c2024-06-28 13:43:35 +010075function waitForAllCmHandlesToBeReady() {
76 const POLLING_INTERVAL_SECONDS = 5;
danielhanrahanf66694a2024-06-10 21:32:12 +010077 let cmHandlesReady = 0;
danielhanrahan4e9f68c2024-06-28 13:43:35 +010078 do {
79 sleep(POLLING_INTERVAL_SECONDS);
80 cmHandlesReady = getNumberOfReadyCmHandles();
81 console.log(`${cmHandlesReady}/${TOTAL_CM_HANDLES} CM handles are READY`);
82 } while (cmHandlesReady < TOTAL_CM_HANDLES);
danielhanrahanf66694a2024-06-10 21:32:12 +010083}
84
85function getNumberOfReadyCmHandles() {
danielhanrahan4e9f68c2024-06-28 13:43:35 +010086 const response = executeCmHandleIdSearch('readyCmHandles');
87 const arrayOfCmHandleIds = JSON.parse(response.body);
88 return arrayOfCmHandleIds.length;
danielhanrahanf66694a2024-06-10 21:32:12 +010089}