Refactoring persistence classes
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / inventory / CompositeStateBuilder.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2022 Bell Canada
4  * Modifications Copyright (C) 2022-2023 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
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.ncmp.api.impl.inventory;
23
24 import org.onap.cps.ncmp.api.impl.inventory.CompositeState.DataStores;
25 import org.onap.cps.ncmp.api.impl.inventory.CompositeState.LockReason;
26 import org.onap.cps.ncmp.api.impl.inventory.CompositeState.Operational;
27 import org.onap.cps.spi.model.DataNode;
28
29 public class CompositeStateBuilder {
30
31     private CmHandleState cmHandleState;
32     private LockReason lockReason;
33     private DataStores datastores;
34     private String lastUpdatedTime;
35     private Boolean dataSyncEnabled;
36
37     /**
38      * To create the {@link CompositeState}.
39      *
40      * @return {@link DataNode}
41      */
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;
50     }
51
52     /**
53      * To use attributes for creating {@link CompositeState}.
54      *
55      * @param cmHandleState for the data node
56      * @return CompositeStateBuilder
57      */
58     public CompositeStateBuilder withCmHandleState(final CmHandleState cmHandleState) {
59         this.cmHandleState = cmHandleState;
60         return this;
61     }
62
63     /**
64      * To use attributes for creating {@link CompositeState}.
65      *
66      * @param reason for the locked state
67      * @param details for the locked state
68      * @return CompositeStateBuilder
69      */
70     public CompositeStateBuilder withLockReason(final LockReasonCategory reason, final String details) {
71         this.lockReason = LockReason.builder().lockReasonCategory(reason).details(details).build();
72         return this;
73     }
74
75     /**
76      * To use attributes for creating {@link CompositeState}.
77      *
78      * @param time for the state change
79      * @return CompositeStateBuilder
80      */
81     public CompositeStateBuilder withLastUpdatedTime(final String time) {
82         this.lastUpdatedTime = time;
83         return this;
84     }
85
86     /**
87      * To use attributes for creating {@link CompositeState}.
88      *
89      * @return composite state builder
90      */
91     public CompositeStateBuilder withLastUpdatedTimeNow() {
92         this.lastUpdatedTime = CompositeState.nowInSyncTimeFormat();
93         return this;
94     }
95
96     /**
97      * To use attributes for creating {@link CompositeState}.
98      *
99      * @param dataStoreSyncState for the locked state
100      * @param lastSyncTime for the locked state
101      * @return CompositeStateBuilder
102      */
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();
107         return this;
108     }
109
110     /**
111      * To use dataNode for creating {@link CompositeState}.
112      *
113      * @param dataNode for the dataNode
114      * @return CompositeState
115      */
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");
122         }
123         for (final DataNode stateChildNode : dataNode.getChildDataNodes()) {
124             if (stateChildNode.getXpath().endsWith("/lock-reason")) {
125                 this.lockReason = getLockReason(stateChildNode);
126             }
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);
132                     }
133                     this.datastores = DataStores.builder().operationalDataStore(operationalDataStore).build();
134                 }
135             }
136         }
137         return this;
138     }
139
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"))
144                 .build();
145     }
146
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"));
152     }
153
154 }