2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Bell Canada
4 * Modifications Copyright (C) 2022 Nordix Foundation.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.api.inventory;
24 import org.onap.cps.ncmp.api.inventory.CompositeState.DataStores;
25 import org.onap.cps.ncmp.api.inventory.CompositeState.LockReason;
26 import org.onap.cps.ncmp.api.inventory.CompositeState.Operational;
27 import org.onap.cps.spi.model.DataNode;
29 public class CompositeStateBuilder {
31 private CmHandleState cmHandleState;
32 private LockReason lockReason;
33 private DataStores datastores;
34 private String lastUpdatedTime;
35 private Boolean dataSyncEnabled;
38 * To create the {@link CompositeState}.
40 * @return {@link DataNode}
42 public CompositeState build() {
43 final CompositeState compositeState = new CompositeState();
44 compositeState.setCmHandleState(cmHandleState);
45 compositeState.setLockReason(lockReason);
46 compositeState.setDataStores(datastores);
47 compositeState.setLastUpdateTime(lastUpdatedTime);
48 compositeState.setDataSyncEnabled(dataSyncEnabled);
49 return compositeState;
53 * To use attributes for creating {@link CompositeState}.
55 * @param cmHandleState for the data node
56 * @return CompositeStateBuilder
58 public CompositeStateBuilder withCmHandleState(final CmHandleState cmHandleState) {
59 this.cmHandleState = cmHandleState;
64 * To use attributes for creating {@link CompositeState}.
66 * @param reason for the locked state
67 * @param details for the locked state
68 * @return CompositeStateBuilder
70 public CompositeStateBuilder withLockReason(final LockReasonCategory reason, final String details) {
71 this.lockReason = LockReason.builder().lockReasonCategory(reason).details(details).build();
76 * To use attributes for creating {@link CompositeState}.
78 * @param time for the state change
79 * @return CompositeStateBuilder
81 public CompositeStateBuilder withLastUpdatedTime(final String time) {
82 this.lastUpdatedTime = time;
87 * To use attributes for creating {@link CompositeState}.
89 * @return composite state builder
91 public CompositeStateBuilder withLastUpdatedTimeNow() {
92 this.lastUpdatedTime = CompositeState.nowInSyncTimeFormat();
97 * To use attributes for creating {@link CompositeState}.
99 * @param dataStoreSyncState for the locked state
100 * @param lastSyncTime for the locked state
101 * @return CompositeStateBuilder
103 public CompositeStateBuilder withOperationalDataStores(final DataStoreSyncState dataStoreSyncState,
104 final String lastSyncTime) {
105 this.datastores = DataStores.builder().operationalDataStore(
106 Operational.builder().dataStoreSyncState(dataStoreSyncState).lastSyncTime(lastSyncTime).build()).build();
111 * To use dataNode for creating {@link CompositeState}.
113 * @param dataNode for the dataNode
114 * @return CompositeState
116 public CompositeStateBuilder fromDataNode(final DataNode dataNode) {
117 this.cmHandleState = CmHandleState.valueOf((String) dataNode.getLeaves()
118 .get("cm-handle-state"));
119 this.lastUpdatedTime = (String) dataNode.getLeaves().get("last-update-time");
120 if (this.cmHandleState == CmHandleState.READY) {
121 this.dataSyncEnabled = (Boolean) dataNode.getLeaves().get("data-sync-enabled");
123 for (final DataNode stateChildNode : dataNode.getChildDataNodes()) {
124 if (stateChildNode.getXpath().endsWith("/lock-reason")) {
125 this.lockReason = getLockReason(stateChildNode);
127 if (stateChildNode.getXpath().endsWith("/datastores")) {
128 for (final DataNode dataStoreNodes : stateChildNode.getChildDataNodes()) {
129 Operational operationalDataStore = null;
130 if (dataStoreNodes.getXpath().contains("/operational")) {
131 operationalDataStore = getOperationalDataStore(dataStoreNodes);
133 this.datastores = DataStores.builder().operationalDataStore(operationalDataStore).build();
140 private Operational getOperationalDataStore(final DataNode dataStoreNodes) {
141 return Operational.builder()
142 .dataStoreSyncState(DataStoreSyncState.valueOf((String) dataStoreNodes.getLeaves().get("sync-state")))
143 .lastSyncTime((String) dataStoreNodes.getLeaves().get("last-sync-time"))
147 private LockReason getLockReason(final DataNode stateChildNode) {
148 final boolean isLockReasonExists = stateChildNode.getLeaves().containsKey("reason");
149 return new LockReason(isLockReasonExists
150 ? LockReasonCategory.valueOf((String) stateChildNode.getLeaves().get("reason"))
151 : null, (String) stateChildNode.getLeaves().get("details"));