47279d47fa8df55934a55fb3c7592018d488aa1d
[policy/common.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  * ============LICENSE_END=========================================================
17  */
18
19 package org.onap.policy.common.endpoints.event.comm.bus;
20
21 import com.google.re2j.Pattern;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Properties;
26 import org.apache.commons.lang3.StringUtils;
27 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
28 import org.onap.policy.common.endpoints.event.comm.bus.internal.SingleThreadedKafkaTopicSource;
29 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
30 import org.onap.policy.common.endpoints.utils.KafkaPropertyUtils;
31 import org.onap.policy.common.endpoints.utils.PropertyUtils;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Factory of KAFKA Source Topics indexed by topic name.
37  */
38 class IndexedKafkaTopicSourceFactory implements KafkaTopicSourceFactory {
39     private static final Pattern COMMA_SPACE_PAT = Pattern.compile("\\s*,\\s*");
40     private static final String MISSING_TOPIC = "A topic must be provided";
41
42     /**
43      * Logger.
44      */
45     private static Logger logger = LoggerFactory.getLogger(IndexedKafkaTopicSourceFactory.class);
46
47     /**
48      * KAFKA Topic Name Index.
49      */
50     protected HashMap<String, KafkaTopicSource> kafkaTopicSources = new HashMap<>();
51
52     @Override
53     public KafkaTopicSource build(BusTopicParams busTopicParams) {
54         if (busTopicParams.getServers() == null || busTopicParams.getServers().isEmpty()) {
55             throw new IllegalArgumentException("KAFKA Server(s) must be provided");
56         }
57
58         if (busTopicParams.getTopic() == null || busTopicParams.getTopic().isEmpty()) {
59             throw new IllegalArgumentException(MISSING_TOPIC);
60         }
61
62         synchronized (this) {
63             if (kafkaTopicSources.containsKey(busTopicParams.getTopic())) {
64                 return kafkaTopicSources.get(busTopicParams.getTopic());
65             }
66
67             var kafkaTopicSource = makeSource(busTopicParams);
68             kafkaTopicSources.put(busTopicParams.getTopic(), kafkaTopicSource);
69
70             return kafkaTopicSource;
71         }
72     }
73
74     @Override
75     public List<KafkaTopicSource> build(Properties properties) {
76
77         String readTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_KAFKA_SOURCE_TOPICS);
78         if (StringUtils.isBlank(readTopics)) {
79             logger.info("{}: no topic for KAFKA Source", this);
80             return new ArrayList<>();
81         }
82
83         List<KafkaTopicSource> newKafkaTopicSources = new ArrayList<>();
84         synchronized (this) {
85             for (String topic : COMMA_SPACE_PAT.split(readTopics)) {
86                 addTopic(newKafkaTopicSources, topic, properties);
87             }
88         }
89         return newKafkaTopicSources;
90     }
91
92     @Override
93     public KafkaTopicSource build(List<String> servers, String topic) {
94         return this.build(BusTopicParams.builder()
95                 .servers(servers)
96                 .topic(topic)
97                 .managed(true)
98                 .useHttps(false).build());
99     }
100
101     private void addTopic(List<KafkaTopicSource> newKafkaTopicSources, String topic, Properties properties) {
102         if (this.kafkaTopicSources.containsKey(topic)) {
103             newKafkaTopicSources.add(this.kafkaTopicSources.get(topic));
104             return;
105         }
106
107         String topicPrefix = PolicyEndPointProperties.PROPERTY_KAFKA_SOURCE_TOPICS + "." + topic;
108
109         var props = new PropertyUtils(properties, topicPrefix,
110             (name, value, ex) -> logger.warn("{}: {} {} is in invalid format for topic {} ", this, name, value, topic));
111
112         String servers = properties.getProperty(topicPrefix + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX);
113         if (StringUtils.isBlank(servers)) {
114             logger.error("{}: no KAFKA servers configured for sink {}", this, topic);
115             return;
116         }
117
118         var kafkaTopicSource = this.build(KafkaPropertyUtils.makeBuilder(props, topic, servers)
119                 .build());
120
121         newKafkaTopicSources.add(kafkaTopicSource);
122     }
123
124     /**
125      * Makes a new source.
126      *
127      * @param busTopicParams parameters to use to configure the source
128      * @return a new source
129      */
130     protected KafkaTopicSource makeSource(BusTopicParams busTopicParams) {
131         return new SingleThreadedKafkaTopicSource(busTopicParams);
132     }
133
134     @Override
135     public void destroy(String topic) {
136
137         if (topic == null || topic.isEmpty()) {
138             throw new IllegalArgumentException(MISSING_TOPIC);
139         }
140
141         KafkaTopicSource kafkaTopicSource;
142
143         synchronized (this) {
144             if (!kafkaTopicSources.containsKey(topic)) {
145                 return;
146             }
147
148             kafkaTopicSource = kafkaTopicSources.remove(topic);
149         }
150
151         kafkaTopicSource.shutdown();
152     }
153
154     @Override
155     public void destroy() {
156         List<KafkaTopicSource> readers = this.inventory();
157         for (KafkaTopicSource reader : readers) {
158             reader.shutdown();
159         }
160
161         synchronized (this) {
162             this.kafkaTopicSources.clear();
163         }
164     }
165
166     @Override
167     public KafkaTopicSource get(String topic) {
168
169         if (topic == null || topic.isEmpty()) {
170             throw new IllegalArgumentException(MISSING_TOPIC);
171         }
172
173         synchronized (this) {
174             if (kafkaTopicSources.containsKey(topic)) {
175                 return kafkaTopicSources.get(topic);
176             } else {
177                 throw new IllegalStateException("KafkaTopiceSource for " + topic + " not found");
178             }
179         }
180     }
181
182     @Override
183     public synchronized List<KafkaTopicSource> inventory() {
184         return new ArrayList<>(this.kafkaTopicSources.values());
185     }
186
187     @Override
188     public String toString() {
189         return "IndexedKafkaTopicSourceFactory " + kafkaTopicSources.keySet();
190     }
191 }