349b1c5b02e81f0efe8a238a32b541a785efe0a0
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / init / AbstractModelLoader.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 com.fasterxml.jackson.databind.ObjectMapper;
24 import java.io.InputStream;
25 import java.nio.charset.StandardCharsets;
26 import java.time.OffsetDateTime;
27 import java.util.Map;
28 import lombok.NonNull;
29 import lombok.RequiredArgsConstructor;
30 import lombok.extern.slf4j.Slf4j;
31 import org.onap.cps.api.CpsAdminService;
32 import org.onap.cps.api.CpsDataService;
33 import org.onap.cps.api.CpsModuleService;
34 import org.onap.cps.ncmp.api.impl.exception.NcmpStartUpException;
35 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
36 import org.onap.cps.utils.JsonObjectMapper;
37 import org.springframework.beans.factory.annotation.Value;
38 import org.springframework.boot.SpringApplication;
39 import org.springframework.boot.context.event.ApplicationReadyEvent;
40 import org.springframework.stereotype.Service;
41
42 @Slf4j
43 @Service
44 @RequiredArgsConstructor
45 abstract class AbstractModelLoader implements ModelLoader {
46
47     private final CpsAdminService cpsAdminService;
48     private final CpsModuleService cpsModuleService;
49     private final CpsDataService cpsDataService;
50
51     private static final int EXIT_CODE_ON_ERROR = 1;
52
53     private final JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper());
54
55     @Value("${ncmp.model-loader.maximum-attempt-count:20}")
56     int maximumAttemptCount;
57
58     @Value("${ncmp.timers.model-loader.retry-time-ms:1000}")
59     long retryTimeMs;
60
61     @Override
62     public void onApplicationEvent(@NonNull final ApplicationReadyEvent applicationReadyEvent) {
63         try {
64             onboardOrUpgradeModel();
65         } catch (final NcmpStartUpException ncmpStartUpException) {
66             log.error("Onboarding model for NCMP failed: {} ", ncmpStartUpException.getMessage());
67             SpringApplication.exit(applicationReadyEvent.getApplicationContext(), () -> EXIT_CODE_ON_ERROR);
68         }
69     }
70
71     void waitUntilDataspaceIsAvailable(final String dataspaceName) {
72         int attemptCount = 0;
73         while (cpsAdminService.getDataspace(dataspaceName) == null) {
74             if (attemptCount < maximumAttemptCount) {
75                 try {
76                     Thread.sleep(attemptCount * retryTimeMs);
77                     log.info("Retrieving dataspace {} ... {} attempt(s) ", dataspaceName, ++attemptCount);
78                 } catch (final InterruptedException e) {
79                     Thread.currentThread().interrupt();
80                 }
81             } else {
82                 throw new NcmpStartUpException("Retrieval of NCMP dataspace failed",
83                     dataspaceName + " not available (yet)");
84             }
85         }
86     }
87
88     void createSchemaSet(final String dataspaceName, final String schemaSetName, final String resourceName) {
89         try {
90             final Map<String, String> yangResourceContentMap = createYangResourceToContentMap(resourceName);
91             cpsModuleService.createSchemaSet(dataspaceName, schemaSetName, yangResourceContentMap);
92         } catch (final AlreadyDefinedException alreadyDefinedException) {
93             log.warn("Creating new schema set failed as schema set already exists");
94         } catch (final Exception exception) {
95             log.error("Creating schema set for subscription model failed: {} ", exception.getMessage());
96             throw new NcmpStartUpException("Creating schema set failed", exception.getMessage());
97         }
98     }
99
100     void createAnchor(final String dataspaceName, final String schemaSetName, final String anchorName) {
101         try {
102             cpsAdminService.createAnchor(dataspaceName, schemaSetName, anchorName);
103         } catch (final AlreadyDefinedException alreadyDefinedException) {
104             log.warn("Creating new anchor failed as anchor already exists");
105         } catch (final Exception exception) {
106             log.error("Creating anchor for subscription model failed: {} ", exception.getMessage());
107             throw new NcmpStartUpException("Creating anchor failed", exception.getMessage());
108         }
109     }
110
111     void createTopLevelDataNode(final String dataspaceName,
112                                         final String anchorName,
113                                         final String dataNodeName) {
114         final String nodeData = jsonObjectMapper.asJsonString(Map.of(dataNodeName, Map.of()));
115         try {
116             cpsDataService.saveData(dataspaceName, anchorName, nodeData, OffsetDateTime.now());
117         } catch (final AlreadyDefinedException exception) {
118             log.warn("Creating new data node '{}' failed as data node already exists", dataNodeName);
119         } catch (final Exception exception) {
120             log.error("Creating data node for subscription model failed: {}", exception.getMessage());
121             throw new NcmpStartUpException("Creating data node failed", exception.getMessage());
122         }
123     }
124
125     Map<String, String> createYangResourceToContentMap(final String resourceName) {
126         return Map.of(resourceName, getFileContentAsString("models/" + resourceName));
127     }
128
129     private String getFileContentAsString(final String fileName) {
130         try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(fileName)) {
131             return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
132         } catch (final Exception exception) {
133             final String message = String.format("Onboarding failed as unable to read file: %s", fileName);
134             log.debug(message);
135             throw new NcmpStartUpException(message, exception.getMessage());
136         }
137     }
138
139 }