Define Initial Data Sync Enabled Flag and state
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / inventory / InventoryPersistence.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 Nordix Foundation
4  *  Modifications Copyright (C) 2022 Bell Canada
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.ncmp.api.inventory;
23
24 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME;
25
26 import java.time.OffsetDateTime;
27 import java.util.Collection;
28 import java.util.List;
29 import lombok.RequiredArgsConstructor;
30 import org.onap.cps.api.CpsDataService;
31 import org.onap.cps.api.CpsModuleService;
32 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
33 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
34 import org.onap.cps.spi.CpsDataPersistenceService;
35 import org.onap.cps.spi.FetchDescendantsOption;
36 import org.onap.cps.spi.model.DataNode;
37 import org.onap.cps.spi.model.ModuleDefinition;
38 import org.onap.cps.utils.CpsValidator;
39 import org.onap.cps.utils.JsonObjectMapper;
40 import org.springframework.stereotype.Component;
41
42 @RequiredArgsConstructor
43 @Component
44 public class InventoryPersistence {
45
46     private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
47
48     private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
49
50     private String xpathCmHandle = "/dmi-registry/cm-handles[@id='" + "%s" + "']";
51
52     private static final String ANCESTOR_CM_HANDLES = "\"]/ancestor::cm-handles";
53
54     private final JsonObjectMapper jsonObjectMapper;
55
56     private final CpsDataService cpsDataService;
57
58     private final CpsModuleService cpsModuleService;
59
60     private final CpsDataPersistenceService cpsDataPersistenceService;
61
62     /**
63      * Get the Cm Handle Composite State from the data node.
64      *
65      * @param cmHandleId cm handle id
66      * @return the cm handle composite state
67      */
68     public CompositeState getCmHandleState(final String cmHandleId) {
69         final DataNode stateAsDataNode = cpsDataService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
70             String.format(xpathCmHandle, cmHandleId) + "/state",
71             FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
72         return new CompositeStateBuilder().fromDataNode(stateAsDataNode).build();
73     }
74
75     /**
76      * Save the cm handles state.
77      *
78      * @param cmHandleId    cm handle id
79      * @param compositeState composite state
80      */
81     public void saveCmHandleState(final String cmHandleId, final CompositeState compositeState) {
82         final String cmHandleJsonData = String.format("{\"state\":%s}",
83             jsonObjectMapper.asJsonString(compositeState));
84         cpsDataService.replaceNodeTree(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
85             String.format(xpathCmHandle, cmHandleId),
86             cmHandleJsonData, OffsetDateTime.now());
87     }
88
89     /**
90      * Method which returns cm handles by the cm handles state.
91      *
92      * @param cmHandleState cm handle state
93      * @return a list of cm handles
94      */
95     public List<DataNode> getCmHandlesByState(final CmHandleState cmHandleState) {
96         return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME,
97             NCMP_DMI_REGISTRY_ANCHOR, "//state[@cm-handle-state=\""
98                 + cmHandleState + ANCESTOR_CM_HANDLES,
99             FetchDescendantsOption.OMIT_DESCENDANTS);
100     }
101
102     /**
103      * Method to return data nodes representing the cm handles.
104      *
105      * @param cpsPath cps path for which the cmHandle is requested
106      * @param fetchDescendantsOption defines the scope of data to fetch: either single node or all the descendant nodes
107      * @return a list of data nodes representing the cm handles.
108      */
109     public List<DataNode> getCmHandleDataNodesByCpsPath(final String cpsPath,
110                                                         final FetchDescendantsOption fetchDescendantsOption) {
111         return cpsDataPersistenceService.queryDataNodes(
112             NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, cpsPath, fetchDescendantsOption);
113     }
114
115     /**
116      * Method which returns cm handles by the cm handle id and state.
117      * @param cmHandleId cm handle id
118      * @param cmHandleState cm handle state
119      * @return a list of cm handles
120      */
121     public List<DataNode> getCmHandlesByIdAndState(final String cmHandleId, final CmHandleState cmHandleState) {
122         return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME,
123             NCMP_DMI_REGISTRY_ANCHOR, "//cm-handles[@id='" + cmHandleId + "']/state[@cm-handle-state=\""
124                 + cmHandleState + ANCESTOR_CM_HANDLES,
125             FetchDescendantsOption.OMIT_DESCENDANTS);
126     }
127
128     /**
129      * Method which returns cm handles by the operational sync state of cm handle.
130      * @param dataStoreSyncState sync state
131      * @return a list of cm handles
132      */
133     public List<DataNode> getCmHandlesByOperationalSyncState(final DataStoreSyncState dataStoreSyncState) {
134         return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME,
135             NCMP_DMI_REGISTRY_ANCHOR, "//state/datastores"
136                 + "/operational[@sync-state=\"" + dataStoreSyncState + ANCESTOR_CM_HANDLES,
137             FetchDescendantsOption.OMIT_DESCENDANTS);
138     }
139
140     /**
141      * This method retrieves DMI service name, DMI properties and the state for a given cm handle.
142      * @param cmHandleId the id of the cm handle
143      * @return yang model cm handle
144      */
145     public YangModelCmHandle getYangModelCmHandle(final String cmHandleId) {
146         CpsValidator.validateNameCharacters(cmHandleId);
147         return YangDataConverter.convertCmHandleToYangModel(getCmHandleDataNode(cmHandleId), cmHandleId);
148     }
149
150     /**
151      * Method to return module definitions by cmHandleId.
152      *
153      * @param cmHandleId cm handle ID
154      * @return a collection of module definitions (moduleName, revision and yang resource content)
155      */
156     public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
157         return cpsModuleService.getModuleDefinitionsByAnchorName(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId);
158     }
159
160     private DataNode getCmHandleDataNode(final String cmHandle) {
161         return cpsDataService.getDataNode(NCMP_DATASPACE_NAME,
162             NCMP_DMI_REGISTRY_ANCHOR,
163             String.format(xpathCmHandle, cmHandle),
164             FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
165     }
166
167 }