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