danielhanrahan | f66694a | 2024-06-10 21:32:12 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 21 | import http from 'k6/http'; |
danielhanrahan | 4e9f68c | 2024-06-28 13:43:35 +0100 | [diff] [blame] | 22 | import { check, sleep } from 'k6'; |
| 23 | import { NCMP_BASE_URL, DMI_PLUGIN_URL, TOTAL_CM_HANDLES, REGISTRATION_BATCH_SIZE, CONTENT_TYPE_JSON_PARAM, makeBatchOfCmHandleIds } from './utils.js'; |
| 24 | import { executeCmHandleIdSearch } from './search-base.js'; |
danielhanrahan | f66694a | 2024-06-10 21:32:12 +0100 | [diff] [blame] | 25 | |
danielhanrahan | 4e9f68c | 2024-06-28 13:43:35 +0100 | [diff] [blame] | 26 | export function registerAllCmHandles() { |
| 27 | forEachBatchOfCmHandles(createCmHandles); |
| 28 | waitForAllCmHandlesToBeReady(); |
| 29 | } |
| 30 | |
| 31 | export function deregisterAllCmHandles() { |
| 32 | forEachBatchOfCmHandles(deleteCmHandles); |
| 33 | } |
| 34 | |
| 35 | function 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 | |
| 43 | function createCmHandles(cmHandleIds) { |
danielhanrahan | f66694a | 2024-06-10 21:32:12 +0100 | [diff] [blame] | 44 | const url = `${NCMP_BASE_URL}/ncmpInventory/v1/ch`; |
| 45 | const payload = { |
| 46 | "dmiPlugin": DMI_PLUGIN_URL, |
| 47 | "createdCmHandles": cmHandleIds.map(cmHandleId => ({ |
| 48 | "cmHandle": cmHandleId, |
seanbeirne | 2855952 | 2024-07-19 16:55:06 +0100 | [diff] [blame^] | 49 | "alternateId": `alt-${cmHandleId}`, |
danielhanrahan | f66694a | 2024-06-10 21:32:12 +0100 | [diff] [blame] | 50 | "cmHandleProperties": {"neType": "RadioNode"}, |
| 51 | "publicCmHandleProperties": { |
| 52 | "Color": "yellow", |
| 53 | "Size": "small", |
| 54 | "Shape": "cube" |
| 55 | } |
| 56 | })), |
| 57 | }; |
danielhanrahan | 4e9f68c | 2024-06-28 13:43:35 +0100 | [diff] [blame] | 58 | const response = http.post(url, JSON.stringify(payload), CONTENT_TYPE_JSON_PARAM); |
| 59 | check(response, { 'create CM-handles status equals 200': (r) => r.status === 200 }); |
danielhanrahan | f66694a | 2024-06-10 21:32:12 +0100 | [diff] [blame] | 60 | return response; |
| 61 | } |
| 62 | |
danielhanrahan | 4e9f68c | 2024-06-28 13:43:35 +0100 | [diff] [blame] | 63 | function deleteCmHandles(cmHandleIds) { |
danielhanrahan | f66694a | 2024-06-10 21:32:12 +0100 | [diff] [blame] | 64 | const url = `${NCMP_BASE_URL}/ncmpInventory/v1/ch`; |
| 65 | const payload = { |
| 66 | "dmiPlugin": DMI_PLUGIN_URL, |
| 67 | "removedCmHandles": cmHandleIds, |
| 68 | }; |
danielhanrahan | 4e9f68c | 2024-06-28 13:43:35 +0100 | [diff] [blame] | 69 | const response = http.post(url, JSON.stringify(payload), CONTENT_TYPE_JSON_PARAM); |
| 70 | check(response, { 'delete CM-handles status equals 200': (r) => r.status === 200 }); |
danielhanrahan | f66694a | 2024-06-10 21:32:12 +0100 | [diff] [blame] | 71 | return response; |
| 72 | } |
| 73 | |
danielhanrahan | 4e9f68c | 2024-06-28 13:43:35 +0100 | [diff] [blame] | 74 | function waitForAllCmHandlesToBeReady() { |
| 75 | const POLLING_INTERVAL_SECONDS = 5; |
danielhanrahan | f66694a | 2024-06-10 21:32:12 +0100 | [diff] [blame] | 76 | let cmHandlesReady = 0; |
danielhanrahan | 4e9f68c | 2024-06-28 13:43:35 +0100 | [diff] [blame] | 77 | do { |
| 78 | sleep(POLLING_INTERVAL_SECONDS); |
| 79 | cmHandlesReady = getNumberOfReadyCmHandles(); |
| 80 | console.log(`${cmHandlesReady}/${TOTAL_CM_HANDLES} CM handles are READY`); |
| 81 | } while (cmHandlesReady < TOTAL_CM_HANDLES); |
danielhanrahan | f66694a | 2024-06-10 21:32:12 +0100 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | function getNumberOfReadyCmHandles() { |
danielhanrahan | 4e9f68c | 2024-06-28 13:43:35 +0100 | [diff] [blame] | 85 | const response = executeCmHandleIdSearch('readyCmHandles'); |
| 86 | const arrayOfCmHandleIds = JSON.parse(response.body); |
| 87 | return arrayOfCmHandleIds.length; |
danielhanrahan | f66694a | 2024-06-10 21:32:12 +0100 | [diff] [blame] | 88 | } |