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; |
| 19 | import org.onosproject.net.OchPort; |
| 20 | import org.onosproject.net.OduSignalType; |
Rimon Ashkenazy | 9ab0dc4 | 2016-02-22 18:22:57 +0200 | [diff] [blame] | 21 | import org.onosproject.net.OtuPort; |
| 22 | import org.onosproject.net.OtuSignalType; |
Toru Furusawa | c23f583 | 2015-12-04 11:39:18 -0800 | [diff] [blame] | 23 | import org.onosproject.net.Port; |
| 24 | import org.onosproject.net.PortNumber; |
| 25 | import org.onosproject.net.TributarySlot; |
| 26 | import org.onosproject.net.device.DeviceService; |
| 27 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 28 | import org.onosproject.net.behaviour.TributarySlotQuery; |
| 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
| 32 | import java.util.Collections; |
| 33 | import java.util.Set; |
| 34 | import java.util.stream.IntStream; |
| 35 | |
| 36 | /** |
| 37 | * TributarySlotQuery implementation which responds that all slots of ODU2 or ODU4 are available for the port. |
| 38 | */ |
| 39 | public class DefaultTributarySlotQuery extends AbstractHandlerBehaviour implements TributarySlotQuery { |
| 40 | |
| 41 | private static final Logger log = LoggerFactory.getLogger(DefaultTributarySlotQuery.class); |
| 42 | |
| 43 | private static final int TOTAL_ODU2_TRIBUTARY_SLOTS = 8; |
| 44 | private static final int TOTAL_ODU4_TRIBUTARY_SLOTS = 80; |
| 45 | |
| 46 | private static Set<TributarySlot> getEntireOdu2TributarySlots() { |
| 47 | return IntStream.rangeClosed(1, TOTAL_ODU2_TRIBUTARY_SLOTS) |
| 48 | .mapToObj(TributarySlot::of) |
| 49 | .collect(GuavaCollectors.toImmutableSet()); |
| 50 | } |
| 51 | |
| 52 | private static Set<TributarySlot> getEntireOdu4TributarySlots() { |
| 53 | return IntStream.rangeClosed(1, TOTAL_ODU4_TRIBUTARY_SLOTS) |
| 54 | .mapToObj(TributarySlot::of) |
| 55 | .collect(GuavaCollectors.toImmutableSet()); |
| 56 | } |
| 57 | |
| 58 | private static final Set<TributarySlot> ENTIRE_ODU2_TRIBUTARY_SLOTS = getEntireOdu2TributarySlots(); |
| 59 | private static final Set<TributarySlot> ENTIRE_ODU4_TRIBUTARY_SLOTS = getEntireOdu4TributarySlots(); |
| 60 | |
| 61 | @Override |
| 62 | public Set<TributarySlot> queryTributarySlots(PortNumber port) { |
| 63 | // currently return all slots by default. |
| 64 | DeviceService deviceService = this.handler().get(DeviceService.class); |
| 65 | Port p = deviceService.getPort(this.data().deviceId(), port); |
| 66 | |
Rimon Ashkenazy | 9ab0dc4 | 2016-02-22 18:22:57 +0200 | [diff] [blame] | 67 | switch (p.type()) { |
| 68 | case OCH: |
| 69 | return queryOchTributarySlots((OchPort) p); |
| 70 | case OTU: |
| 71 | return queryOtuTributarySlots((OtuPort) p); |
| 72 | default: |
| 73 | return Collections.emptySet(); |
Toru Furusawa | c23f583 | 2015-12-04 11:39:18 -0800 | [diff] [blame] | 74 | } |
Rimon Ashkenazy | 9ab0dc4 | 2016-02-22 18:22:57 +0200 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | private Set<TributarySlot> queryOchTributarySlots(OchPort ochPort) { |
| 78 | OduSignalType signalType = ochPort.signalType(); |
Toru Furusawa | c23f583 | 2015-12-04 11:39:18 -0800 | [diff] [blame] | 79 | switch (signalType) { |
| 80 | case ODU2: |
| 81 | return ENTIRE_ODU2_TRIBUTARY_SLOTS; |
| 82 | case ODU4: |
| 83 | return ENTIRE_ODU4_TRIBUTARY_SLOTS; |
| 84 | default: |
Rimon Ashkenazy | 9ab0dc4 | 2016-02-22 18:22:57 +0200 | [diff] [blame] | 85 | log.error("Unsupported signal type {} for {}", signalType, ochPort); |
| 86 | return Collections.emptySet(); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | private Set<TributarySlot> queryOtuTributarySlots(OtuPort otuPort) { |
| 91 | OtuSignalType signalType = otuPort.signalType(); |
| 92 | switch (signalType) { |
| 93 | case OTU2: |
| 94 | return ENTIRE_ODU2_TRIBUTARY_SLOTS; |
| 95 | case OTU4: |
| 96 | return ENTIRE_ODU4_TRIBUTARY_SLOTS; |
| 97 | default: |
| 98 | log.error("Unsupported signal type {} for {}", signalType, otuPort); |
Toru Furusawa | c23f583 | 2015-12-04 11:39:18 -0800 | [diff] [blame] | 99 | return Collections.emptySet(); |
| 100 | } |
| 101 | } |
| 102 | } |