c46bdb96cb4a551514a25548553d58e2c58d54fb
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 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.UUID;
24 import javax.persistence.Convert;
25 import javax.persistence.Entity;
26 import javax.persistence.Id;
27 import javax.persistence.Inheritance;
28 import javax.persistence.InheritanceType;
29 import javax.persistence.Lob;
30 import javax.persistence.Table;
31 import lombok.Data;
32 import lombok.EqualsAndHashCode;
33 import lombok.NonNull;
34 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
35 import org.onap.policy.clamp.models.acm.document.base.ToscaServiceTemplateValidation;
36 import org.onap.policy.clamp.models.acm.document.concepts.DocToscaServiceTemplate;
37 import org.onap.policy.common.parameters.BeanValidationResult;
38 import org.onap.policy.common.parameters.annotations.NotNull;
39 import org.onap.policy.common.parameters.annotations.Valid;
40 import org.onap.policy.models.base.PfAuthorative;
41 import org.onap.policy.models.base.Validated;
42
43 /**
44  * Class to represent a automation composition definition in the database.
45  */
46 @Entity
47 @Table(name = "AutomationCompositionDefinition")
48 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
49 @Data
50 @EqualsAndHashCode(callSuper = false)
51 public class JpaAutomationCompositionDefinition extends Validated
52         implements PfAuthorative<AutomationCompositionDefinition> {
53
54     @Id
55     @NotNull
56     private String compositionId;
57
58     @NotNull
59     private String name;
60
61     @NotNull
62     private String version;
63
64     @Lob
65     @Convert(converter = StringToServiceTemplateConverter.class)
66     @NotNull
67     @Valid
68     private DocToscaServiceTemplate serviceTemplate;
69
70     @Override
71     public AutomationCompositionDefinition toAuthorative() {
72         var acmDefinition = new AutomationCompositionDefinition();
73         acmDefinition.setCompositionId(UUID.fromString(compositionId));
74         acmDefinition.setServiceTemplate(serviceTemplate.toAuthorative());
75         return acmDefinition;
76     }
77
78     @Override
79     public void fromAuthorative(final AutomationCompositionDefinition copyConcept) {
80         compositionId = copyConcept.getCompositionId().toString();
81         serviceTemplate = new DocToscaServiceTemplate(copyConcept.getServiceTemplate());
82         setName(serviceTemplate.getName());
83         setVersion(serviceTemplate.getVersion());
84     }
85
86     public JpaAutomationCompositionDefinition(final AutomationCompositionDefinition acmDefinition) {
87         fromAuthorative(acmDefinition);
88     }
89
90     public JpaAutomationCompositionDefinition() {
91         super();
92     }
93
94     @Override
95     public BeanValidationResult validate(@NonNull String fieldName) {
96         var result = super.validate(fieldName);
97         ToscaServiceTemplateValidation.validate(result, serviceTemplate);
98
99         if (!result.isValid()) {
100             return result;
101         }
102
103         return result;
104     }
105 }