Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-2015 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 | */ |
| 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.ConfigOperator; |
| 23 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 24 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 25 | import org.onosproject.net.device.DeviceDescription; |
| 26 | import org.slf4j.Logger; |
| 27 | |
Sho SHIMIZU | 0044f57 | 2015-09-09 14:14:59 -0700 | [diff] [blame] | 28 | import java.util.Objects; |
| 29 | |
andrea | fe3308f | 2015-10-06 15:51:25 -0700 | [diff] [blame] | 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | import static org.slf4j.LoggerFactory.getLogger; |
| 32 | |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 33 | /** |
| 34 | * Implementations of merge policies for various sources of device configuration |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 35 | * information. This includes applications, providers, and network configurations. |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 36 | */ |
Ayaka Koshibe | 5373e76 | 2015-08-06 12:31:44 -0700 | [diff] [blame] | 37 | public final class BasicDeviceOperator implements ConfigOperator { |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 38 | |
Ayaka Koshibe | 0891129 | 2015-08-05 15:07:08 -0700 | [diff] [blame] | 39 | protected static final double DEFAULT_COORD = -1.0; |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 40 | private static final Logger log = getLogger(BasicDeviceOperator.class); |
| 41 | |
| 42 | private BasicDeviceOperator() { |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Generates a DeviceDescription containing fields from a DeviceDescription and |
| 47 | * a DeviceConfig. |
| 48 | * |
andrea | fe3308f | 2015-10-06 15:51:25 -0700 | [diff] [blame] | 49 | * @param bdc the device config entity from network config |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 50 | * @param descr a DeviceDescription |
| 51 | * @return DeviceDescription based on both sources |
| 52 | */ |
| 53 | public static DeviceDescription combine(BasicDeviceConfig bdc, DeviceDescription descr) { |
Thomas Vachuska | 1627dc8 | 2015-11-13 12:22:14 -0800 | [diff] [blame] | 54 | if (bdc == null || descr == null) { |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 55 | return descr; |
| 56 | } |
| 57 | |
| 58 | Device.Type type = descr.type(); |
| 59 | if (bdc.type() != null && bdc.type() != type) { |
| 60 | type = bdc.type(); |
| 61 | } |
Andrea Campanella | b75b488 | 2016-01-15 15:15:09 -0800 | [diff] [blame] | 62 | String manufacturer = descr.manufacturer(); |
| 63 | if (bdc.manufacturer() != null && !bdc.manufacturer().equals(manufacturer)) { |
| 64 | manufacturer = bdc.manufacturer(); |
| 65 | } |
| 66 | String hwVersion = descr.hwVersion(); |
| 67 | if (bdc.hwVersion() != null && !bdc.hwVersion().equals(hwVersion)) { |
| 68 | hwVersion = bdc.hwVersion(); |
| 69 | } |
| 70 | String swVersion = descr.swVersion(); |
| 71 | if (bdc.swVersion() != null && !bdc.swVersion().equals(swVersion)) { |
| 72 | swVersion = bdc.swVersion(); |
| 73 | } |
| 74 | String serial = descr.serialNumber(); |
| 75 | if (bdc.serial() != null && !bdc.serial().equals(serial)) { |
| 76 | serial = bdc.serial(); |
| 77 | } |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 78 | |
| 79 | SparseAnnotations sa = combine(bdc, descr.annotations()); |
Andrea Campanella | b75b488 | 2016-01-15 15:15:09 -0800 | [diff] [blame] | 80 | return new DefaultDeviceDescription(descr.deviceUri(), type, manufacturer, |
| 81 | hwVersion, swVersion, |
| 82 | serial, descr.chassisId(), sa); |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Generates an annotation from an existing annotation and DeviceConfig. |
| 87 | * |
| 88 | * @param bdc the device config entity from network config |
andrea | fe3308f | 2015-10-06 15:51:25 -0700 | [diff] [blame] | 89 | * @param an the annotation |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 90 | * @return annotation combining both sources |
| 91 | */ |
| 92 | public static SparseAnnotations combine(BasicDeviceConfig bdc, SparseAnnotations an) { |
| 93 | DefaultAnnotations.Builder newBuilder = DefaultAnnotations.builder(); |
Sho SHIMIZU | 0044f57 | 2015-09-09 14:14:59 -0700 | [diff] [blame] | 94 | if (!Objects.equals(bdc.driver(), an.value(AnnotationKeys.DRIVER))) { |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 95 | newBuilder.set(AnnotationKeys.DRIVER, bdc.driver()); |
| 96 | } |
| 97 | if (bdc.name() != null) { |
| 98 | newBuilder.set(AnnotationKeys.NAME, bdc.name()); |
| 99 | } |
| 100 | if (bdc.latitude() != DEFAULT_COORD) { |
| 101 | newBuilder.set(AnnotationKeys.LATITUDE, Double.toString(bdc.latitude())); |
| 102 | } |
| 103 | if (bdc.longitude() != DEFAULT_COORD) { |
| 104 | newBuilder.set(AnnotationKeys.LONGITUDE, Double.toString(bdc.longitude())); |
| 105 | } |
| 106 | if (bdc.rackAddress() != null) { |
Ayaka Koshibe | 0891129 | 2015-08-05 15:07:08 -0700 | [diff] [blame] | 107 | newBuilder.set(AnnotationKeys.RACK_ADDRESS, bdc.rackAddress()); |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 108 | } |
| 109 | if (bdc.owner() != null) { |
Ayaka Koshibe | 0891129 | 2015-08-05 15:07:08 -0700 | [diff] [blame] | 110 | newBuilder.set(AnnotationKeys.OWNER, bdc.owner()); |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 111 | } |
andrea | fe3308f | 2015-10-06 15:51:25 -0700 | [diff] [blame] | 112 | if (bdc.managementAddress() != null) { |
| 113 | newBuilder.set(AnnotationKeys.MANAGEMENT_ADDRESS, bdc.managementAddress()); |
| 114 | } |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 115 | DefaultAnnotations newAnnotations = newBuilder.build(); |
| 116 | return DefaultAnnotations.union(an, newAnnotations); |
| 117 | } |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 118 | |
| 119 | public static DeviceDescription descriptionOf(Device device) { |
| 120 | checkNotNull(device, "Must supply non-null Device"); |
| 121 | return new DefaultDeviceDescription(device.id().uri(), device.type(), |
| 122 | device.manufacturer(), device.hwVersion(), |
| 123 | device.swVersion(), device.serialNumber(), |
| 124 | device.chassisId(), (SparseAnnotations) device.annotations()); |
| 125 | } |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 126 | } |