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