2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-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.api.impl.inventory.sync;
23 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DATASPACE_NAME;
24 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DMI_REGISTRY_ANCHOR;
25 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DMI_REGISTRY_PARENT;
26 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME;
28 import java.time.OffsetDateTime;
29 import java.util.Collection;
30 import java.util.Collections;
31 import java.util.List;
33 import lombok.AllArgsConstructor;
34 import lombok.RequiredArgsConstructor;
35 import lombok.extern.slf4j.Slf4j;
36 import org.apache.commons.lang3.StringUtils;
37 import org.onap.cps.api.CpsAnchorService;
38 import org.onap.cps.api.CpsDataService;
39 import org.onap.cps.api.CpsModuleService;
40 import org.onap.cps.ncmp.api.impl.inventory.CmHandleQueries;
41 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState;
42 import org.onap.cps.ncmp.api.impl.operations.DmiModelOperations;
43 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
44 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
45 import org.onap.cps.spi.CascadeDeleteAllowed;
46 import org.onap.cps.spi.FetchDescendantsOption;
47 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException;
48 import org.onap.cps.spi.model.DataNode;
49 import org.onap.cps.spi.model.ModuleReference;
50 import org.onap.cps.utils.JsonObjectMapper;
51 import org.springframework.stereotype.Service;
55 @RequiredArgsConstructor
56 public class ModuleSyncService {
58 private final DmiModelOperations dmiModelOperations;
59 private final CpsModuleService cpsModuleService;
60 private final CmHandleQueries cmHandleQueries;
61 private final CpsDataService cpsDataService;
62 private final CpsAnchorService cpsAnchorService;
63 private final JsonObjectMapper jsonObjectMapper;
64 private static final Map<String, String> NO_NEW_MODULES = Collections.emptyMap();
67 private static final class ModuleDelta {
68 Collection<ModuleReference> allModuleReferences;
69 Map<String, String> newModuleNameToContentMap;
73 * This method creates a cm handle and initiates modules sync.
75 * @param yangModelCmHandle the yang model of cm handle.
77 public void syncAndCreateSchemaSetAndAnchor(final YangModelCmHandle yangModelCmHandle) {
78 final ModuleDelta moduleDelta = getModuleDelta(yangModelCmHandle, yangModelCmHandle.getModuleSetTag());
79 final String cmHandleId = yangModelCmHandle.getId();
80 cpsModuleService.createSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId,
81 moduleDelta.newModuleNameToContentMap, moduleDelta.allModuleReferences);
82 cpsAnchorService.createAnchor(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId, cmHandleId);
86 * This method upgrades a cm handle and initiates modules sync.
88 * @param yangModelCmHandle the yang model of cm handle.
90 public void syncAndUpgradeSchemaSet(final YangModelCmHandle yangModelCmHandle) {
91 final String upgradedModuleSetTag = ModuleOperationsUtils.getUpgradedModuleSetTagFromLockReason(
92 yangModelCmHandle.getCompositeState().getLockReason());
93 final ModuleDelta moduleDelta = getModuleDelta(yangModelCmHandle, upgradedModuleSetTag);
94 cpsModuleService.upgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME,
95 yangModelCmHandle.getId(), moduleDelta.newModuleNameToContentMap, moduleDelta.allModuleReferences);
96 setCmHandleModuleSetTag(yangModelCmHandle, upgradedModuleSetTag);
100 * Deletes the SchemaSet for schema set id if the SchemaSet Exists.
102 * @param schemaSetId the schema set id to be deleted
104 public void deleteSchemaSetIfExists(final String schemaSetId) {
106 cpsModuleService.deleteSchemaSet(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetId,
107 CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED);
108 log.debug("SchemaSet for {} has been deleted. Ready to be recreated.", schemaSetId);
109 } catch (final SchemaSetNotFoundException e) {
110 log.debug("No SchemaSet for {}. Assuming CmHandle has not been previously Module Synced.", schemaSetId);
114 private ModuleDelta getModuleDelta(final YangModelCmHandle yangModelCmHandle, final String targetModuleSetTag) {
115 final Collection<ModuleReference> allModuleReferences;
116 final Map<String, String> newYangResources;
118 final YangModelCmHandle cmHandleWithSameModuleSetTag = getAnyReadyCmHandleByModuleSetTag(targetModuleSetTag);
119 if (cmHandleWithSameModuleSetTag == null) {
120 allModuleReferences = dmiModelOperations.getModuleReferences(yangModelCmHandle);
121 newYangResources = dmiModelOperations.getNewYangResourcesFromDmi(yangModelCmHandle,
122 cpsModuleService.identifyNewModuleReferences(allModuleReferences));
124 log.info("Found other cm handle having same module set tag: {}", targetModuleSetTag);
125 allModuleReferences = cpsModuleService.getYangResourcesModuleReferences(
126 NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleWithSameModuleSetTag.getId());
127 newYangResources = NO_NEW_MODULES;
129 return new ModuleDelta(allModuleReferences, newYangResources);
132 private YangModelCmHandle getAnyReadyCmHandleByModuleSetTag(final String moduleSetTag) {
133 if (StringUtils.isBlank(moduleSetTag)) {
136 final String escapedModuleSetTag = moduleSetTag.replace("'", "''");
137 final List<DataNode> dataNodes = cmHandleQueries.queryNcmpRegistryByCpsPath(
138 NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@module-set-tag='" + escapedModuleSetTag + "']",
139 FetchDescendantsOption.DIRECT_CHILDREN_ONLY);
140 return dataNodes.stream().map(YangDataConverter::convertCmHandleToYangModel)
141 .filter(cmHandle -> cmHandle.getCompositeState().getCmHandleState() == CmHandleState.READY)
142 .findFirst().orElse(null);
145 private void setCmHandleModuleSetTag(final YangModelCmHandle yangModelCmHandle, final String newModuleSetTag) {
146 final String jsonForUpdate = jsonObjectMapper.asJsonString(Map.of(
147 "cm-handles", Map.of("id", yangModelCmHandle.getId(), "module-set-tag", newModuleSetTag)));
148 cpsDataService.updateNodeLeaves(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT,
149 jsonForUpdate, OffsetDateTime.now());