6ee4eac3e0b1d30531538f23d2fcc85411ff1656
[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.simulator.simulation;
22
23 import java.util.List;
24 import java.util.Map;
25 import java.util.UUID;
26 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
27 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
28 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
29 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant;
30 import org.onap.policy.clamp.controlloop.models.messages.rest.TypedSimpleResponse;
31 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi;
32 import org.springframework.stereotype.Service;
33
34 /**
35  * This provider class simulation of participants and control loop elements.
36  */
37 @Service
38 public class SimulationProvider {
39
40     private final ParticipantIntermediaryApi intermediaryApi;
41
42     /**
43      * Create a participant simulation provider.
44      */
45     public SimulationProvider(ParticipantIntermediaryApi intermediaryApi) {
46         this.intermediaryApi = intermediaryApi;
47     }
48
49     /**
50      * Get the control loops.
51      *
52      * @param name the controlLoop, null to get all
53      * @param version the controlLoop, null to get all
54      * @return the control loops
55      * @throws ControlLoopException on errors getting the control loops
56      */
57     public ControlLoops getControlLoops(String name, String version) throws ControlLoopException {
58         return intermediaryApi.getControlLoops(name, version);
59     }
60
61     /**
62      * Get the simulated control loop elements.
63      *
64      * @param name the controlLoopElement, null to get all
65      * @param version the controlLoopElement, null to get all
66      * @return the control loop elements
67      */
68     public Map<UUID, ControlLoopElement> getControlLoopElements(String name, String version) {
69         return intermediaryApi.getControlLoopElements(name, version);
70     }
71
72     /**
73      * Update the given control loop element in the simulator.
74      *
75      * @param element the control loop element to update
76      * @return response simple response returned
77      */
78     public TypedSimpleResponse<ControlLoopElement> updateControlLoopElement(ControlLoopElement element) {
79         TypedSimpleResponse<ControlLoopElement> response = new TypedSimpleResponse<>();
80         response.setResponse(intermediaryApi.updateControlLoopElementState(element.getId(), element.getOrderedState(),
81                 element.getState()));
82         return response;
83     }
84
85     /**
86      * Get the current simulated participants.
87      *
88      * @param name the participant, null to get all
89      * @param version the participant, null to get all
90      * @return the list of participants
91      */
92     public List<Participant> getParticipants(String name, String version) {
93         return intermediaryApi.getParticipants(name, version);
94     }
95
96     /**
97      * Update a simulated participant.
98      *
99      * @param participant the participant to update
100      * @return TypedSimpleResponse simple response
101      */
102     public TypedSimpleResponse<Participant> updateParticipant(Participant participant) {
103         TypedSimpleResponse<Participant> response = new TypedSimpleResponse<>();
104         response.setResponse(
105                 intermediaryApi.updateParticipantState(participant.getDefinition(), participant.getParticipantState()));
106         return response;
107     }
108 }