4fb0d7babcfd0b10dff1fadcb5d5da785276a5d1
[onosfw.git] /
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 com.google.common.collect.Lists;
19 import org.onosproject.net.Device;
20 import org.onosproject.net.Port;
21 import org.onosproject.net.OchPort;
22 import org.onosproject.net.TributarySlot;
23 import org.onosproject.net.OduSignalType;
24 import org.onosproject.net.device.DeviceEvent;
25 import org.onosproject.net.device.DeviceListener;
26 import org.onosproject.net.newresource.ResourceAdminService;
27 import org.onosproject.net.newresource.ResourcePath;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import java.util.List;
32 import java.util.concurrent.ExecutorService;
33 import java.util.stream.Collectors;
34 import java.util.stream.IntStream;
35
36 import static com.google.common.base.Preconditions.checkNotNull;
37
38 /**
39  * An implementation of DeviceListener registering devices as resources.
40  */
41 final class ResourceDeviceListener implements DeviceListener {
42
43     private static final Logger log = LoggerFactory.getLogger(ResourceDeviceListener.class);
44
45     private static final int TOTAL_ODU2_TRIBUTARY_SLOTS = 8;
46     private static final int TOTAL_ODU4_TRIBUTARY_SLOTS = 80;
47     private static final List<TributarySlot> ENTIRE_ODU2_TRIBUTARY_SLOTS = getEntireOdu2TributarySlots();
48     private static final List<TributarySlot> ENTIRE_ODU4_TRIBUTARY_SLOTS = getEntireOdu4TributarySlots();
49
50     private final ResourceAdminService adminService;
51     private final ExecutorService executor;
52
53     /**
54      * Creates an instance with the specified ResourceAdminService and ExecutorService.
55      *
56      * @param adminService instance invoked to register resources
57      * @param executor executor used for processing resource registration
58      */
59     ResourceDeviceListener(ResourceAdminService adminService, ExecutorService executor) {
60         this.adminService = checkNotNull(adminService);
61         this.executor = checkNotNull(executor);
62     }
63
64     @Override
65     public void event(DeviceEvent event) {
66         Device device = event.subject();
67         switch (event.type()) {
68             case DEVICE_ADDED:
69                 registerDeviceResource(device);
70                 break;
71             case DEVICE_REMOVED:
72                 unregisterDeviceResource(device);
73                 break;
74             case PORT_ADDED:
75                 registerPortResource(device, event.port());
76                 break;
77             case PORT_REMOVED:
78                 unregisterPortResource(device, event.port());
79                 break;
80             default:
81                 break;
82         }
83     }
84
85     private void registerDeviceResource(Device device) {
86         executor.submit(() -> adminService.registerResources(ResourcePath.discrete(device.id())));
87     }
88
89     private void unregisterDeviceResource(Device device) {
90         executor.submit(() -> adminService.unregisterResources(ResourcePath.discrete(device.id())));
91     }
92
93     private void registerPortResource(Device device, Port port) {
94         ResourcePath portPath = ResourcePath.discrete(device.id(), port.number());
95         executor.submit(() -> {
96             adminService.registerResources(portPath);
97
98             switch (port.type()) {
99                 case OCH:
100                     // register ODU TributarySlots against the OCH port
101                     registerTributarySlotsResources(((OchPort) port).signalType(), portPath);
102                     break;
103                 default:
104                     break;
105             }
106         });
107     }
108
109     private void registerTributarySlotsResources(OduSignalType oduSignalType, ResourcePath portPath) {
110         switch (oduSignalType) {
111             case ODU2:
112                 adminService.registerResources(Lists.transform(ENTIRE_ODU2_TRIBUTARY_SLOTS, portPath::child));
113                 break;
114             case ODU4:
115                 adminService.registerResources(Lists.transform(ENTIRE_ODU4_TRIBUTARY_SLOTS, portPath::child));
116                 break;
117             default:
118                 break;
119         }
120     }
121
122     private void unregisterPortResource(Device device, Port port) {
123         ResourcePath resource = ResourcePath.discrete(device.id(), port.number());
124         executor.submit(() -> adminService.unregisterResources(resource));
125     }
126
127     private static List<TributarySlot> getEntireOdu2TributarySlots() {
128         return IntStream.rangeClosed(1, TOTAL_ODU2_TRIBUTARY_SLOTS)
129                 .mapToObj(x -> TributarySlot.of(x))
130                 .collect(Collectors.toList());
131     }
132     private static List<TributarySlot> getEntireOdu4TributarySlots() {
133         return IntStream.rangeClosed(1, TOTAL_ODU4_TRIBUTARY_SLOTS)
134                 .mapToObj(x -> TributarySlot.of(x))
135                 .collect(Collectors.toList());
136     }
137
138 }