blob: 4129fd534c3770ca2e7ad390cedc5b11bbbbd647 [file] [log] [blame]
Toru Furusawac23f5832015-12-04 11:39:18 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Toru Furusawac23f5832015-12-04 11:39:18 -08003 *
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.driver.query;
17
18import org.onlab.util.GuavaCollectors;
Toru Furusawac23f5832015-12-04 11:39:18 -080019import org.onosproject.net.OduSignalType;
Rimon Ashkenazy9ab0dc42016-02-22 18:22:57 +020020import org.onosproject.net.OtuPort;
21import org.onosproject.net.OtuSignalType;
Toru Furusawac23f5832015-12-04 11:39:18 -080022import org.onosproject.net.Port;
23import org.onosproject.net.PortNumber;
24import org.onosproject.net.TributarySlot;
25import org.onosproject.net.device.DeviceService;
26import org.onosproject.net.driver.AbstractHandlerBehaviour;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080027import org.onosproject.net.optical.OchPort;
Toru Furusawac23f5832015-12-04 11:39:18 -080028import org.onosproject.net.behaviour.TributarySlotQuery;
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080032import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView;
33
Toru Furusawac23f5832015-12-04 11:39:18 -080034import java.util.Collections;
35import java.util.Set;
36import java.util.stream.IntStream;
37
38/**
39 * TributarySlotQuery implementation which responds that all slots of ODU2 or ODU4 are available for the port.
40 */
41public 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 Yuta34a3f692016-01-09 21:08:57 -080066 DeviceService deviceService = opticalView(this.handler().get(DeviceService.class));
Toru Furusawac23f5832015-12-04 11:39:18 -080067 Port p = deviceService.getPort(this.data().deviceId(), port);
68
Rimon Ashkenazy9ab0dc42016-02-22 18:22:57 +020069 switch (p.type()) {
70 case OCH:
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080071 return queryOchTributarySlots(p);
Rimon Ashkenazy9ab0dc42016-02-22 18:22:57 +020072 case OTU:
73 return queryOtuTributarySlots((OtuPort) p);
74 default:
75 return Collections.emptySet();
Toru Furusawac23f5832015-12-04 11:39:18 -080076 }
Rimon Ashkenazy9ab0dc42016-02-22 18:22:57 +020077 }
78
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080079 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 Furusawac23f5832015-12-04 11:39:18 -080094 switch (signalType) {
95 case ODU2:
96 return ENTIRE_ODU2_TRIBUTARY_SLOTS;
97 case ODU4:
98 return ENTIRE_ODU4_TRIBUTARY_SLOTS;
99 default:
Rimon Ashkenazy9ab0dc42016-02-22 18:22:57 +0200100 log.error("Unsupported signal type {} for {}", signalType, ochPort);
101 return Collections.emptySet();
102 }
103 }
104
105 private Set<TributarySlot> queryOtuTributarySlots(OtuPort otuPort) {
106 OtuSignalType signalType = otuPort.signalType();
107 switch (signalType) {
108 case OTU2:
109 return ENTIRE_ODU2_TRIBUTARY_SLOTS;
110 case OTU4:
111 return ENTIRE_ODU4_TRIBUTARY_SLOTS;
112 default:
113 log.error("Unsupported signal type {} for {}", signalType, otuPort);
Toru Furusawac23f5832015-12-04 11:39:18 -0800114 return Collections.emptySet();
115 }
116 }
117}