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.model.Dataspace;
35 import org.springframework.beans.factory.annotation.Value;
36 import org.springframework.boot.SpringApplication;
37 import org.springframework.boot.context.event.ApplicationReadyEvent;
38 import org.springframework.stereotype.Component;
42 @RequiredArgsConstructor
43 public class SubscriptionModelLoader implements ModelLoader {
45 private final CpsAdminService cpsAdminService;
46 private final CpsModuleService cpsModuleService;
47 private final CpsDataService cpsDataService;
48 private static final String SUBSCRIPTION_MODEL_FILENAME = "subscription.yang";
49 private static final String SUBSCRIPTION_MODEL_RESOURCE_PATH = "model/" + SUBSCRIPTION_MODEL_FILENAME;
50 private static final String SUBSCRIPTION_DATASPACE_NAME = "NCMP-Admin";
51 private static final String SUBSCRIPTION_ANCHOR_NAME = "AVC-Subscriptions";
52 private static final String SUBSCRIPTION_SCHEMASET_NAME = "subscriptions";
53 private static final String SUBSCRIPTION_REGISTRY_DATANODE_NAME = "subscription-registry";
55 @Value("${ncmp.model-loader.maximumAttemptCount:20}")
56 private int maximumAttemptCount;
58 @Value("${ncmp.model-loader.retryTimeMs:1000}")
59 private long retryTimeMs;
61 @Value("${ncmp.model-loader.subscription:false}")
62 private boolean subscriptionModelLoaderEnabled;
65 * Method calls boarding subscription model when Application is ready.
67 * @param applicationReadyEvent the event to respond to
70 public void onApplicationEvent(final ApplicationReadyEvent applicationReadyEvent) {
72 if (subscriptionModelLoaderEnabled) {
73 checkNcmpDataspaceExists();
74 onboardSubscriptionModel(createYangResourceToContentMap());
76 log.info("Subscription Model Loader is disabled");
78 } catch (final NcmpStartUpException ncmpStartUpException) {
79 log.debug("Onboarding model for NCMP failed: {} ", ncmpStartUpException.getMessage());
80 SpringApplication.exit(applicationReadyEvent.getApplicationContext(), () -> 1);
84 private void checkNcmpDataspaceExists() {
85 boolean ncmpDataspaceExists = false;
87 while (!ncmpDataspaceExists) {
88 final Dataspace ncmpDataspace = cpsAdminService.getDataspace(SUBSCRIPTION_DATASPACE_NAME);
89 if (ncmpDataspace != null) {
90 ncmpDataspaceExists = true;
92 if (attemptCount < maximumAttemptCount) {
94 Thread.sleep(attemptCount * retryTimeMs);
96 log.info("Retrieving NCMP dataspace... {} attempt(s) ", attemptCount);
97 } catch (final InterruptedException e) {
98 Thread.currentThread().interrupt();
101 throw new NcmpStartUpException("Retrieval of NCMP dataspace fails",
102 "NCMP dataspace does not exist");
108 * Method to onboard subscription model for NCMP.
110 private void onboardSubscriptionModel(final Map<String, String> yangResourceContentMap) {
111 createSchemaSet(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, yangResourceContentMap);
112 createAnchor(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, SUBSCRIPTION_ANCHOR_NAME);
113 createTopLevelDataNode(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_ANCHOR_NAME,
114 SUBSCRIPTION_REGISTRY_DATANODE_NAME);
119 public boolean createSchemaSet(final String dataspaceName,
120 final String schemaSetName,
121 final Map<String, String> yangResourceContentMap) {
123 cpsModuleService.createSchemaSet(dataspaceName, schemaSetName, yangResourceContentMap);
124 } catch (final AlreadyDefinedException exception) {
125 log.info("Creating new schema set failed as schema set already exists");
126 } catch (final Exception exception) {
127 log.debug("Creating schema set for subscription model failed: {} ", exception.getMessage());
128 throw new NcmpStartUpException("Creating schema set failed", exception.getMessage());
136 * @param dataspaceName dataspace name
137 * @param schemaSetName schema set name
138 * @param anchorName anchor name
141 public boolean createAnchor(final String dataspaceName, final String schemaSetName,
142 final String anchorName) {
144 cpsAdminService.createAnchor(dataspaceName, schemaSetName, anchorName);
145 } catch (final AlreadyDefinedException exception) {
146 log.info("Creating new anchor failed as anchor already exists");
147 } catch (final Exception exception) {
148 log.debug("Creating anchor for subscription model failed: {} ", exception.getMessage());
149 throw new NcmpStartUpException("Creating anchor failed", exception.getMessage());
154 private void createTopLevelDataNode(final String dataspaceName,
155 final String anchorName,
156 final String dataNodeName) {
157 final String nodeData = "{\"" + dataNodeName + "\":{}}";
159 cpsDataService.saveData(dataspaceName, anchorName, nodeData, OffsetDateTime.now());
160 } catch (final AlreadyDefinedException exception) {
161 log.info("Creating new data node '{}' failed as data node already exists", dataNodeName);
162 } catch (final Exception exception) {
163 log.debug("Creating data node for subscription model failed: {}", exception.getMessage());
164 throw new NcmpStartUpException("Creating data node failed", exception.getMessage());
168 private String getFileContentAsString(final String fileName) {
169 try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(fileName)) {
170 return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
171 } catch (final Exception exception) {
172 final String message = String.format("Onboarding failed as unable to read file: %s", fileName);
174 throw new NcmpStartUpException(message, exception.getMessage());
178 private Map<String, String> createYangResourceToContentMap() {
179 return Map.of(SUBSCRIPTION_MODEL_FILENAME, getFileContentAsString(SUBSCRIPTION_MODEL_RESOURCE_PATH));