e7bacb0d3c83f4be8cac1962f12e1a756578d518
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2022-2026 OpenInfra Foundation Europe. All rights reserved.
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 jakarta.ws.rs.core.Response;
24 import java.util.List;
25 import lombok.Getter;
26 import lombok.NonNull;
27 import lombok.RequiredArgsConstructor;
28 import org.onap.policy.clamp.acm.element.handler.MessageActivator;
29 import org.onap.policy.clamp.acm.element.handler.MessageHandler;
30 import org.onap.policy.clamp.acm.element.main.concepts.ElementConfig;
31 import org.onap.policy.clamp.acm.element.main.parameters.ElementTopicParameters;
32 import org.onap.policy.clamp.common.acm.exception.AutomationCompositionRuntimeException;
33 import org.onap.policy.common.parameters.BeanValidator;
34 import org.onap.policy.common.parameters.topic.TopicParameterGroup;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.stereotype.Service;
38
39 @Service
40 @RequiredArgsConstructor
41 public class ConfigService {
42
43     private static final Logger LOGGER = LoggerFactory.getLogger(ConfigService.class);
44
45     @Getter
46     private ElementConfig elementConfig = new ElementConfig();
47
48     private final MessageHandler handler;
49     private final MessageActivator messageActivator;
50
51     /**
52      * Activate messages and service and create the element configuration.
53      *
54      * @param elementConfig the configuration
55      */
56     public void activateElement(@NonNull ElementConfig elementConfig) {
57         var listenerTopicParameters = new ElementTopicParameters(elementConfig.getTopicParameterGroup());
58
59         var publisherTopicParameters = new ElementTopicParameters(elementConfig.getTopicParameterGroup());
60         publisherTopicParameters.setTopic(elementConfig.getTopicParameterGroup().getPublisherTopic());
61
62         var parameters = new TopicParameterGroup();
63         parameters.setTopicSinks(List.of(publisherTopicParameters));
64         parameters.setTopicSources(List.of(listenerTopicParameters));
65
66         if (!BeanValidator.validate(parameters).isValid()) {
67             throw new AutomationCompositionRuntimeException(Response.Status.BAD_REQUEST,
68                     "Validation failed for topic parameter group. Kafka config not activated");
69         }
70
71         if (messageActivator.isAlive()) {
72             throw new AutomationCompositionRuntimeException(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      * Deactivate messages and service and delete the element config.
85      */
86     public void deleteConfig() {
87         handler.deactivateElement();
88         messageActivator.deactivate();
89         elementConfig = new ElementConfig();
90         LOGGER.info("Messages and service deactivated");
91     }
92 }