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