fce5782a74bed305b9754d804a65c2a940369ce5
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022-2025 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.CascadeType;
24 import jakarta.persistence.Column;
25 import jakarta.persistence.Convert;
26 import jakarta.persistence.Entity;
27 import jakarta.persistence.FetchType;
28 import jakarta.persistence.ForeignKey;
29 import jakarta.persistence.Id;
30 import jakarta.persistence.Inheritance;
31 import jakarta.persistence.InheritanceType;
32 import jakarta.persistence.JoinColumn;
33 import jakarta.persistence.OneToMany;
34 import jakarta.persistence.Table;
35 import java.sql.Timestamp;
36 import java.util.HashSet;
37 import java.util.Set;
38 import java.util.UUID;
39 import lombok.Data;
40 import lombok.EqualsAndHashCode;
41 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
42 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
43 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
44 import org.onap.policy.clamp.models.acm.document.concepts.DocToscaServiceTemplate;
45 import org.onap.policy.clamp.models.acm.utils.TimestampHelper;
46 import org.onap.policy.common.parameters.annotations.NotNull;
47 import org.onap.policy.common.parameters.annotations.Pattern;
48 import org.onap.policy.common.parameters.annotations.Valid;
49 import org.onap.policy.models.base.PfAuthorative;
50 import org.onap.policy.models.base.PfKey;
51 import org.onap.policy.models.base.Validated;
52
53 /**
54  * Class to represent an automation composition definition in the database.
55  */
56 @Entity
57 @Table(name = "AutomationCompositionDefinition")
58 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
59 @Data
60 @EqualsAndHashCode(callSuper = false)
61 public class JpaAutomationCompositionDefinition extends Validated
62         implements PfAuthorative<AutomationCompositionDefinition> {
63
64     @Id
65     @NotNull
66     private String compositionId;
67
68     @Column
69     @NotNull
70     @Pattern(regexp = PfKey.NAME_REGEXP)
71     private String name;
72
73     @Column
74     @NotNull
75     @Pattern(regexp = PfKey.VERSION_REGEXP)
76     private String version;
77
78     @Column
79     @NotNull
80     private AcTypeState state;
81
82     @Column
83     private StateChangeResult stateChangeResult;
84
85     @Column
86     @NotNull
87     private Timestamp lastMsg;
88
89     @Column
90     @NotNull
91     private String revisionId;
92
93     @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
94     @JoinColumn(name = "compositionId", foreignKey = @ForeignKey(name = "dt_element_fk"))
95     private Set<JpaNodeTemplateState> elements = new HashSet<>();
96
97     @Column(length = 200000)
98     @Convert(converter = StringToServiceTemplateConverter.class)
99     @NotNull
100     @Valid
101     private DocToscaServiceTemplate serviceTemplate;
102
103     @Override
104     public AutomationCompositionDefinition toAuthorative() {
105         var acmDefinition = new AutomationCompositionDefinition();
106         acmDefinition.setCompositionId(UUID.fromString(this.compositionId));
107         acmDefinition.setState(this.state);
108         acmDefinition.setStateChangeResult(this.stateChangeResult);
109         acmDefinition.setLastMsg(this.lastMsg.toString());
110         acmDefinition.setRevisionId(UUID.fromString(this.revisionId));
111         acmDefinition.setServiceTemplate(this.serviceTemplate.toAuthorative());
112         for (var element : this.elements) {
113             var key = element.getNodeTemplateId().getName();
114             acmDefinition.getElementStateMap().put(key, element.toAuthorative());
115         }
116         return acmDefinition;
117     }
118
119     @Override
120     public void fromAuthorative(final AutomationCompositionDefinition copyConcept) {
121         this.compositionId = copyConcept.getCompositionId().toString();
122         this.state = copyConcept.getState();
123         this.stateChangeResult = copyConcept.getStateChangeResult();
124         this.lastMsg = TimestampHelper.toTimestamp(copyConcept.getLastMsg());
125         this.revisionId = copyConcept.getRevisionId().toString();
126         this.serviceTemplate = new DocToscaServiceTemplate(copyConcept.getServiceTemplate());
127         setName(this.serviceTemplate.getName());
128         setVersion(this.serviceTemplate.getVersion());
129         this.elements = new HashSet<>(copyConcept.getElementStateMap().size());
130         for (var element : copyConcept.getElementStateMap().values()) {
131             var nodeTemplateStateId = element.getNodeTemplateStateId().toString();
132             var jpaNodeTemplateState = new JpaNodeTemplateState(nodeTemplateStateId, this.compositionId);
133             jpaNodeTemplateState.fromAuthorative(element);
134             this.elements.add(jpaNodeTemplateState);
135         }
136     }
137
138     public JpaAutomationCompositionDefinition(final AutomationCompositionDefinition acmDefinition) {
139         fromAuthorative(acmDefinition);
140     }
141
142     public JpaAutomationCompositionDefinition() {
143         super();
144     }
145 }