417e02a5ad9a048aa1eb4d315082d53abd730064
[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.concepts;
22
23 import java.util.List;
24 import java.util.UUID;
25 import lombok.Data;
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;
34
35 /**
36  * Class to represent a control loop instance.
37  */
38 @NoArgsConstructor
39 @Data
40 @EqualsAndHashCode(callSuper = true)
41 public class ControlLoop extends ToscaEntity implements Comparable<ControlLoop> {
42     @NonNull
43     private ToscaConceptIdentifier definition = new ToscaConceptIdentifier(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_NAME);
44
45     @NonNull
46     private ControlLoopState state = ControlLoopState.UNINITIALISED;
47
48     @NonNull
49     private ControlLoopOrderedState orderedState = ControlLoopOrderedState.UNINITIALISED;
50
51     private List<ControlLoopElement> elements;
52
53     @Override
54     public String getType() {
55         return definition.getName();
56     }
57
58     @Override
59     public String getTypeVersion() {
60         return definition.getVersion();
61     }
62
63     /**
64      * Copy contructor, does a deep copy.
65      *
66      * @param otherControlLoop the other element to copy from
67      */
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);
74     }
75
76     @Override
77     public int compareTo(final ControlLoop other) {
78         return compareNameVersion(this, other);
79     }
80
81     /**
82      * Set the ordered state on the control loop and on all its control loop elements.
83      *
84      * @param orderedState the state we want the control loop to transition to
85      */
86     public void setCascadedOrderedState(final ControlLoopOrderedState orderedState) {
87         this.orderedState = orderedState;
88
89         if (CollectionUtils.isEmpty(elements)) {
90             return;
91         }
92
93         elements.forEach(element -> element.setOrderedState(orderedState));
94     }
95
96     /**
97      * Find the element with a given UUID for the control loop.
98      *
99      * @param id the UUID to search for
100      * @return the element or null if its not found
101      */
102
103     public ControlLoopElement getElement(final UUID id) {
104         if (CollectionUtils.isEmpty(elements)) {
105             return null;
106         }
107
108         return elements.stream().filter(element -> id.equals(element.getId())).findAny().orElse(null);
109     }
110 }