Skip to content

Commit 3199de6

Browse files
wilderrodriguesspark404
authored andcommitted
Fixes on Contrail and Mon InMemory plugins; adding comments about the changes.
Signed-off-by: Hugo Trippaers <htrippaers@schubergphilis.com>
1 parent cbea6d2 commit 3199de6

11 files changed

Lines changed: 567 additions & 181 deletions

File tree

plugins/event-bus/inmemory/src/org/apache/cloudstack/mom/inmemory/InMemoryEventBus.java

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,36 @@
2626
import javax.ejb.Local;
2727
import javax.naming.ConfigurationException;
2828

29-
import com.cloud.utils.Pair;
30-
import org.apache.log4j.Logger;
31-
3229
import org.apache.cloudstack.framework.events.Event;
3330
import org.apache.cloudstack.framework.events.EventBus;
3431
import org.apache.cloudstack.framework.events.EventBusException;
3532
import org.apache.cloudstack.framework.events.EventSubscriber;
3633
import org.apache.cloudstack.framework.events.EventTopic;
34+
import org.apache.log4j.Logger;
3735

36+
import com.cloud.utils.Pair;
3837
import com.cloud.utils.component.ManagerBase;
3938

4039
@Local(value = EventBus.class)
4140
public class InMemoryEventBus extends ManagerBase implements EventBus {
4241

43-
private String name;
4442
private static final Logger s_logger = Logger.getLogger(InMemoryEventBus.class);
45-
private static ConcurrentHashMap<UUID, Pair<EventTopic, EventSubscriber>> s_subscribers;
43+
44+
private final static ConcurrentHashMap<UUID, Pair<EventTopic, EventSubscriber>> subscribers;
45+
46+
static {
47+
subscribers = new ConcurrentHashMap<UUID, Pair<EventTopic, EventSubscriber>>();
48+
}
4649

4750
@Override
4851
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
49-
s_subscribers = new ConcurrentHashMap<UUID, Pair<EventTopic, EventSubscriber>>();
52+
_name = name;
5053
return true;
5154
}
5255

5356
@Override
5457
public void setName(String name) {
55-
this.name = name;
58+
_name = name;
5659
}
5760

5861
@Override
@@ -62,29 +65,35 @@ public UUID subscribe(EventTopic topic, EventSubscriber subscriber) throws Event
6265
}
6366
UUID subscriberId = UUID.randomUUID();
6467

65-
s_subscribers.put(subscriberId, new Pair(topic, subscriber));
68+
subscribers.put(subscriberId, new Pair<EventTopic, EventSubscriber>(topic, subscriber));
6669
return subscriberId;
6770
}
6871

6972
@Override
7073
public void unsubscribe(UUID subscriberId, EventSubscriber subscriber) throws EventBusException {
71-
if (s_subscribers != null && s_subscribers.isEmpty()) {
74+
if (subscriberId == null) {
75+
throw new EventBusException("Cannot unregister a null subscriberId.");
76+
}
77+
78+
if (subscribers.isEmpty()) {
7279
throw new EventBusException("There are no registered subscribers to unregister.");
7380
}
74-
if (s_subscribers.get(subscriberId) == null) {
81+
82+
if (!subscribers.containsKey(subscriberId)) {
7583
throw new EventBusException("No subscriber found with subscriber id " + subscriberId);
84+
} else {
85+
subscribers.remove(subscriberId);
7686
}
77-
s_subscribers.remove(subscriberId);
7887
}
7988

8089
@Override
8190
public void publish(Event event) throws EventBusException {
82-
if (s_subscribers == null || s_subscribers.isEmpty()) {
91+
if (subscribers == null || subscribers.isEmpty()) {
8392
return; // no subscriber to publish to, so just return
8493
}
8594

86-
for (UUID subscriberId : s_subscribers.keySet()) {
87-
Pair<EventTopic, EventSubscriber> subscriberDetails = s_subscribers.get(subscriberId);
95+
for (UUID subscriberId : subscribers.keySet()) {
96+
Pair<EventTopic, EventSubscriber> subscriberDetails = subscribers.get(subscriberId);
8897
// if the event matches subscribers interested event topic then call back the subscriber with the event
8998
if (isEventMatchesTopic(event, subscriberDetails.first())) {
9099
EventSubscriber subscriber = subscriberDetails.second();
@@ -108,6 +117,10 @@ public boolean stop() {
108117
return true;
109118
}
110119

120+
public int totalSubscribers() {
121+
return subscribers.size();
122+
}
123+
111124
private String replaceNullWithWildcard(String key) {
112125
if (key == null || key.isEmpty()) {
113126
return "*";
@@ -122,42 +135,42 @@ private boolean isEventMatchesTopic(Event event, EventTopic topic) {
122135
eventTopicSource = eventTopicSource.replace(".", "-");
123136
String eventSource = replaceNullWithWildcard(event.getEventSource());
124137
eventSource = eventSource.replace(".", "-");
125-
if (eventTopicSource != "*" && eventSource != "*" && !eventTopicSource.equalsIgnoreCase(eventSource)) {
138+
if (!eventTopicSource.equals("*") && !eventSource.equals("*") && !eventTopicSource.equalsIgnoreCase(eventSource)) {
126139
return false;
127140
}
128141

129142
String eventTopicCategory = replaceNullWithWildcard(topic.getEventCategory());
130143
eventTopicCategory = eventTopicCategory.replace(".", "-");
131144
String eventCategory = replaceNullWithWildcard(event.getEventCategory());
132145
eventCategory = eventCategory.replace(".", "-");
133-
if (eventTopicCategory != "*" && eventCategory != "*" && !eventTopicCategory.equalsIgnoreCase(eventCategory)) {
146+
if (!eventTopicCategory.equals("*") && !eventCategory.equals("*") && !eventTopicCategory.equalsIgnoreCase(eventCategory)) {
134147
return false;
135148
}
136149

137150
String eventTopicType = replaceNullWithWildcard(topic.getEventType());
138151
eventTopicType = eventTopicType.replace(".", "-");
139152
String eventType = replaceNullWithWildcard(event.getEventType());
140153
eventType = eventType.replace(".", "-");
141-
if (eventTopicType != "*" && eventType != "*" && !eventTopicType.equalsIgnoreCase(eventType)) {
154+
if (!eventTopicType.equals("*") && !eventType.equals("*") && !eventTopicType.equalsIgnoreCase(eventType)) {
142155
return false;
143156
}
144157

145158
String eventTopicResourceType = replaceNullWithWildcard(topic.getResourceType());
146159
eventTopicResourceType = eventTopicResourceType.replace(".", "-");
147160
String resourceType = replaceNullWithWildcard(event.getResourceType());
148161
resourceType = resourceType.replace(".", "-");
149-
if (eventTopicResourceType != "*" && resourceType != "*" && !eventTopicResourceType.equalsIgnoreCase(resourceType)) {
162+
if (!eventTopicResourceType.equals("*") && !resourceType.equals("*") && !eventTopicResourceType.equalsIgnoreCase(resourceType)) {
150163
return false;
151164
}
152165

153166
String resourceUuid = replaceNullWithWildcard(event.getResourceUUID());
154167
resourceUuid = resourceUuid.replace(".", "-");
155168
String eventTopicresourceUuid = replaceNullWithWildcard(topic.getResourceUUID());
156169
eventTopicresourceUuid = eventTopicresourceUuid.replace(".", "-");
157-
if (resourceUuid != "*" && eventTopicresourceUuid != "*" && !resourceUuid.equalsIgnoreCase(eventTopicresourceUuid)) {
170+
if (!resourceUuid.equals("*") && !eventTopicresourceUuid.equals("*") && !resourceUuid.equalsIgnoreCase(eventTopicresourceUuid)) {
158171
return false;
159172
}
160173

161174
return true;
162175
}
163-
}
176+
}
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.cloudstack.mom.inmemory;
21+
22+
import static org.junit.Assert.assertNotNull;
23+
import static org.junit.Assert.assertTrue;
24+
import static org.mockito.Mockito.mock;
25+
import static org.mockito.Mockito.times;
26+
import static org.mockito.Mockito.verify;
27+
28+
import java.util.UUID;
29+
30+
import org.apache.cloudstack.framework.events.Event;
31+
import org.apache.cloudstack.framework.events.EventBusException;
32+
import org.apache.cloudstack.framework.events.EventSubscriber;
33+
import org.apache.cloudstack.framework.events.EventTopic;
34+
import org.junit.Test;
35+
36+
import com.cloud.utils.UuidUtils;
37+
38+
public class InMemoryEventBusTest {
39+
40+
@Test
41+
public void testConfigure() throws Exception {
42+
String name = "test001";
43+
44+
InMemoryEventBus bus = new InMemoryEventBus();
45+
boolean success = bus.configure(name, null);
46+
47+
assertTrue(success);
48+
assertTrue(name.equals(bus.getName()));
49+
}
50+
51+
@Test
52+
public void testSubscribe() throws Exception {
53+
EventTopic topic = mock(EventTopic.class);
54+
EventSubscriber subscriber = mock(EventSubscriber.class);
55+
56+
InMemoryEventBus bus = new InMemoryEventBus();
57+
58+
UUID uuid = bus.subscribe(topic, subscriber);
59+
assertNotNull(uuid);
60+
61+
String uuidStr = uuid.toString();
62+
assertTrue(UuidUtils.validateUUID(uuidStr));
63+
assertTrue(bus.totalSubscribers() == 1);
64+
65+
bus.unsubscribe(uuid, subscriber);
66+
assertTrue(bus.totalSubscribers() == 0);
67+
}
68+
69+
@Test(expected = EventBusException.class)
70+
public void testSubscribeFailTopic() throws Exception {
71+
EventSubscriber subscriber = mock(EventSubscriber.class);
72+
73+
InMemoryEventBus bus = new InMemoryEventBus();
74+
75+
bus.subscribe(null, subscriber);
76+
}
77+
78+
@Test(expected = EventBusException.class)
79+
public void testSubscribeFailSubscriber() throws Exception {
80+
EventTopic topic = mock(EventTopic.class);
81+
82+
InMemoryEventBus bus = new InMemoryEventBus();
83+
84+
bus.subscribe(topic, null);
85+
}
86+
87+
@Test
88+
public void testUnsubscribe() throws Exception {
89+
EventTopic topic = mock(EventTopic.class);
90+
EventSubscriber subscriber = mock(EventSubscriber.class);
91+
92+
InMemoryEventBus bus = new InMemoryEventBus();
93+
94+
UUID uuid = bus.subscribe(topic, subscriber);
95+
assertNotNull(uuid);
96+
97+
String uuidStr = uuid.toString();
98+
99+
assertTrue(UuidUtils.validateUUID(uuidStr));
100+
assertTrue(bus.totalSubscribers() == 1);
101+
//
102+
bus.unsubscribe(uuid, subscriber);
103+
assertTrue(bus.totalSubscribers() == 0);
104+
}
105+
106+
@Test(expected = EventBusException.class)
107+
public void testUnsubscribeFailEmpty() throws Exception {
108+
UUID uuid = UUID.randomUUID();
109+
110+
InMemoryEventBus bus = new InMemoryEventBus();
111+
bus.unsubscribe(uuid, null);
112+
}
113+
114+
@Test(expected = EventBusException.class)
115+
public void testUnsubscribeFailNull() throws Exception {
116+
InMemoryEventBus bus = new InMemoryEventBus();
117+
bus.unsubscribe(null, null);
118+
}
119+
120+
@Test(expected = EventBusException.class)
121+
public void testUnsubscribeFailWrongUUID() throws Exception {
122+
EventSubscriber subscriber = mock(EventSubscriber.class);
123+
InMemoryEventBus bus = new InMemoryEventBus();
124+
UUID uuid = UUID.randomUUID();
125+
126+
bus.unsubscribe(uuid, subscriber);
127+
}
128+
129+
@Test
130+
public void testPublish() throws Exception {
131+
EventTopic topic = mock(EventTopic.class);
132+
EventSubscriber subscriber = mock(EventSubscriber.class);
133+
Event event = mock(Event.class);
134+
135+
InMemoryEventBus bus = new InMemoryEventBus();
136+
137+
UUID uuid = bus.subscribe(topic, subscriber);
138+
assertNotNull(uuid);
139+
140+
String uuidStr = uuid.toString();
141+
assertTrue(UuidUtils.validateUUID(uuidStr));
142+
assertTrue(bus.totalSubscribers() == 1);
143+
144+
bus.publish(event);
145+
146+
verify(subscriber, times(1)).onEvent(event);
147+
148+
bus.unsubscribe(uuid, subscriber);
149+
assertTrue(bus.totalSubscribers() == 0);
150+
}
151+
152+
@Test
153+
public void testPublishEmpty() throws Exception {
154+
EventSubscriber subscriber = mock(EventSubscriber.class);
155+
Event event = mock(Event.class);
156+
157+
InMemoryEventBus bus = new InMemoryEventBus();
158+
bus.publish(event);
159+
160+
verify(subscriber, times(0)).onEvent(event);
161+
}
162+
}

0 commit comments

Comments
 (0)