Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
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; |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 20 | import java.util.Collections; |
| 21 | import java.util.Map; |
| 22 | import java.util.concurrent.ConcurrentHashMap; |
| 23 | import java.util.concurrent.ConcurrentMap; |
| 24 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.net.PortNumber; |
| 26 | import org.onosproject.net.SparseAnnotations; |
| 27 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 28 | import org.onosproject.net.device.DefaultPortDescription; |
| 29 | import org.onosproject.net.device.DeviceDescription; |
| 30 | import org.onosproject.net.device.PortDescription; |
| 31 | import org.onosproject.store.Timestamp; |
| 32 | import org.onosproject.store.impl.Timestamped; |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 33 | |
| 34 | /* |
| 35 | * Collection of Description of a Device and Ports, given from a Provider. |
| 36 | */ |
| 37 | class DeviceDescriptions { |
| 38 | |
| 39 | private volatile Timestamped<DeviceDescription> deviceDesc; |
| 40 | |
| 41 | private final ConcurrentMap<PortNumber, Timestamped<PortDescription>> portDescs; |
| 42 | |
| 43 | public DeviceDescriptions(Timestamped<DeviceDescription> desc) { |
| 44 | this.deviceDesc = checkNotNull(desc); |
| 45 | this.portDescs = new ConcurrentHashMap<>(); |
| 46 | } |
| 47 | |
| 48 | public Timestamp getLatestTimestamp() { |
| 49 | Timestamp latest = deviceDesc.timestamp(); |
| 50 | for (Timestamped<PortDescription> desc : portDescs.values()) { |
| 51 | if (desc.timestamp().compareTo(latest) > 0) { |
| 52 | latest = desc.timestamp(); |
| 53 | } |
| 54 | } |
| 55 | return latest; |
| 56 | } |
| 57 | |
| 58 | public Timestamped<DeviceDescription> getDeviceDesc() { |
| 59 | return deviceDesc; |
| 60 | } |
| 61 | |
| 62 | public Timestamped<PortDescription> getPortDesc(PortNumber number) { |
| 63 | return portDescs.get(number); |
| 64 | } |
| 65 | |
| 66 | public Map<PortNumber, Timestamped<PortDescription>> getPortDescs() { |
| 67 | return Collections.unmodifiableMap(portDescs); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Puts DeviceDescription, merging annotations as necessary. |
| 72 | * |
| 73 | * @param newDesc new DeviceDescription |
| 74 | */ |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 75 | public void putDeviceDesc(Timestamped<DeviceDescription> newDesc) { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 76 | Timestamped<DeviceDescription> oldOne = deviceDesc; |
| 77 | Timestamped<DeviceDescription> newOne = newDesc; |
| 78 | if (oldOne != null) { |
| 79 | SparseAnnotations merged = union(oldOne.value().annotations(), |
| 80 | newDesc.value().annotations()); |
Sho SHIMIZU | 7a4087b | 2015-09-10 09:23:16 -0700 | [diff] [blame] | 81 | newOne = new Timestamped<>( |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 82 | new DefaultDeviceDescription(newDesc.value(), merged), |
| 83 | newDesc.timestamp()); |
| 84 | } |
| 85 | deviceDesc = newOne; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Puts PortDescription, merging annotations as necessary. |
| 90 | * |
| 91 | * @param newDesc new PortDescription |
| 92 | */ |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 93 | public void putPortDesc(Timestamped<PortDescription> newDesc) { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 94 | Timestamped<PortDescription> oldOne = portDescs.get(newDesc.value().portNumber()); |
| 95 | Timestamped<PortDescription> newOne = newDesc; |
| 96 | if (oldOne != null) { |
| 97 | SparseAnnotations merged = union(oldOne.value().annotations(), |
| 98 | newDesc.value().annotations()); |
HIGUCHI Yuta | 6972ae6 | 2016-05-12 19:57:46 -0700 | [diff] [blame] | 99 | newOne = new Timestamped<>( |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 100 | new DefaultPortDescription(newDesc.value(), merged), |
| 101 | newDesc.timestamp()); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 102 | } |
| 103 | portDescs.put(newOne.value().portNumber(), newOne); |
| 104 | } |
| 105 | } |