Composite State to handle dmi-reg YANG updates
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / inventory / sync / SyncUtils.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.sync;
22
23 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NCMP_DATASPACE_NAME;
24 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NCMP_DMI_REGISTRY_ANCHOR;
25 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NCMP_DMI_REGISTRY_PARENT;
26
27 import java.security.SecureRandom;
28 import java.time.OffsetDateTime;
29 import java.util.List;
30 import lombok.RequiredArgsConstructor;
31 import lombok.extern.slf4j.Slf4j;
32 import org.onap.cps.api.CpsDataService;
33 import org.onap.cps.ncmp.api.impl.operations.YangModelCmHandleRetriever;
34 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
35 import org.onap.cps.ncmp.api.inventory.CmHandleState;
36 import org.onap.cps.spi.CpsDataPersistenceService;
37 import org.onap.cps.spi.FetchDescendantsOption;
38 import org.onap.cps.spi.model.DataNode;
39 import org.onap.cps.utils.JsonObjectMapper;
40 import org.springframework.stereotype.Component;
41
42 @Slf4j
43 @Component
44 @RequiredArgsConstructor
45 public class SyncUtils {
46
47     private static final SecureRandom secureRandom = new SecureRandom();
48     private final CpsDataService cpsDataService;
49
50     private final CpsDataPersistenceService cpsDataPersistenceService;
51
52     private final JsonObjectMapper jsonObjectMapper;
53
54     private final YangModelCmHandleRetriever yangModelCmHandleRetriever;
55
56     /**
57      * Query data nodes for cm handles with an "ADVISED" cm handle state, and select a random entry for processing.
58      *
59      * @return a random yang model cm handle with an ADVISED state, return null if not found
60      */
61     public YangModelCmHandle getAnAdvisedCmHandle() {
62         final List<DataNode> advisedCmHandles = cpsDataPersistenceService.queryDataNodes("NCMP-Admin",
63             "ncmp-dmi-registry", "//cm-handles[@state=\"ADVISED\"]",
64             FetchDescendantsOption.OMIT_DESCENDANTS);
65         if (advisedCmHandles.isEmpty()) {
66             return null;
67         }
68         final int randomElementIndex = secureRandom.nextInt(advisedCmHandles.size());
69         final String cmHandleId = advisedCmHandles.get(randomElementIndex).getLeaves()
70             .get("id").toString();
71         return yangModelCmHandleRetriever.getYangModelCmHandle(cmHandleId);
72     }
73
74     /**
75      * Update the Cm Handle state to "READY".
76      *
77      * @param yangModelCmHandle yang model cm handle
78      * @param cmHandleState cm handle state
79      */
80     public void updateCmHandleState(final YangModelCmHandle yangModelCmHandle, final CmHandleState cmHandleState) {
81         yangModelCmHandle.getCompositeState().setCmhandleState(cmHandleState);
82         final String cmHandleJsonData = String.format("{\"cm-handles\":[%s]}",
83             jsonObjectMapper.asJsonString(yangModelCmHandle));
84         cpsDataService.updateNodeLeaves(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT,
85             cmHandleJsonData, OffsetDateTime.now());
86     }
87
88 }