blob: 23f3c57a61ed89cf824f101b4dc9959f85d589bb [file] [log] [blame]
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -08001/*
2 * Copyright 2016 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 */
HIGUCHI Yutadeae31e2016-05-11 09:50:39 -070016package org.onosproject.net.optical;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080017
18import static com.google.common.base.Preconditions.checkNotNull;
19import static com.google.common.base.Preconditions.checkState;
20import static org.slf4j.LoggerFactory.getLogger;
21
22import java.util.Map;
23import java.util.Optional;
24import org.onlab.osgi.DefaultServiceDirectory;
25import org.onosproject.net.Device;
26import org.onosproject.net.Port;
27import org.onosproject.net.device.DeviceService;
28import org.onosproject.net.driver.AbstractBehaviour;
29import org.onosproject.net.driver.DriverData;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080030import org.onosproject.net.optical.device.port.OchPortMapper;
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -070031import org.onosproject.net.optical.device.port.OduCltPortMapper;
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -070032import org.onosproject.net.optical.device.port.OmsPortMapper;
HIGUCHI Yuta5be3e822016-05-03 13:51:42 -070033import org.onosproject.net.optical.device.port.OtuPortMapper;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080034import org.onosproject.net.optical.device.port.PortMapper;
35import org.onosproject.net.optical.utils.ForwardingDevice;
36import org.slf4j.Logger;
37
38import com.google.common.annotations.Beta;
39import com.google.common.base.MoreObjects;
40import com.google.common.collect.ImmutableMap;
41
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080042/**
43 * Implementation of {@link OpticalDevice}.
44 * <p>
45 * Currently supports
46 * <ul>
47 * <li> {@link OchPort}
HIGUCHI Yutadeae31e2016-05-11 09:50:39 -070048 * <li> {@link OmsPort}
49 * <li> {@link OduCltPort}
50 * <li> {@link OtuPort}
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080051 * </ul>
52 */
53@Beta
54public class DefaultOpticalDevice
55 extends AbstractBehaviour
56 implements OpticalDevice, ForwardingDevice {
57
58 private static final Logger log = getLogger(DefaultOpticalDevice.class);
59
60 // shared Port type handler map.
61 // TODO Is there a use case, where we need to differentiate this map per Device?
62 private static final Map<Class<? extends Port>, PortMapper<? extends Port>> MAPPERS
63 = ImmutableMap.<Class<? extends Port>, PortMapper<? extends Port>>builder()
64 .put(OchPort.class, new OchPortMapper())
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -070065 .put(OmsPort.class, new OmsPortMapper())
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -070066 .put(OduCltPort.class, new OduCltPortMapper())
HIGUCHI Yuta5be3e822016-05-03 13:51:42 -070067 .put(OtuPort.class, new OtuPortMapper())
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080068 // TODO add other optical port type here
69 .build();
70
71
72
73 // effectively final
74 private Device delegate;
75
76 // Default constructor required as a Behaviour.
77 public DefaultOpticalDevice() {}
78
79 @Override
80 public Device delegate() {
81 if (delegate == null) {
82 // dirty work around.
83 // wanted to pass delegate Device at construction,
84 // but was not possible. A Behaviour requires no-arg constructor.
85 checkState(data() != null, "DriverData must exist");
86 DriverData data = data();
87 DeviceService service = DefaultServiceDirectory.getService(DeviceService.class);
88 delegate = checkNotNull(service.getDevice(data.deviceId()),
89 "No Device found for %s", data.deviceId());
90 }
91 return delegate;
92 }
93
94 @Override
95 public <T extends Port> boolean portIs(Port port, Class<T> portClass) {
96
97 PortMapper<? extends Port> mapper = MAPPERS.get(portClass);
98 if (mapper != null) {
99 return mapper.is(port);
100 }
101 return false;
102 }
103
104 @Override
105 public <T extends Port> Optional<T> portAs(Port port, Class<T> portClass) {
106 PortMapper<? extends Port> mapper = MAPPERS.get(portClass);
107 if (mapper != null) {
108 return (Optional<T>) (mapper.as(port));
109 }
110 return Optional.empty();
111 }
112
113 @Override
114 public Port port(Port port) {
115 for (PortMapper<? extends Port> mapper : MAPPERS.values()) {
116 if (mapper.is(port)) {
117 return mapper.as(port).map(Port.class::cast).orElse(port);
118 }
119 }
120 return port;
121 }
122
123 @Override
124 public boolean equals(Object obj) {
125 return delegate().equals(obj);
126 }
127
128 @Override
129 public int hashCode() {
130 return delegate().hashCode();
131 }
132
133 @Override
134 public String toString() {
135 return MoreObjects.toStringHelper(this)
136 .add("delegate", delegate)
137 .toString();
138 }
139}