98b6ed6f413f6d5a8fadd68956d9019665d7d8b8
[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.Arrays;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.Properties;
28 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
29 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
30
31 /**
32  * Noop Topic Factory.
33  */
34 public abstract class NoopTopicFactory<T extends NoopTopicEndpoint> extends TopicBaseHashedFactory<T> {
35
36     /**
37      * Get Topics Property Name.
38      *
39      * @return property name.
40      */
41     protected abstract String getTopicsPropertyName();
42
43     /**
44      * {@inheritDoc}.
45      */
46     @Override
47     protected List<String> getTopicNames(Properties properties) {
48         String topics = properties.getProperty(getTopicsPropertyName());
49         if (topics == null || topics.isEmpty()) {
50             return new ArrayList<>();
51         }
52
53         return Arrays.asList(topics.split("\\s*,\\s*"));
54     }
55
56     /**
57      * {@inheritDoc}.
58      */
59     @Override
60     protected List<String> getServers(String topicName, Properties properties) {
61         String servers =
62             properties.getProperty(getTopicsPropertyName() + "." + topicName
63                 + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX);
64
65         if (servers == null || servers.isEmpty()) {
66             servers = CommInfrastructure.NOOP.toString();
67         }
68
69         return new ArrayList<>(Arrays.asList(servers.split("\\s*,\\s*")));
70     }
71
72     /**
73      * {@inheritDoc}.
74      */
75     @Override
76     protected boolean isManaged(String topicName, Properties properties) {
77         String managedString =
78             properties.getProperty(getTopicsPropertyName()
79                 + "." + topicName + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
80
81         boolean managed = true;
82         if (managedString != null && !managedString.isEmpty()) {
83             managed = Boolean.parseBoolean(managedString);
84         }
85
86         return managed;
87     }
88
89     /**
90      * {@inheritDoc}.
91      */
92     @Override
93     public T build(List<String> serverList, String topic, boolean managed) {
94         List<String> servers;
95         if (serverList == null || serverList.isEmpty()) {
96             servers = Collections.singletonList(CommInfrastructure.NOOP.toString());
97         } else {
98             servers = serverList;
99         }
100
101         return super.build(servers, topic, managed);
102     }
103
104     /**
105      * {@inheritDoc}.
106      */
107     @Override
108     public String toString() {
109         return "NoopTopicFactory[ " + super.toString() + " ]";
110     }
111 }
112