Refactoring persistence classes
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / inventory / CompositeState.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2022-2023 Nordix Foundation
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.impl.inventory;
22
23 import com.fasterxml.jackson.annotation.JsonInclude;
24 import com.fasterxml.jackson.annotation.JsonProperty;
25 import java.time.OffsetDateTime;
26 import java.time.format.DateTimeFormatter;
27 import lombok.Builder;
28 import lombok.Data;
29 import lombok.Getter;
30 import lombok.NoArgsConstructor;
31 import lombok.Setter;
32
33 /**
34  * State Model to store state corresponding to the Yang resource dmi-registry model.
35  */
36 @Getter
37 @Setter
38 @NoArgsConstructor
39 @JsonInclude(JsonInclude.Include.NON_NULL)
40 public class CompositeState {
41
42     @JsonProperty("cm-handle-state")
43     private CmHandleState cmHandleState;
44
45     @JsonProperty("lock-reason")
46     private LockReason lockReason;
47
48     @JsonProperty("last-update-time")
49     private String lastUpdateTime;
50
51     @JsonProperty("data-sync-enabled")
52     private Boolean dataSyncEnabled;
53
54     @JsonProperty("datastores")
55     private DataStores dataStores;
56
57     /**
58      * Date and Time in the format of yyyy-MM-dd'T'HH:mm:ss.SSSZ
59      */
60     private static final DateTimeFormatter dateTimeFormatter =
61             DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
62
63     /**
64      * Composite State copy constructor.
65      *
66      * @param compositeState Composite State
67      */
68     public CompositeState(final CompositeState compositeState) {
69         this.cmHandleState = compositeState.getCmHandleState();
70         this.lockReason = compositeState.getLockReason();
71         this.lastUpdateTime = compositeState.getLastUpdateTime();
72         this.dataSyncEnabled = compositeState.getDataSyncEnabled();
73         this.dataStores = compositeState.getDataStores();
74     }
75
76
77     /**
78      * This will specify the latest lock reason for a specific cm handle. If a cm handle is in a state other than LOCKED
79      * it specifies the last lock reason.
80      * This can be used to track retry attempts as part of the lock details.
81      */
82     @Data
83     @Builder
84     @JsonInclude(JsonInclude.Include.NON_NULL)
85     public static class LockReason {
86
87         @JsonProperty("reason")
88         private LockReasonCategory lockReasonCategory;
89
90         @JsonProperty("details")
91         private String details;
92
93     }
94
95     @Data
96     @Builder
97     @JsonInclude(JsonInclude.Include.NON_NULL)
98     public static class DataStores {
99
100         @JsonProperty("operational")
101         private Operational operationalDataStore;
102     }
103
104     @Data
105     @Builder
106     @JsonInclude(JsonInclude.Include.NON_NULL)
107     public static class Operational {
108
109         @JsonProperty("sync-state")
110         private DataStoreSyncState dataStoreSyncState;
111
112         @JsonProperty("last-sync-time")
113         private String lastSyncTime;
114     }
115
116     @Data
117     @Builder
118     @JsonInclude(JsonInclude.Include.NON_NULL)
119     public static class Running {
120
121         @JsonProperty("sync-state")
122         private String syncState;
123
124         @JsonProperty("last-sync-time")
125         private String lastSyncTime;
126     }
127
128     /**
129      * The date and time format used for the cm handle sync state.
130      *
131      * @return the date and time in the format of yyyy-MM-dd'T'HH:mm:ss.SSSZ
132      */
133     public static String nowInSyncTimeFormat() {
134         return dateTimeFormatter.format(OffsetDateTime.now());
135     }
136
137     /**
138      * Sets the last updated date and time for the cm handle sync state.
139      */
140     public void setLastUpdateTimeNow() {
141         lastUpdateTime = CompositeState.nowInSyncTimeFormat();
142     }
143
144 }