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;
26 import lombok.NonNull;
27 import lombok.RequiredArgsConstructor;
28 import lombok.extern.slf4j.Slf4j;
29 import org.onap.cps.api.CpsAdminService;
30 import org.onap.cps.api.CpsModuleService;
31 import org.onap.cps.ncmp.api.impl.exception.NcmpStartUpException;
32 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
33 import org.springframework.boot.SpringApplication;
34 import org.springframework.boot.context.event.ApplicationReadyEvent;
35 import org.springframework.stereotype.Component;
39 @RequiredArgsConstructor
40 public class SubscriptionModelLoader implements ModelLoader {
42 private final CpsAdminService cpsAdminService;
43 private final CpsModuleService cpsModuleService;
44 private static final String SUBSCRIPTION_DATASPACE_NAME = "NCMP-Admin";
45 private static final String SUBSCRIPTION_ANCHOR_NAME = "AVC-Subscriptions";
46 private static final String SUBSCRIPTION_SCHEMASET_NAME = "subscriptions";
49 * Method calls boarding subscription model when Application is ready.
51 * @param applicationReadyEvent the event to respond to
54 public void onApplicationEvent(@NonNull final ApplicationReadyEvent applicationReadyEvent) {
56 onboardSubscriptionModel();
57 } catch (final NcmpStartUpException ncmpStartUpException) {
58 log.debug("Onboarding model for NCMP failed: {} ", ncmpStartUpException.getMessage());
59 SpringApplication.exit(applicationReadyEvent.getApplicationContext(), () -> 1);
64 * Method to onboard subscription model for NCMP.
66 private void onboardSubscriptionModel() {
67 final Map<String, String> yangResourceContentMap = createYangResourceToContentMap();
68 if (!yangResourceContentMap.get("subscription.yang").isEmpty()) {
69 createSchemaSet(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, yangResourceContentMap);
70 createAnchor(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, SUBSCRIPTION_ANCHOR_NAME);
76 public boolean createSchemaSet(final String dataspaceName,
77 final String schemaSetName,
78 final Map<String, String> yangResourceContentMap) {
80 cpsModuleService.createSchemaSet(dataspaceName, schemaSetName, yangResourceContentMap);
81 } catch (final AlreadyDefinedException exception) {
82 log.info("Creating new schema set failed as schema set already exists");
83 } catch (final Exception exception) {
84 log.debug("Creating schema set for subscription model failed: {} ", exception.getMessage());
85 throw new NcmpStartUpException("Creating schema set failed", exception.getMessage());
93 * @param dataspaceName dataspace name
94 * @param schemaSetName schema set name
95 * @param anchorName anchor name
98 public boolean createAnchor(final String dataspaceName, final String schemaSetName,
99 final String anchorName) {
101 cpsAdminService.createAnchor(dataspaceName, schemaSetName, anchorName);
102 } catch (final AlreadyDefinedException exception) {
103 log.info("Creating new anchor failed as anchor already exists");
104 } catch (final Exception exception) {
105 log.debug("Creating anchor for subscription model failed: {} ", exception.getMessage());
106 throw new NcmpStartUpException("Creating anchor failed", exception.getMessage());
111 private String getFileContentAsString() {
112 try (InputStream inputStream = ClassLoader.getSystemClassLoader()
113 .getResourceAsStream("model/subscription.yang")) {
114 return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
115 } catch (final Exception exception) {
116 log.debug("Onboarding failed as unable to read file: {}", exception.getCause().toString());
117 throw new NcmpStartUpException("Onboarding failed as unable to read file: {}", exception.getMessage());
121 private Map<String, String> createYangResourceToContentMap() {
122 return Map.of("subscription.yang", getFileContentAsString());