Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | */ |
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; |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 20 | |
| 21 | import java.util.Collections; |
| 22 | import java.util.Map; |
| 23 | import java.util.concurrent.ConcurrentHashMap; |
| 24 | import java.util.concurrent.ConcurrentMap; |
| 25 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.net.PortNumber; |
| 27 | import org.onosproject.net.SparseAnnotations; |
| 28 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 29 | import org.onosproject.net.device.DefaultPortDescription; |
| 30 | import org.onosproject.net.device.DeviceDescription; |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 31 | import org.onosproject.net.device.OchPortDescription; |
| 32 | import org.onosproject.net.device.OduCltPortDescription; |
| 33 | import org.onosproject.net.device.OmsPortDescription; |
Rimon Ashkenazy | 8ebfff0 | 2016-02-01 11:56:36 +0200 | [diff] [blame] | 34 | import org.onosproject.net.device.OtuPortDescription; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import org.onosproject.net.device.PortDescription; |
| 36 | import org.onosproject.store.Timestamp; |
| 37 | import org.onosproject.store.impl.Timestamped; |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 38 | |
| 39 | /* |
| 40 | * Collection of Description of a Device and Ports, given from a Provider. |
| 41 | */ |
| 42 | class DeviceDescriptions { |
| 43 | |
| 44 | private volatile Timestamped<DeviceDescription> deviceDesc; |
| 45 | |
| 46 | private final ConcurrentMap<PortNumber, Timestamped<PortDescription>> portDescs; |
| 47 | |
| 48 | public DeviceDescriptions(Timestamped<DeviceDescription> desc) { |
| 49 | this.deviceDesc = checkNotNull(desc); |
| 50 | this.portDescs = new ConcurrentHashMap<>(); |
| 51 | } |
| 52 | |
| 53 | public Timestamp getLatestTimestamp() { |
| 54 | Timestamp latest = deviceDesc.timestamp(); |
| 55 | for (Timestamped<PortDescription> desc : portDescs.values()) { |
| 56 | if (desc.timestamp().compareTo(latest) > 0) { |
| 57 | latest = desc.timestamp(); |
| 58 | } |
| 59 | } |
| 60 | return latest; |
| 61 | } |
| 62 | |
| 63 | public Timestamped<DeviceDescription> getDeviceDesc() { |
| 64 | return deviceDesc; |
| 65 | } |
| 66 | |
| 67 | public Timestamped<PortDescription> getPortDesc(PortNumber number) { |
| 68 | return portDescs.get(number); |
| 69 | } |
| 70 | |
| 71 | public Map<PortNumber, Timestamped<PortDescription>> getPortDescs() { |
| 72 | return Collections.unmodifiableMap(portDescs); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Puts DeviceDescription, merging annotations as necessary. |
| 77 | * |
| 78 | * @param newDesc new DeviceDescription |
| 79 | */ |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 80 | public void putDeviceDesc(Timestamped<DeviceDescription> newDesc) { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 81 | Timestamped<DeviceDescription> oldOne = deviceDesc; |
| 82 | Timestamped<DeviceDescription> newOne = newDesc; |
| 83 | if (oldOne != null) { |
| 84 | SparseAnnotations merged = union(oldOne.value().annotations(), |
| 85 | newDesc.value().annotations()); |
Sho SHIMIZU | 7a4087b | 2015-09-10 09:23:16 -0700 | [diff] [blame] | 86 | newOne = new Timestamped<>( |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 87 | new DefaultDeviceDescription(newDesc.value(), merged), |
| 88 | newDesc.timestamp()); |
| 89 | } |
| 90 | deviceDesc = newOne; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Puts PortDescription, merging annotations as necessary. |
| 95 | * |
| 96 | * @param newDesc new PortDescription |
| 97 | */ |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 98 | public void putPortDesc(Timestamped<PortDescription> newDesc) { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 99 | Timestamped<PortDescription> oldOne = portDescs.get(newDesc.value().portNumber()); |
| 100 | Timestamped<PortDescription> newOne = newDesc; |
| 101 | if (oldOne != null) { |
| 102 | SparseAnnotations merged = union(oldOne.value().annotations(), |
| 103 | newDesc.value().annotations()); |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 104 | newOne = null; |
| 105 | switch (newDesc.value().type()) { |
| 106 | case OMS: |
| 107 | OmsPortDescription omsDesc = (OmsPortDescription) (newDesc.value()); |
Sho SHIMIZU | 7a4087b | 2015-09-10 09:23:16 -0700 | [diff] [blame] | 108 | newOne = new Timestamped<>( |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 109 | new OmsPortDescription( |
| 110 | omsDesc, omsDesc.minFrequency(), omsDesc.maxFrequency(), omsDesc.grid(), merged), |
| 111 | newDesc.timestamp()); |
| 112 | break; |
| 113 | case OCH: |
| 114 | OchPortDescription ochDesc = (OchPortDescription) (newDesc.value()); |
Sho SHIMIZU | 7a4087b | 2015-09-10 09:23:16 -0700 | [diff] [blame] | 115 | newOne = new Timestamped<>( |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 116 | new OchPortDescription( |
| 117 | ochDesc, ochDesc.signalType(), ochDesc.isTunable(), ochDesc.lambda(), merged), |
| 118 | newDesc.timestamp()); |
| 119 | break; |
| 120 | case ODUCLT: |
| 121 | OduCltPortDescription ocDesc = (OduCltPortDescription) (newDesc.value()); |
Sho SHIMIZU | 7a4087b | 2015-09-10 09:23:16 -0700 | [diff] [blame] | 122 | newOne = new Timestamped<>( |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 123 | new OduCltPortDescription( |
| 124 | ocDesc, ocDesc.signalType(), merged), |
| 125 | newDesc.timestamp()); |
| 126 | break; |
Rimon Ashkenazy | 8ebfff0 | 2016-02-01 11:56:36 +0200 | [diff] [blame] | 127 | case OTU: |
| 128 | OtuPortDescription otuDesc = (OtuPortDescription) (newDesc.value()); |
| 129 | newOne = new Timestamped<>( |
| 130 | new OtuPortDescription( |
| 131 | otuDesc, otuDesc.signalType(), merged), |
| 132 | newDesc.timestamp()); |
| 133 | break; |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 134 | default: |
Sho SHIMIZU | 7a4087b | 2015-09-10 09:23:16 -0700 | [diff] [blame] | 135 | newOne = new Timestamped<>( |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 136 | new DefaultPortDescription(newDesc.value(), merged), |
| 137 | newDesc.timestamp()); |
| 138 | } |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 139 | } |
| 140 | portDescs.put(newOne.value().portNumber(), newOne); |
| 141 | } |
| 142 | } |