fc0a31fcba5cb17a87cf667233f6001193df9414
[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.io.Closeable;
24 import java.io.IOException;
25 import java.util.List;
26 import lombok.Getter;
27 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
28 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
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.ControlLoops;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant;
33 import org.onap.policy.clamp.controlloop.models.messages.rest.TypedSimpleResponse;
34 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi;
35 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryFactory;
36 import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantIntermediaryParameters;
37
38 /**
39  * This provider class simulation of participants and control loop elements.
40  */
41 public class SimulationProvider implements Closeable {
42     @Getter
43     private final ParticipantIntermediaryApi intermediaryApi;
44
45     /**
46      * Create a participant simulation provider.
47      *
48      * @throws ControlLoopRuntimeException on errors creating the provider
49      */
50     public SimulationProvider(ParticipantIntermediaryParameters participantParameters)
51                      throws ControlLoopRuntimeException {
52         intermediaryApi = new ParticipantIntermediaryFactory().createApiImplementation();
53         intermediaryApi.init(participantParameters);
54     }
55
56     @Override
57     public void close() throws IOException {
58         intermediaryApi.close();
59     }
60
61     /**
62      * Get the control loops.
63      *
64      * @param name the controlLoop, null to get all
65      * @param version the controlLoop, null to get all
66      * @return the control loops
67      * @throws ControlLoopException on errors getting the control loops
68      */
69     public ControlLoops getControlLoops(String name, String version) throws ControlLoopException {
70         return intermediaryApi.getControlLoops(name, version);
71     }
72
73     /**
74      * Update the given control loop in the simulator.
75      *
76      * @param controlLoop the control loop to update
77      * @return response simple response returned
78      * @throws ControlLoopException on errors updating the control loop
79      */
80     public TypedSimpleResponse<ControlLoop> updateControlLoop(ControlLoop controlLoop)
81             throws ControlLoopException {
82         TypedSimpleResponse<ControlLoop> response = new TypedSimpleResponse<>();
83         ControlLoop updatedControlLoop = intermediaryApi.updateControlLoopState(
84                 controlLoop.getDefinition(), controlLoop.getOrderedState());
85         response.setResponse(updatedControlLoop);
86         return response;
87     }
88
89     /**
90      * Get the simulated control loop elements.
91      *
92      * @param name the controlLoopElement, null to get all
93      * @param version the controlLoopElement, null to get all
94      * @return the control loop elements
95      * @throws ControlLoopException on errors getting the control loop elements
96      */
97     public List<ControlLoopElement> getControlLoopElements(String name, String version) throws ControlLoopException {
98         return intermediaryApi.getControlLoopElements(name, version);
99     }
100
101     /**
102      * Update the given control loop element in the simulator.
103      *
104      * @param element the control loop element to update
105      * @return response simple response returned
106      * @throws ControlLoopException on errors updating the control loop element
107      */
108     public TypedSimpleResponse<ControlLoopElement> updateControlLoopElement(ControlLoopElement element)
109             throws ControlLoopException {
110         TypedSimpleResponse<ControlLoopElement> response = new TypedSimpleResponse<>();
111         response.setResponse(intermediaryApi.updateControlLoopElementState(
112                 element.getId(), element.getOrderedState()));
113         return response;
114     }
115
116     /**
117      * Get the current simulated participants.
118      *
119      * @param name the participant, null to get all
120      * @param version the participant, null to get all
121      * @return the list of participants
122      * @throws ControlLoopException on errors getting the participants
123      */
124     public List<Participant> getParticipants(String name, String version) throws ControlLoopException {
125         return intermediaryApi.getParticipants(name, version);
126     }
127
128     /**
129      * Update a simulated participant.
130      *
131      * @param participant the participant to update
132      * @return TypedSimpleResponse simple response
133      * @throws ControlLoopException on errors updating the participant
134      */
135
136     public TypedSimpleResponse<Participant> updateParticipant(Participant participant) throws ControlLoopException {
137         TypedSimpleResponse<Participant> response = new TypedSimpleResponse<>();
138         response.setResponse(intermediaryApi.updateParticipantState(
139                 participant.getDefinition(), participant.getParticipantState()));
140         return response;
141     }
142 }