aa1febaddc8f4faad6d7ec60e17604a89210ec7e
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.clamp.controlloop.participant.intermediary.api.impl;
23
24 import java.util.LinkedHashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.UUID;
28 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics;
29 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
30 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatistics;
37 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessageType;
38 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ControlLoopElementListener;
39 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi;
40 import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ControlLoopHandler;
41 import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
43 import org.springframework.stereotype.Component;
44
45 /**
46  * This class is api implementation used by participant intermediary.
47  */
48 @Component
49 public class ParticipantIntermediaryApiImpl implements ParticipantIntermediaryApi {
50
51     // The handler for the participant intermediary
52     private final ParticipantHandler participantHandler;
53
54     // The handler for the controlLoop intermediary
55     private final ControlLoopHandler controlLoopHandler;
56
57     /**
58      * Constructor.
59      *
60      * @param participantHandler ParticipantHandler
61      * @param controlLoopHandler ControlLoopHandler
62      */
63     public ParticipantIntermediaryApiImpl(ParticipantHandler participantHandler,
64             ControlLoopHandler controlLoopHandler) {
65         this.participantHandler = participantHandler;
66         this.controlLoopHandler = controlLoopHandler;
67     }
68
69     @Override
70     public void registerControlLoopElementListener(ControlLoopElementListener controlLoopElementListener) {
71         controlLoopHandler.registerControlLoopElementListener(controlLoopElementListener);
72     }
73
74     @Override
75     public List<Participant> getParticipants(String name, String version) {
76         return List.of(participantHandler.getParticipant(name, version));
77     }
78
79     @Override
80     public Participant updateParticipantState(ToscaConceptIdentifier definition, ParticipantState state) {
81         return participantHandler.updateParticipantState(definition, state);
82     }
83
84     @Override
85     public void updateParticipantStatistics(ParticipantStatistics participantStatistics) {
86         // TODO Auto-generated method stub
87     }
88
89     @Override
90     public ControlLoops getControlLoops(String name, String version) {
91         return controlLoopHandler.getControlLoops();
92     }
93
94     @Override
95     public Map<UUID, ControlLoopElement> getControlLoopElements(String name, String version) {
96         List<ControlLoop> controlLoops = controlLoopHandler.getControlLoops().getControlLoopList();
97
98         for (ControlLoop controlLoop : controlLoops) {
99             if (name.equals(controlLoop.getDefinition().getName())) {
100                 return controlLoop.getElements();
101             }
102         }
103         return new LinkedHashMap<>();
104     }
105
106     @Override
107     public ControlLoopElement getControlLoopElement(UUID id) {
108         List<ControlLoop> controlLoops = controlLoopHandler.getControlLoops().getControlLoopList();
109
110         for (ControlLoop controlLoop : controlLoops) {
111             ControlLoopElement clElement = controlLoop.getElements().get(id);
112             if (clElement != null) {
113                 return clElement;
114             }
115         }
116         return null;
117     }
118
119     @Override
120     public ControlLoopElement updateControlLoopElementState(ToscaConceptIdentifier controlLoopId,
121             UUID id, ControlLoopOrderedState currentState,
122             ControlLoopState newState, ParticipantMessageType messageType) {
123         return controlLoopHandler.updateControlLoopElementState(controlLoopId,
124             id, currentState, newState);
125     }
126
127     @Override
128     public void updateControlLoopElementStatistics(UUID id, ClElementStatistics elementStatistics) {
129         controlLoopHandler.updateControlLoopElementStatistics(id, elementStatistics);
130     }
131 }