blob: a7d488b3f5c721e6e9853b7e636b33d9d7b08c86 [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;
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -070019import static com.google.common.base.Preconditions.checkNotNull;
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070020
Ray Milkeya4122362015-08-18 15:19:08 -070021import org.onosproject.net.config.ConfigOperator;
Thomas Vachuska4998caa2015-08-26 13:28:38 -070022import org.onosproject.net.config.basics.BasicDeviceConfig;
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070023import org.onosproject.net.AnnotationKeys;
24import org.onosproject.net.DefaultAnnotations;
25import org.onosproject.net.Device;
26import org.onosproject.net.SparseAnnotations;
27import org.onosproject.net.device.DefaultDeviceDescription;
28import org.onosproject.net.device.DeviceDescription;
29import org.slf4j.Logger;
30
31/**
32 * Implementations of merge policies for various sources of device configuration
33 * information. This includes applications, provides, and network configurations.
34 */
Ayaka Koshibe5373e762015-08-06 12:31:44 -070035public final class BasicDeviceOperator implements ConfigOperator {
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070036
Ayaka Koshibe08911292015-08-05 15:07:08 -070037 protected static final double DEFAULT_COORD = -1.0;
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070038 private static final Logger log = getLogger(BasicDeviceOperator.class);
39
40 private BasicDeviceOperator() {
41 }
42
43 /**
44 * Generates a DeviceDescription containing fields from a DeviceDescription and
45 * a DeviceConfig.
46 *
47 * @param bdc the device config entity from network config
48 * @param descr a DeviceDescription
49 * @return DeviceDescription based on both sources
50 */
51 public static DeviceDescription combine(BasicDeviceConfig bdc, DeviceDescription descr) {
52 if (bdc == null) {
53 return descr;
54 }
55
56 Device.Type type = descr.type();
57 if (bdc.type() != null && bdc.type() != type) {
58 type = bdc.type();
59 }
60
61 SparseAnnotations sa = combine(bdc, descr.annotations());
62 return new DefaultDeviceDescription(descr.deviceURI(), type, descr.manufacturer(),
63 descr.hwVersion(), descr.swVersion(),
64 descr.serialNumber(), descr.chassisId(), sa);
65 }
66
67 /**
68 * Generates an annotation from an existing annotation and DeviceConfig.
69 *
70 * @param bdc the device config entity from network config
71 * @param an the annotation
72 * @return annotation combining both sources
73 */
74 public static SparseAnnotations combine(BasicDeviceConfig bdc, SparseAnnotations an) {
75 DefaultAnnotations.Builder newBuilder = DefaultAnnotations.builder();
Ayaka Koshibe08911292015-08-05 15:07:08 -070076 if (bdc.driver() != an.value(AnnotationKeys.DRIVER)) {
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070077 newBuilder.set(AnnotationKeys.DRIVER, bdc.driver());
78 }
79 if (bdc.name() != null) {
80 newBuilder.set(AnnotationKeys.NAME, bdc.name());
81 }
82 if (bdc.latitude() != DEFAULT_COORD) {
83 newBuilder.set(AnnotationKeys.LATITUDE, Double.toString(bdc.latitude()));
84 }
85 if (bdc.longitude() != DEFAULT_COORD) {
86 newBuilder.set(AnnotationKeys.LONGITUDE, Double.toString(bdc.longitude()));
87 }
88 if (bdc.rackAddress() != null) {
Ayaka Koshibe08911292015-08-05 15:07:08 -070089 newBuilder.set(AnnotationKeys.RACK_ADDRESS, bdc.rackAddress());
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070090 }
91 if (bdc.owner() != null) {
Ayaka Koshibe08911292015-08-05 15:07:08 -070092 newBuilder.set(AnnotationKeys.OWNER, bdc.owner());
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070093 }
94 DefaultAnnotations newAnnotations = newBuilder.build();
95 return DefaultAnnotations.union(an, newAnnotations);
96 }
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -070097
98 public static DeviceDescription descriptionOf(Device device) {
99 checkNotNull(device, "Must supply non-null Device");
100 return new DefaultDeviceDescription(device.id().uri(), device.type(),
101 device.manufacturer(), device.hwVersion(),
102 device.swVersion(), device.serialNumber(),
103 device.chassisId(), (SparseAnnotations) device.annotations());
104 }
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -0700105}