911a520899a54ea5a546db02657542ee81e48533
[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.Column;
27 import javax.persistence.EmbeddedId;
28 import javax.persistence.Entity;
29 import javax.persistence.Inheritance;
30 import javax.persistence.InheritanceType;
31 import javax.persistence.Table;
32 import lombok.Data;
33 import lombok.EqualsAndHashCode;
34 import lombok.NonNull;
35 import org.apache.commons.lang3.ObjectUtils;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
39 import org.onap.policy.common.parameters.annotations.NotNull;
40 import org.onap.policy.models.base.PfAuthorative;
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.validation.annotations.VerifyKey;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
47
48 /**
49  * Class to represent a participant control loop element in the database.
50  *
51  * @author Liam Fallon (liam.fallon@est.tech)
52  */
53 @Entity
54 @Table(name = "ControlLoopElement")
55 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
56 @Data
57 @EqualsAndHashCode(callSuper = false)
58 public class JpaControlLoopElement extends PfConcept implements PfAuthorative<ControlLoopElement> {
59     private static final long serialVersionUID = -1791732273187890213L;
60
61     @EmbeddedId
62     @VerifyKey
63     @NotNull
64     private PfReferenceKey key;
65
66     // @formatter:off
67     @VerifyKey
68     @NotNull
69     @AttributeOverride(name = "name",    column = @Column(name = "definition_name"))
70     @AttributeOverride(name = "version", column = @Column(name = "definition_version"))
71     private PfConceptKey definition;
72
73     @VerifyKey
74     @NotNull
75     @AttributeOverride(name = "name",    column = @Column(name = "participant_type_name"))
76     @AttributeOverride(name = "version", column = @Column(name = "participant_type_version"))
77     private PfConceptKey participantType;
78
79     @NotNull
80     @AttributeOverride(name = "name",    column = @Column(name = "participant_name"))
81     @AttributeOverride(name = "version", column = @Column(name = "participant_version"))
82     private PfConceptKey participantId;
83     // @formatter:on
84
85     @Column
86     @NotNull
87     private ControlLoopState state;
88
89     @Column
90     @NotNull
91     private ControlLoopOrderedState orderedState;
92
93     @Column
94     private String description;
95
96     /**
97      * The Default Constructor creates a {@link JpaControlLoopElement} object with a null key.
98      */
99     public JpaControlLoopElement() {
100         this(new PfReferenceKey());
101     }
102
103     /**
104      * The Key Constructor creates a {@link JpaControlLoopElement} object with the given concept key.
105      *
106      * @param key the key
107      */
108     public JpaControlLoopElement(@NonNull final PfReferenceKey key) {
109         this(key, new PfConceptKey(), new PfConceptKey(), ControlLoopState.UNINITIALISED);
110     }
111
112     /**
113      * The Key Constructor creates a {@link JpaControlLoopElement} object with all mandatory fields.
114      *
115      * @param key the key
116      * @param definition the TOSCA definition of the control loop element
117      * @param participantType the TOSCA definition of the participant running the control loop element
118      * @param state the state of the control loop
119      */
120     public JpaControlLoopElement(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey definition,
121             @NonNull final PfConceptKey participantType, @NonNull final ControlLoopState state) {
122         this.key = key;
123         this.definition = definition;
124         this.participantType = participantType;
125         this.state = state;
126     }
127
128     /**
129      * Copy constructor.
130      *
131      * @param copyConcept the concept to copy from
132      */
133     public JpaControlLoopElement(@NonNull final JpaControlLoopElement copyConcept) {
134         super(copyConcept);
135         this.key = new PfReferenceKey(copyConcept.key);
136         this.definition = new PfConceptKey(copyConcept.definition);
137         this.participantType = new PfConceptKey(copyConcept.participantType);
138         this.participantId = new PfConceptKey(copyConcept.participantId);
139         this.state = copyConcept.state;
140         this.orderedState = copyConcept.orderedState;
141         this.description = copyConcept.description;
142     }
143
144     /**
145      * Authorative constructor.
146      *
147      * @param authorativeConcept the authorative concept to copy from
148      */
149     public JpaControlLoopElement(@NonNull final ControlLoopElement authorativeConcept) {
150         this.fromAuthorative(authorativeConcept);
151     }
152
153     @Override
154     public ControlLoopElement toAuthorative() {
155         ControlLoopElement element = new ControlLoopElement();
156
157         element.setId(UUID.fromString(getKey().getLocalName()));
158         element.setDefinition(new ToscaConceptIdentifier(definition));
159         element.setParticipantType(new ToscaConceptIdentifier(participantType));
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.participantType = element.getParticipantType().asConceptKey();
177         this.participantId = element.getParticipantId().asConceptKey();
178         this.state = element.getState();
179         this.orderedState = element.getOrderedState();
180         this.description = element.getDescription();
181     }
182
183     @Override
184     public List<PfKey> getKeys() {
185         List<PfKey> keyList = getKey().getKeys();
186
187         keyList.add(definition);
188         keyList.add(participantType);
189         keyList.add(participantId);
190
191         return keyList;
192     }
193
194     @Override
195     public void clean() {
196         key.clean();
197         definition.clean();
198         participantType.clean();
199         participantId.clean();
200
201         if (description != null) {
202             description = description.trim();
203         }
204     }
205
206     @Override
207     public int compareTo(final PfConcept otherConcept) {
208         if (otherConcept == null) {
209             return -1;
210         }
211         if (this == otherConcept) {
212             return 0;
213         }
214         if (getClass() != otherConcept.getClass()) {
215             return this.getClass().getName().compareTo(otherConcept.getClass().getName());
216         }
217
218         final JpaControlLoopElement other = (JpaControlLoopElement) otherConcept;
219         int result = key.compareTo(other.key);
220         if (result != 0) {
221             return result;
222         }
223
224         result = definition.compareTo(other.definition);
225         if (result != 0) {
226             return result;
227         }
228
229         result = participantType.compareTo(other.participantType);
230         if (result != 0) {
231             return result;
232         }
233
234         result = participantId.compareTo(other.participantId);
235         if (result != 0) {
236             return result;
237         }
238
239         result = ObjectUtils.compare(state, other.state);
240         if (result != 0) {
241             return result;
242         }
243
244         result = ObjectUtils.compare(orderedState, other.orderedState);
245         if (result != 0) {
246             return result;
247         }
248
249         return ObjectUtils.compare(description, other.description);
250     }
251 }