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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.models.controlloop.concepts;
23 import java.util.List;
24 import java.util.UUID;
26 import lombok.EqualsAndHashCode;
27 import lombok.NoArgsConstructor;
28 import lombok.NonNull;
29 import org.apache.commons.collections4.CollectionUtils;
30 import org.onap.policy.models.base.PfKey;
31 import org.onap.policy.models.base.PfUtils;
32 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
33 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
36 * Class to represent a control loop instance.
40 @EqualsAndHashCode(callSuper = true)
41 public class ControlLoop extends ToscaEntity implements Comparable<ControlLoop> {
43 private ToscaConceptIdentifier definition = new ToscaConceptIdentifier(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_NAME);
46 private ControlLoopState state = ControlLoopState.UNINITIALISED;
49 private ControlLoopOrderedState orderedState = ControlLoopOrderedState.UNINITIALISED;
51 private List<ControlLoopElement> elements;
54 public String getType() {
55 return definition.getName();
59 public String getTypeVersion() {
60 return definition.getVersion();
64 * Copy contructor, does a deep copy.
66 * @param otherControlLoop the other element to copy from
68 public ControlLoop(final ControlLoop otherControlLoop) {
69 super(otherControlLoop);
70 this.definition = new ToscaConceptIdentifier(otherControlLoop.definition);
71 this.state = otherControlLoop.state;
72 this.orderedState = otherControlLoop.orderedState;
73 this.elements = PfUtils.mapList(otherControlLoop.elements, ControlLoopElement::new);
77 public int compareTo(final ControlLoop other) {
78 return compareNameVersion(this, other);
82 * Set the ordered state on the control loop and on all its control loop elements.
84 * @param orderedState the state we want the control loop to transition to
86 public void setCascadedOrderedState(final ControlLoopOrderedState orderedState) {
87 this.orderedState = orderedState;
89 if (CollectionUtils.isEmpty(elements)) {
93 elements.forEach(element -> element.setOrderedState(orderedState));
97 * Find the element with a given UUID for the control loop.
99 * @param id the UUID to search for
100 * @return the element or null if its not found
103 public ControlLoopElement getElement(final UUID id) {
104 if (CollectionUtils.isEmpty(elements)) {
108 return elements.stream().filter(element -> id.equals(element.getId())).findAny().orElse(null);