blob: a498b3f4c5755bc12b502e6eb7f6192c8f4bc4f9 [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
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070018import org.onosproject.net.AnnotationKeys;
19import org.onosproject.net.DefaultAnnotations;
20import org.onosproject.net.Device;
21import org.onosproject.net.SparseAnnotations;
andreafe3308f2015-10-06 15:51:25 -070022import org.onosproject.net.config.ConfigOperator;
23import org.onosproject.net.config.basics.BasicDeviceConfig;
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070024import org.onosproject.net.device.DefaultDeviceDescription;
25import org.onosproject.net.device.DeviceDescription;
26import org.slf4j.Logger;
27
Sho SHIMIZU0044f572015-09-09 14:14:59 -070028import java.util.Objects;
29
andreafe3308f2015-10-06 15:51:25 -070030import static com.google.common.base.Preconditions.checkNotNull;
31import static org.slf4j.LoggerFactory.getLogger;
32
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070033/**
34 * Implementations of merge policies for various sources of device configuration
35 * information. This includes applications, provides, and network configurations.
36 */
Ayaka Koshibe5373e762015-08-06 12:31:44 -070037public final class BasicDeviceOperator implements ConfigOperator {
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070038
Ayaka Koshibe08911292015-08-05 15:07:08 -070039 protected static final double DEFAULT_COORD = -1.0;
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070040 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 *
andreafe3308f2015-10-06 15:51:25 -070049 * @param bdc the device config entity from network config
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070050 * @param descr a DeviceDescription
51 * @return DeviceDescription based on both sources
52 */
53 public static DeviceDescription combine(BasicDeviceConfig bdc, DeviceDescription descr) {
Thomas Vachuska1627dc82015-11-13 12:22:14 -080054 if (bdc == null || descr == null) {
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070055 return descr;
56 }
57
58 Device.Type type = descr.type();
59 if (bdc.type() != null && bdc.type() != type) {
60 type = bdc.type();
61 }
62
63 SparseAnnotations sa = combine(bdc, descr.annotations());
Jonathan Hartd9df7bd2015-11-10 17:10:25 -080064 return new DefaultDeviceDescription(descr.deviceUri(), type, descr.manufacturer(),
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070065 descr.hwVersion(), descr.swVersion(),
66 descr.serialNumber(), descr.chassisId(), sa);
67 }
68
69 /**
70 * Generates an annotation from an existing annotation and DeviceConfig.
71 *
72 * @param bdc the device config entity from network config
andreafe3308f2015-10-06 15:51:25 -070073 * @param an the annotation
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070074 * @return annotation combining both sources
75 */
76 public static SparseAnnotations combine(BasicDeviceConfig bdc, SparseAnnotations an) {
77 DefaultAnnotations.Builder newBuilder = DefaultAnnotations.builder();
Sho SHIMIZU0044f572015-09-09 14:14:59 -070078 if (!Objects.equals(bdc.driver(), an.value(AnnotationKeys.DRIVER))) {
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070079 newBuilder.set(AnnotationKeys.DRIVER, bdc.driver());
80 }
81 if (bdc.name() != null) {
82 newBuilder.set(AnnotationKeys.NAME, bdc.name());
83 }
84 if (bdc.latitude() != DEFAULT_COORD) {
85 newBuilder.set(AnnotationKeys.LATITUDE, Double.toString(bdc.latitude()));
86 }
87 if (bdc.longitude() != DEFAULT_COORD) {
88 newBuilder.set(AnnotationKeys.LONGITUDE, Double.toString(bdc.longitude()));
89 }
90 if (bdc.rackAddress() != null) {
Ayaka Koshibe08911292015-08-05 15:07:08 -070091 newBuilder.set(AnnotationKeys.RACK_ADDRESS, bdc.rackAddress());
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070092 }
93 if (bdc.owner() != null) {
Ayaka Koshibe08911292015-08-05 15:07:08 -070094 newBuilder.set(AnnotationKeys.OWNER, bdc.owner());
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070095 }
andreafe3308f2015-10-06 15:51:25 -070096 if (bdc.managementAddress() != null) {
97 newBuilder.set(AnnotationKeys.MANAGEMENT_ADDRESS, bdc.managementAddress());
98 }
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070099 DefaultAnnotations newAnnotations = newBuilder.build();
100 return DefaultAnnotations.union(an, newAnnotations);
101 }
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700102
103 public static DeviceDescription descriptionOf(Device device) {
104 checkNotNull(device, "Must supply non-null Device");
105 return new DefaultDeviceDescription(device.id().uri(), device.type(),
106 device.manufacturer(), device.hwVersion(),
107 device.swVersion(), device.serialNumber(),
108 device.chassisId(), (SparseAnnotations) device.annotations());
109 }
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -0700110}