012ba5ede20ad7e55db3f60f36f3de706f5e1558
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / inventory / CompositeStateBuilder.java
1 /*
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
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.inventory;
23
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;
28
29 public class CompositeStateBuilder {
30
31     private CmHandleState cmHandleState;
32     private LockReason lockReason;
33     private DataStores datastores;
34     private String lastUpdatedTime;
35
36     /**
37      * To create the {@link CompositeState}.
38      *
39      * @return {@link DataNode}
40      */
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;
48     }
49
50     /**
51      * To use attributes for creating {@link CompositeState}.
52      *
53      * @param cmHandleState for the data node
54      * @return CompositeStateBuilder
55      */
56     public CompositeStateBuilder withCmHandleState(final CmHandleState cmHandleState) {
57         this.cmHandleState = cmHandleState;
58         return this;
59     }
60
61     /**
62      * To use attributes for creating {@link CompositeState}.
63      *
64      * @param reason for the locked state
65      * @param details for the locked state
66      * @return CompositeStateBuilder
67      */
68     public CompositeStateBuilder withLockReason(final LockReasonCategory reason, final String details) {
69         this.lockReason = LockReason.builder().lockReasonCategory(reason).details(details).build();
70         return this;
71     }
72
73     /**
74      * To use attributes for creating {@link CompositeState}.
75      *
76      * @param time for the state change
77      * @return CompositeStateBuilder
78      */
79     public CompositeStateBuilder withLastUpdatedTime(final String time) {
80         this.lastUpdatedTime = time;
81         return this;
82     }
83
84     /**
85      * To use attributes for creating {@link CompositeState}.
86      *
87      * @return composite state builder
88      */
89     public CompositeStateBuilder withLastUpdatedTimeNow() {
90         this.lastUpdatedTime = CompositeState.nowInSyncTimeFormat();
91         return this;
92     }
93
94     /**
95      * To use attributes for creating {@link CompositeState}.
96      *
97      * @param syncState for the locked state
98      * @param lastSyncTime for the locked state
99      * @return CompositeStateBuilder
100      */
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();
104         return this;
105     }
106
107     /**
108      * To use dataNode for creating {@link CompositeState}.
109      *
110      * @param dataNode for the dataNode
111      * @return CompositeState
112      */
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 = getLockReason(stateChildNode);
119             }
120             if (stateChildNode.getXpath().endsWith("/datastores")) {
121                 for (final DataNode dataStoreNodes : stateChildNode.getChildDataNodes()) {
122                     Operational operationalDataStore = null;
123                     if (dataStoreNodes.getXpath().contains("/operational")) {
124                         operationalDataStore = getOperationalDataStore(dataStoreNodes);
125                     }
126                     this.datastores = DataStores.builder().operationalDataStore(operationalDataStore).build();
127                 }
128             }
129         }
130         return this;
131     }
132
133     private Operational getOperationalDataStore(final DataNode dataStoreNodes) {
134         return Operational.builder()
135                 .syncState(SyncState.valueOf((String) dataStoreNodes.getLeaves().get("sync-state")))
136                 .lastSyncTime((String) dataStoreNodes.getLeaves().get("last-sync-time"))
137                 .build();
138     }
139
140     private LockReason getLockReason(final DataNode stateChildNode) {
141         final boolean isLockReasonExists = stateChildNode.getLeaves().containsKey("reason");
142         return new LockReason(isLockReasonExists
143                 ? LockReasonCategory.valueOf((String) stateChildNode.getLeaves().get("reason"))
144                 : null, (String) stateChildNode.getLeaves().get("details"));
145     }
146
147 }