Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.store.device.impl; |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 19 | import static org.onosproject.net.DefaultAnnotations.union; |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 20 | import static org.onosproject.net.optical.device.OchPortHelper.ochPortDescription; |
HIGUCHI Yuta | 4c0ef6b | 2016-05-02 19:45:41 -0700 | [diff] [blame] | 21 | import static org.onosproject.net.optical.device.OduCltPortHelper.oduCltPortDescription; |
HIGUCHI Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 22 | import static org.onosproject.net.optical.device.OmsPortHelper.omsPortDescription; |
HIGUCHI Yuta | 5be3e82 | 2016-05-03 13:51:42 -0700 | [diff] [blame^] | 23 | import static org.onosproject.net.optical.device.OtuPortHelper.otuPortDescription; |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 24 | |
| 25 | import java.util.Collections; |
| 26 | import java.util.Map; |
| 27 | import java.util.concurrent.ConcurrentHashMap; |
| 28 | import java.util.concurrent.ConcurrentMap; |
| 29 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.net.PortNumber; |
| 31 | import org.onosproject.net.SparseAnnotations; |
| 32 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 33 | import org.onosproject.net.device.DefaultPortDescription; |
| 34 | import org.onosproject.net.device.DeviceDescription; |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 35 | import org.onosproject.net.device.OchPortDescription; |
| 36 | import org.onosproject.net.device.OduCltPortDescription; |
| 37 | import org.onosproject.net.device.OmsPortDescription; |
Rimon Ashkenazy | 8ebfff0 | 2016-02-01 11:56:36 +0200 | [diff] [blame] | 38 | import org.onosproject.net.device.OtuPortDescription; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 39 | import org.onosproject.net.device.PortDescription; |
| 40 | import org.onosproject.store.Timestamp; |
| 41 | import org.onosproject.store.impl.Timestamped; |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * Collection of Description of a Device and Ports, given from a Provider. |
| 45 | */ |
| 46 | class DeviceDescriptions { |
| 47 | |
| 48 | private volatile Timestamped<DeviceDescription> deviceDesc; |
| 49 | |
| 50 | private final ConcurrentMap<PortNumber, Timestamped<PortDescription>> portDescs; |
| 51 | |
| 52 | public DeviceDescriptions(Timestamped<DeviceDescription> desc) { |
| 53 | this.deviceDesc = checkNotNull(desc); |
| 54 | this.portDescs = new ConcurrentHashMap<>(); |
| 55 | } |
| 56 | |
| 57 | public Timestamp getLatestTimestamp() { |
| 58 | Timestamp latest = deviceDesc.timestamp(); |
| 59 | for (Timestamped<PortDescription> desc : portDescs.values()) { |
| 60 | if (desc.timestamp().compareTo(latest) > 0) { |
| 61 | latest = desc.timestamp(); |
| 62 | } |
| 63 | } |
| 64 | return latest; |
| 65 | } |
| 66 | |
| 67 | public Timestamped<DeviceDescription> getDeviceDesc() { |
| 68 | return deviceDesc; |
| 69 | } |
| 70 | |
| 71 | public Timestamped<PortDescription> getPortDesc(PortNumber number) { |
| 72 | return portDescs.get(number); |
| 73 | } |
| 74 | |
| 75 | public Map<PortNumber, Timestamped<PortDescription>> getPortDescs() { |
| 76 | return Collections.unmodifiableMap(portDescs); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Puts DeviceDescription, merging annotations as necessary. |
| 81 | * |
| 82 | * @param newDesc new DeviceDescription |
| 83 | */ |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 84 | public void putDeviceDesc(Timestamped<DeviceDescription> newDesc) { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 85 | Timestamped<DeviceDescription> oldOne = deviceDesc; |
| 86 | Timestamped<DeviceDescription> newOne = newDesc; |
| 87 | if (oldOne != null) { |
| 88 | SparseAnnotations merged = union(oldOne.value().annotations(), |
| 89 | newDesc.value().annotations()); |
Sho SHIMIZU | 7a4087b | 2015-09-10 09:23:16 -0700 | [diff] [blame] | 90 | newOne = new Timestamped<>( |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 91 | new DefaultDeviceDescription(newDesc.value(), merged), |
| 92 | newDesc.timestamp()); |
| 93 | } |
| 94 | deviceDesc = newOne; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Puts PortDescription, merging annotations as necessary. |
| 99 | * |
| 100 | * @param newDesc new PortDescription |
| 101 | */ |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 102 | public void putPortDesc(Timestamped<PortDescription> newDesc) { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 103 | Timestamped<PortDescription> oldOne = portDescs.get(newDesc.value().portNumber()); |
| 104 | Timestamped<PortDescription> newOne = newDesc; |
| 105 | if (oldOne != null) { |
| 106 | SparseAnnotations merged = union(oldOne.value().annotations(), |
| 107 | newDesc.value().annotations()); |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 108 | newOne = null; |
| 109 | switch (newDesc.value().type()) { |
| 110 | case OMS: |
HIGUCHI Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 111 | if (newDesc.value() instanceof OmsPortDescription) { |
| 112 | // remove if-block after deprecation is complete |
| 113 | OmsPortDescription omsDesc = (OmsPortDescription) (newDesc.value()); |
| 114 | newOne = new Timestamped<>( |
| 115 | omsPortDescription(omsDesc, |
| 116 | omsDesc.minFrequency(), |
| 117 | omsDesc.maxFrequency(), |
| 118 | omsDesc.grid(), merged), |
| 119 | newDesc.timestamp()); |
| 120 | } else { |
| 121 | // same as default case |
| 122 | newOne = new Timestamped<>( |
| 123 | new DefaultPortDescription(newDesc.value(), merged), |
| 124 | newDesc.timestamp()); |
| 125 | } |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 126 | break; |
| 127 | case OCH: |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 128 | if (newDesc.value() instanceof OchPortDescription) { |
| 129 | // remove if-block after Och related deprecation is complete |
| 130 | OchPortDescription ochDesc = (OchPortDescription) (newDesc.value()); |
| 131 | newOne = new Timestamped<>( |
| 132 | ochPortDescription(ochDesc, |
| 133 | ochDesc.signalType(), |
| 134 | ochDesc.isTunable(), |
| 135 | ochDesc.lambda(), merged), |
| 136 | newDesc.timestamp()); |
| 137 | } else { |
| 138 | // same as default case |
| 139 | newOne = new Timestamped<>( |
| 140 | new DefaultPortDescription(newDesc.value(), merged), |
| 141 | newDesc.timestamp()); |
| 142 | } |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 143 | break; |
| 144 | case ODUCLT: |
HIGUCHI Yuta | 4c0ef6b | 2016-05-02 19:45:41 -0700 | [diff] [blame] | 145 | if (newDesc.value() instanceof OduCltPortDescription) { |
| 146 | // remove if-block after deprecation is complete |
| 147 | OduCltPortDescription ocDesc = (OduCltPortDescription) (newDesc.value()); |
| 148 | newOne = new Timestamped<>( |
| 149 | oduCltPortDescription(ocDesc, |
| 150 | ocDesc.signalType(), |
| 151 | merged), |
| 152 | newDesc.timestamp()); |
| 153 | } else { |
| 154 | // same as default case |
| 155 | newOne = new Timestamped<>( |
| 156 | new DefaultPortDescription(newDesc.value(), merged), |
| 157 | newDesc.timestamp()); |
| 158 | } |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 159 | break; |
Rimon Ashkenazy | 8ebfff0 | 2016-02-01 11:56:36 +0200 | [diff] [blame] | 160 | case OTU: |
HIGUCHI Yuta | 5be3e82 | 2016-05-03 13:51:42 -0700 | [diff] [blame^] | 161 | if (newDesc.value() instanceof OtuPortDescription) { |
| 162 | // remove if-block after deprecation is complete |
| 163 | OtuPortDescription otuDesc = (OtuPortDescription) (newDesc.value()); |
| 164 | newOne = new Timestamped<>( |
| 165 | otuPortDescription( |
| 166 | otuDesc, otuDesc.signalType(), merged), |
| 167 | newDesc.timestamp()); |
| 168 | } else { |
| 169 | // same as default case |
| 170 | newOne = new Timestamped<>( |
| 171 | new DefaultPortDescription(newDesc.value(), merged), |
| 172 | newDesc.timestamp()); |
| 173 | } |
Rimon Ashkenazy | 8ebfff0 | 2016-02-01 11:56:36 +0200 | [diff] [blame] | 174 | break; |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 175 | default: |
Sho SHIMIZU | 7a4087b | 2015-09-10 09:23:16 -0700 | [diff] [blame] | 176 | newOne = new Timestamped<>( |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 177 | new DefaultPortDescription(newDesc.value(), merged), |
| 178 | newDesc.timestamp()); |
| 179 | } |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 180 | } |
| 181 | portDescs.put(newOne.value().portNumber(), newOne); |
| 182 | } |
| 183 | } |