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