ONOS-2690: Register a device as resource when it is added
Change-Id: I37f931665e06cb2415d569e61e8c6592faea485a
Showing
2 changed files
with
73 additions
and
0 deletions
| 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.impl; | ||
| 17 | + | ||
| 18 | +import org.onosproject.net.Device; | ||
| 19 | +import org.onosproject.net.DeviceId; | ||
| 20 | +import org.onosproject.net.device.DeviceEvent; | ||
| 21 | +import org.onosproject.net.device.DeviceListener; | ||
| 22 | +import org.onosproject.net.newresource.ResourceAdminService; | ||
| 23 | +import org.onosproject.net.newresource.ResourcePath; | ||
| 24 | + | ||
| 25 | +import java.util.concurrent.ExecutorService; | ||
| 26 | + | ||
| 27 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
| 28 | + | ||
| 29 | +/** | ||
| 30 | + * An implementation of DeviceListener registering devices as resources. | ||
| 31 | + */ | ||
| 32 | +final class ResourceDeviceListener implements DeviceListener { | ||
| 33 | + | ||
| 34 | + private final ResourceAdminService adminService; | ||
| 35 | + private final ExecutorService executor; | ||
| 36 | + | ||
| 37 | + /** | ||
| 38 | + * Creates an instance with the specified ResourceAdminService and ExecutorService. | ||
| 39 | + * | ||
| 40 | + * @param adminService instance invoked to register resources | ||
| 41 | + * @param executor executor used for processing resource registration | ||
| 42 | + */ | ||
| 43 | + ResourceDeviceListener(ResourceAdminService adminService, ExecutorService executor) { | ||
| 44 | + this.adminService = checkNotNull(adminService); | ||
| 45 | + this.executor = checkNotNull(executor); | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + @Override | ||
| 49 | + public void event(DeviceEvent event) { | ||
| 50 | + Device device = event.subject(); | ||
| 51 | + switch (event.type()) { | ||
| 52 | + case DEVICE_ADDED: | ||
| 53 | + registerDeviceResource(device); | ||
| 54 | + break; | ||
| 55 | + default: | ||
| 56 | + break; | ||
| 57 | + } | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + private void registerDeviceResource(Device device) { | ||
| 61 | + DeviceId deviceId = device.id(); | ||
| 62 | + executor.submit(() -> adminService.registerResources(new ResourcePath(ResourcePath.ROOT, deviceId))); | ||
| 63 | + } | ||
| 64 | +} |
| ... | @@ -21,6 +21,8 @@ import org.apache.felix.scr.annotations.Component; | ... | @@ -21,6 +21,8 @@ import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; | 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; | 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; | 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | +import org.onosproject.net.device.DeviceListener; | ||
| 25 | +import org.onosproject.net.device.DeviceService; | ||
| 24 | import org.onosproject.net.link.LinkListener; | 26 | import org.onosproject.net.link.LinkListener; |
| 25 | import org.onosproject.net.link.LinkService; | 27 | import org.onosproject.net.link.LinkService; |
| 26 | import org.onosproject.net.newresource.ResourceAdminService; | 28 | import org.onosproject.net.newresource.ResourceAdminService; |
| ... | @@ -41,20 +43,27 @@ public final class ResourceRegistrar { | ... | @@ -41,20 +43,27 @@ public final class ResourceRegistrar { |
| 41 | protected ResourceAdminService adminService; | 43 | protected ResourceAdminService adminService; |
| 42 | 44 | ||
| 43 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | 45 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 46 | + protected DeviceService deviceService; | ||
| 47 | + | ||
| 48 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
| 44 | protected LinkService linkService; | 49 | protected LinkService linkService; |
| 45 | 50 | ||
| 51 | + private DeviceListener deviceListener; | ||
| 46 | private LinkListener linkListener; | 52 | private LinkListener linkListener; |
| 47 | private final ExecutorService executor = | 53 | private final ExecutorService executor = |
| 48 | Executors.newSingleThreadExecutor(groupedThreads("onos/resource", "registrar")); | 54 | Executors.newSingleThreadExecutor(groupedThreads("onos/resource", "registrar")); |
| 49 | 55 | ||
| 50 | @Activate | 56 | @Activate |
| 51 | public void activate() { | 57 | public void activate() { |
| 58 | + deviceListener = new ResourceDeviceListener(adminService, executor); | ||
| 59 | + deviceService.addListener(deviceListener); | ||
| 52 | linkListener = new ResourceLinkListener(adminService, executor); | 60 | linkListener = new ResourceLinkListener(adminService, executor); |
| 53 | linkService.addListener(linkListener); | 61 | linkService.addListener(linkListener); |
| 54 | } | 62 | } |
| 55 | 63 | ||
| 56 | @Deactivate | 64 | @Deactivate |
| 57 | public void deactivate() { | 65 | public void deactivate() { |
| 66 | + deviceService.removeListener(deviceListener); | ||
| 58 | linkService.removeListener(linkListener); | 67 | linkService.removeListener(linkListener); |
| 59 | } | 68 | } |
| 60 | } | 69 | } | ... | ... |
-
Please register or login to post a comment