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