924546433eb7e04e2b362e2d9fd939b4c9bb9ae0
[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.spi.CpsDataPersistenceService;
36 import org.onap.cps.spi.FetchDescendantsOption;
37 import org.onap.cps.spi.model.DataNode;
38 import org.onap.cps.utils.JsonObjectMapper;
39 import org.springframework.stereotype.Component;
40
41 @Slf4j
42 @Component
43 @RequiredArgsConstructor
44 public class SyncUtils {
45
46     private static final SecureRandom secureRandom = new SecureRandom();
47     private final CpsDataService cpsDataService;
48
49     private final CpsDataPersistenceService cpsDataPersistenceService;
50
51     private final JsonObjectMapper jsonObjectMapper;
52
53     private final YangModelCmHandleRetriever yangModelCmHandleRetriever;
54
55     /**
56      * Query data nodes for cm handles with an "ADVISED" cm handle state, and select a random entry for processing.
57      *
58      * @return a random yang model cm handle with an ADVISED state, return null if not found
59      */
60     public YangModelCmHandle getAnAdvisedCmHandle() {
61         final List<DataNode> advisedCmHandles = cpsDataPersistenceService.queryDataNodes("NCMP-Admin",
62             "ncmp-dmi-registry", "//cm-handles[@state=\"ADVISED\"]",
63             FetchDescendantsOption.OMIT_DESCENDANTS);
64         if (advisedCmHandles.isEmpty()) {
65             return null;
66         }
67         final int randomElementIndex = secureRandom.nextInt(advisedCmHandles.size());
68         final String cmHandleId = advisedCmHandles.get(randomElementIndex).getLeaves()
69             .get("id").toString();
70         return yangModelCmHandleRetriever.getYangModelCmHandle(cmHandleId);
71     }
72
73     /**
74      * Update the Cm Handle state to "READY".
75      *
76      * @param yangModelCmHandle yang model cm handle
77      * @param state cm handle state
78      */
79     public void updateCmHandleState(final YangModelCmHandle yangModelCmHandle, final String state) {
80         yangModelCmHandle.setCmHandleState(state);
81         final String cmHandleJsonData = String.format("{\"cm-handles\":[%s]}",
82             jsonObjectMapper.asJsonString(yangModelCmHandle));
83         cpsDataService.updateNodeLeaves(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT,
84             cmHandleJsonData, OffsetDateTime.now());
85     }
86
87 }