a2ad4d3df9bbf4740a53b646aea155cfc12d9533
[policy/common.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Samsung Electronics Co., Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.common.endpoints.event.comm.bus;
23
24 import java.util.List;
25 import org.onap.policy.common.endpoints.event.comm.bus.internal.TopicBase;
26 import org.onap.policy.common.utils.slf4j.LoggerFactoryWrapper;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * No Operation topic endpoint.
32  */
33 public abstract class NoopTopicEndpoint extends TopicBase {
34
35     /**
36      * Logger.
37      */
38     private static Logger logger = LoggerFactory.getLogger(NoopTopicEndpoint.class);
39
40     /**
41      * Network logger.
42      */
43     private static final Logger netLogger = LoggerFactoryWrapper.getNetworkLogger();
44
45     /**
46      * {@inheritDoc}.
47      */
48     public NoopTopicEndpoint(List<String> servers, String topic) {
49         super(servers, topic);
50     }
51
52     /**
53      *  I/O.
54      *
55      * @param message message.
56      * @return true if sucessful.
57      */
58     protected boolean io(String message) {
59
60         if (message == null || message.isEmpty()) {
61             throw new IllegalArgumentException("Message is empty");
62         }
63
64         if (!this.alive) {
65             throw new IllegalStateException(this + " is stopped");
66         }
67
68         try {
69             synchronized (this) {
70                 this.recentEvents.add(message);
71             }
72
73             netLogger.info("[OUT|{}|{}]{}{}", this.getTopicCommInfrastructure(), this.topic, System.lineSeparator(),
74                     message);
75
76             broadcast(message);
77         } catch (Exception e) {
78             logger.warn("{}: cannot send because of {}", this, e.getMessage(), e);
79             return false;
80         }
81
82         return true;
83     }
84
85     /**
86      * {@inheritDoc}.
87      */
88     @Override
89     public CommInfrastructure getTopicCommInfrastructure() {
90         return CommInfrastructure.NOOP;
91     }
92
93     /**
94      * {@inheritDoc}.
95      */
96     @Override
97     public boolean start() {
98         logger.info("{}: starting", this);
99
100         synchronized (this) {
101
102             if (this.alive) {
103                 return true;
104             }
105
106             if (locked) {
107                 throw new IllegalStateException(this + " is locked.");
108             }
109
110             this.alive = true;
111         }
112
113         return true;
114     }
115
116     /**
117      * {@inheritDoc}.
118      */
119     @Override
120     public boolean stop() {
121         logger.info("{}: stopping", this);
122
123         synchronized (this) {
124             this.alive = false;
125         }
126         return true;
127     }
128
129     /**
130      * {@inheritDoc}.
131      */
132     @Override
133     public void shutdown() {
134         logger.info("{}: shutdown", this);
135
136         this.stop();
137     }
138
139     /**
140      * {@inheritDoc}.
141      */
142     @Override
143     public String toString() {
144         return "NoopTopicEndpoint[" + super.toString() + "]";
145     }
146 }