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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.api.inventory.sync;
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;
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;
43 @RequiredArgsConstructor
44 public class SyncUtils {
46 private static final SecureRandom secureRandom = new SecureRandom();
47 private final CpsDataService cpsDataService;
49 private final CpsDataPersistenceService cpsDataPersistenceService;
51 private final JsonObjectMapper jsonObjectMapper;
53 private final YangModelCmHandleRetriever yangModelCmHandleRetriever;
56 * Query data nodes for cm handles with an "ADVISED" cm handle state, and select a random entry for processing.
58 * @return a random yang model cm handle with an ADVISED state, return null if not found
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()) {
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);
74 * Update the Cm Handle state to "READY".
76 * @param yangModelCmHandle yang model cm handle
77 * @param state cm handle state
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());