22773e06427ce59a1bcf6d917579091b77f666c8
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts;
24
25 import java.util.List;
26 import java.util.UUID;
27 import javax.persistence.AttributeOverride;
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 import lombok.Data;
35 import lombok.EqualsAndHashCode;
36 import lombok.NonNull;
37 import org.apache.commons.lang3.ObjectUtils;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
39 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
40 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
41 import org.onap.policy.common.parameters.annotations.NotNull;
42 import org.onap.policy.models.base.PfAuthorative;
43 import org.onap.policy.models.base.PfConcept;
44 import org.onap.policy.models.base.PfConceptKey;
45 import org.onap.policy.models.base.PfKey;
46 import org.onap.policy.models.base.PfReferenceKey;
47 import org.onap.policy.models.base.validation.annotations.VerifyKey;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
49
50 /**
51  * Class to represent a participant control loop element in the database.
52  *
53  * @author Liam Fallon (liam.fallon@est.tech)
54  */
55 @Entity
56 @Table(name = "ControlLoopElement")
57 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
58 @Data
59 @EqualsAndHashCode(callSuper = false)
60 public class JpaControlLoopElement extends PfConcept implements PfAuthorative<ControlLoopElement> {
61     private static final long serialVersionUID = -1791732273187890213L;
62
63     @EmbeddedId
64     @VerifyKey
65     @NotNull
66     private PfReferenceKey key;
67
68     // @formatter:off
69     @VerifyKey
70     @NotNull
71     @AttributeOverride(name = "name",    column = @Column(name = "definition_name"))
72     @AttributeOverride(name = "version", column = @Column(name = "definition_version"))
73     private PfConceptKey definition;
74
75     @VerifyKey
76     @NotNull
77     @AttributeOverride(name = "name",    column = @Column(name = "participant_type_name"))
78     @AttributeOverride(name = "version", column = @Column(name = "participant_type_version"))
79     private PfConceptKey participantType;
80
81     @NotNull
82     @AttributeOverride(name = "name",    column = @Column(name = "participant_name"))
83     @AttributeOverride(name = "version", column = @Column(name = "participant_version"))
84     private PfConceptKey participantId;
85     // @formatter:on
86
87     @Column
88     @NotNull
89     private ControlLoopState state;
90
91     @Column
92     @NotNull
93     private ControlLoopOrderedState orderedState;
94
95     @Column
96     private String description;
97
98     /**
99      * The Default Constructor creates a {@link JpaControlLoopElement} object with a null key.
100      */
101     public JpaControlLoopElement() {
102         this(new PfReferenceKey());
103     }
104
105     /**
106      * The Key Constructor creates a {@link JpaControlLoopElement} object with the given concept key.
107      *
108      * @param key the key
109      */
110     public JpaControlLoopElement(@NonNull final PfReferenceKey key) {
111         this(key, new PfConceptKey(), new PfConceptKey(), ControlLoopState.UNINITIALISED);
112     }
113
114     /**
115      * The Key Constructor creates a {@link JpaControlLoopElement} object with all mandatory fields.
116      *
117      * @param key the key
118      * @param definition the TOSCA definition of the control loop element
119      * @param participantType the TOSCA definition of the participant running the control loop element
120      * @param state the state of the control loop
121      */
122     public JpaControlLoopElement(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey definition,
123             @NonNull final PfConceptKey participantType, @NonNull final ControlLoopState state) {
124         this.key = key;
125         this.definition = definition;
126         this.participantType = participantType;
127         this.state = state;
128     }
129
130     /**
131      * Copy constructor.
132      *
133      * @param copyConcept the concept to copy from
134      */
135     public JpaControlLoopElement(@NonNull final JpaControlLoopElement copyConcept) {
136         super(copyConcept);
137         this.key = new PfReferenceKey(copyConcept.key);
138         this.definition = new PfConceptKey(copyConcept.definition);
139         this.participantType = new PfConceptKey(copyConcept.participantType);
140         this.participantId = new PfConceptKey(copyConcept.participantId);
141         this.state = copyConcept.state;
142         this.orderedState = copyConcept.orderedState;
143         this.description = copyConcept.description;
144     }
145
146     /**
147      * Authorative constructor.
148      *
149      * @param authorativeConcept the authorative concept to copy from
150      */
151     public JpaControlLoopElement(@NonNull final ControlLoopElement authorativeConcept) {
152         this.fromAuthorative(authorativeConcept);
153     }
154
155     @Override
156     public ControlLoopElement toAuthorative() {
157         var element = new ControlLoopElement();
158
159         element.setId(UUID.fromString(getKey().getLocalName()));
160         element.setDefinition(new ToscaConceptIdentifier(definition));
161         element.setParticipantType(new ToscaConceptIdentifier(participantType));
162         element.setParticipantId(new ToscaConceptIdentifier(participantId));
163         element.setState(state);
164         element.setOrderedState(orderedState != null ? orderedState : state.asOrderedState());
165         element.setDescription(description);
166
167         return element;
168     }
169
170     @Override
171     public void fromAuthorative(@NonNull final ControlLoopElement element) {
172         if (this.key == null || this.getKey().isNullKey()) {
173             this.setKey(new PfReferenceKey());
174             getKey().setLocalName(element.getId().toString());
175         }
176
177         this.definition = element.getDefinition().asConceptKey();
178         this.participantType = element.getParticipantType().asConceptKey();
179         this.participantId = element.getParticipantId().asConceptKey();
180         this.state = element.getState();
181         this.orderedState = element.getOrderedState();
182         this.description = element.getDescription();
183     }
184
185     @Override
186     public List<PfKey> getKeys() {
187         List<PfKey> keyList = getKey().getKeys();
188
189         keyList.add(definition);
190         keyList.add(participantType);
191         keyList.add(participantId);
192
193         return keyList;
194     }
195
196     @Override
197     public void clean() {
198         key.clean();
199         definition.clean();
200         participantType.clean();
201         participantId.clean();
202
203         if (description != null) {
204             description = description.trim();
205         }
206     }
207
208     @Override
209     public int compareTo(final PfConcept otherConcept) {
210         if (otherConcept == null) {
211             return -1;
212         }
213         if (this == otherConcept) {
214             return 0;
215         }
216         if (getClass() != otherConcept.getClass()) {
217             return this.getClass().getName().compareTo(otherConcept.getClass().getName());
218         }
219
220         final JpaControlLoopElement other = (JpaControlLoopElement) otherConcept;
221         int result = key.compareTo(other.key);
222         if (result != 0) {
223             return result;
224         }
225
226         result = definition.compareTo(other.definition);
227         if (result != 0) {
228             return result;
229         }
230
231         result = participantType.compareTo(other.participantType);
232         if (result != 0) {
233             return result;
234         }
235
236         result = participantId.compareTo(other.participantId);
237         if (result != 0) {
238             return result;
239         }
240
241         result = ObjectUtils.compare(state, other.state);
242         if (result != 0) {
243             return result;
244         }
245
246         result = ObjectUtils.compare(orderedState, other.orderedState);
247         if (result != 0) {
248             return result;
249         }
250
251         return ObjectUtils.compare(description, other.description);
252     }
253 }