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