Remove Model Sync From Registration
[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  *  ================================================================================
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.inventory;
22
23 import java.time.OffsetDateTime;
24 import java.util.List;
25 import lombok.RequiredArgsConstructor;
26 import org.onap.cps.api.CpsDataService;
27 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
28 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
29 import org.onap.cps.spi.CpsDataPersistenceService;
30 import org.onap.cps.spi.FetchDescendantsOption;
31 import org.onap.cps.spi.model.DataNode;
32 import org.onap.cps.utils.CpsValidator;
33 import org.onap.cps.utils.JsonObjectMapper;
34 import org.springframework.stereotype.Component;
35
36 @RequiredArgsConstructor
37 @Component
38 public class InventoryPersistence {
39
40     private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
41
42     private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
43
44     private final JsonObjectMapper jsonObjectMapper;
45
46     private final CpsDataService cpsDataService;
47
48     private final CpsDataPersistenceService cpsDataPersistenceService;
49
50     private static final CompositeStateBuilder compositeStateBuilder = new CompositeStateBuilder();
51
52     /**
53      * Get the Cm Handle Composite State from the data node.
54      *
55      * @param cmHandleId cm handle id
56      * @return the cm handle composite state
57      */
58     public CompositeState getCmHandleState(final String cmHandleId) {
59         final DataNode stateAsDataNode = cpsDataService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
60             "/dmi-registry/cm-handles[@id='" + cmHandleId + "']/state",
61             FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
62         return compositeStateBuilder.fromDataNode(stateAsDataNode).build();
63     }
64
65     /**
66      * Save the cm handles state.
67      *
68      * @param cmHandleId    cm handle id
69      * @param compositeState composite state
70      */
71     public void saveCmHandleState(final String cmHandleId, final CompositeState compositeState) {
72         final String cmHandleJsonData = String.format("{\"state\":%s}",
73             jsonObjectMapper.asJsonString(compositeState));
74         cpsDataService.replaceNodeTree(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
75             "/dmi-registry/cm-handles[@id='" + cmHandleId + "']",
76             cmHandleJsonData, OffsetDateTime.now());
77     }
78
79     /**
80      * Method which returns cm handles by the cm handles state.
81      *
82      * @param cmHandleState cm handle state
83      * @return a list of cm handles
84      */
85     public List<DataNode> getCmHandlesByState(final CmHandleState cmHandleState) {
86         return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME,
87             NCMP_DMI_REGISTRY_ANCHOR, "//state[@cm-handle-state=\""
88                 + cmHandleState + "\"]/ancestor::cm-handles",
89             FetchDescendantsOption.OMIT_DESCENDANTS);
90     }
91
92     /**
93      * This method retrieves DMI service name, DMI properties and the state for a given cm handle.
94      * @param cmHandleId the id of the cm handle
95      * @return yang model cm handle
96      */
97     public YangModelCmHandle getYangModelCmHandle(final String cmHandleId) {
98         CpsValidator.validateNameCharacters(cmHandleId);
99         return YangDataConverter.convertCmHandleToYangModel(getCmHandleDataNode(cmHandleId), cmHandleId);
100     }
101
102     private DataNode getCmHandleDataNode(final String cmHandle) {
103         final String xpathForDmiRegistryToFetchCmHandle = "/dmi-registry/cm-handles[@id='" + cmHandle + "']";
104         return cpsDataService.getDataNode(NCMP_DATASPACE_NAME,
105             NCMP_DMI_REGISTRY_ANCHOR,
106             xpathForDmiRegistryToFetchCmHandle,
107             FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
108     }
109
110 }