f958bd017daf88c4c17e0bf9beb3925d1b11dc20
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.common.endpoints.event.comm.bus;
22
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
30 /**
31  * Topic Factory implementation that indexes T instances in a hash table.
32  */
33 public abstract class TopicBaseHashedFactory<T extends Topic> implements TopicBaseFactory<T> {
34
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";
37
38     /**
39      * endpoints.
40      */
41     protected final HashMap<String, T> endpoints = new HashMap<>();
42
43     /**
44      * get the topic names.
45      *
46      * @param properties properties.
47      * @return list of topic names.
48      */
49     protected abstract List<String> getTopicNames(Properties properties);
50
51     /**
52      * get the servers that this topic uses.
53      *
54      * @param topicName name.
55      * @param properties properties.
56      * @return list of servers.
57      */
58     protected abstract List<String> getServers(String topicName, Properties properties);
59
60     /**
61      * is this topic managed?
62      *
63      * @param topicName name.
64      * @param properties properties.
65      * @return if managed.
66      */
67     protected abstract boolean isManaged(String topicName, Properties properties);
68
69     /**
70      * construct an instance of an endpoint.
71      *
72      * @param servers servers,
73      * @param topic topic.
74      * @return an instance of T.
75      */
76     protected abstract T build(List<String> servers, String topic);
77
78     /**
79      * {@inheritDoc}.
80      */
81     @Override
82     public List<T> build(Properties properties) {
83         List<String> topicNames = getTopicNames(properties);
84         if (topicNames == null || topicNames.isEmpty()) {
85             return Collections.emptyList();
86         }
87
88         List<T> newEndpoints = new ArrayList<>();
89         synchronized (this) {
90             for (String name : topicNames) {
91                 if (this.endpoints.containsKey(name)) {
92                     newEndpoints.add(this.endpoints.get(name));
93                     continue;
94                 }
95
96                 newEndpoints.add(this.build(getServers(name, properties), name, isManaged(name, properties)));
97             }
98         }
99         return newEndpoints;
100     }
101
102     /**
103      * {@inheritDoc}.
104      */
105     @Override
106     public T build(List<String> servers, String topic, boolean managed) {
107         if (servers == null || servers.isEmpty()) {
108             throw new IllegalArgumentException(MISSING_SERVERS_MESSAGE);
109         }
110
111         if (topic == null || topic.isEmpty()) {
112             throw new IllegalArgumentException(MISSING_TOPIC_MESSAGE);
113         }
114
115         synchronized (this) {
116             if (this.endpoints.containsKey(topic)) {
117                 return this.endpoints.get(topic);
118             }
119
120             T endpoint = build(servers, topic);
121             if (managed) {
122                 this.endpoints.put(topic, endpoint);
123             }
124
125             return endpoint;
126         }
127     }
128
129     /**
130      * {@inheritDoc}.
131      */
132     @Override
133     public void destroy(String topic) {
134         if (topic == null || topic.isEmpty()) {
135             throw new IllegalArgumentException(MISSING_TOPIC_MESSAGE);
136         }
137
138         T endpoint;
139         synchronized (this) {
140             if (!this.endpoints.containsKey(topic)) {
141                 return;
142             }
143
144             endpoint = this.endpoints.remove(topic);
145         }
146         endpoint.shutdown();
147     }
148
149     /**
150      * {@inheritDoc}.
151      */
152     @Override
153     public void destroy() {
154         final List<T> snapshotEndpoints = this.inventory();
155         for (final T snapshot : snapshotEndpoints) {
156             snapshot.shutdown();
157         }
158
159         synchronized (this) {
160             this.endpoints.clear();
161         }
162     }
163
164     /**
165      * {@inheritDoc}.
166      */
167     @Override
168     public T get(String topic) {
169         if (topic == null || topic.isEmpty()) {
170             throw new IllegalArgumentException(MISSING_TOPIC_MESSAGE);
171         }
172
173         synchronized (this) {
174             if (this.endpoints.containsKey(topic)) {
175                 return this.endpoints.get(topic);
176             } else {
177                 throw new IllegalStateException(topic + " not found");
178             }
179         }
180     }
181
182     /**
183      * {@inheritDoc}.
184      */
185     @Override
186     public List<T> inventory() {
187         return new ArrayList<>(this.endpoints.values());
188     }
189
190     /**
191      * {@inheritDoc}.
192      */
193     @Override
194     public String toString() {
195         return "TopicBaseHashedFactory[ " + super.toString() + " ]";
196     }
197 }