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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.models.acm.persistence.concepts;
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;
41 import java.util.UUID;
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;
53 * Class to represent an automation composition definition in the database.
56 @Table(name = "AutomationCompositionDefinition")
57 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
60 public class JpaAutomationCompositionDefinition implements PfAuthorative<AutomationCompositionDefinition> {
64 private String compositionId;
68 @Pattern(regexp = PfKey.NAME_REGEXP)
73 @Pattern(regexp = PfKey.VERSION_REGEXP)
74 private String version;
78 private AcTypeState state;
81 private StateChangeResult stateChangeResult;
85 private Timestamp lastMsg;
89 private String revisionId;
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<>();
95 @Column(length = 200000)
96 @Convert(converter = StringToServiceTemplateConverter.class)
99 private DocToscaServiceTemplate serviceTemplate;
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());
114 return acmDefinition;
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);
136 public JpaAutomationCompositionDefinition(final AutomationCompositionDefinition acmDefinition) {
137 fromAuthorative(acmDefinition);
140 public JpaAutomationCompositionDefinition() {