Add support for legacy guard policies
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / ToscaEventFilter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Model
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 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
28 import javax.persistence.Column;
29 import javax.persistence.EmbeddedId;
30 import javax.persistence.Entity;
31 import javax.persistence.Inheritance;
32 import javax.persistence.InheritanceType;
33 import javax.persistence.Table;
34
35 import lombok.Data;
36 import lombok.EqualsAndHashCode;
37 import lombok.NonNull;
38
39 import org.apache.commons.lang3.ObjectUtils;
40 import org.onap.policy.common.utils.validation.Assertions;
41 import org.onap.policy.models.base.PfConcept;
42 import org.onap.policy.models.base.PfConceptKey;
43 import org.onap.policy.models.base.PfKey;
44 import org.onap.policy.models.base.PfReferenceKey;
45 import org.onap.policy.models.base.PfValidationMessage;
46 import org.onap.policy.models.base.PfValidationResult;
47 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
48
49 /**
50  * Class to represent the EventFilter in TOSCA definition.
51  *
52  * @author Chenfei Gao (cgao@research.att.com)
53  * @author Liam Fallon (liam.fallon@est.tech)
54  */
55 @Entity
56 @Table(name = "ToscaEventFilter")
57 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
58 @Data
59 @EqualsAndHashCode(callSuper = false)
60 public class ToscaEventFilter extends PfConcept {
61     private static final long serialVersionUID = 8769020537228210247L;
62
63     @EmbeddedId
64     private PfReferenceKey key;
65
66     @Column
67     private PfConceptKey node;
68
69     @Column
70     private String requirement;
71
72     @Column
73     private String capability;
74
75     /**
76      * The Default Constructor creates a {@link ToscaEventFilter} object with a null key.
77      */
78     public ToscaEventFilter() {
79         this(new PfReferenceKey());
80     }
81
82     /**
83      * The Key Constructor creates a {@link ToscaEventFilter} object with the given concept key.
84      *
85      * @param key the key
86      */
87     public ToscaEventFilter(@NonNull final PfReferenceKey key) {
88         this(key, new PfConceptKey());
89     }
90
91     /**
92      * The full Constructor creates a {@link ToscaEventFilter} object with the given concept key and node.
93      *
94      * @param key the key
95      */
96     public ToscaEventFilter(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey node) {
97         this.key = key;
98         this.node = node;
99     }
100
101     /**
102      * Copy constructor.
103      *
104      * @param copyConcept the concept to copy from
105      */
106     public ToscaEventFilter(final ToscaEventFilter copyConcept) {
107         super(copyConcept);
108     }
109
110     @Override
111     public List<PfKey> getKeys() {
112         final List<PfKey> keyList = getKey().getKeys();
113         keyList.addAll(node.getKeys());
114         return keyList;
115     }
116
117     @Override
118     public void clean() {
119         key.clean();
120         node.clean();
121
122         requirement = (requirement != null ? requirement.trim() : requirement);
123         capability = (capability != null ? capability.trim() : capability);
124     }
125
126     @Override
127     public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
128         PfValidationResult result = resultIn;
129
130         if (key.isNullKey()) {
131             result.addValidationMessage(
132                     new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
133         }
134
135         result = key.validate(result);
136
137         if (node == null || node.isNullKey()) {
138             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
139                     "node on an event filter may not be null"));
140         }
141
142         if (requirement != null && requirement.trim().length() == 0) {
143             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
144                     "event filter requirement may not be blank"));
145         }
146
147         if (capability != null && capability.trim().length() == 0) {
148             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
149                     "event filter capability may not be blank"));
150         }
151
152         return result;
153     }
154
155     @Override
156     public int compareTo(final PfConcept otherConcept) {
157         if (otherConcept == null) {
158             return -1;
159         }
160         if (this == otherConcept) {
161             return 0;
162         }
163         if (getClass() != otherConcept.getClass()) {
164             return this.hashCode() - otherConcept.hashCode();
165         }
166
167         final ToscaEventFilter other = (ToscaEventFilter) otherConcept;
168         if (!key.equals(other.key)) {
169             return key.compareTo(other.key);
170         }
171
172         if (!node.equals(other.node)) {
173             return node.compareTo(other.node);
174         }
175
176         int result = ObjectUtils.compare(requirement, other.requirement);
177         if (result != 0) {
178             return result;
179         }
180
181         return ObjectUtils.compare(capability, other.capability);
182     }
183
184     @Override
185     public PfConcept copyTo(@NonNull final PfConcept target) {
186         final Object copyObject = target;
187         Assertions.instanceOf(copyObject, ToscaEventFilter.class);
188
189         final ToscaEventFilter copy = ((ToscaEventFilter) copyObject);
190         copy.setKey(new PfReferenceKey(key));
191         copy.setNode(new PfConceptKey(node));
192         copy.setRequirement(requirement);
193         copy.setCapability(capability);
194
195         return copy;
196     }
197 }