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