2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2024 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.dmi.rest.stub.service;
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import jakarta.annotation.PostConstruct;
26 import java.time.LocalDate;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
31 import lombok.RequiredArgsConstructor;
32 import lombok.extern.slf4j.Slf4j;
33 import org.onap.cps.ncmp.dmi.rest.stub.model.module.ModuleReference;
34 import org.onap.cps.ncmp.dmi.rest.stub.model.module.ModuleReferences;
35 import org.onap.cps.ncmp.dmi.rest.stub.model.module.ModuleResource;
36 import org.springframework.stereotype.Service;
40 @RequiredArgsConstructor
41 public class YangModuleFactory {
43 private static final int TARGET_FILE_SIZE_IN_KB = 32 * 1024;
44 private static final List<String> MODULE_SET_TAGS = generateTags();
45 private static final String DEFAULT_TAG = "tagDefault";
46 private static final int NUMBER_OF_MODULES_PER_MODULE_SET = 200;
47 private static final int NUMBER_OF_MODULES_NOT_IN_MODULE_SET = 10;
48 private static final String SERIALIZATION_ERROR = "Error serializing {}: {}";
49 private static final String MODULE_TEMPLATE = """
50 module <MODULE_NAME> {
52 namespace "org:onap:cps:test:<MODULE_NAME>";
54 revision "<MODULE_REVISION>" {
55 description "<DESCRIPTION>";
68 private final ObjectMapper objectMapper;
69 private final Map<String, String> moduleReferencesJsonMap = new HashMap<>();
70 private final Map<String, String> moduleResourcesJsonMap = new HashMap<>();
73 private void initializeModuleJsonStrings() {
74 MODULE_SET_TAGS.forEach(tag -> {
75 moduleReferencesJsonMap.put(tag, createModuleReferencesJson(tag, NUMBER_OF_MODULES_PER_MODULE_SET));
76 moduleResourcesJsonMap.put(tag, createModuleResourcesJson(tag, NUMBER_OF_MODULES_PER_MODULE_SET));
79 // Initialize default tag
80 moduleReferencesJsonMap.put(DEFAULT_TAG,
81 createModuleReferencesJson(DEFAULT_TAG, NUMBER_OF_MODULES_NOT_IN_MODULE_SET));
82 moduleResourcesJsonMap.put(DEFAULT_TAG,
83 createModuleResourcesJson(DEFAULT_TAG, NUMBER_OF_MODULES_NOT_IN_MODULE_SET));
87 * Retrieves the JSON representation of module references for the given tag.
89 * @param tag the tag identifying the set of module references
90 * @return the JSON string of module references for the specified tag, or the default tag if not found
92 public String getModuleReferencesJson(final String tag) {
93 return moduleReferencesJsonMap.getOrDefault(tag, moduleReferencesJsonMap.get(DEFAULT_TAG));
97 * Retrieves the JSON representation of module resources for the given tag.
99 * @param tag the tag identifying the set of module resources
100 * @return the JSON string of module resources for the specified tag, or the default tag if not found
102 public String getModuleResourcesJson(final String tag) {
103 return moduleResourcesJsonMap.getOrDefault(tag, moduleResourcesJsonMap.get(DEFAULT_TAG));
107 * Generates a list of tags from 'A' to 'E'.
109 * @return a list of tags in the format "tagX" where X is each character from 'A' to 'E'
111 public static List<String> generateTags() {
112 final List<String> tags = new ArrayList<>(5);
113 for (char currentChar = 'A'; currentChar <= 'E'; currentChar++) {
114 tags.add("tag" + currentChar);
119 private String createModuleReferencesJson(final String tag, final int numberOfModules) {
120 final List<ModuleReference> moduleReferencesList = new ArrayList<>(numberOfModules);
121 final String moduleRevision = generateModuleRevision(tag);
122 for (int i = 0; i < numberOfModules; i++) {
123 moduleReferencesList.add(new ModuleReference("module" + i, moduleRevision));
125 return serializeToJson(new ModuleReferences(moduleReferencesList), "ModuleReferences");
128 private String createModuleResourcesJson(final String tag, final int numberOfModules) {
129 final List<ModuleResource> moduleResourceList = new ArrayList<>(numberOfModules);
130 final String moduleRevision = generateModuleRevision(tag);
131 for (int i = 0; i < numberOfModules; i++) {
132 final String moduleName = "module" + i;
133 final String yangSource = generateYangSource(moduleName, moduleRevision);
134 moduleResourceList.add(new ModuleResource(moduleName, moduleRevision, yangSource));
136 return serializeToJson(moduleResourceList, "ModuleResources");
139 private String serializeToJson(final Object objectToSerialize, final String objectType) {
141 return objectMapper.writeValueAsString(objectToSerialize);
142 } catch (final JsonProcessingException jsonProcessingException) {
143 log.error(SERIALIZATION_ERROR, objectType, jsonProcessingException.getMessage());
148 private String generateModuleRevision(final String tag) {
149 // set tagIndex to 0 for the default tag, otherwise set it to the index of the tag in the list
150 final int tagIndex = tag.equals(DEFAULT_TAG) ? 0 : MODULE_SET_TAGS.indexOf(tag);
151 return LocalDate.of(2024, tagIndex + 1, tagIndex + 1).toString();
154 private static String generateYangSource(final String moduleName, final String moduleRevision) {
155 final int paddingSize = TARGET_FILE_SIZE_IN_KB - MODULE_TEMPLATE.length();
156 final String padding = "*".repeat(Math.max(0, paddingSize));
157 return MODULE_TEMPLATE.replaceAll("<MODULE_NAME>", moduleName)
158 .replace("<MODULE_REVISION>", moduleRevision)
159 .replace("<DESCRIPTION>", padding);