moduleSetTag and cmhandle upgrade functionalities fix
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / inventory / sync / ModuleSyncService.java
1 /*
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
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.api.impl.inventory.sync;
22
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;
27
28 import java.time.OffsetDateTime;
29 import java.util.Collection;
30 import java.util.Collections;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Optional;
35 import lombok.RequiredArgsConstructor;
36 import lombok.extern.slf4j.Slf4j;
37 import org.apache.commons.lang3.StringUtils;
38 import org.onap.cps.api.CpsAnchorService;
39 import org.onap.cps.api.CpsDataService;
40 import org.onap.cps.api.CpsModuleService;
41 import org.onap.cps.ncmp.api.impl.inventory.CmHandleQueries;
42 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState;
43 import org.onap.cps.ncmp.api.impl.inventory.CompositeState;
44 import org.onap.cps.ncmp.api.impl.operations.DmiModelOperations;
45 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
46 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
47 import org.onap.cps.spi.CascadeDeleteAllowed;
48 import org.onap.cps.spi.FetchDescendantsOption;
49 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException;
50 import org.onap.cps.spi.model.DataNode;
51 import org.onap.cps.spi.model.ModuleReference;
52 import org.onap.cps.utils.JsonObjectMapper;
53 import org.springframework.stereotype.Service;
54
55 @Slf4j
56 @Service
57 @RequiredArgsConstructor
58 public class ModuleSyncService {
59
60     private final DmiModelOperations dmiModelOperations;
61     private final CpsModuleService cpsModuleService;
62     private final CmHandleQueries cmHandleQueries;
63     private final CpsDataService cpsDataService;
64     private final CpsAnchorService cpsAnchorService;
65     private final JsonObjectMapper jsonObjectMapper;
66     private final Map<String, Collection<ModuleReference>> moduleSetTagCache;
67     private static final Map<String, String> NO_NEW_MODULES = Collections.emptyMap();
68
69     /**
70      * This method registers a cm handle and initiates modules sync.
71      *
72      * @param yangModelCmHandle the yang model of cm handle.
73      */
74     public void syncAndCreateOrUpgradeSchemaSetAndAnchor(final YangModelCmHandle yangModelCmHandle) {
75
76         final String cmHandleId = yangModelCmHandle.getId();
77         final CompositeState compositeState = yangModelCmHandle.getCompositeState();
78         final boolean inUpgrade = ModuleOperationsUtils.isInUpgradeOrUpgradeFailed(compositeState);
79         final String moduleSetTag = getModuleSetTag(yangModelCmHandle, compositeState, inUpgrade);
80
81         final Collection<ModuleReference> moduleReferencesFromCache = moduleSetTagCache.get(moduleSetTag);
82
83         if (moduleReferencesFromCache == null) {
84             final Optional<DataNode> existingCmHandleWithSameModuleSetTag
85                     = getFirstReadyDataNodeWithModuleSetTag(moduleSetTag);
86
87             if (existingCmHandleWithSameModuleSetTag.isPresent()) {
88                 final String existingAnchorName = existingCmHandleWithSameModuleSetTag.get().getAnchorName();
89                 final Collection<ModuleReference> moduleReferencesFromExistingCmHandle =
90                         upgradeOrCreateSchemaSetUsingModuleSetTag(yangModelCmHandle.getId(), moduleSetTag,
91                                 existingAnchorName, inUpgrade);
92                 updateModuleSetTagCache(moduleSetTag, moduleReferencesFromExistingCmHandle);
93             } else {
94                 if (inUpgrade) {
95                     deleteSchemaSetIfExists(cmHandleId);
96                 }
97                 final Collection<ModuleReference> allModuleReferencesFromCmHandle
98                         = syncAndCreateSchemaSet(yangModelCmHandle);
99                 updateModuleSetTagCache(moduleSetTag, allModuleReferencesFromCmHandle);
100             }
101         } else {
102             if (inUpgrade) {
103                 cpsModuleService.upgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId,
104                         NO_NEW_MODULES, moduleReferencesFromCache);
105             } else {
106                 cpsModuleService.createSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME,
107                         cmHandleId, NO_NEW_MODULES, moduleReferencesFromCache);
108             }
109         }
110         if (!inUpgrade) {
111             cpsAnchorService.createAnchor(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId, cmHandleId);
112         }
113         setCmHandleModuleSetTag(yangModelCmHandle, moduleSetTag);
114     }
115
116     /**
117      * Deletes the SchemaSet for schema set id if the SchemaSet Exists.
118      *
119      * @param schemaSetId the schema set id to be deleted
120      */
121     public void deleteSchemaSetIfExists(final String schemaSetId) {
122         try {
123             cpsModuleService.deleteSchemaSet(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetId,
124                 CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED);
125             log.debug("SchemaSet for {} has been deleted. Ready to be recreated.", schemaSetId);
126         } catch (final SchemaSetNotFoundException e) {
127             log.debug("No SchemaSet for {}. Assuming CmHandle has not been previously Module Synced.", schemaSetId);
128         }
129     }
130
131     private Optional<DataNode> getFirstReadyDataNodeWithModuleSetTag(final String moduleSetTag) {
132         final List<DataNode> dataNodes = StringUtils.isNotBlank(moduleSetTag) ? cmHandleQueries
133                 .queryNcmpRegistryByCpsPath("//cm-handles[@module-set-tag='" + moduleSetTag + "']",
134                         FetchDescendantsOption.OMIT_DESCENDANTS) : Collections.emptyList();
135         return dataNodes.stream().filter(dataNode -> {
136             final String cmHandleId = YangDataConverter.extractCmHandleIdFromXpath(dataNode.getXpath());
137             return cmHandleQueries.cmHandleHasState(cmHandleId, CmHandleState.READY);
138         }).findFirst();
139     }
140
141     private void setCmHandleModuleSetTag(final YangModelCmHandle upgradedCmHandle, final String moduleSetTag) {
142         final Map<String, Map<String, String>> dmiRegistryProperties = new HashMap<>(1);
143         final Map<String, String> cmHandleProperties = new HashMap<>(2);
144         cmHandleProperties.put("id", upgradedCmHandle.getId());
145         cmHandleProperties.put("module-set-tag", moduleSetTag);
146         dmiRegistryProperties.put("cm-handles", cmHandleProperties);
147         cpsDataService.updateNodeLeaves(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT,
148                 jsonObjectMapper.asJsonString(dmiRegistryProperties), OffsetDateTime.now());
149     }
150
151     private Collection<ModuleReference> syncAndCreateSchemaSet(final YangModelCmHandle yangModelCmHandle) {
152         final Collection<ModuleReference> allModuleReferencesFromCmHandle =
153                 dmiModelOperations.getModuleReferences(yangModelCmHandle);
154         final Collection<ModuleReference> identifiedNewModuleReferencesFromCmHandle = cpsModuleService
155                 .identifyNewModuleReferences(allModuleReferencesFromCmHandle);
156         final Map<String, String> newModuleNameToContentMap;
157         if (identifiedNewModuleReferencesFromCmHandle.isEmpty()) {
158             newModuleNameToContentMap = NO_NEW_MODULES;
159         } else {
160             newModuleNameToContentMap = dmiModelOperations.getNewYangResourcesFromDmi(yangModelCmHandle,
161                     identifiedNewModuleReferencesFromCmHandle);
162         }
163         cpsModuleService.createSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME,
164                 yangModelCmHandle.getId(), newModuleNameToContentMap, allModuleReferencesFromCmHandle);
165         return allModuleReferencesFromCmHandle;
166     }
167
168     private Collection<ModuleReference> upgradeOrCreateSchemaSetUsingModuleSetTag(final String schemaSetName,
169                                                                                   final String moduleSetTag,
170                                                                                   final String existingAnchorName,
171                                                                                   final boolean inUpgrade) {
172         log.info("Found cm handle having module set tag: {}", moduleSetTag);
173         final Collection<ModuleReference> moduleReferencesFromExistingCmHandle =
174                 cpsModuleService.getYangResourcesModuleReferences(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME,
175                         existingAnchorName);
176         if (inUpgrade) {
177             cpsModuleService.upgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetName,
178                     NO_NEW_MODULES, moduleReferencesFromExistingCmHandle);
179         } else {
180             cpsModuleService.createSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME,
181                     schemaSetName, NO_NEW_MODULES, moduleReferencesFromExistingCmHandle);
182         }
183         return moduleReferencesFromExistingCmHandle;
184     }
185
186     private String getModuleSetTag(final YangModelCmHandle yangModelCmHandle,
187                                    final CompositeState compositeState,
188                                    final boolean inUpgrade) {
189         if (inUpgrade) {
190             return ModuleOperationsUtils.getLockedCompositeStateDetails(compositeState.getLockReason())
191                     .get(ModuleOperationsUtils.MODULE_SET_TAG_KEY);
192         }
193         return yangModelCmHandle.getModuleSetTag();
194     }
195
196     private void updateModuleSetTagCache(final String moduleSetTag,
197                                          final Collection<ModuleReference> allModuleReferencesFromCmHandle) {
198         if (StringUtils.isNotBlank(moduleSetTag)) {
199             moduleSetTagCache.putIfAbsent(moduleSetTag, allModuleReferencesFromCmHandle);
200         }
201     }
202
203 }