73ae80e293fe4fe384fa70a85faebbf29340236b
[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.acm.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.acm.participant.intermediary.api.ParticipantIntermediaryApi;
27 import org.onap.policy.clamp.common.acm.exception.AutomationCompositionException;
28 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
29 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
30 import org.onap.policy.clamp.models.acm.concepts.Participant;
31 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessageType;
32 import org.onap.policy.clamp.models.acm.messages.rest.TypedSimpleResponse;
33 import org.springframework.stereotype.Service;
34
35 /**
36  * This provider class simulation of participants and automation composition elements.
37  */
38 @Service
39 public class SimulationProvider {
40
41     private final ParticipantIntermediaryApi intermediaryApi;
42
43     /**
44      * Create a participant simulation provider.
45      *
46      * @param intermediaryApi the intermediary to use for talking to the CLAMP runtime
47      */
48     public SimulationProvider(ParticipantIntermediaryApi intermediaryApi) {
49         this.intermediaryApi = intermediaryApi;
50     }
51
52     /**
53      * Get the automation compositions.
54      *
55      * @param name the automationComposition, null to get all
56      * @param version the automationComposition, null to get all
57      * @return the automation compositions
58      * @throws AutomationCompositionException on errors getting the automation compositions
59      */
60     public AutomationCompositions getAutomationCompositions(String name, String version)
61         throws AutomationCompositionException {
62         return intermediaryApi.getAutomationCompositions(name, version);
63     }
64
65     /**
66      * Get the simulated automation composition elements.
67      *
68      * @param name the automationCompositionElement, null to get all
69      * @param version the automationCompositionElement, null to get all
70      * @return the automation composition elements
71      */
72     public Map<UUID, AutomationCompositionElement> getAutomationCompositionElements(String name, String version) {
73         return intermediaryApi.getAutomationCompositionElements(name, version);
74     }
75
76     /**
77      * Update the given automation composition element in the simulator.
78      *
79      * @param element the automation composition element to update
80      * @return response simple response returned
81      */
82     public TypedSimpleResponse<AutomationCompositionElement> updateAutomationCompositionElement(
83         AutomationCompositionElement element) {
84         TypedSimpleResponse<AutomationCompositionElement> response = new TypedSimpleResponse<>();
85         response.setResponse(intermediaryApi.updateAutomationCompositionElementState(null, element.getId(),
86             element.getOrderedState(), element.getState(), ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE));
87         return response;
88     }
89
90     /**
91      * Get the current simulated participants.
92      *
93      * @param name the participant, null to get all
94      * @param version the participant, null to get all
95      * @return the list of participants
96      */
97     public List<Participant> getParticipants(String name, String version) {
98         return intermediaryApi.getParticipants(name, version);
99     }
100
101     /**
102      * Update a simulated participant.
103      *
104      * @param participant the participant to update
105      * @return TypedSimpleResponse simple response
106      */
107     public TypedSimpleResponse<Participant> updateParticipant(Participant participant) {
108         TypedSimpleResponse<Participant> response = new TypedSimpleResponse<>();
109         response.setResponse(
110             intermediaryApi.updateParticipantState(participant.getDefinition(), participant.getParticipantState()));
111         return response;
112     }
113 }