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
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 java.sql.Timestamp;
36 import java.util.HashSet;
38 import java.util.UUID;
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;
56 * Class to represent a automation composition definition in the database.
59 @Table(name = "AutomationCompositionDefinition")
60 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
62 @EqualsAndHashCode(callSuper = false)
63 public class JpaAutomationCompositionDefinition extends Validated
64 implements PfAuthorative<AutomationCompositionDefinition> {
68 private String compositionId;
72 @Pattern(regexp = PfKey.NAME_REGEXP)
77 @Pattern(regexp = PfKey.VERSION_REGEXP)
78 private String version;
82 private AcTypeState state;
85 private StateChangeResult stateChangeResult;
89 private Timestamp lastMsg;
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.setServiceTemplate(this.serviceTemplate.toAuthorative());
109 for (var element : this.elements) {
110 var key = element.getNodeTemplateId().getName();
111 acmDefinition.getElementStateMap().put(key, element.toAuthorative());
113 return acmDefinition;
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);
134 public JpaAutomationCompositionDefinition(final AutomationCompositionDefinition acmDefinition) {
135 fromAuthorative(acmDefinition);
138 public JpaAutomationCompositionDefinition() {
143 public BeanValidationResult validate(@NonNull String fieldName) {
144 var result = super.validate(fieldName);
145 if (!result.isValid()) {