091e46bf2e90f00bf83b36411b7cdb9596de984a
[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.List;
24 import org.onap.policy.common.endpoints.event.comm.bus.internal.TopicBase;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * No Operation topic endpoint.
30  */
31 public abstract class NoopTopicEndpoint extends TopicBase {
32
33     /**
34      * Logger.
35      */
36     private static Logger logger = LoggerFactory.getLogger(NoopTopicEndpoint.class);
37
38     /**
39      * Network logger.
40      */
41     private static final Logger netLogger = LoggerFactory.getLogger(NETWORK_LOGGER);
42
43     /**
44      * {@inheritDoc}.
45      */
46     public NoopTopicEndpoint(List<String> servers, String topic) {
47         super(servers, topic);
48     }
49
50     /**
51      *  I/O.
52      *
53      * @param message message.
54      * @return true if sucessful.
55      */
56     protected boolean io(String message) {
57
58         if (message == null || message.isEmpty()) {
59             throw new IllegalArgumentException("Message is empty");
60         }
61
62         if (!this.alive) {
63             throw new IllegalStateException(this + " is stopped");
64         }
65
66         try {
67             synchronized (this) {
68                 this.recentEvents.add(message);
69             }
70
71             netLogger.info("[OUT|{}|{}]{}{}", this.getTopicCommInfrastructure(), this.topic, System.lineSeparator(),
72                     message);
73
74             broadcast(message);
75         } catch (Exception e) {
76             logger.warn("{}: cannot send because of {}", this, e.getMessage(), e);
77             return false;
78         }
79
80         return true;
81     }
82
83     /**
84      * {@inheritDoc}.
85      */
86     @Override
87     public CommInfrastructure getTopicCommInfrastructure() {
88         return CommInfrastructure.NOOP;
89     }
90
91     /**
92      * {@inheritDoc}.
93      */
94     @Override
95     public boolean start() {
96         logger.info("{}: starting", this);
97
98         synchronized (this) {
99
100             if (this.alive) {
101                 return true;
102             }
103
104             if (locked) {
105                 throw new IllegalStateException(this + " is locked.");
106             }
107
108             this.alive = true;
109         }
110
111         return true;
112     }
113
114     /**
115      * {@inheritDoc}.
116      */
117     @Override
118     public boolean stop() {
119         logger.info("{}: stopping", this);
120
121         synchronized (this) {
122             this.alive = false;
123         }
124         return true;
125     }
126
127     /**
128      * {@inheritDoc}.
129      */
130     @Override
131     public void shutdown() {
132         logger.info("{}: shutdown", this);
133
134         this.stop();
135     }
136
137     /**
138      * {@inheritDoc}.
139      */
140     @Override
141     public String toString() {
142         return "NoopTopicEndpoint[" + super.toString() + "]";
143     }
144 }