blob: bdd2f67548a568ca94a9dddd3fcc679d0b2b4c10 [file] [log] [blame]
Sho SHIMIZU47e7b802015-08-18 08:54:30 -07001/*
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 */
16package org.onosproject.net.newresource.impl;
17
18import org.onosproject.net.Link;
19import org.onosproject.net.LinkKey;
20import org.onosproject.net.link.LinkEvent;
21import org.onosproject.net.link.LinkListener;
22import org.onosproject.net.newresource.ResourceAdminService;
23import org.onosproject.net.newresource.ResourcePath;
24
25import java.util.concurrent.ExecutorService;
26
27import static com.google.common.base.Preconditions.checkNotNull;
28
29/**
30 * An implementation of LinkListener registering links as resources.
31 */
32final class ResourceLinkListener implements LinkListener {
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 ResourceLinkListener(ResourceAdminService adminService, ExecutorService executor) {
44 this.adminService = checkNotNull(adminService);
45 this.executor = checkNotNull(executor);
46 }
47
48 @Override
49 public void event(LinkEvent event) {
50 Link link = event.subject();
51 switch (event.type()) {
52 case LINK_ADDED:
53 registerLinkResource(link);
54 break;
Sho SHIMIZUd28842c2015-08-20 11:42:43 -070055 case LINK_REMOVED:
56 unregisterLinkResource(link);
57 break;
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070058 default:
59 break;
60 }
61 }
62
63 private void registerLinkResource(Link link) {
64 LinkKey linkKey = LinkKey.linkKey(link);
65 executor.submit(() -> adminService.registerResources(ResourcePath.ROOT, linkKey));
66 }
Sho SHIMIZUd28842c2015-08-20 11:42:43 -070067
68 private void unregisterLinkResource(Link link) {
69 LinkKey linkKey = LinkKey.linkKey(link);
70 executor.submit(() -> adminService.unregisterResources(ResourcePath.ROOT, linkKey));
71 }
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070072}