Sho SHIMIZU

Implement event notification of resource event

When a resource is registered or unregistered, a resource event is notified

Change-Id: I40e66761966ef2126366424a14bb3193fc850e5a
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.net.newresource;
17 +
18 +import com.google.common.annotations.Beta;
19 +import org.onosproject.event.AbstractEvent;
20 +
21 +import static com.google.common.base.Preconditions.checkNotNull;
22 +
23 +/**
24 + * Describes an event related to a resource.
25 + */
26 +@Beta
27 +public final class ResourceEvent extends AbstractEvent<ResourceEvent.Type, ResourcePath> {
28 +
29 + /**
30 + * Type of resource events.
31 + */
32 + @Beta
33 + public enum Type {
34 + /**
35 + * Signifies that a new resource has been detected.
36 + */
37 + RESOURCE_ADDED,
38 +
39 + /**
40 + * Signifies that a resource has been removed.
41 + */
42 + RESOURCE_REMOVED
43 + }
44 +
45 + /**
46 + * Create a resource event.
47 + *
48 + * @param type type of resource event
49 + * @param subject subject of resource event
50 + */
51 + public ResourceEvent(Type type, ResourcePath subject) {
52 + super(checkNotNull(type), checkNotNull(subject));
53 + }
54 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.net.newresource;
17 +
18 +import com.google.common.annotations.Beta;
19 +import org.onosproject.event.EventListener;
20 +
21 +/**
22 + * Entity capable of receiving resource related events.
23 + */
24 +@Beta
25 +public interface ResourceListener extends EventListener<ResourceEvent> {
26 +}
...@@ -17,6 +17,7 @@ package org.onosproject.net.newresource; ...@@ -17,6 +17,7 @@ package org.onosproject.net.newresource;
17 17
18 import com.google.common.annotations.Beta; 18 import com.google.common.annotations.Beta;
19 import com.google.common.collect.ImmutableList; 19 import com.google.common.collect.ImmutableList;
20 +import org.onosproject.event.ListenerService;
20 21
21 import java.util.Arrays; 22 import java.util.Arrays;
22 import java.util.Collection; 23 import java.util.Collection;
...@@ -29,7 +30,7 @@ import static com.google.common.base.Preconditions.checkNotNull; ...@@ -29,7 +30,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
29 * Service for allocating/releasing resource(s) and retrieving allocation(s) and availability. 30 * Service for allocating/releasing resource(s) and retrieving allocation(s) and availability.
30 */ 31 */
31 @Beta 32 @Beta
32 -public interface ResourceService { 33 +public interface ResourceService extends ListenerService<ResourceEvent, ResourceListener> {
33 /** 34 /**
34 * Allocates the specified resource to the specified user. 35 * Allocates the specified resource to the specified user.
35 * 36 *
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
16 package org.onosproject.net.newresource; 16 package org.onosproject.net.newresource;
17 17
18 import com.google.common.annotations.Beta; 18 import com.google.common.annotations.Beta;
19 +import org.onosproject.store.Store;
19 20
20 import java.util.Collection; 21 import java.util.Collection;
21 import java.util.List; 22 import java.util.List;
...@@ -25,7 +26,7 @@ import java.util.Optional; ...@@ -25,7 +26,7 @@ import java.util.Optional;
25 * Service for storing resource and consumer information. 26 * Service for storing resource and consumer information.
26 */ 27 */
27 @Beta 28 @Beta
28 -public interface ResourceStore { 29 +public interface ResourceStore extends Store<ResourceEvent, ResourceStoreDelegate> {
29 30
30 /** 31 /**
31 * Registers the resources in transactional way. 32 * Registers the resources in transactional way.
......
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.net.newresource;
17 +
18 +import org.onosproject.store.StoreDelegate;
19 +
20 +/**
21 + * Resource store delegate abstraction.
22 + */
23 +public interface ResourceStoreDelegate extends StoreDelegate<ResourceEvent> {
24 +}
...@@ -18,16 +18,22 @@ package org.onosproject.net.newresource.impl; ...@@ -18,16 +18,22 @@ package org.onosproject.net.newresource.impl;
18 import com.google.common.annotations.Beta; 18 import com.google.common.annotations.Beta;
19 import com.google.common.collect.ImmutableList; 19 import com.google.common.collect.ImmutableList;
20 import com.google.common.collect.Lists; 20 import com.google.common.collect.Lists;
21 +import org.apache.felix.scr.annotations.Activate;
21 import org.apache.felix.scr.annotations.Component; 22 import org.apache.felix.scr.annotations.Component;
23 +import org.apache.felix.scr.annotations.Deactivate;
22 import org.apache.felix.scr.annotations.Reference; 24 import org.apache.felix.scr.annotations.Reference;
23 import org.apache.felix.scr.annotations.ReferenceCardinality; 25 import org.apache.felix.scr.annotations.ReferenceCardinality;
24 import org.apache.felix.scr.annotations.Service; 26 import org.apache.felix.scr.annotations.Service;
27 +import org.onosproject.event.AbstractListenerManager;
25 import org.onosproject.net.newresource.ResourceAdminService; 28 import org.onosproject.net.newresource.ResourceAdminService;
26 import org.onosproject.net.newresource.ResourceAllocation; 29 import org.onosproject.net.newresource.ResourceAllocation;
27 import org.onosproject.net.newresource.ResourceConsumer; 30 import org.onosproject.net.newresource.ResourceConsumer;
31 +import org.onosproject.net.newresource.ResourceEvent;
32 +import org.onosproject.net.newresource.ResourceListener;
28 import org.onosproject.net.newresource.ResourceService; 33 import org.onosproject.net.newresource.ResourceService;
29 import org.onosproject.net.newresource.ResourcePath; 34 import org.onosproject.net.newresource.ResourcePath;
30 import org.onosproject.net.newresource.ResourceStore; 35 import org.onosproject.net.newresource.ResourceStore;
36 +import org.onosproject.net.newresource.ResourceStoreDelegate;
31 37
32 import java.util.ArrayList; 38 import java.util.ArrayList;
33 import java.util.Collection; 39 import java.util.Collection;
...@@ -44,11 +50,26 @@ import static com.google.common.base.Preconditions.checkNotNull; ...@@ -44,11 +50,26 @@ import static com.google.common.base.Preconditions.checkNotNull;
44 @Component(immediate = true) 50 @Component(immediate = true)
45 @Service 51 @Service
46 @Beta 52 @Beta
47 -public final class ResourceManager implements ResourceService, ResourceAdminService { 53 +public final class ResourceManager extends AbstractListenerManager<ResourceEvent, ResourceListener>
54 + implements ResourceService, ResourceAdminService {
48 55
49 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 56 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
50 protected ResourceStore store; 57 protected ResourceStore store;
51 58
59 + private final ResourceStoreDelegate delegate = new InternalStoreDelegate();
60 +
61 + @Activate
62 + public void activate() {
63 + store.setDelegate(delegate);
64 + eventDispatcher.addSink(ResourceEvent.class, listenerRegistry);
65 + }
66 +
67 + @Deactivate
68 + public void deactivate() {
69 + store.unsetDelegate(delegate);
70 + eventDispatcher.addSink(ResourceEvent.class, listenerRegistry);
71 + }
72 +
52 @Override 73 @Override
53 public List<ResourceAllocation> allocate(ResourceConsumer consumer, 74 public List<ResourceAllocation> allocate(ResourceConsumer consumer,
54 List<ResourcePath> resources) { 75 List<ResourcePath> resources) {
...@@ -161,4 +182,11 @@ public final class ResourceManager implements ResourceService, ResourceAdminServ ...@@ -161,4 +182,11 @@ public final class ResourceManager implements ResourceService, ResourceAdminServ
161 List<ResourcePath> resources = Lists.transform(children, x -> ResourcePath.child(parent, x)); 182 List<ResourcePath> resources = Lists.transform(children, x -> ResourcePath.child(parent, x));
162 return store.unregister(resources); 183 return store.unregister(resources);
163 } 184 }
185 +
186 + private class InternalStoreDelegate implements ResourceStoreDelegate {
187 + @Override
188 + public void notify(ResourceEvent event) {
189 + post(event);
190 + }
191 + }
164 } 192 }
......
...@@ -19,6 +19,7 @@ import com.google.common.collect.ImmutableList; ...@@ -19,6 +19,7 @@ import com.google.common.collect.ImmutableList;
19 import org.onlab.packet.MplsLabel; 19 import org.onlab.packet.MplsLabel;
20 import org.onosproject.net.newresource.ResourceAllocation; 20 import org.onosproject.net.newresource.ResourceAllocation;
21 import org.onosproject.net.newresource.ResourceConsumer; 21 import org.onosproject.net.newresource.ResourceConsumer;
22 +import org.onosproject.net.newresource.ResourceListener;
22 import org.onosproject.net.newresource.ResourcePath; 23 import org.onosproject.net.newresource.ResourcePath;
23 import org.onosproject.net.newresource.ResourceService; 24 import org.onosproject.net.newresource.ResourceService;
24 25
...@@ -97,4 +98,10 @@ class MockResourceService implements ResourceService { ...@@ -97,4 +98,10 @@ class MockResourceService implements ResourceService {
97 public boolean isAvailable(ResourcePath resource) { 98 public boolean isAvailable(ResourcePath resource) {
98 return true; 99 return true;
99 } 100 }
101 +
102 + @Override
103 + public void addListener(ResourceListener listener) {}
104 +
105 + @Override
106 + public void removeListener(ResourceListener listener) {}
100 } 107 }
......
...@@ -22,8 +22,11 @@ import org.apache.felix.scr.annotations.Reference; ...@@ -22,8 +22,11 @@ import org.apache.felix.scr.annotations.Reference;
22 import org.apache.felix.scr.annotations.ReferenceCardinality; 22 import org.apache.felix.scr.annotations.ReferenceCardinality;
23 import org.apache.felix.scr.annotations.Service; 23 import org.apache.felix.scr.annotations.Service;
24 import org.onosproject.net.newresource.ResourceConsumer; 24 import org.onosproject.net.newresource.ResourceConsumer;
25 +import org.onosproject.net.newresource.ResourceEvent;
25 import org.onosproject.net.newresource.ResourcePath; 26 import org.onosproject.net.newresource.ResourcePath;
26 import org.onosproject.net.newresource.ResourceStore; 27 import org.onosproject.net.newresource.ResourceStore;
28 +import org.onosproject.net.newresource.ResourceStoreDelegate;
29 +import org.onosproject.store.AbstractStore;
27 import org.onosproject.store.serializers.KryoNamespaces; 30 import org.onosproject.store.serializers.KryoNamespaces;
28 import org.onosproject.store.service.ConsistentMap; 31 import org.onosproject.store.service.ConsistentMap;
29 import org.onosproject.store.service.Serializer; 32 import org.onosproject.store.service.Serializer;
...@@ -47,6 +50,7 @@ import java.util.stream.Collectors; ...@@ -47,6 +50,7 @@ import java.util.stream.Collectors;
47 50
48 import static com.google.common.base.Preconditions.checkArgument; 51 import static com.google.common.base.Preconditions.checkArgument;
49 import static com.google.common.base.Preconditions.checkNotNull; 52 import static com.google.common.base.Preconditions.checkNotNull;
53 +import static org.onosproject.net.newresource.ResourceEvent.Type.*;
50 54
51 /** 55 /**
52 * Implementation of ResourceStore using TransactionalMap. 56 * Implementation of ResourceStore using TransactionalMap.
...@@ -54,7 +58,8 @@ import static com.google.common.base.Preconditions.checkNotNull; ...@@ -54,7 +58,8 @@ import static com.google.common.base.Preconditions.checkNotNull;
54 @Component(immediate = true) 58 @Component(immediate = true)
55 @Service 59 @Service
56 @Beta 60 @Beta
57 -public class ConsistentResourceStore implements ResourceStore { 61 +public class ConsistentResourceStore extends AbstractStore<ResourceEvent, ResourceStoreDelegate>
62 + implements ResourceStore {
58 private static final Logger log = LoggerFactory.getLogger(ConsistentResourceStore.class); 63 private static final Logger log = LoggerFactory.getLogger(ConsistentResourceStore.class);
59 64
60 private static final String CONSUMER_MAP = "onos-resource-consumers"; 65 private static final String CONSUMER_MAP = "onos-resource-consumers";
...@@ -116,7 +121,15 @@ public class ConsistentResourceStore implements ResourceStore { ...@@ -116,7 +121,15 @@ public class ConsistentResourceStore implements ResourceStore {
116 } 121 }
117 } 122 }
118 123
119 - return tx.commit(); 124 + boolean success = tx.commit();
125 + if (success) {
126 + List<ResourceEvent> events = resources.stream()
127 + .filter(x -> x.parent().isPresent())
128 + .map(x -> new ResourceEvent(RESOURCE_ADDED, x))
129 + .collect(Collectors.toList());
130 + notifyDelegate(events);
131 + }
132 + return success;
120 } 133 }
121 134
122 @Override 135 @Override
...@@ -147,7 +160,15 @@ public class ConsistentResourceStore implements ResourceStore { ...@@ -147,7 +160,15 @@ public class ConsistentResourceStore implements ResourceStore {
147 } 160 }
148 } 161 }
149 162
150 - return tx.commit(); 163 + boolean success = tx.commit();
164 + if (success) {
165 + List<ResourceEvent> events = resources.stream()
166 + .filter(x -> x.parent().isPresent())
167 + .map(x -> new ResourceEvent(RESOURCE_REMOVED, x))
168 + .collect(Collectors.toList());
169 + notifyDelegate(events);
170 + }
171 + return success;
151 } 172 }
152 173
153 @Override 174 @Override
......