e203799378302844d14594ba20857f64ff232033
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2024-2026 OpenInfra Foundation Europe. All rights reserved.
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.policy.clamp.models.acm.persistence.concepts;
22
23 import jakarta.persistence.Column;
24 import jakarta.persistence.Entity;
25 import jakarta.persistence.Id;
26 import jakarta.persistence.Inheritance;
27 import jakarta.persistence.InheritanceType;
28 import jakarta.persistence.Table;
29 import jakarta.validation.constraints.NotNull;
30 import java.sql.Timestamp;
31 import java.util.UUID;
32 import lombok.Data;
33 import lombok.EqualsAndHashCode;
34 import lombok.NonNull;
35 import org.onap.policy.clamp.models.acm.concepts.ParticipantReplica;
36 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
37 import org.onap.policy.clamp.models.acm.utils.TimestampHelper;
38 import org.onap.policy.models.base.PfAuthorative;
39
40 @Entity
41 @Table(name = "ParticipantReplica")
42 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
43 @Data
44 @EqualsAndHashCode
45 public class JpaParticipantReplica implements PfAuthorative<ParticipantReplica> {
46
47     @Id
48     @NotNull
49     private String replicaId;
50
51     @NotNull
52     @Column
53     private String participantId;
54
55     @Column
56     @NotNull
57     private ParticipantState participantState;
58
59     @Column
60     @NotNull
61     private Timestamp lastMsg;
62
63     public JpaParticipantReplica() {
64         this(UUID.randomUUID().toString(), UUID.randomUUID().toString());
65     }
66
67     public JpaParticipantReplica(@NonNull String replicaId, @NonNull String participantId) {
68         this.replicaId = replicaId;
69         this.participantId = participantId;
70     }
71
72     @Override
73     public ParticipantReplica toAuthorative() {
74         var participantReplica = new ParticipantReplica();
75         participantReplica.setReplicaId(UUID.fromString(replicaId));
76         participantReplica.setParticipantState(participantState);
77         participantReplica.setLastMsg(lastMsg.toString());
78         return participantReplica;
79     }
80
81     @Override
82     public void fromAuthorative(@NonNull ParticipantReplica participantReplica) {
83         this.replicaId = participantReplica.getReplicaId().toString();
84         this.participantState = participantReplica.getParticipantState();
85         this.lastMsg = TimestampHelper.toTimestamp(participantReplica.getLastMsg());
86     }
87 }