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;
31 * Topic Factory implementation that indexes T instances in a hash table.
33 public abstract class TopicBaseHashedFactory<T extends Topic> implements TopicBaseFactory<T> {
35 protected static final String MISSING_TOPIC_MESSAGE = "A topic must be provided";
36 protected static final String MISSING_SERVERS_MESSAGE = "Servers must be provided";
41 protected final HashMap<String, T> endpoints = new HashMap<>();
44 * get the topic names.
46 * @param properties properties.
47 * @return list of topic names.
49 protected abstract List<String> getTopicNames(Properties properties);
52 * get the servers that this topic uses.
54 * @param topicName name.
55 * @param properties properties.
56 * @return list of servers.
58 protected abstract List<String> getServers(String topicName, Properties properties);
61 * is this topic managed?
63 * @param topicName name.
64 * @param properties properties.
67 protected abstract boolean isManaged(String topicName, Properties properties);
70 * construct an instance of an endpoint.
72 * @param servers servers,
74 * @return an instance of T.
76 protected abstract T build(List<String> servers, String topic);
82 public List<T> build(Properties properties) {
83 List<String> topicNames = getTopicNames(properties);
84 if (topicNames == null || topicNames.isEmpty()) {
85 return Collections.emptyList();
88 List<T> newEndpoints = new ArrayList<>();
90 for (String name : topicNames) {
91 if (this.endpoints.containsKey(name)) {
92 newEndpoints.add(this.endpoints.get(name));
96 newEndpoints.add(this.build(getServers(name, properties), name, isManaged(name, properties)));
106 public T build(List<String> servers, String topic, boolean managed) {
107 if (servers == null || servers.isEmpty()) {
108 throw new IllegalArgumentException(MISSING_SERVERS_MESSAGE);
111 if (topic == null || topic.isEmpty()) {
112 throw new IllegalArgumentException(MISSING_TOPIC_MESSAGE);
115 synchronized (this) {
116 if (this.endpoints.containsKey(topic)) {
117 return this.endpoints.get(topic);
120 T endpoint = build(servers, topic);
122 this.endpoints.put(topic, endpoint);
133 public void destroy(String topic) {
134 if (topic == null || topic.isEmpty()) {
135 throw new IllegalArgumentException(MISSING_TOPIC_MESSAGE);
139 synchronized (this) {
140 if (!this.endpoints.containsKey(topic)) {
144 endpoint = this.endpoints.remove(topic);
153 public void destroy() {
154 final List<T> snapshotEndpoints = this.inventory();
155 for (final T snapshot : snapshotEndpoints) {
159 synchronized (this) {
160 this.endpoints.clear();
168 public T get(String topic) {
169 if (topic == null || topic.isEmpty()) {
170 throw new IllegalArgumentException(MISSING_TOPIC_MESSAGE);
173 synchronized (this) {
174 if (this.endpoints.containsKey(topic)) {
175 return this.endpoints.get(topic);
177 throw new IllegalStateException(topic + " not found");
186 public List<T> inventory() {
187 return new ArrayList<>(this.endpoints.values());
194 public String toString() {
195 return "TopicBaseHashedFactory[ " + super.toString() + " ]";