2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Bell Canada
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;
23 import org.onap.cps.ncmp.api.inventory.CompositeState.DataStores;
24 import org.onap.cps.ncmp.api.inventory.CompositeState.LockReason;
25 import org.onap.cps.ncmp.api.inventory.CompositeState.Operational;
26 import org.onap.cps.ncmp.api.inventory.CompositeState.Running;
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 String reason, final String details) {
69 this.lockReason = LockReason.builder().reason(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 * @param syncState for the locked state
88 * @param lastSyncTime for the locked state
89 * @return CompositeStateBuilder
91 public CompositeStateBuilder withOperationalDataStores(final String syncState, final String lastSyncTime) {
92 this.datastores = DataStores.builder().operationalDataStore(
93 Operational.builder().syncState(syncState).lastSyncTime(lastSyncTime).build()).build();
98 * To use attributes for creating {@link CompositeState}.
100 * @param syncState for the locked state
101 * @param lastSyncTime for the locked state
102 * @return CompositeStateBuilder
104 public CompositeStateBuilder withRunningDataStores(final String syncState, final String lastSyncTime) {
105 this.datastores = DataStores.builder().runningDataStore(
106 Running.builder().syncState(syncState).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 for (final DataNode stateChildNode : dataNode.getChildDataNodes()) {
120 if (stateChildNode.getXpath().endsWith("/lock-reason")) {
121 this.lockReason = new LockReason((String) stateChildNode.getLeaves().get("reason"),
122 (String) stateChildNode.getLeaves().get("details"));
124 if (stateChildNode.getXpath().endsWith("/datastores")) {
125 for (final DataNode dataStoreNodes : stateChildNode.getChildDataNodes()) {
126 Operational operationalDataStore = null;
127 Running runningDataStore = null;
128 if (dataStoreNodes.getXpath().contains("/operational")) {
129 operationalDataStore = Operational.builder()
130 .syncState((String) dataStoreNodes.getLeaves().get("sync-state"))
131 .lastSyncTime((String) dataStoreNodes.getLeaves().get("last-sync-time"))
134 runningDataStore = Running.builder()
135 .syncState((String) dataStoreNodes.getLeaves().get("sync-state"))
136 .lastSyncTime((String) dataStoreNodes.getLeaves().get("last-sync-time"))
139 this.datastores = DataStores.builder().operationalDataStore(operationalDataStore)
140 .runningDataStore(runningDataStore).build();