042cb16e1f5c24aecbd7302dacb8fd10a4b28ad2
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021 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.clamp.controlloop.models.controlloop.persistence.concepts;
22
23 import java.util.List;
24 import java.util.UUID;
25 import javax.persistence.AttributeOverride;
26 import javax.persistence.AttributeOverrides;
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.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
39 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
40 import org.onap.policy.common.parameters.annotations.NotNull;
41 import org.onap.policy.models.base.PfAuthorative;
42 import org.onap.policy.models.base.PfConcept;
43 import org.onap.policy.models.base.PfConceptKey;
44 import org.onap.policy.models.base.PfKey;
45 import org.onap.policy.models.base.PfReferenceKey;
46 import org.onap.policy.models.base.validation.annotations.VerifyKey;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
48
49 /**
50  * Class to represent a participant control loop element in the database.
51  *
52  * @author Liam Fallon (liam.fallon@est.tech)
53  */
54 @Entity
55 @Table(name = "ControlLoopElement")
56 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
57 @Data
58 @EqualsAndHashCode(callSuper = false)
59 public class JpaControlLoopElement extends PfConcept implements PfAuthorative<ControlLoopElement> {
60     private static final long serialVersionUID = -1791732273187890213L;
61
62     @EmbeddedId
63     @VerifyKey
64     @NotNull
65     private PfReferenceKey key;
66
67     // @formatter:off
68     @VerifyKey
69     @NotNull
70     @AttributeOverrides({
71             @AttributeOverride(name = "name",    column = @Column(name = "definition_name")),
72             @AttributeOverride(name = "version", column = @Column(name = "definition_version"))
73         }
74     )
75     private PfConceptKey definition;
76
77     @VerifyKey
78     @NotNull
79     @AttributeOverrides({
80             @AttributeOverride(name = "name",    column = @Column(name = "participant_name")),
81             @AttributeOverride(name = "version", column = @Column(name = "participant_version"))
82         }
83     )
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 participantId 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 participantId, @NonNull final ControlLoopState state) {
124         this.key = key;
125         this.definition = definition;
126         this.participantId = participantId;
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.participantId = new PfConceptKey(copyConcept.participantId);
140         this.state = copyConcept.state;
141         this.orderedState = copyConcept.orderedState;
142         this.description = copyConcept.description;
143     }
144
145     /**
146      * Authorative constructor.
147      *
148      * @param authorativeConcept the authorative concept to copy from
149      */
150     public JpaControlLoopElement(@NonNull final ControlLoopElement authorativeConcept) {
151         this.fromAuthorative(authorativeConcept);
152     }
153
154     @Override
155     public ControlLoopElement toAuthorative() {
156         ControlLoopElement element = new ControlLoopElement();
157
158         element.setId(UUID.fromString(getKey().getLocalName()));
159         element.setDefinition(new ToscaConceptIdentifier(definition));
160         element.setParticipantId(new ToscaConceptIdentifier(participantId));
161         element.setState(state);
162         element.setOrderedState(orderedState != null ? orderedState : state.asOrderedState());
163         element.setDescription(description);
164
165         return element;
166     }
167
168     @Override
169     public void fromAuthorative(@NonNull final ControlLoopElement element) {
170         if (this.key == null || this.getKey().isNullKey()) {
171             this.setKey(new PfReferenceKey());
172             getKey().setLocalName(element.getId().toString());
173         }
174
175         this.definition = element.getDefinition().asConceptKey();
176         this.participantId = element.getParticipantId().asConceptKey();
177         this.state = element.getState();
178         this.orderedState = element.getOrderedState();
179         this.description = element.getDescription();
180     }
181
182     @Override
183     public List<PfKey> getKeys() {
184         List<PfKey> keyList = getKey().getKeys();
185
186         keyList.add(definition);
187         keyList.add(participantId);
188
189         return keyList;
190     }
191
192     @Override
193     public void clean() {
194         key.clean();
195         definition.clean();
196         participantId.clean();
197
198         if (description != null) {
199             description = description.trim();
200         }
201     }
202
203     @Override
204     public int compareTo(final PfConcept otherConcept) {
205         if (otherConcept == null) {
206             return -1;
207         }
208         if (this == otherConcept) {
209             return 0;
210         }
211         if (getClass() != otherConcept.getClass()) {
212             return this.getClass().getName().compareTo(otherConcept.getClass().getName());
213         }
214
215         final JpaControlLoopElement other = (JpaControlLoopElement) otherConcept;
216         int result = key.compareTo(other.key);
217         if (result != 0) {
218             return result;
219         }
220
221         result = definition.compareTo(other.definition);
222         if (result != 0) {
223             return result;
224         }
225
226         result = participantId.compareTo(other.participantId);
227         if (result != 0) {
228             return result;
229         }
230
231         result = ObjectUtils.compare(state, other.state);
232         if (result != 0) {
233             return result;
234         }
235
236         result = ObjectUtils.compare(orderedState, other.orderedState);
237         if (result != 0) {
238             return result;
239         }
240
241         return ObjectUtils.compare(description, other.description);
242     }
243 }