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