02ec19a6f9ff127a8d8616efe329dc9000e09af5
[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 lombok.NonNull;
42 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
43 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
44 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
45 import org.onap.policy.clamp.models.acm.document.concepts.DocToscaServiceTemplate;
46 import org.onap.policy.clamp.models.acm.utils.TimestampHelper;
47 import org.onap.policy.common.parameters.BeanValidationResult;
48 import org.onap.policy.common.parameters.annotations.NotNull;
49 import org.onap.policy.common.parameters.annotations.Pattern;
50 import org.onap.policy.common.parameters.annotations.Valid;
51 import org.onap.policy.models.base.PfAuthorative;
52 import org.onap.policy.models.base.PfKey;
53 import org.onap.policy.models.base.Validated;
54
55 /**
56  * Class to represent a automation composition definition in the database.
57  */
58 @Entity
59 @Table(name = "AutomationCompositionDefinition")
60 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
61 @Data
62 @EqualsAndHashCode(callSuper = false)
63 public class JpaAutomationCompositionDefinition extends Validated
64         implements PfAuthorative<AutomationCompositionDefinition> {
65
66     @Id
67     @NotNull
68     private String compositionId;
69
70     @Column
71     @NotNull
72     @Pattern(regexp = PfKey.NAME_REGEXP)
73     private String name;
74
75     @Column
76     @NotNull
77     @Pattern(regexp = PfKey.VERSION_REGEXP)
78     private String version;
79
80     @Column
81     @NotNull
82     private AcTypeState state;
83
84     @Column
85     private StateChangeResult stateChangeResult;
86
87     @Column
88     @NotNull
89     private Timestamp lastMsg;
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.setServiceTemplate(this.serviceTemplate.toAuthorative());
109         for (var element : this.elements) {
110             var key = element.getNodeTemplateId().getName();
111             acmDefinition.getElementStateMap().put(key, element.toAuthorative());
112         }
113         return acmDefinition;
114     }
115
116     @Override
117     public void fromAuthorative(final AutomationCompositionDefinition copyConcept) {
118         this.compositionId = copyConcept.getCompositionId().toString();
119         this.state = copyConcept.getState();
120         this.stateChangeResult = copyConcept.getStateChangeResult();
121         this.lastMsg = TimestampHelper.toTimestamp(copyConcept.getLastMsg());
122         this.serviceTemplate = new DocToscaServiceTemplate(copyConcept.getServiceTemplate());
123         setName(this.serviceTemplate.getName());
124         setVersion(this.serviceTemplate.getVersion());
125         this.elements = new HashSet<>(copyConcept.getElementStateMap().size());
126         for (var element : copyConcept.getElementStateMap().values()) {
127             var nodeTemplateStateId = element.getNodeTemplateStateId().toString();
128             var jpaNodeTemplateState = new JpaNodeTemplateState(nodeTemplateStateId, this.compositionId);
129             jpaNodeTemplateState.fromAuthorative(element);
130             this.elements.add(jpaNodeTemplateState);
131         }
132     }
133
134     public JpaAutomationCompositionDefinition(final AutomationCompositionDefinition acmDefinition) {
135         fromAuthorative(acmDefinition);
136     }
137
138     public JpaAutomationCompositionDefinition() {
139         super();
140     }
141
142     @Override
143     public BeanValidationResult validate(@NonNull String fieldName) {
144         var result = super.validate(fieldName);
145         if (!result.isValid()) {
146             return result;
147         }
148
149         return result;
150     }
151 }