Toru Furusawa | c23f583 | 2015-12-04 11:39:18 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Toru Furusawa | c23f583 | 2015-12-04 11:39:18 -0800 | [diff] [blame] | 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.driver.query; |
| 17 | |
| 18 | import org.onlab.util.GuavaCollectors; |
Toru Furusawa | c23f583 | 2015-12-04 11:39:18 -0800 | [diff] [blame] | 19 | import org.onosproject.net.OduSignalType; |
Rimon Ashkenazy | 9ab0dc4 | 2016-02-22 18:22:57 +0200 | [diff] [blame] | 20 | import org.onosproject.net.OtuSignalType; |
Toru Furusawa | c23f583 | 2015-12-04 11:39:18 -0800 | [diff] [blame] | 21 | import org.onosproject.net.Port; |
| 22 | import org.onosproject.net.PortNumber; |
| 23 | import org.onosproject.net.TributarySlot; |
| 24 | import org.onosproject.net.device.DeviceService; |
| 25 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 26 | import org.onosproject.net.optical.OchPort; |
HIGUCHI Yuta | 5be3e82 | 2016-05-03 13:51:42 -0700 | [diff] [blame^] | 27 | import org.onosproject.net.optical.OtuPort; |
Toru Furusawa | c23f583 | 2015-12-04 11:39:18 -0800 | [diff] [blame] | 28 | import org.onosproject.net.behaviour.TributarySlotQuery; |
| 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 32 | import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView; |
| 33 | |
Toru Furusawa | c23f583 | 2015-12-04 11:39:18 -0800 | [diff] [blame] | 34 | import java.util.Collections; |
| 35 | import java.util.Set; |
| 36 | import java.util.stream.IntStream; |
| 37 | |
| 38 | /** |
| 39 | * TributarySlotQuery implementation which responds that all slots of ODU2 or ODU4 are available for the port. |
| 40 | */ |
| 41 | public class DefaultTributarySlotQuery extends AbstractHandlerBehaviour implements TributarySlotQuery { |
| 42 | |
| 43 | private static final Logger log = LoggerFactory.getLogger(DefaultTributarySlotQuery.class); |
| 44 | |
| 45 | private static final int TOTAL_ODU2_TRIBUTARY_SLOTS = 8; |
| 46 | private static final int TOTAL_ODU4_TRIBUTARY_SLOTS = 80; |
| 47 | |
| 48 | private static Set<TributarySlot> getEntireOdu2TributarySlots() { |
| 49 | return IntStream.rangeClosed(1, TOTAL_ODU2_TRIBUTARY_SLOTS) |
| 50 | .mapToObj(TributarySlot::of) |
| 51 | .collect(GuavaCollectors.toImmutableSet()); |
| 52 | } |
| 53 | |
| 54 | private static Set<TributarySlot> getEntireOdu4TributarySlots() { |
| 55 | return IntStream.rangeClosed(1, TOTAL_ODU4_TRIBUTARY_SLOTS) |
| 56 | .mapToObj(TributarySlot::of) |
| 57 | .collect(GuavaCollectors.toImmutableSet()); |
| 58 | } |
| 59 | |
| 60 | private static final Set<TributarySlot> ENTIRE_ODU2_TRIBUTARY_SLOTS = getEntireOdu2TributarySlots(); |
| 61 | private static final Set<TributarySlot> ENTIRE_ODU4_TRIBUTARY_SLOTS = getEntireOdu4TributarySlots(); |
| 62 | |
| 63 | @Override |
| 64 | public Set<TributarySlot> queryTributarySlots(PortNumber port) { |
| 65 | // currently return all slots by default. |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 66 | DeviceService deviceService = opticalView(this.handler().get(DeviceService.class)); |
Toru Furusawa | c23f583 | 2015-12-04 11:39:18 -0800 | [diff] [blame] | 67 | Port p = deviceService.getPort(this.data().deviceId(), port); |
| 68 | |
Rimon Ashkenazy | 9ab0dc4 | 2016-02-22 18:22:57 +0200 | [diff] [blame] | 69 | switch (p.type()) { |
| 70 | case OCH: |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 71 | return queryOchTributarySlots(p); |
Rimon Ashkenazy | 9ab0dc4 | 2016-02-22 18:22:57 +0200 | [diff] [blame] | 72 | case OTU: |
HIGUCHI Yuta | 5be3e82 | 2016-05-03 13:51:42 -0700 | [diff] [blame^] | 73 | return queryOtuTributarySlots(p); |
Rimon Ashkenazy | 9ab0dc4 | 2016-02-22 18:22:57 +0200 | [diff] [blame] | 74 | default: |
| 75 | return Collections.emptySet(); |
Toru Furusawa | c23f583 | 2015-12-04 11:39:18 -0800 | [diff] [blame] | 76 | } |
Rimon Ashkenazy | 9ab0dc4 | 2016-02-22 18:22:57 +0200 | [diff] [blame] | 77 | } |
| 78 | |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 79 | private Set<TributarySlot> queryOchTributarySlots(Port ochPort) { |
| 80 | OduSignalType signalType = null; |
| 81 | if (ochPort instanceof org.onosproject.net.OchPort) { |
| 82 | // remove once deprecation of old OchPort model is done |
| 83 | signalType = ((org.onosproject.net.OchPort) ochPort).signalType(); |
| 84 | } |
| 85 | if (ochPort instanceof OchPort) { |
| 86 | signalType = ((OchPort) ochPort).signalType(); |
| 87 | } |
| 88 | |
| 89 | if (signalType == null) { |
| 90 | log.warn("{} was not an OchPort", ochPort); |
| 91 | return Collections.emptySet(); |
| 92 | } |
| 93 | |
Toru Furusawa | c23f583 | 2015-12-04 11:39:18 -0800 | [diff] [blame] | 94 | switch (signalType) { |
| 95 | case ODU2: |
| 96 | return ENTIRE_ODU2_TRIBUTARY_SLOTS; |
| 97 | case ODU4: |
| 98 | return ENTIRE_ODU4_TRIBUTARY_SLOTS; |
| 99 | default: |
Rimon Ashkenazy | 9ab0dc4 | 2016-02-22 18:22:57 +0200 | [diff] [blame] | 100 | log.error("Unsupported signal type {} for {}", signalType, ochPort); |
| 101 | return Collections.emptySet(); |
| 102 | } |
| 103 | } |
| 104 | |
HIGUCHI Yuta | 5be3e82 | 2016-05-03 13:51:42 -0700 | [diff] [blame^] | 105 | private Set<TributarySlot> queryOtuTributarySlots(Port otuPort) { |
| 106 | OtuSignalType signalType = null; |
| 107 | if (otuPort instanceof org.onosproject.net.OtuPort) { |
| 108 | // remove once deprecation of old OtuPort model is done |
| 109 | signalType = ((org.onosproject.net.OtuPort) otuPort).signalType(); |
| 110 | } |
| 111 | if (otuPort instanceof OtuPort) { |
| 112 | signalType = ((OtuPort) otuPort).signalType(); |
| 113 | } |
| 114 | |
| 115 | if (signalType == null) { |
| 116 | log.warn("{} was not an OtuPort", otuPort); |
| 117 | return Collections.emptySet(); |
| 118 | } |
| 119 | |
Rimon Ashkenazy | 9ab0dc4 | 2016-02-22 18:22:57 +0200 | [diff] [blame] | 120 | switch (signalType) { |
| 121 | case OTU2: |
| 122 | return ENTIRE_ODU2_TRIBUTARY_SLOTS; |
| 123 | case OTU4: |
| 124 | return ENTIRE_ODU4_TRIBUTARY_SLOTS; |
| 125 | default: |
| 126 | log.error("Unsupported signal type {} for {}", signalType, otuPort); |
Toru Furusawa | c23f583 | 2015-12-04 11:39:18 -0800 | [diff] [blame] | 127 | return Collections.emptySet(); |
| 128 | } |
| 129 | } |
| 130 | } |