e879a1305bc43361e76b4b12ee36feb6b2db7cf9
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaEventFilter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Model
4  * ================================================================================
5  * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019-2020 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.models.tosca.simple.concepts;
25
26 import java.util.List;
27 import javax.persistence.Column;
28 import javax.persistence.EmbeddedId;
29 import javax.persistence.Entity;
30 import javax.persistence.Inheritance;
31 import javax.persistence.InheritanceType;
32 import javax.persistence.Table;
33 import lombok.Data;
34 import lombok.EqualsAndHashCode;
35 import lombok.NonNull;
36 import org.apache.commons.lang3.ObjectUtils;
37 import org.onap.policy.common.parameters.annotations.NotBlank;
38 import org.onap.policy.common.parameters.annotations.NotNull;
39 import org.onap.policy.models.base.PfConcept;
40 import org.onap.policy.models.base.PfConceptKey;
41 import org.onap.policy.models.base.PfKey;
42 import org.onap.policy.models.base.PfReferenceKey;
43 import org.onap.policy.models.base.validation.annotations.VerifyKey;
44
45 /**
46  * Class to represent the EventFilter in TOSCA definition.
47  *
48  * @author Chenfei Gao (cgao@research.att.com)
49  * @author Liam Fallon (liam.fallon@est.tech)
50  */
51 @Entity
52 @Table(name = "ToscaEventFilter")
53 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
54 @Data
55 @EqualsAndHashCode(callSuper = false)
56 public class JpaToscaEventFilter extends PfConcept {
57     private static final long serialVersionUID = 8769020537228210247L;
58
59     @EmbeddedId
60     @VerifyKey
61     @NotNull
62     private PfReferenceKey key;
63
64     @Column
65     @VerifyKey
66     @NotNull
67     private PfConceptKey node;
68
69     @Column
70     @NotBlank
71     private String requirement;
72
73     @Column
74     @NotBlank
75     private String capability;
76
77     /**
78      * The Default Constructor creates a {@link JpaToscaEventFilter} object with a null key.
79      */
80     public JpaToscaEventFilter() {
81         this(new PfReferenceKey());
82     }
83
84     /**
85      * The Key Constructor creates a {@link JpaToscaEventFilter} object with the given concept key.
86      *
87      * @param key the key
88      */
89     public JpaToscaEventFilter(@NonNull final PfReferenceKey key) {
90         this(key, new PfConceptKey());
91     }
92
93     /**
94      * The full Constructor creates a {@link JpaToscaEventFilter} object with the given concept key and node.
95      *
96      * @param key the key
97      */
98     public JpaToscaEventFilter(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey node) {
99         this.key = key;
100         this.node = node;
101     }
102
103     /**
104      * Copy constructor.
105      *
106      * @param copyConcept the concept to copy from
107      */
108     public JpaToscaEventFilter(final JpaToscaEventFilter copyConcept) {
109         super(copyConcept);
110         this.key = new PfReferenceKey(copyConcept.key);
111         this.node = new PfConceptKey(copyConcept.node);
112         this.requirement = copyConcept.requirement;
113         this.capability = copyConcept.capability;
114     }
115
116     @Override
117     public List<PfKey> getKeys() {
118         final List<PfKey> keyList = getKey().getKeys();
119         keyList.addAll(node.getKeys());
120         return keyList;
121     }
122
123     @Override
124     public void clean() {
125         key.clean();
126         node.clean();
127
128         requirement = (requirement != null ? requirement.trim() : requirement);
129         capability = (capability != null ? capability.trim() : capability);
130     }
131
132     @Override
133     public int compareTo(final PfConcept otherConcept) {
134         if (otherConcept == null) {
135             return -1;
136         }
137         if (this == otherConcept) {
138             return 0;
139         }
140         if (getClass() != otherConcept.getClass()) {
141             return getClass().getName().compareTo(otherConcept.getClass().getName());
142         }
143
144         final JpaToscaEventFilter other = (JpaToscaEventFilter) otherConcept;
145         int result = key.compareTo(other.key);
146         if (result != 0) {
147             return result;
148         }
149
150         result = node.compareTo(other.node);
151         if (result != 0) {
152             return result;
153         }
154
155         result = ObjectUtils.compare(requirement, other.requirement);
156         if (result != 0) {
157             return result;
158         }
159
160         return ObjectUtils.compare(capability, other.capability);
161     }
162 }