833574a3ef73ee8124cca63688e5a1f758c5b431
[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  * ================================================================================
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      * Constructs the object.
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             if (!this.alive) {
98                 if (locked) {
99                     throw new IllegalStateException(this + " is locked.");
100                 }
101
102                 this.alive = true;
103             }
104         }
105
106         return true;
107     }
108
109     /**
110      * {@inheritDoc}.
111      */
112     @Override
113     public boolean stop() {
114         logger.info("{}: stopping", this);
115
116         synchronized (this) {
117             this.alive = false;
118         }
119         return true;
120     }
121
122     /**
123      * {@inheritDoc}.
124      */
125     @Override
126     public void shutdown() {
127         logger.info("{}: shutdown", this);
128
129         this.stop();
130     }
131
132     /**
133      * {@inheritDoc}.
134      */
135     @Override
136     public String toString() {
137         return "NoopTopicEndpoint[" + super.toString() + "]";
138     }
139 }