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