1be5b2cc6a8bb217688a9186507972f217d1e712
[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.time.OffsetDateTime;
26 import java.util.Map;
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;
40
41 @Slf4j
42 @Component
43 @RequiredArgsConstructor
44 public class SubscriptionModelLoader implements ModelLoader {
45
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";
55
56     @Value("${ncmp.model-loader.maximum-attempt-count:20}")
57     private int maximumAttemptCount;
58
59     @Value("${ncmp.timers.model-loader.retry-time-ms:1000}")
60     private long retryTimeMs;
61
62     @Value("${ncmp.model-loader.subscription:true}")
63     private boolean subscriptionModelLoaderEnabled;
64
65     /**
66      * Method calls boarding subscription model when Application is ready.
67      *
68      * @param applicationReadyEvent the event to respond to
69      */
70     @Override
71     public void onApplicationEvent(final ApplicationReadyEvent applicationReadyEvent) {
72         try {
73             if (subscriptionModelLoaderEnabled) {
74                 checkNcmpDataspaceExists();
75                 onboardSubscriptionModel(createYangResourceToContentMap());
76             } else {
77                 log.info("Subscription Model Loader is disabled");
78             }
79         } catch (final NcmpStartUpException ncmpStartUpException) {
80             log.debug("Onboarding model for NCMP failed: {} ", ncmpStartUpException.getMessage());
81             SpringApplication.exit(applicationReadyEvent.getApplicationContext(), () -> 1);
82         }
83     }
84
85     private void checkNcmpDataspaceExists() {
86         boolean ncmpDataspaceExists = false;
87         int attemptCount = 0;
88         while (!ncmpDataspaceExists) {
89             final Dataspace ncmpDataspace = cpsAdminService.getDataspace(SUBSCRIPTION_DATASPACE_NAME);
90             if (ncmpDataspace != null) {
91                 ncmpDataspaceExists = true;
92             }
93             if (attemptCount < maximumAttemptCount) {
94                 try {
95                     Thread.sleep(attemptCount * retryTimeMs);
96                     attemptCount++;
97                     log.info("Retrieving NCMP dataspace... {} attempt(s) ", attemptCount);
98                 } catch (final InterruptedException e) {
99                     Thread.currentThread().interrupt();
100                 }
101             } else {
102                 throw new NcmpStartUpException("Retrieval of NCMP dataspace fails",
103                     "NCMP dataspace does not exist");
104             }
105         }
106     }
107
108     /**
109      * Method to onboard subscription model for NCMP.
110      */
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);
116     }
117
118
119     @Override
120     public boolean createSchemaSet(final String dataspaceName,
121                                    final String schemaSetName,
122                                    final Map<String, String> yangResourceContentMap) {
123         try {
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());
130         }
131         return true;
132     }
133
134     /**
135      * Create Anchor.
136      *
137      * @param dataspaceName dataspace name
138      * @param schemaSetName schema set name
139      * @param anchorName anchor name
140      */
141     @Override
142     public boolean createAnchor(final String dataspaceName, final String schemaSetName,
143                                 final String anchorName) {
144         try {
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());
151         }
152         return true;
153     }
154
155     private void createTopLevelDataNode(final String dataspaceName,
156                                         final String anchorName,
157                                         final String dataNodeName) {
158         final String nodeData = "{\"" + dataNodeName + "\":{}}";
159         try {
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());
166         }
167     }
168
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);
174             log.debug(message);
175             throw new NcmpStartUpException(message, exception.getMessage());
176         }
177     }
178
179     private Map<String, String> createYangResourceToContentMap() {
180         return Map.of(SUBSCRIPTION_MODEL_FILENAME, getFileContentAsString(SUBSCRIPTION_MODEL_RESOURCE_PATH));
181     }
182 }