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