2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.common.endpoints.event.comm.bus;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Properties;
28 import org.onap.policy.common.endpoints.event.comm.Topic;
29 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
32 * Topic Factory implementation that indexes T instances in a hash table.
34 public abstract class TopicBaseHashedFactory<T extends Topic> implements TopicBaseFactory<T> {
36 protected static final String MISSING_TOPIC_MESSAGE = "A topic must be provided";
37 protected static final String MISSING_SERVERS_MESSAGE = "Servers must be provided";
42 protected final HashMap<String, T> endpoints = new HashMap<>();
45 * get the topic names.
47 * @param properties properties.
48 * @return list of topic names.
50 protected abstract List<String> getTopicNames(Properties properties);
53 * get the servers that this topic uses.
55 * @param topicName name.
56 * @param properties properties.
57 * @return list of servers.
59 protected abstract List<String> getServers(String topicName, Properties properties);
62 * is this topic managed?
64 * @param topicName name.
65 * @param properties properties.
68 protected abstract boolean isManaged(String topicName, Properties properties);
71 * construct an instance of an endpoint.
73 * @param servers servers,
75 * @return an instance of T.
77 protected abstract T build(List<String> servers, String topic);
83 public List<T> build(Properties properties) {
84 List<String> topicNames = getTopicNames(properties);
85 if (topicNames == null || topicNames.isEmpty()) {
86 return Collections.emptyList();
89 List<T> newEndpoints = new ArrayList<>();
91 for (String name : topicNames) {
92 if (this.endpoints.containsKey(name)) {
93 newEndpoints.add(this.endpoints.get(name));
97 newEndpoints.add(this.build(getServers(name, properties), name, isManaged(name, properties)));
107 public T build(BusTopicParams param) {
108 return this.build(param.getServers(), param.getTopic(), param.isManaged());
115 public T build(List<String> servers, String topic, boolean managed) {
116 if (servers == null || servers.isEmpty()) {
117 throw new IllegalArgumentException(MISSING_SERVERS_MESSAGE);
120 if (topic == null || topic.isEmpty()) {
121 throw new IllegalArgumentException(MISSING_TOPIC_MESSAGE);
124 synchronized (this) {
125 if (this.endpoints.containsKey(topic)) {
126 return this.endpoints.get(topic);
129 T endpoint = build(servers, topic);
131 this.endpoints.put(topic, endpoint);
142 public void destroy(String topic) {
143 if (topic == null || topic.isEmpty()) {
144 throw new IllegalArgumentException(MISSING_TOPIC_MESSAGE);
148 synchronized (this) {
149 if (!this.endpoints.containsKey(topic)) {
153 endpoint = this.endpoints.remove(topic);
162 public void destroy() {
163 final List<T> snapshotEndpoints = this.inventory();
164 for (final T snapshot : snapshotEndpoints) {
168 synchronized (this) {
169 this.endpoints.clear();
177 public T get(String topic) {
178 if (topic == null || topic.isEmpty()) {
179 throw new IllegalArgumentException(MISSING_TOPIC_MESSAGE);
182 synchronized (this) {
183 if (this.endpoints.containsKey(topic)) {
184 return this.endpoints.get(topic);
186 throw new IllegalStateException(topic + " not found");
195 public List<T> inventory() {
196 return new ArrayList<>(this.endpoints.values());
203 public String toString() {
204 return "TopicBaseHashedFactory[ " + super.toString() + " ]";