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