Introduce control switch
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / init / SubscriptionModelLoader.java
1 /*
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
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 package org.onap.cps.ncmp.init;
22
23 import java.io.InputStream;
24 import java.nio.charset.StandardCharsets;
25 import java.util.Map;
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.beans.factory.annotation.Value;
34 import org.springframework.boot.SpringApplication;
35 import org.springframework.boot.context.event.ApplicationReadyEvent;
36 import org.springframework.stereotype.Component;
37
38 @Slf4j
39 @Component
40 @RequiredArgsConstructor
41 public class SubscriptionModelLoader implements ModelLoader {
42
43     private final CpsAdminService cpsAdminService;
44     private final CpsModuleService cpsModuleService;
45     private static final String SUBSCRIPTION_DATASPACE_NAME = "NCMP-Admin";
46     private static final String SUBSCRIPTION_ANCHOR_NAME = "AVC-Subscriptions";
47     private static final String SUBSCRIPTION_SCHEMASET_NAME = "subscriptions";
48
49     @Value("${ncmp.model-loader.subscription:false}")
50     private boolean subscriptionModelLoaderEnabled;
51
52     /**
53      * Method calls boarding subscription model when Application is ready.
54      *
55      * @param applicationReadyEvent the event to respond to
56      */
57     @Override
58     public void onApplicationEvent(@NonNull final ApplicationReadyEvent applicationReadyEvent) {
59         try {
60             if (subscriptionModelLoaderEnabled) {
61                 onboardSubscriptionModel();
62             } else {
63                 log.info("Subscription Model Loader is disabled");
64             }
65         } catch (final NcmpStartUpException ncmpStartUpException) {
66             log.debug("Onboarding model for NCMP failed: {} ", ncmpStartUpException.getMessage());
67             SpringApplication.exit(applicationReadyEvent.getApplicationContext(), () -> 1);
68         }
69     }
70
71     /**
72      * Method to onboard subscription model for NCMP.
73      */
74     private void onboardSubscriptionModel() {
75         final Map<String, String> yangResourceContentMap = createYangResourceToContentMap();
76         if (!yangResourceContentMap.get("subscription.yang").isEmpty()) {
77             createSchemaSet(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, yangResourceContentMap);
78             createAnchor(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, SUBSCRIPTION_ANCHOR_NAME);
79         }
80     }
81
82
83     @Override
84     public boolean createSchemaSet(final String dataspaceName,
85                                 final String schemaSetName,
86                                 final Map<String, String> yangResourceContentMap) {
87         try {
88             cpsModuleService.createSchemaSet(dataspaceName, schemaSetName, yangResourceContentMap);
89         } catch (final AlreadyDefinedException exception) {
90             log.info("Creating new schema set failed as schema set already exists");
91         } catch (final Exception exception) {
92             log.debug("Creating schema set for subscription model failed: {} ", exception.getMessage());
93             throw new NcmpStartUpException("Creating schema set failed", exception.getMessage());
94         }
95         return true;
96     }
97
98     /**
99      * Create Anchor.
100      *
101      * @param dataspaceName dataspace name
102      * @param schemaSetName schema set name
103      * @param anchorName anchor name
104      */
105     @Override
106     public boolean createAnchor(final String dataspaceName, final String schemaSetName,
107                              final String anchorName) {
108         try {
109             cpsAdminService.createAnchor(dataspaceName, schemaSetName, anchorName);
110         } catch (final AlreadyDefinedException exception) {
111             log.info("Creating new anchor failed as anchor already exists");
112         } catch (final Exception exception) {
113             log.debug("Creating anchor for subscription model failed: {} ", exception.getMessage());
114             throw new NcmpStartUpException("Creating anchor failed", exception.getMessage());
115         }
116         return true;
117     }
118
119     private String getFileContentAsString() {
120         try (InputStream inputStream = getClass().getClassLoader()
121                 .getResourceAsStream("model/subscription.yang")) {
122             return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
123         } catch (final Exception exception) {
124             log.debug("Onboarding failed as unable to read file: {}", exception.getCause().toString());
125             throw new NcmpStartUpException("Onboarding failed as unable to read file: {}", exception.getMessage());
126         }
127     }
128
129     private Map<String, String> createYangResourceToContentMap() {
130         return Map.of("subscription.yang", getFileContentAsString());
131     }
132 }