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