CompositeStateBuilder added for building the compositeState
[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  * ================================================================================
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
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.inventory;
22
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;
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 String reason, final String details) {
69         this.lockReason = LockReason.builder().reason(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      * @param syncState for the locked state
88      * @param lastSyncTime for the locked state
89      * @return CompositeStateBuilder
90      */
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();
94         return this;
95     }
96
97     /**
98      * To use attributes for creating {@link CompositeState}.
99      *
100      * @param syncState for the locked state
101      * @param lastSyncTime for the locked state
102      * @return CompositeStateBuilder
103      */
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();
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         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"));
123             }
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"))
132                             .build();
133                     } else {
134                         runningDataStore = Running.builder()
135                             .syncState((String) dataStoreNodes.getLeaves().get("sync-state"))
136                             .lastSyncTime((String) dataStoreNodes.getLeaves().get("last-sync-time"))
137                             .build();
138                     }
139                     this.datastores = DataStores.builder().operationalDataStore(operationalDataStore)
140                         .runningDataStore(runningDataStore).build();
141                 }
142             }
143         }
144         return this;
145     }
146
147 }