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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.acm.element.service;
23 import jakarta.ws.rs.core.Response;
24 import java.util.List;
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.acm.element.main.parameters.ElementTopicParameters;
30 import org.onap.policy.clamp.common.acm.exception.AutomationCompositionRuntimeException;
31 import org.onap.policy.clamp.models.acm.messages.rest.element.ElementConfig;
32 import org.onap.policy.common.endpoints.parameters.TopicParameterGroup;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.stereotype.Service;
38 @RequiredArgsConstructor
39 public class ConfigService {
41 private static final Logger LOGGER = LoggerFactory.getLogger(ConfigService.class);
43 private ElementConfig elementConfig = new ElementConfig();
45 private final MessageHandler handler;
46 private final MessageActivator messageActivator;
49 * Activate messages and service and create the element configuration.
51 * @param elementConfig the configuration
53 public void activateElement(@NonNull ElementConfig elementConfig) {
54 var listenerTopicParameters = new ElementTopicParameters(elementConfig.getTopicParameterGroup());
56 var publisherTopicParameters = new ElementTopicParameters(elementConfig.getTopicParameterGroup());
57 publisherTopicParameters.setTopic(elementConfig.getTopicParameterGroup().getPublisherTopic());
59 var parameters = new TopicParameterGroup();
60 parameters.setTopicSinks(List.of(publisherTopicParameters));
61 parameters.setTopicSources(List.of(listenerTopicParameters));
63 if (!parameters.isValid()) {
64 throw new AutomationCompositionRuntimeException(Response.Status.BAD_REQUEST,
65 "Validation failed for topic parameter group. Kafka config not activated");
68 if (messageActivator.isAlive()) {
69 throw new AutomationCompositionRuntimeException(Response.Status.CONFLICT,
70 "Service Manager already running, cannot add Topic endpoint management");
73 handler.active(elementConfig);
74 messageActivator.activate(parameters);
75 this.elementConfig = elementConfig;
77 LOGGER.info("Messages and service activated");
81 * Fetch element configuration.
83 * @return element configuration present
85 public ElementConfig getElementConfig() {
90 * Deactivate messages and service and delete the element config.
92 public void deleteConfig() {
93 handler.deactivateElement();
94 messageActivator.deactivate();
95 elementConfig = new ElementConfig();
96 LOGGER.info("Messages and service deactivated");