d0d79274c3171109c2eac5fa7846dc0f2d3fb131
[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.ArrayList;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.UUID;
27 import java.util.stream.Collectors;
28 import lombok.Data;
29 import lombok.EqualsAndHashCode;
30 import lombok.NoArgsConstructor;
31 import lombok.NonNull;
32 import org.apache.commons.collections4.MapUtils;
33 import org.onap.policy.models.base.PfConceptKey;
34 import org.onap.policy.models.base.PfUtils;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
37
38 /**
39  * Class to represent a control loop instance.
40  */
41 @NoArgsConstructor
42 @Data
43 @EqualsAndHashCode(callSuper = true)
44 public class ControlLoop extends ToscaEntity implements Comparable<ControlLoop> {
45     @NonNull
46     private ToscaConceptIdentifier definition = new ToscaConceptIdentifier(PfConceptKey.getNullKey());
47
48     @NonNull
49     private ControlLoopState state = ControlLoopState.UNINITIALISED;
50
51     @NonNull
52     private ControlLoopOrderedState orderedState = ControlLoopOrderedState.UNINITIALISED;
53
54     private Map<UUID, ControlLoopElement> elements;
55
56     @Override
57     public String getType() {
58         return definition.getName();
59     }
60
61     @Override
62     public String getTypeVersion() {
63         return definition.getVersion();
64     }
65
66     /**
67      * Copy contructor, does a deep copy.
68      *
69      * @param otherControlLoop the other element to copy from
70      */
71     public ControlLoop(final ControlLoop otherControlLoop) {
72         super(otherControlLoop);
73         this.definition = new ToscaConceptIdentifier(otherControlLoop.definition);
74         this.state = otherControlLoop.state;
75         this.orderedState = otherControlLoop.orderedState;
76         this.elements = PfUtils.mapMap(otherControlLoop.elements, ControlLoopElement::new);
77     }
78
79     @Override
80     public int compareTo(final ControlLoop other) {
81         return compareNameVersion(this, other);
82     }
83
84     /**
85      * Set the ordered state on the control loop and on all its control loop elements.
86      *
87      * @param orderedState the state we want the control loop to transition to
88      */
89     public void setCascadedOrderedState(final ControlLoopOrderedState orderedState) {
90         this.orderedState = orderedState;
91
92         if (MapUtils.isEmpty(elements)) {
93             return;
94         }
95
96         elements.values().forEach(element -> element.setOrderedState(orderedState));
97     }
98
99     /**
100      * Get a list of control loop element statistics.
101      *
102      * @param controlLoop the control loop
103      * @return List of ClElementStatistics
104      */
105     public List<ClElementStatistics> getControlLoopElementStatisticsList(final ControlLoop controlLoop) {
106         if (MapUtils.isEmpty(controlLoop.elements)) {
107             return null;
108         }
109
110         return controlLoop.elements.values().stream().map(ControlLoopElement::getClElementStatistics)
111                 .collect(Collectors.toList());
112     }
113 }