9652f1a8d9b98ec319da0f9f9d661944903cf33d
[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.participant.intermediary.api.impl;
22
23 import java.util.LinkedHashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.UUID;
27 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics;
28 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
29 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
30 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatistics;
36 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ControlLoopElementListener;
37 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi;
38 import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
40 import org.springframework.stereotype.Component;
41
42 /**
43  * This class is api implementation used by participant intermediary.
44  */
45 @Component
46 public class ParticipantIntermediaryApiImpl implements ParticipantIntermediaryApi {
47
48     // The handler for the participant intermediary
49     private ParticipantHandler participantHandler;
50
51     /**
52      * Constructor.
53      *
54      * @param participantHandler ParticipantHandler
55      */
56     public ParticipantIntermediaryApiImpl(ParticipantHandler participantHandler) {
57         this.participantHandler = participantHandler;
58     }
59
60     @Override
61     public void registerControlLoopElementListener(ControlLoopElementListener controlLoopElementListener) {
62         participantHandler.getControlLoopHandler().registerControlLoopElementListener(controlLoopElementListener);
63     }
64
65     @Override
66     public void sendParticipantRegister() {
67         participantHandler.sendParticipantRegister();
68     }
69
70     @Override
71     public void sendParticipantDeregister() {
72         participantHandler.sendParticipantDeregister();
73     }
74
75     @Override
76     public List<Participant> getParticipants(String name, String version) {
77         return List.of(participantHandler.getParticipant(name, version));
78     }
79
80     @Override
81     public Participant updateParticipantState(ToscaConceptIdentifier definition, ParticipantState state) {
82         return participantHandler.updateParticipantState(definition, state);
83     }
84
85     @Override
86     public void updateParticipantStatistics(ParticipantStatistics participantStatistics) {
87         // TODO Auto-generated method stub
88     }
89
90     @Override
91     public ControlLoops getControlLoops(String name, String version) {
92         return participantHandler.getControlLoopHandler().getControlLoops();
93     }
94
95     @Override
96     public Map<UUID, ControlLoopElement> getControlLoopElements(String name, String version) {
97         List<ControlLoop> controlLoops =
98                 participantHandler.getControlLoopHandler().getControlLoops().getControlLoopList();
99
100         for (ControlLoop controlLoop : controlLoops) {
101             if (name.equals(controlLoop.getDefinition().getName())) {
102                 return controlLoop.getElements();
103             }
104         }
105         return new LinkedHashMap<>();
106     }
107
108     @Override
109     public ControlLoopElement getControlLoopElement(UUID id) {
110         List<ControlLoop> controlLoops =
111                 participantHandler.getControlLoopHandler().getControlLoops().getControlLoopList();
112
113         for (ControlLoop controlLoop : controlLoops) {
114             ControlLoopElement clElement = controlLoop.getElements().get(id);
115             if (clElement != null) {
116                 return clElement;
117             }
118         }
119         return null;
120     }
121
122     @Override
123     public ControlLoopElement updateControlLoopElementState(UUID id, ControlLoopOrderedState currentState,
124             ControlLoopState newState) {
125         return participantHandler.getControlLoopHandler().updateControlLoopElementState(id, currentState, newState);
126     }
127
128     @Override
129     public void updateControlLoopElementStatistics(UUID id, ClElementStatistics elementStatistics) {
130         participantHandler.getControlLoopHandler().updateControlLoopElementStatistics(id, elementStatistics);
131     }
132 }