summaryrefslogtreecommitdiffstats
path: root/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CompositeStateBuilder.java
blob: a0fc0c3a91f8c2720bc3de4f30fa189f462be819 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * ============LICENSE_START=======================================================
 * Copyright (C) 2022 Bell Canada
 * Modifications Copyright (C) 2022 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.cps.ncmp.api.inventory;

import org.onap.cps.ncmp.api.inventory.CompositeState.DataStores;
import org.onap.cps.ncmp.api.inventory.CompositeState.LockReason;
import org.onap.cps.ncmp.api.inventory.CompositeState.Operational;
import org.onap.cps.spi.model.DataNode;

public class CompositeStateBuilder {

    private CmHandleState cmHandleState;
    private LockReason lockReason;
    private DataStores datastores;
    private String lastUpdatedTime;

    /**
     * To create the {@link CompositeState}.
     *
     * @return {@link DataNode}
     */
    public CompositeState build() {
        final CompositeState compositeState = new CompositeState();
        compositeState.setCmHandleState(cmHandleState);
        compositeState.setLockReason(lockReason);
        compositeState.setDataStores(datastores);
        compositeState.setLastUpdateTime(lastUpdatedTime);
        return compositeState;
    }

    /**
     * To use attributes for creating {@link CompositeState}.
     *
     * @param cmHandleState for the data node
     * @return CompositeStateBuilder
     */
    public CompositeStateBuilder withCmHandleState(final CmHandleState cmHandleState) {
        this.cmHandleState = cmHandleState;
        return this;
    }

    /**
     * To use attributes for creating {@link CompositeState}.
     *
     * @param reason for the locked state
     * @param details for the locked state
     * @return CompositeStateBuilder
     */
    public CompositeStateBuilder withLockReason(final LockReasonCategory reason, final String details) {
        this.lockReason = LockReason.builder().lockReasonCategory(reason).details(details).build();
        return this;
    }

    /**
     * To use attributes for creating {@link CompositeState}.
     *
     * @param time for the state change
     * @return CompositeStateBuilder
     */
    public CompositeStateBuilder withLastUpdatedTime(final String time) {
        this.lastUpdatedTime = time;
        return this;
    }

    /**
     * To use attributes for creating {@link CompositeState}.
     *
     * @return composite state builder
     */
    public CompositeStateBuilder withLastUpdatedTimeNow() {
        this.lastUpdatedTime = CompositeState.nowInSyncTimeFormat();
        return this;
    }

    /**
     * To use attributes for creating {@link CompositeState}.
     *
     * @param dataStoreSyncState for the locked state
     * @param lastSyncTime for the locked state
     * @return CompositeStateBuilder
     */
    public CompositeStateBuilder withOperationalDataStores(final DataStoreSyncState dataStoreSyncState,
                                                           final String lastSyncTime) {
        this.datastores = DataStores.builder().operationalDataStore(
            Operational.builder().dataStoreSyncState(dataStoreSyncState).lastSyncTime(lastSyncTime).build()).build();
        return this;
    }

    /**
     * To use dataNode for creating {@link CompositeState}.
     *
     * @param dataNode for the dataNode
     * @return CompositeState
     */
    public CompositeStateBuilder fromDataNode(final DataNode dataNode) {
        this.cmHandleState = CmHandleState.valueOf((String) dataNode.getLeaves()
            .get("cm-handle-state"));
        this.lastUpdatedTime = (String) dataNode.getLeaves().get("last-update-time");
        for (final DataNode stateChildNode : dataNode.getChildDataNodes()) {
            if (stateChildNode.getXpath().endsWith("/lock-reason")) {
                this.lockReason = getLockReason(stateChildNode);
            }
            if (stateChildNode.getXpath().endsWith("/datastores")) {
                for (final DataNode dataStoreNodes : stateChildNode.getChildDataNodes()) {
                    Operational operationalDataStore = null;
                    if (dataStoreNodes.getXpath().contains("/operational")) {
                        operationalDataStore = getOperationalDataStore(dataStoreNodes);
                    }
                    this.datastores = DataStores.builder().operationalDataStore(operationalDataStore).build();
                }
            }
        }
        return this;
    }

    private Operational getOperationalDataStore(final DataNode dataStoreNodes) {
        return Operational.builder()
                .dataStoreSyncState(DataStoreSyncState.valueOf((String) dataStoreNodes.getLeaves().get("sync-state")))
                .lastSyncTime((String) dataStoreNodes.getLeaves().get("last-sync-time"))
                .build();
    }

    private LockReason getLockReason(final DataNode stateChildNode) {
        final boolean isLockReasonExists = stateChildNode.getLeaves().containsKey("reason");
        return new LockReason(isLockReasonExists
                ? LockReasonCategory.valueOf((String) stateChildNode.getLeaves().get("reason"))
                : null, (String) stateChildNode.getLeaves().get("details"));
    }

}