01b7876a32789660a9ff11bcc27862add034c757
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023-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.AttributeOverride;
24 import jakarta.persistence.Column;
25 import jakarta.persistence.Convert;
26 import jakarta.persistence.Entity;
27 import jakarta.persistence.Id;
28 import jakarta.persistence.Inheritance;
29 import jakarta.persistence.InheritanceType;
30 import jakarta.persistence.Table;
31 import jakarta.validation.Valid;
32 import jakarta.validation.constraints.NotNull;
33 import java.util.Map;
34 import java.util.UUID;
35 import java.util.function.UnaryOperator;
36 import lombok.Data;
37 import lombok.EqualsAndHashCode;
38 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
39 import org.onap.policy.clamp.models.acm.concepts.NodeTemplateState;
40 import org.onap.policy.models.base.PfAuthorative;
41 import org.onap.policy.models.base.PfConceptKey;
42 import org.onap.policy.models.base.PfUtils;
43 import org.onap.policy.models.base.validation.annotations.VerifyKey;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
45
46 @Entity
47 @Table(name = "NodeTemplateState")
48 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
49 @Data
50 @EqualsAndHashCode
51 public class JpaNodeTemplateState implements PfAuthorative<NodeTemplateState> {
52
53     @Id
54     @NotNull
55     private String nodeTemplateStateId;
56
57     @Column
58     @NotNull
59     private String compositionId;
60
61     @Column
62     private String participantId;
63
64     @Valid
65     @VerifyKey
66     @NotNull
67     @AttributeOverride(name = "name",    column = @Column(name = "nodeTemplate_name"))
68     @AttributeOverride(name = "version", column = @Column(name = "nodeTemplate_version"))
69     private PfConceptKey nodeTemplateId;
70
71     @Column
72     @NotNull
73     private AcTypeState state;
74
75     @Column
76     private String message;
77
78     @NotNull
79     @Valid
80     @Convert(converter = StringToMapConverter.class)
81     @Column(length = 100000)
82     private Map<String, Object> outProperties;
83
84     /**
85      * The Default Constructor.
86      */
87     public JpaNodeTemplateState() {
88         this(UUID.randomUUID().toString(), UUID.randomUUID().toString());
89     }
90
91     /**
92      * Constructor.
93      *
94      * @param nodeTemplateStateId the nodeTemplateStateId
95      * @param compositionId the compositionId
96      */
97     public JpaNodeTemplateState(@NotNull String nodeTemplateStateId, @NotNull String compositionId) {
98         this.nodeTemplateStateId = nodeTemplateStateId;
99         this.compositionId = compositionId;
100     }
101
102     @Override
103     public void fromAuthorative(NodeTemplateState copyConcept) {
104         this.nodeTemplateStateId = copyConcept.getNodeTemplateStateId().toString();
105         if (copyConcept.getParticipantId() != null) {
106             this.participantId = copyConcept.getParticipantId().toString();
107         }
108         this.nodeTemplateId = copyConcept.getNodeTemplateId().asConceptKey();
109         this.state = copyConcept.getState();
110         this.message = copyConcept.getMessage();
111         this.outProperties = PfUtils.mapMap(copyConcept.getOutProperties(), UnaryOperator.identity());
112     }
113
114     @Override
115     public NodeTemplateState toAuthorative() {
116         var nodeTemplateState = new NodeTemplateState();
117         nodeTemplateState.setNodeTemplateStateId(UUID.fromString(this.nodeTemplateStateId));
118         if (this.participantId != null) {
119             nodeTemplateState.setParticipantId(UUID.fromString(this.participantId));
120         }
121         nodeTemplateState.setNodeTemplateId(new ToscaConceptIdentifier(this.nodeTemplateId));
122         nodeTemplateState.setState(this.state);
123         nodeTemplateState.setMessage(this.message);
124         nodeTemplateState.setOutProperties(PfUtils.mapMap(outProperties, UnaryOperator.identity()));
125         return nodeTemplateState;
126     }
127 }