Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -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 | */ |
| 16 | package org.onosproject.net.device.impl; |
| 17 | |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 18 | import org.onosproject.net.AnnotationKeys; |
| 19 | import org.onosproject.net.DefaultAnnotations; |
| 20 | import org.onosproject.net.Device; |
| 21 | import org.onosproject.net.SparseAnnotations; |
andrea | fe3308f | 2015-10-06 15:51:25 -0700 | [diff] [blame] | 22 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 23 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 24 | import org.onosproject.net.device.DeviceDescription; |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 25 | |
Sho SHIMIZU | 0044f57 | 2015-09-09 14:14:59 -0700 | [diff] [blame] | 26 | import java.util.Objects; |
| 27 | |
andrea | fe3308f | 2015-10-06 15:51:25 -0700 | [diff] [blame] | 28 | import static com.google.common.base.Preconditions.checkNotNull; |
andrea | fe3308f | 2015-10-06 15:51:25 -0700 | [diff] [blame] | 29 | |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 30 | /** |
| 31 | * Implementations of merge policies for various sources of device configuration |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 32 | * information. This includes applications, providers, and network configurations. |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 33 | */ |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 34 | public final class BasicDeviceOperator extends BasicElementOperator { |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 35 | |
| 36 | private BasicDeviceOperator() { |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Generates a DeviceDescription containing fields from a DeviceDescription and |
| 41 | * a DeviceConfig. |
| 42 | * |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 43 | * @param cfg the device config entity from network config |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 44 | * @param descr a DeviceDescription |
| 45 | * @return DeviceDescription based on both sources |
| 46 | */ |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 47 | public static DeviceDescription combine(BasicDeviceConfig cfg, DeviceDescription descr) { |
| 48 | if (cfg == null || descr == null) { |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 49 | return descr; |
| 50 | } |
| 51 | |
| 52 | Device.Type type = descr.type(); |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 53 | if (cfg.type() != null && cfg.type() != type) { |
| 54 | type = cfg.type(); |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 55 | } |
Andrea Campanella | b75b488 | 2016-01-15 15:15:09 -0800 | [diff] [blame] | 56 | String manufacturer = descr.manufacturer(); |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 57 | if (cfg.manufacturer() != null && !cfg.manufacturer().equals(manufacturer)) { |
| 58 | manufacturer = cfg.manufacturer(); |
Andrea Campanella | b75b488 | 2016-01-15 15:15:09 -0800 | [diff] [blame] | 59 | } |
| 60 | String hwVersion = descr.hwVersion(); |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 61 | if (cfg.hwVersion() != null && !cfg.hwVersion().equals(hwVersion)) { |
| 62 | hwVersion = cfg.hwVersion(); |
Andrea Campanella | b75b488 | 2016-01-15 15:15:09 -0800 | [diff] [blame] | 63 | } |
| 64 | String swVersion = descr.swVersion(); |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 65 | if (cfg.swVersion() != null && !cfg.swVersion().equals(swVersion)) { |
| 66 | swVersion = cfg.swVersion(); |
Andrea Campanella | b75b488 | 2016-01-15 15:15:09 -0800 | [diff] [blame] | 67 | } |
| 68 | String serial = descr.serialNumber(); |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 69 | if (cfg.serial() != null && !cfg.serial().equals(serial)) { |
| 70 | serial = cfg.serial(); |
Andrea Campanella | b75b488 | 2016-01-15 15:15:09 -0800 | [diff] [blame] | 71 | } |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 72 | |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 73 | SparseAnnotations sa = combine(cfg, descr.annotations()); |
Andrea Campanella | b75b488 | 2016-01-15 15:15:09 -0800 | [diff] [blame] | 74 | return new DefaultDeviceDescription(descr.deviceUri(), type, manufacturer, |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 75 | hwVersion, swVersion, |
| 76 | serial, descr.chassisId(), |
| 77 | descr.isDefaultAvailable(), sa); |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Generates an annotation from an existing annotation and DeviceConfig. |
| 82 | * |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 83 | * @param cfg the device config entity from network config |
andrea | fe3308f | 2015-10-06 15:51:25 -0700 | [diff] [blame] | 84 | * @param an the annotation |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 85 | * @return annotation combining both sources |
| 86 | */ |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 87 | public static SparseAnnotations combine(BasicDeviceConfig cfg, SparseAnnotations an) { |
| 88 | DefaultAnnotations.Builder builder = DefaultAnnotations.builder(); |
Yuta HIGUCHI | 9eed0b1 | 2017-06-07 11:59:18 -0700 | [diff] [blame] | 89 | builder.putAll(an); |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 90 | if (!Objects.equals(cfg.driver(), an.value(AnnotationKeys.DRIVER))) { |
| 91 | builder.set(AnnotationKeys.DRIVER, cfg.driver()); |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 92 | } |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 93 | |
| 94 | combineElementAnnotations(cfg, builder); |
| 95 | |
| 96 | if (cfg.managementAddress() != null) { |
| 97 | builder.set(AnnotationKeys.MANAGEMENT_ADDRESS, cfg.managementAddress()); |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 98 | } |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 99 | |
Yuta HIGUCHI | 9eed0b1 | 2017-06-07 11:59:18 -0700 | [diff] [blame] | 100 | return builder.build(); |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 101 | } |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 102 | |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 103 | /** |
| 104 | * Returns a description of the given device. |
| 105 | * |
| 106 | * @param device the device |
| 107 | * @return a description of the device |
| 108 | */ |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 109 | public static DeviceDescription descriptionOf(Device device) { |
| 110 | checkNotNull(device, "Must supply non-null Device"); |
| 111 | return new DefaultDeviceDescription(device.id().uri(), device.type(), |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 112 | device.manufacturer(), device.hwVersion(), |
| 113 | device.swVersion(), device.serialNumber(), |
| 114 | device.chassisId(), (SparseAnnotations) device.annotations()); |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 115 | } |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 116 | } |