11e3f583e4e4e852b9bedd4ea181cb397433946e
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022-2023 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
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 java.util.HashSet;
24 import java.util.Set;
25 import java.util.UUID;
26 import javax.persistence.CascadeType;
27 import javax.persistence.Column;
28 import javax.persistence.Convert;
29 import javax.persistence.Entity;
30 import javax.persistence.FetchType;
31 import javax.persistence.ForeignKey;
32 import javax.persistence.Id;
33 import javax.persistence.Inheritance;
34 import javax.persistence.InheritanceType;
35 import javax.persistence.JoinColumn;
36 import javax.persistence.Lob;
37 import javax.persistence.OneToMany;
38 import javax.persistence.Table;
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.document.base.ToscaServiceTemplateValidation;
45 import org.onap.policy.clamp.models.acm.document.concepts.DocToscaServiceTemplate;
46 import org.onap.policy.common.parameters.BeanValidationResult;
47 import org.onap.policy.common.parameters.annotations.NotNull;
48 import org.onap.policy.common.parameters.annotations.Valid;
49 import org.onap.policy.models.base.PfAuthorative;
50 import org.onap.policy.models.base.Validated;
51
52 /**
53  * Class to represent a automation composition definition in the database.
54  */
55 @Entity
56 @Table(name = "AutomationCompositionDefinition")
57 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
58 @Data
59 @EqualsAndHashCode(callSuper = false)
60 public class JpaAutomationCompositionDefinition extends Validated
61         implements PfAuthorative<AutomationCompositionDefinition> {
62
63     @Id
64     @NotNull
65     private String compositionId;
66
67     @Column
68     @NotNull
69     private String name;
70
71     @Column
72     @NotNull
73     private String version;
74
75     @Column
76     @NotNull
77     private AcTypeState state;
78
79     @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
80     @JoinColumn(name = "compositionId", foreignKey = @ForeignKey(name = "dt_element_fk"))
81     private Set<JpaNodeTemplateState> elements = new HashSet<>();
82
83     @Lob
84     @Convert(converter = StringToServiceTemplateConverter.class)
85     @NotNull
86     @Valid
87     private DocToscaServiceTemplate serviceTemplate;
88
89     @Override
90     public AutomationCompositionDefinition toAuthorative() {
91         var acmDefinition = new AutomationCompositionDefinition();
92         acmDefinition.setCompositionId(UUID.fromString(this.compositionId));
93         acmDefinition.setState(this.state);
94         acmDefinition.setServiceTemplate(this.serviceTemplate.toAuthorative());
95         for (var element : this.elements) {
96             var key = element.getNodeTemplateId().getName();
97             acmDefinition.getElementStateMap().put(key, element.toAuthorative());
98         }
99         return acmDefinition;
100     }
101
102     @Override
103     public void fromAuthorative(final AutomationCompositionDefinition copyConcept) {
104         this.compositionId = copyConcept.getCompositionId().toString();
105         this.state = copyConcept.getState();
106         this.serviceTemplate = new DocToscaServiceTemplate(copyConcept.getServiceTemplate());
107         setName(this.serviceTemplate.getName());
108         setVersion(this.serviceTemplate.getVersion());
109         this.elements = new HashSet<>(copyConcept.getElementStateMap().size());
110         for (var element : copyConcept.getElementStateMap().values()) {
111             var nodeTemplateStateId = element.getNodeTemplateStateId().toString();
112             var jpaNodeTemplateState = new JpaNodeTemplateState(nodeTemplateStateId, this.compositionId);
113             jpaNodeTemplateState.fromAuthorative(element);
114             this.elements.add(jpaNodeTemplateState);
115         }
116     }
117
118     public JpaAutomationCompositionDefinition(final AutomationCompositionDefinition acmDefinition) {
119         fromAuthorative(acmDefinition);
120     }
121
122     public JpaAutomationCompositionDefinition() {
123         super();
124     }
125
126     @Override
127     public BeanValidationResult validate(@NonNull String fieldName) {
128         var result = super.validate(fieldName);
129         ToscaServiceTemplateValidation.validate(result, serviceTemplate);
130
131         if (!result.isValid()) {
132             return result;
133         }
134
135         return result;
136     }
137 }