2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2024 Nordix Foundation.
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.Lob;
34 import jakarta.persistence.OneToMany;
35 import jakarta.persistence.Table;
36 import java.sql.Timestamp;
37 import java.util.HashSet;
39 import java.util.UUID;
41 import lombok.EqualsAndHashCode;
42 import lombok.NonNull;
43 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
44 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
45 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
46 import org.onap.policy.clamp.models.acm.document.concepts.DocToscaServiceTemplate;
47 import org.onap.policy.clamp.models.acm.utils.TimestampHelper;
48 import org.onap.policy.common.parameters.BeanValidationResult;
49 import org.onap.policy.common.parameters.annotations.NotNull;
50 import org.onap.policy.common.parameters.annotations.Pattern;
51 import org.onap.policy.common.parameters.annotations.Valid;
52 import org.onap.policy.models.base.PfAuthorative;
53 import org.onap.policy.models.base.PfKey;
54 import org.onap.policy.models.base.Validated;
57 * Class to represent a automation composition definition in the database.
60 @Table(name = "AutomationCompositionDefinition")
61 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
63 @EqualsAndHashCode(callSuper = false)
64 public class JpaAutomationCompositionDefinition extends Validated
65 implements PfAuthorative<AutomationCompositionDefinition> {
69 private String compositionId;
73 @Pattern(regexp = PfKey.NAME_REGEXP)
78 @Pattern(regexp = PfKey.VERSION_REGEXP)
79 private String version;
82 private Boolean restarting;
86 private AcTypeState state;
89 private StateChangeResult stateChangeResult;
93 private Timestamp lastMsg;
95 @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
96 @JoinColumn(name = "compositionId", foreignKey = @ForeignKey(name = "dt_element_fk"))
97 private Set<JpaNodeTemplateState> elements = new HashSet<>();
100 @Column(length = 100000)
101 @Convert(converter = StringToServiceTemplateConverter.class)
104 private DocToscaServiceTemplate serviceTemplate;
107 public AutomationCompositionDefinition toAuthorative() {
108 var acmDefinition = new AutomationCompositionDefinition();
109 acmDefinition.setCompositionId(UUID.fromString(this.compositionId));
110 acmDefinition.setRestarting(this.restarting);
111 acmDefinition.setState(this.state);
112 acmDefinition.setStateChangeResult(this.stateChangeResult);
113 acmDefinition.setLastMsg(this.lastMsg.toString());
114 acmDefinition.setServiceTemplate(this.serviceTemplate.toAuthorative());
115 for (var element : this.elements) {
116 var key = element.getNodeTemplateId().getName();
117 acmDefinition.getElementStateMap().put(key, element.toAuthorative());
119 return acmDefinition;
123 public void fromAuthorative(final AutomationCompositionDefinition copyConcept) {
124 this.compositionId = copyConcept.getCompositionId().toString();
125 this.restarting = copyConcept.getRestarting();
126 this.state = copyConcept.getState();
127 this.stateChangeResult = copyConcept.getStateChangeResult();
128 this.lastMsg = TimestampHelper.toTimestamp(copyConcept.getLastMsg());
129 this.serviceTemplate = new DocToscaServiceTemplate(copyConcept.getServiceTemplate());
130 setName(this.serviceTemplate.getName());
131 setVersion(this.serviceTemplate.getVersion());
132 this.elements = new HashSet<>(copyConcept.getElementStateMap().size());
133 for (var element : copyConcept.getElementStateMap().values()) {
134 var nodeTemplateStateId = element.getNodeTemplateStateId().toString();
135 var jpaNodeTemplateState = new JpaNodeTemplateState(nodeTemplateStateId, this.compositionId);
136 jpaNodeTemplateState.fromAuthorative(element);
137 this.elements.add(jpaNodeTemplateState);
141 public JpaAutomationCompositionDefinition(final AutomationCompositionDefinition acmDefinition) {
142 fromAuthorative(acmDefinition);
145 public JpaAutomationCompositionDefinition() {
150 public BeanValidationResult validate(@NonNull String fieldName) {
151 var result = super.validate(fieldName);
152 if (!result.isValid()) {