2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Bell Canada
4 * 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;
37 * To create the {@link CompositeState}.
39 * @return {@link DataNode}
41 public CompositeState build() {
42 final CompositeState compositeState = new CompositeState();
43 compositeState.setCmHandleState(cmHandleState);
44 compositeState.setLockReason(lockReason);
45 compositeState.setDataStores(datastores);
46 compositeState.setLastUpdateTime(lastUpdatedTime);
47 return compositeState;
51 * To use attributes for creating {@link CompositeState}.
53 * @param cmHandleState for the data node
54 * @return CompositeStateBuilder
56 public CompositeStateBuilder withCmHandleState(final CmHandleState cmHandleState) {
57 this.cmHandleState = cmHandleState;
62 * To use attributes for creating {@link CompositeState}.
64 * @param reason for the locked state
65 * @param details for the locked state
66 * @return CompositeStateBuilder
68 public CompositeStateBuilder withLockReason(final LockReasonCategory reason, final String details) {
69 this.lockReason = LockReason.builder().lockReasonCategory(reason).details(details).build();
74 * To use attributes for creating {@link CompositeState}.
76 * @param time for the state change
77 * @return CompositeStateBuilder
79 public CompositeStateBuilder withLastUpdatedTime(final String time) {
80 this.lastUpdatedTime = time;
85 * To use attributes for creating {@link CompositeState}.
87 * @return composite state builder
89 public CompositeStateBuilder withLastUpdatedTimeNow() {
90 this.lastUpdatedTime = CompositeState.nowInSyncTimeFormat();
95 * To use attributes for creating {@link CompositeState}.
97 * @param syncState for the locked state
98 * @param lastSyncTime for the locked state
99 * @return CompositeStateBuilder
101 public CompositeStateBuilder withOperationalDataStores(final SyncState syncState, final String lastSyncTime) {
102 this.datastores = DataStores.builder().operationalDataStore(
103 Operational.builder().syncState(syncState).lastSyncTime(lastSyncTime).build()).build();
108 * To use dataNode for creating {@link CompositeState}.
110 * @param dataNode for the dataNode
111 * @return CompositeState
113 public CompositeStateBuilder fromDataNode(final DataNode dataNode) {
114 this.cmHandleState = CmHandleState.valueOf((String) dataNode.getLeaves()
115 .get("cm-handle-state"));
116 for (final DataNode stateChildNode : dataNode.getChildDataNodes()) {
117 if (stateChildNode.getXpath().endsWith("/lock-reason")) {
118 this.lockReason = new LockReason(LockReasonCategory.valueOf(
119 (String) stateChildNode.getLeaves().get("reason")),
120 (String) stateChildNode.getLeaves().get("details"));
122 if (stateChildNode.getXpath().endsWith("/datastores")) {
123 for (final DataNode dataStoreNodes : stateChildNode.getChildDataNodes()) {
124 Operational operationalDataStore = null;
125 if (dataStoreNodes.getXpath().contains("/operational")) {
126 operationalDataStore = Operational.builder()
127 .syncState(SyncState.valueOf((String) dataStoreNodes.getLeaves().get("sync-state")))
128 .lastSyncTime((String) dataStoreNodes.getLeaves().get("last-sync-time"))
131 this.datastores = DataStores.builder().operationalDataStore(operationalDataStore).build();