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