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