2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023 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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.init;
23 import java.io.InputStream;
24 import java.nio.charset.StandardCharsets;
25 import java.time.OffsetDateTime;
27 import lombok.RequiredArgsConstructor;
28 import lombok.extern.slf4j.Slf4j;
29 import org.onap.cps.api.CpsAdminService;
30 import org.onap.cps.api.CpsDataService;
31 import org.onap.cps.api.CpsModuleService;
32 import org.onap.cps.ncmp.api.impl.exception.NcmpStartUpException;
33 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
34 import org.onap.cps.spi.exceptions.AlreadyDefinedExceptionBatch;
35 import org.onap.cps.spi.model.Dataspace;
36 import org.springframework.beans.factory.annotation.Value;
37 import org.springframework.boot.SpringApplication;
38 import org.springframework.boot.context.event.ApplicationReadyEvent;
39 import org.springframework.stereotype.Component;
43 @RequiredArgsConstructor
44 public class SubscriptionModelLoader implements ModelLoader {
46 private final CpsAdminService cpsAdminService;
47 private final CpsModuleService cpsModuleService;
48 private final CpsDataService cpsDataService;
49 private static final String SUBSCRIPTION_MODEL_FILENAME = "subscription.yang";
50 private static final String SUBSCRIPTION_MODEL_RESOURCE_PATH = "model/" + SUBSCRIPTION_MODEL_FILENAME;
51 private static final String SUBSCRIPTION_DATASPACE_NAME = "NCMP-Admin";
52 private static final String SUBSCRIPTION_ANCHOR_NAME = "AVC-Subscriptions";
53 private static final String SUBSCRIPTION_SCHEMASET_NAME = "subscriptions";
54 private static final String SUBSCRIPTION_REGISTRY_DATANODE_NAME = "subscription-registry";
56 @Value("${ncmp.model-loader.maximum-attempt-count:20}")
57 private int maximumAttemptCount;
59 @Value("${ncmp.timers.model-loader.retry-time-ms:1000}")
60 private long retryTimeMs;
62 @Value("${ncmp.model-loader.subscription:true}")
63 private boolean subscriptionModelLoaderEnabled;
66 * Method calls boarding subscription model when Application is ready.
68 * @param applicationReadyEvent the event to respond to
71 public void onApplicationEvent(final ApplicationReadyEvent applicationReadyEvent) {
73 if (subscriptionModelLoaderEnabled) {
74 checkNcmpDataspaceExists();
75 onboardSubscriptionModel(createYangResourceToContentMap());
77 log.info("Subscription Model Loader is disabled");
79 } catch (final NcmpStartUpException ncmpStartUpException) {
80 log.debug("Onboarding model for NCMP failed: {} ", ncmpStartUpException.getMessage());
81 SpringApplication.exit(applicationReadyEvent.getApplicationContext(), () -> 1);
85 private void checkNcmpDataspaceExists() {
86 boolean ncmpDataspaceExists = false;
88 while (!ncmpDataspaceExists) {
89 final Dataspace ncmpDataspace = cpsAdminService.getDataspace(SUBSCRIPTION_DATASPACE_NAME);
90 if (ncmpDataspace != null) {
91 ncmpDataspaceExists = true;
93 if (attemptCount < maximumAttemptCount) {
95 Thread.sleep(attemptCount * retryTimeMs);
97 log.info("Retrieving NCMP dataspace... {} attempt(s) ", attemptCount);
98 } catch (final InterruptedException e) {
99 Thread.currentThread().interrupt();
102 throw new NcmpStartUpException("Retrieval of NCMP dataspace fails",
103 "NCMP dataspace does not exist");
109 * Method to onboard subscription model for NCMP.
111 private void onboardSubscriptionModel(final Map<String, String> yangResourceContentMap) {
112 createSchemaSet(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, yangResourceContentMap);
113 createAnchor(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, SUBSCRIPTION_ANCHOR_NAME);
114 createTopLevelDataNode(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_ANCHOR_NAME,
115 SUBSCRIPTION_REGISTRY_DATANODE_NAME);
120 public boolean createSchemaSet(final String dataspaceName,
121 final String schemaSetName,
122 final Map<String, String> yangResourceContentMap) {
124 cpsModuleService.createSchemaSet(dataspaceName, schemaSetName, yangResourceContentMap);
125 } catch (final AlreadyDefinedException exception) {
126 log.info("Creating new schema set failed as schema set already exists");
127 } catch (final Exception exception) {
128 log.debug("Creating schema set for subscription model failed: {} ", exception.getMessage());
129 throw new NcmpStartUpException("Creating schema set failed", exception.getMessage());
137 * @param dataspaceName dataspace name
138 * @param schemaSetName schema set name
139 * @param anchorName anchor name
142 public boolean createAnchor(final String dataspaceName, final String schemaSetName,
143 final String anchorName) {
145 cpsAdminService.createAnchor(dataspaceName, schemaSetName, anchorName);
146 } catch (final AlreadyDefinedException exception) {
147 log.info("Creating new anchor failed as anchor already exists");
148 } catch (final Exception exception) {
149 log.debug("Creating anchor for subscription model failed: {} ", exception.getMessage());
150 throw new NcmpStartUpException("Creating anchor failed", exception.getMessage());
155 private void createTopLevelDataNode(final String dataspaceName,
156 final String anchorName,
157 final String dataNodeName) {
158 final String nodeData = "{\"" + dataNodeName + "\":{}}";
160 cpsDataService.saveData(dataspaceName, anchorName, nodeData, OffsetDateTime.now());
161 } catch (final AlreadyDefinedExceptionBatch exception) {
162 log.info("Creating new data node '{}' failed as data node already exists", dataNodeName);
163 } catch (final Exception exception) {
164 log.debug("Creating data node for subscription model failed: {}", exception.getMessage());
165 throw new NcmpStartUpException("Creating data node failed", exception.getMessage());
169 private String getFileContentAsString(final String fileName) {
170 try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(fileName)) {
171 return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
172 } catch (final Exception exception) {
173 final String message = String.format("Onboarding failed as unable to read file: %s", fileName);
175 throw new NcmpStartUpException(message, exception.getMessage());
179 private Map<String, String> createYangResourceToContentMap() {
180 return Map.of(SUBSCRIPTION_MODEL_FILENAME, getFileContentAsString(SUBSCRIPTION_MODEL_RESOURCE_PATH));