blob: ef0fb7c04b5063d11c20cfc72ed245238b89b0eb [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 */
HIGUCHI Yuta07a9e562016-05-23 16:41:17 -070016package org.onosproject.driver.optical.query;
Toru Furusawac23f5832015-12-04 11:39:18 -080017
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.OtuSignalType;
Toru Furusawac23f5832015-12-04 11:39:18 -080021import org.onosproject.net.Port;
22import org.onosproject.net.PortNumber;
23import org.onosproject.net.TributarySlot;
24import org.onosproject.net.device.DeviceService;
25import org.onosproject.net.driver.AbstractHandlerBehaviour;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080026import org.onosproject.net.optical.OchPort;
HIGUCHI Yuta5be3e822016-05-03 13:51:42 -070027import org.onosproject.net.optical.OtuPort;
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
Yuta HIGUCHI62f80182016-06-28 13:27:53 -070069 if (p == null) {
70 return Collections.emptySet();
71 }
72
Rimon Ashkenazy9ab0dc42016-02-22 18:22:57 +020073 switch (p.type()) {
74 case OCH:
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080075 return queryOchTributarySlots(p);
Rimon Ashkenazy9ab0dc42016-02-22 18:22:57 +020076 case OTU:
HIGUCHI Yuta5be3e822016-05-03 13:51:42 -070077 return queryOtuTributarySlots(p);
Rimon Ashkenazy9ab0dc42016-02-22 18:22:57 +020078 default:
79 return Collections.emptySet();
Toru Furusawac23f5832015-12-04 11:39:18 -080080 }
Rimon Ashkenazy9ab0dc42016-02-22 18:22:57 +020081 }
82
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080083 private Set<TributarySlot> queryOchTributarySlots(Port ochPort) {
84 OduSignalType signalType = null;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080085 if (ochPort instanceof OchPort) {
86 signalType = ((OchPort) ochPort).signalType();
Andrea Campanella5d73df72017-05-09 14:26:00 -070087 } else {
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080088 log.warn("{} was not an OchPort", ochPort);
89 return Collections.emptySet();
90 }
91
Toru Furusawac23f5832015-12-04 11:39:18 -080092 switch (signalType) {
93 case ODU2:
94 return ENTIRE_ODU2_TRIBUTARY_SLOTS;
95 case ODU4:
96 return ENTIRE_ODU4_TRIBUTARY_SLOTS;
97 default:
Rimon Ashkenazy9ab0dc42016-02-22 18:22:57 +020098 log.error("Unsupported signal type {} for {}", signalType, ochPort);
99 return Collections.emptySet();
100 }
101 }
102
HIGUCHI Yuta5be3e822016-05-03 13:51:42 -0700103 private Set<TributarySlot> queryOtuTributarySlots(Port otuPort) {
104 OtuSignalType signalType = null;
HIGUCHI Yuta5be3e822016-05-03 13:51:42 -0700105 if (otuPort instanceof OtuPort) {
106 signalType = ((OtuPort) otuPort).signalType();
Andrea Campanella5d73df72017-05-09 14:26:00 -0700107 } else {
HIGUCHI Yuta5be3e822016-05-03 13:51:42 -0700108 log.warn("{} was not an OtuPort", otuPort);
109 return Collections.emptySet();
110 }
111
Rimon Ashkenazy9ab0dc42016-02-22 18:22:57 +0200112 switch (signalType) {
113 case OTU2:
114 return ENTIRE_ODU2_TRIBUTARY_SLOTS;
115 case OTU4:
116 return ENTIRE_ODU4_TRIBUTARY_SLOTS;
117 default:
118 log.error("Unsupported signal type {} for {}", signalType, otuPort);
Toru Furusawac23f5832015-12-04 11:39:18 -0800119 return Collections.emptySet();
120 }
121 }
122}