blob: 4af8000aaf4ec3ca8f57a3c60d8583f69d829a73 [file] [log] [blame]
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -07001/*
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 */
16package org.onosproject.net.device.impl;
17
18import static org.slf4j.LoggerFactory.getLogger;
19
20import org.onosproject.incubator.net.config.basics.BasicDeviceConfig;
21import org.onosproject.net.AnnotationKeys;
22import org.onosproject.net.DefaultAnnotations;
23import org.onosproject.net.Device;
24import org.onosproject.net.SparseAnnotations;
25import org.onosproject.net.device.DefaultDeviceDescription;
26import org.onosproject.net.device.DeviceDescription;
27import 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 Koshibe08911292015-08-05 15:07:08 -070033public final class BasicDeviceOperator {
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070034
Ayaka Koshibe08911292015-08-05 15:07:08 -070035 protected static final double DEFAULT_COORD = -1.0;
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070036 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 Koshibe08911292015-08-05 15:07:08 -070074 if (bdc.driver() != an.value(AnnotationKeys.DRIVER)) {
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070075 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 Koshibe08911292015-08-05 15:07:08 -070087 newBuilder.set(AnnotationKeys.RACK_ADDRESS, bdc.rackAddress());
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070088 }
89 if (bdc.owner() != null) {
Ayaka Koshibe08911292015-08-05 15:07:08 -070090 newBuilder.set(AnnotationKeys.OWNER, bdc.owner());
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070091 }
92 DefaultAnnotations newAnnotations = newBuilder.build();
93 return DefaultAnnotations.union(an, newAnnotations);
94 }
95}