247d8f28c55c418922c7bcadf1c8643a57282ade
[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 com.google.gson.annotations.SerializedName;
24 import java.util.UUID;
25 import javax.persistence.CascadeType;
26 import javax.persistence.Entity;
27 import javax.persistence.FetchType;
28 import javax.persistence.Id;
29 import javax.persistence.Inheritance;
30 import javax.persistence.InheritanceType;
31 import javax.persistence.OneToOne;
32 import javax.persistence.Table;
33 import lombok.Data;
34 import lombok.EqualsAndHashCode;
35 import lombok.NonNull;
36 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
37 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
38 import org.onap.policy.common.parameters.BeanValidationResult;
39 import org.onap.policy.common.parameters.annotations.NotNull;
40 import org.onap.policy.common.parameters.annotations.Valid;
41 import org.onap.policy.models.base.PfAuthorative;
42 import org.onap.policy.models.base.Validated;
43 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
44
45 /**
46  * Class to represent a automation composition definition in the database.
47  */
48 @Entity
49 @Table(name = "AutomationCompositionDefinition")
50 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
51 @Data
52 @EqualsAndHashCode(callSuper = false)
53 public class JpaAutomationCompositionDefinition extends Validated
54         implements PfAuthorative<AutomationCompositionDefinition> {
55
56     @Id
57     @NotNull
58     private String compositionId;
59
60     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
61     @SerializedName("serviceTemplate")
62     @Valid
63     private JpaToscaServiceTemplate serviceTemplate;
64
65     @Override
66     public AutomationCompositionDefinition toAuthorative() {
67         var acmDefinition = new AutomationCompositionDefinition();
68         acmDefinition.setCompositionId(UUID.fromString(compositionId));
69         acmDefinition.setServiceTemplate(serviceTemplate.toAuthorative());
70         return acmDefinition;
71     }
72
73     @Override
74     public void fromAuthorative(final AutomationCompositionDefinition copyConcept) {
75         compositionId = copyConcept.getCompositionId().toString();
76         serviceTemplate = new JpaToscaServiceTemplate(copyConcept.getServiceTemplate());
77     }
78
79     public JpaAutomationCompositionDefinition(final AutomationCompositionDefinition acmDefinition) {
80         fromAuthorative(acmDefinition);
81     }
82
83     public JpaAutomationCompositionDefinition() {
84         super();
85     }
86
87     @Override
88     public BeanValidationResult validate(@NonNull String fieldName) {
89         var result = super.validate(fieldName);
90
91         AcmUtils.validateToscaTopologyTemplate(result, serviceTemplate);
92
93         if (!result.isValid()) {
94             return result;
95         }
96
97         return result;
98     }
99 }