Add unit tests for policy type and policy filters
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaModel.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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.models.tosca.simple.concepts;
22
23 import java.util.List;
24
25 import javax.persistence.CascadeType;
26 import javax.persistence.Entity;
27 import javax.persistence.FetchType;
28 import javax.persistence.Inheritance;
29 import javax.persistence.InheritanceType;
30 import javax.persistence.OneToOne;
31 import javax.persistence.Table;
32
33 import lombok.Data;
34 import lombok.EqualsAndHashCode;
35 import lombok.NonNull;
36
37 import org.onap.policy.common.utils.validation.Assertions;
38 import org.onap.policy.models.base.PfConcept;
39 import org.onap.policy.models.base.PfConceptKey;
40 import org.onap.policy.models.base.PfKey;
41 import org.onap.policy.models.base.PfModel;
42 import org.onap.policy.models.base.PfModelService;
43 import org.onap.policy.models.base.PfValidationResult;
44
45 /**
46  * A container class for a TOSCA model with multiple service templates. This class is a container
47  * class that allows a model with many service templates to be constructed that contains a well
48  * formed overall TOSCA model.
49  *
50  * <p>Validation runs {@link JpaToscaModel} validation on the model and all its sub concepts.
51  */
52
53 @Entity
54 @Table(name = "ToscaModel")
55 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
56 @Data
57 @EqualsAndHashCode(callSuper = true)
58 public class JpaToscaModel extends PfModel {
59     private static final long serialVersionUID = 8800599637708309945L;
60
61     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
62     private JpaToscaServiceTemplates serviceTemplates;
63
64     /**
65      * The Default Constructor creates a {@link JpaToscaModel} object with a null concept key and
66      * creates an empty TOSCA model.
67      */
68     public JpaToscaModel() {
69         this(new PfConceptKey());
70     }
71
72     /**
73      * The Key Constructor creates a {@link JpaToscaModel} object with the given concept key and
74      * creates an empty TOSCA model.
75      *
76      * @param key the TOSCA model key
77      */
78     public JpaToscaModel(final PfConceptKey key) {
79         this(key, new JpaToscaServiceTemplates(new PfConceptKey()));
80     }
81
82     /**
83      * Constructor that initiates a {@link JpaToscaModel} with all its fields.
84      *
85      * @param key the TOSCA model key
86      * @param serviceTemplates the service templates in the event model
87      */
88     public JpaToscaModel(@NonNull final PfConceptKey key, @NonNull final JpaToscaServiceTemplates serviceTemplates) {
89         super(key);
90         this.serviceTemplates = serviceTemplates;
91     }
92
93     /**
94      * Copy constructor.
95      *
96      * @param copyConcept the concept to copy from
97      */
98     public JpaToscaModel(@NonNull final JpaToscaModel copyConcept) {
99         super(copyConcept);
100     }
101
102     @Override
103     public void register() {
104         PfModelService.registerModel(serviceTemplates.getId(), getServiceTemplates());
105     }
106
107     @Override
108     public List<PfKey> getKeys() {
109         final List<PfKey> keyList = super.getKeys();
110
111         keyList.addAll(serviceTemplates.getKeys());
112
113         return keyList;
114     }
115
116     @Override
117     public void clean() {
118         super.clean();
119         serviceTemplates.clean();
120     }
121
122     @Override
123     public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
124         PfValidationResult result = super.validate(resultIn);
125
126         return serviceTemplates.validate(result);
127     }
128
129     @Override
130     public int compareTo(final PfConcept otherConcept) {
131         if (otherConcept == null) {
132             return -1;
133         }
134
135         if (this == otherConcept) {
136             return 0;
137         }
138
139         if (getClass() != otherConcept.getClass()) {
140             return this.hashCode() - otherConcept.hashCode();
141         }
142
143         final JpaToscaModel other = (JpaToscaModel) otherConcept;
144         if (!super.equals(other)) {
145             return super.compareTo(other);
146         }
147
148         return serviceTemplates.compareTo(other.serviceTemplates);
149     }
150
151     @Override
152     public PfConcept copyTo(@NonNull final PfConcept targetObject) {
153         Assertions.instanceOf(targetObject, JpaToscaModel.class);
154
155         final JpaToscaModel copy = ((JpaToscaModel) targetObject);
156         super.copyTo(targetObject);
157         copy.setServiceTemplates(new JpaToscaServiceTemplates(serviceTemplates));
158
159         return copy;
160     }
161 }