HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 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 Yuta | deae31e | 2016-05-11 09:50:39 -0700 | [diff] [blame] | 16 | package org.onosproject.net.optical; |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static com.google.common.base.Preconditions.checkState; |
| 20 | import static org.slf4j.LoggerFactory.getLogger; |
| 21 | |
| 22 | import java.util.Map; |
| 23 | import java.util.Optional; |
| 24 | import org.onlab.osgi.DefaultServiceDirectory; |
| 25 | import org.onosproject.net.Device; |
| 26 | import org.onosproject.net.Port; |
| 27 | import org.onosproject.net.device.DeviceService; |
| 28 | import org.onosproject.net.driver.AbstractBehaviour; |
| 29 | import org.onosproject.net.driver.DriverData; |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 30 | import org.onosproject.net.optical.device.port.OchPortMapper; |
HIGUCHI Yuta | 4c0ef6b | 2016-05-02 19:45:41 -0700 | [diff] [blame] | 31 | import org.onosproject.net.optical.device.port.OduCltPortMapper; |
HIGUCHI Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 32 | import org.onosproject.net.optical.device.port.OmsPortMapper; |
HIGUCHI Yuta | 5be3e82 | 2016-05-03 13:51:42 -0700 | [diff] [blame] | 33 | import org.onosproject.net.optical.device.port.OtuPortMapper; |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 34 | import org.onosproject.net.optical.device.port.PortMapper; |
HIGUCHI Yuta | f46dc4f | 2016-05-13 11:24:13 -0700 | [diff] [blame] | 35 | import org.onosproject.net.utils.ForwardingDevice; |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | |
| 38 | import com.google.common.annotations.Beta; |
| 39 | import com.google.common.base.MoreObjects; |
| 40 | import com.google.common.collect.ImmutableMap; |
| 41 | |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 42 | /** |
| 43 | * Implementation of {@link OpticalDevice}. |
| 44 | * <p> |
| 45 | * Currently supports |
| 46 | * <ul> |
| 47 | * <li> {@link OchPort} |
HIGUCHI Yuta | deae31e | 2016-05-11 09:50:39 -0700 | [diff] [blame] | 48 | * <li> {@link OmsPort} |
| 49 | * <li> {@link OduCltPort} |
| 50 | * <li> {@link OtuPort} |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 51 | * </ul> |
| 52 | */ |
| 53 | @Beta |
| 54 | public 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 Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 65 | .put(OmsPort.class, new OmsPortMapper()) |
HIGUCHI Yuta | 4c0ef6b | 2016-05-02 19:45:41 -0700 | [diff] [blame] | 66 | .put(OduCltPort.class, new OduCltPortMapper()) |
HIGUCHI Yuta | 5be3e82 | 2016-05-03 13:51:42 -0700 | [diff] [blame] | 67 | .put(OtuPort.class, new OtuPortMapper()) |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 68 | // 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 | } |