2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.models.tosca.simple.concepts;
26 import com.google.gson.annotations.SerializedName;
28 import java.util.Date;
29 import java.util.List;
31 import javax.persistence.EmbeddedId;
32 import javax.persistence.Entity;
33 import javax.persistence.Inheritance;
34 import javax.persistence.InheritanceType;
35 import javax.persistence.Table;
38 import lombok.EqualsAndHashCode;
39 import lombok.NonNull;
41 import org.onap.policy.common.utils.validation.Assertions;
42 import org.onap.policy.models.base.PfConcept;
43 import org.onap.policy.models.base.PfKey;
44 import org.onap.policy.models.base.PfReferenceKey;
45 import org.onap.policy.models.base.PfUtils;
46 import org.onap.policy.models.base.PfValidationMessage;
47 import org.onap.policy.models.base.PfValidationResult;
48 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
51 * Class to represent the TimeInterval in TOSCA definition.
53 * @author Chenfei Gao (cgao@research.att.com)
54 * @author Liam Fallon (liam.fallon@est.tech)
58 @Table(name = "ToscaTimeInterval")
59 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
61 @EqualsAndHashCode(callSuper = false)
62 public class JpaToscaTimeInterval extends PfConcept {
63 private static final long serialVersionUID = 9151467029611969980L;
66 private PfReferenceKey key;
68 @SerializedName("start_time")
69 private Date startTime;
71 @SerializedName("end_time")
75 * The Default Constructor creates a {@link JpaToscaTimeInterval} object with a null key.
77 public JpaToscaTimeInterval() {
78 this(new PfReferenceKey());
82 * The Key Constructor creates a {@link JpaToscaTimeInterval} object with the given concept key.
86 public JpaToscaTimeInterval(@NonNull final PfReferenceKey key) {
87 this(key, new Date(0), new Date(0));
91 * The full constructor creates a {@link JpaToscaTimeInterval} object with all fields.
95 public JpaToscaTimeInterval(@NonNull final PfReferenceKey key, @NonNull final Date startTime,
96 @NonNull final Date endTime) {
98 this.startTime = startTime;
99 this.endTime = endTime;
105 * @param copyConcept the concept to copy from
107 public JpaToscaTimeInterval(final JpaToscaTimeInterval copyConcept) {
112 public List<PfKey> getKeys() {
113 return getKey().getKeys();
117 public void clean() {
122 public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
123 PfValidationResult result = resultIn;
125 if (key.isNullKey()) {
126 result.addValidationMessage(
127 new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
130 result = key.validate(result);
132 if (startTime == null || startTime.getTime() == 0) {
133 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
134 "start time on time interval may not be null or zero"));
137 if (endTime == null || endTime.getTime() == 0) {
138 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
139 "end time on time interval may not be null or zero"));
142 if (startTime != null && endTime != null && endTime.before(startTime)) {
143 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
144 "end time \"" + endTime.toString() + "\" on time interval may not be before start time \""
145 + startTime.toString() + "\""));
152 public int compareTo(final PfConcept otherConcept) {
153 if (otherConcept == null) {
156 if (this == otherConcept) {
159 if (getClass() != otherConcept.getClass()) {
160 return this.hashCode() - otherConcept.hashCode();
163 final JpaToscaTimeInterval other = (JpaToscaTimeInterval) otherConcept;
164 if (!key.equals(other.key)) {
165 return key.compareTo(other.key);
168 int returnVal = PfUtils.compareObjects(startTime, other.startTime);
169 if (returnVal != 0) {
173 return PfUtils.compareObjects(endTime, other.endTime);
177 public PfConcept copyTo(@NonNull final PfConcept target) {
178 final Object copyObject = target;
179 Assertions.instanceOf(copyObject, JpaToscaTimeInterval.class);
181 final JpaToscaTimeInterval copy = ((JpaToscaTimeInterval) copyObject);
182 copy.setKey(new PfReferenceKey(key));
183 copy.setStartTime(startTime);
184 copy.setEndTime(endTime);