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 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
| 20 | import org.onosproject.incubator.net.config.basics.BasicDeviceConfig; |
| 21 | import org.onosproject.net.AnnotationKeys; |
| 22 | import org.onosproject.net.DefaultAnnotations; |
| 23 | import org.onosproject.net.Device; |
| 24 | import org.onosproject.net.SparseAnnotations; |
| 25 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 26 | import org.onosproject.net.device.DeviceDescription; |
| 27 | import org.slf4j.Logger; |
| 28 | |
| 29 | /** |
| 30 | * Implementations of merge policies for various sources of device configuration |
| 31 | * information. This includes applications, provides, and network configurations. |
| 32 | */ |
Ayaka Koshibe | 0891129 | 2015-08-05 15:07:08 -0700 | [diff] [blame^] | 33 | public final class BasicDeviceOperator { |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 34 | |
Ayaka Koshibe | 0891129 | 2015-08-05 15:07:08 -0700 | [diff] [blame^] | 35 | protected static final double DEFAULT_COORD = -1.0; |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 36 | private static final Logger log = getLogger(BasicDeviceOperator.class); |
| 37 | |
| 38 | private BasicDeviceOperator() { |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Generates a DeviceDescription containing fields from a DeviceDescription and |
| 43 | * a DeviceConfig. |
| 44 | * |
| 45 | * @param bdc the device config entity from network config |
| 46 | * @param descr a DeviceDescription |
| 47 | * @return DeviceDescription based on both sources |
| 48 | */ |
| 49 | public static DeviceDescription combine(BasicDeviceConfig bdc, DeviceDescription descr) { |
| 50 | if (bdc == null) { |
| 51 | return descr; |
| 52 | } |
| 53 | |
| 54 | Device.Type type = descr.type(); |
| 55 | if (bdc.type() != null && bdc.type() != type) { |
| 56 | type = bdc.type(); |
| 57 | } |
| 58 | |
| 59 | SparseAnnotations sa = combine(bdc, descr.annotations()); |
| 60 | return new DefaultDeviceDescription(descr.deviceURI(), type, descr.manufacturer(), |
| 61 | descr.hwVersion(), descr.swVersion(), |
| 62 | descr.serialNumber(), descr.chassisId(), sa); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Generates an annotation from an existing annotation and DeviceConfig. |
| 67 | * |
| 68 | * @param bdc the device config entity from network config |
| 69 | * @param an the annotation |
| 70 | * @return annotation combining both sources |
| 71 | */ |
| 72 | public static SparseAnnotations combine(BasicDeviceConfig bdc, SparseAnnotations an) { |
| 73 | DefaultAnnotations.Builder newBuilder = DefaultAnnotations.builder(); |
Ayaka Koshibe | 0891129 | 2015-08-05 15:07:08 -0700 | [diff] [blame^] | 74 | if (bdc.driver() != an.value(AnnotationKeys.DRIVER)) { |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 75 | newBuilder.set(AnnotationKeys.DRIVER, bdc.driver()); |
| 76 | } |
| 77 | if (bdc.name() != null) { |
| 78 | newBuilder.set(AnnotationKeys.NAME, bdc.name()); |
| 79 | } |
| 80 | if (bdc.latitude() != DEFAULT_COORD) { |
| 81 | newBuilder.set(AnnotationKeys.LATITUDE, Double.toString(bdc.latitude())); |
| 82 | } |
| 83 | if (bdc.longitude() != DEFAULT_COORD) { |
| 84 | newBuilder.set(AnnotationKeys.LONGITUDE, Double.toString(bdc.longitude())); |
| 85 | } |
| 86 | if (bdc.rackAddress() != null) { |
Ayaka Koshibe | 0891129 | 2015-08-05 15:07:08 -0700 | [diff] [blame^] | 87 | newBuilder.set(AnnotationKeys.RACK_ADDRESS, bdc.rackAddress()); |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 88 | } |
| 89 | if (bdc.owner() != null) { |
Ayaka Koshibe | 0891129 | 2015-08-05 15:07:08 -0700 | [diff] [blame^] | 90 | newBuilder.set(AnnotationKeys.OWNER, bdc.owner()); |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 91 | } |
| 92 | DefaultAnnotations newAnnotations = newBuilder.build(); |
| 93 | return DefaultAnnotations.union(an, newAnnotations); |
| 94 | } |
| 95 | } |