f542be2015c292fd3c8658cfc318b18533a3a683
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2022 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.element.service;
22
23 import java.util.List;
24 import javax.ws.rs.core.Response;
25 import lombok.NonNull;
26 import lombok.RequiredArgsConstructor;
27 import org.onap.policy.clamp.acm.element.handler.MessageActivator;
28 import org.onap.policy.clamp.acm.element.handler.MessageHandler;
29 import org.onap.policy.clamp.models.acm.messages.dmaap.element.ElementMessage;
30 import org.onap.policy.clamp.models.acm.messages.rest.element.ElementConfig;
31 import org.onap.policy.common.endpoints.parameters.TopicParameterGroup;
32 import org.onap.policy.common.endpoints.parameters.TopicParameters;
33 import org.onap.policy.models.base.PfModelRuntimeException;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.stereotype.Service;
37
38 @Service
39 @RequiredArgsConstructor
40 public class ConfigService {
41
42     private static final Logger LOGGER = LoggerFactory.getLogger(ConfigService.class);
43
44     private ElementConfig elementConfig = new ElementConfig();
45
46     private final MessageHandler handler;
47     private final MessageActivator messageActivator;
48
49     /**
50      * Activate messages and service and create the element configuration.
51      *
52      * @param elementConfig the configuration
53      */
54     public void activateElement(@NonNull ElementConfig elementConfig) {
55         var topicParameters = new TopicParameters();
56         topicParameters.setTopic(elementConfig.getTopicParameterGroup().getTopic());
57         topicParameters.setServers(List.of(elementConfig.getTopicParameterGroup().getServer()));
58         topicParameters.setFetchTimeout(elementConfig.getTopicParameterGroup().getFetchTimeout());
59         topicParameters.setTopicCommInfrastructure(elementConfig.getTopicParameterGroup().getTopicCommInfrastructure());
60         topicParameters.setUseHttps(elementConfig.getTopicParameterGroup().isUseHttps());
61
62         var parameters = new TopicParameterGroup();
63         parameters.setTopicSinks(List.of(topicParameters));
64         parameters.setTopicSources(List.of(topicParameters));
65
66         if (!parameters.isValid()) {
67             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
68                     "Validation failed for topic parameter group. Kafka config not activated");
69         }
70
71         if (messageActivator.isAlive()) {
72             throw new PfModelRuntimeException(Response.Status.CONFLICT,
73                     "Service Manager already running, cannot add Topic endpoint management");
74         }
75
76         handler.active(elementConfig);
77         messageActivator.activate(parameters);
78         this.elementConfig = elementConfig;
79
80         LOGGER.info("Messages and service activated");
81     }
82
83     /**
84      * Fetch element configuration.
85      *
86      * @return element configuration present
87      */
88     public ElementConfig getElementConfig() {
89         return elementConfig;
90     }
91
92     /**
93      * Deactivate messages and service and delete the element config.
94      */
95     public void deleteConfig() {
96         handler.deactivateElement();
97         messageActivator.deactivate();
98         elementConfig = new ElementConfig();
99         LOGGER.info("Messages and service deactivated");
100     }
101
102     public List<ElementMessage> getMessages() {
103         return handler.getMessages();
104     }
105 }