Merge "Use Seconds as time unit for all performance tests"
[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-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.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.CpsAdminService;
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.inventory.LockReasonCategory;
45 import org.onap.cps.ncmp.api.impl.operations.DmiModelOperations;
46 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
47 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
48 import org.onap.cps.spi.CascadeDeleteAllowed;
49 import org.onap.cps.spi.FetchDescendantsOption;
50 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException;
51 import org.onap.cps.spi.model.DataNode;
52 import org.onap.cps.spi.model.ModuleReference;
53 import org.onap.cps.utils.JsonObjectMapper;
54 import org.springframework.stereotype.Service;
55
56 @Slf4j
57 @Service
58 @RequiredArgsConstructor
59 public class ModuleSyncService {
60
61     private final DmiModelOperations dmiModelOperations;
62     private final CpsModuleService cpsModuleService;
63     private final CpsAdminService cpsAdminService;
64     private final CmHandleQueries cmHandleQueries;
65     private final CpsDataService cpsDataService;
66     private final JsonObjectMapper jsonObjectMapper;
67     private final Map<String, Collection<ModuleReference>> moduleSetTagCache;
68     private static final Map<String, String> NO_NEW_MODULES = Collections.emptyMap();
69
70     /**
71      * This method registers a cm handle and initiates modules sync.
72      *
73      * @param yangModelCmHandle the yang model of cm handle.
74      */
75     public void syncAndCreateOrUpgradeSchemaSetAndAnchor(final YangModelCmHandle yangModelCmHandle) {
76
77         final String moduleSetTag;
78         final String cmHandleId = yangModelCmHandle.getId();
79         final CompositeState compositeState = yangModelCmHandle.getCompositeState();
80         final boolean inUpgrade = isInUpgrade(compositeState);
81
82         if (inUpgrade) {
83             moduleSetTag = extractModuleSetTag(compositeState);
84         } else {
85             moduleSetTag = yangModelCmHandle.getModuleSetTag();
86         }
87
88         final Collection<ModuleReference> moduleReferencesFromCache = moduleSetTagCache.get(moduleSetTag);
89
90         if (moduleReferencesFromCache == null) {
91             final Optional<DataNode> optionalExistingCmHandleWithSameModuleSetTag
92                     = getFirstReadyDataNodeWithModuleSetTag(moduleSetTag);
93
94             if (optionalExistingCmHandleWithSameModuleSetTag.isPresent()) {
95                 final String existingCmHandleAnchorName
96                         = optionalExistingCmHandleWithSameModuleSetTag.get().getAnchorName();
97                 createOrUpgradeSchemaSetUsingModuleSetTag(cmHandleId, moduleSetTag, existingCmHandleAnchorName);
98             } else {
99                 syncAndCreateSchemaSet(yangModelCmHandle, moduleSetTag);
100             }
101         } else {
102             cpsModuleService.createOrUpgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME,
103                     cmHandleId, NO_NEW_MODULES, moduleReferencesFromCache);
104         }
105         if (!inUpgrade) {
106             cpsAdminService.createAnchor(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId, cmHandleId);
107         }
108         setCmHandleModuleSetTag(yangModelCmHandle, moduleSetTag);
109     }
110
111     private void syncAndCreateSchemaSet(final YangModelCmHandle yangModelCmHandle, final String moduleSetTag) {
112         final Collection<ModuleReference> allModuleReferencesFromCmHandle =
113                 dmiModelOperations.getModuleReferences(yangModelCmHandle);
114         final Collection<ModuleReference> identifiedNewModuleReferencesFromCmHandle = cpsModuleService
115                 .identifyNewModuleReferences(allModuleReferencesFromCmHandle);
116         final Map<String, String> newModuleNameToContentMap;
117         if (identifiedNewModuleReferencesFromCmHandle.isEmpty()) {
118             newModuleNameToContentMap = NO_NEW_MODULES;
119         } else {
120             newModuleNameToContentMap = dmiModelOperations.getNewYangResourcesFromDmi(yangModelCmHandle,
121                     identifiedNewModuleReferencesFromCmHandle);
122         }
123         cpsModuleService.createOrUpgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME,
124             yangModelCmHandle.getId(), newModuleNameToContentMap, allModuleReferencesFromCmHandle);
125         moduleSetTagCache.put(moduleSetTag, allModuleReferencesFromCmHandle);
126     }
127
128     /**
129      * Deletes the SchemaSet for schema set id if the SchemaSet Exists.
130      *
131      * @param schemaSetId the schema set id to be deleted
132      */
133     public void deleteSchemaSetIfExists(final String schemaSetId) {
134         try {
135             cpsModuleService.deleteSchemaSet(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetId,
136                 CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED);
137             log.debug("SchemaSet for {} has been deleted. Ready to be recreated.", schemaSetId);
138         } catch (final SchemaSetNotFoundException e) {
139             log.debug("No SchemaSet for {}. Assuming CmHandle has not been previously Module Synced.", schemaSetId);
140         }
141     }
142
143     private Optional<DataNode> getFirstReadyDataNodeWithModuleSetTag(final String moduleSetTag) {
144         final List<DataNode> dataNodes = StringUtils.isNotBlank(moduleSetTag) ? cmHandleQueries
145                 .queryNcmpRegistryByCpsPath("//cm-handles[@module-set-tag='" + moduleSetTag + "']",
146                         FetchDescendantsOption.OMIT_DESCENDANTS) : Collections.emptyList();
147         return dataNodes.stream().filter(dataNode -> {
148             final String cmHandleId = YangDataConverter.extractCmHandleIdFromXpath(dataNode.getXpath());
149             return cmHandleQueries.cmHandleHasState(cmHandleId, CmHandleState.READY);
150         }).findFirst();
151     }
152
153     private void setCmHandleModuleSetTag(final YangModelCmHandle upgradedCmHandle, final String moduleSetTag) {
154         final Map<String, Map<String, String>> dmiRegistryProperties = new HashMap<>(1);
155         final Map<String, String> cmHandleProperties = new HashMap<>(2);
156         cmHandleProperties.put("id", upgradedCmHandle.getId());
157         cmHandleProperties.put("module-set-tag", moduleSetTag);
158         dmiRegistryProperties.put("cm-handles", cmHandleProperties);
159         cpsDataService.updateNodeLeaves(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT,
160                 jsonObjectMapper.asJsonString(dmiRegistryProperties), OffsetDateTime.now());
161     }
162
163     private void createOrUpgradeSchemaSetUsingModuleSetTag(final String schemaSetName,
164                                                            final String moduleSetTag,
165                                                            final String existingCmHandleAnchorName) {
166         log.info("Found cm handle having module set tag: {}", moduleSetTag);
167         final Collection<ModuleReference> moduleReferencesFromExistingCmHandle =
168                 cpsModuleService.getYangResourcesModuleReferences(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME,
169                         existingCmHandleAnchorName);
170         cpsModuleService.createOrUpgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME,
171             schemaSetName, NO_NEW_MODULES, moduleReferencesFromExistingCmHandle);
172         moduleSetTagCache.put(moduleSetTag, moduleReferencesFromExistingCmHandle);
173     }
174
175     private static String extractModuleSetTag(final CompositeState compositeState) {
176         return compositeState.getLockReason().getDetails().split(":")[1].trim();
177     }
178
179     private static boolean isInUpgrade(final CompositeState compositeState) {
180         return compositeState.getLockReason() != null && LockReasonCategory.MODULE_UPGRADE.equals(
181                 compositeState.getLockReason().getLockReasonCategory());
182     }
183 }