blob: 24fcf4c83c9eb844363bcf79ad662ac5fc564b29 [file] [log] [blame]
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -07003 *
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.basics.BasicDeviceConfig;
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070023import org.onosproject.net.device.DefaultDeviceDescription;
24import org.onosproject.net.device.DeviceDescription;
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070025
Sho SHIMIZU0044f572015-09-09 14:14:59 -070026import java.util.Objects;
27
andreafe3308f2015-10-06 15:51:25 -070028import static com.google.common.base.Preconditions.checkNotNull;
andreafe3308f2015-10-06 15:51:25 -070029
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070030/**
31 * Implementations of merge policies for various sources of device configuration
Thomas Vachuska36008462016-01-07 15:38:20 -080032 * information. This includes applications, providers, and network configurations.
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070033 */
Simon Huntbc30e682017-02-15 18:39:23 -080034public final class BasicDeviceOperator extends BasicElementOperator {
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070035
36 private BasicDeviceOperator() {
37 }
38
39 /**
40 * Generates a DeviceDescription containing fields from a DeviceDescription and
41 * a DeviceConfig.
42 *
Simon Huntbc30e682017-02-15 18:39:23 -080043 * @param cfg the device config entity from network config
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070044 * @param descr a DeviceDescription
45 * @return DeviceDescription based on both sources
46 */
Simon Huntbc30e682017-02-15 18:39:23 -080047 public static DeviceDescription combine(BasicDeviceConfig cfg, DeviceDescription descr) {
48 if (cfg == null || descr == null) {
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070049 return descr;
50 }
51
52 Device.Type type = descr.type();
Simon Huntbc30e682017-02-15 18:39:23 -080053 if (cfg.type() != null && cfg.type() != type) {
54 type = cfg.type();
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070055 }
Andrea Campanellab75b4882016-01-15 15:15:09 -080056 String manufacturer = descr.manufacturer();
Simon Huntbc30e682017-02-15 18:39:23 -080057 if (cfg.manufacturer() != null && !cfg.manufacturer().equals(manufacturer)) {
58 manufacturer = cfg.manufacturer();
Andrea Campanellab75b4882016-01-15 15:15:09 -080059 }
60 String hwVersion = descr.hwVersion();
Simon Huntbc30e682017-02-15 18:39:23 -080061 if (cfg.hwVersion() != null && !cfg.hwVersion().equals(hwVersion)) {
62 hwVersion = cfg.hwVersion();
Andrea Campanellab75b4882016-01-15 15:15:09 -080063 }
64 String swVersion = descr.swVersion();
Simon Huntbc30e682017-02-15 18:39:23 -080065 if (cfg.swVersion() != null && !cfg.swVersion().equals(swVersion)) {
66 swVersion = cfg.swVersion();
Andrea Campanellab75b4882016-01-15 15:15:09 -080067 }
68 String serial = descr.serialNumber();
Simon Huntbc30e682017-02-15 18:39:23 -080069 if (cfg.serial() != null && !cfg.serial().equals(serial)) {
70 serial = cfg.serial();
Andrea Campanellab75b4882016-01-15 15:15:09 -080071 }
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070072
Simon Huntbc30e682017-02-15 18:39:23 -080073 SparseAnnotations sa = combine(cfg, descr.annotations());
Andrea Campanellab75b4882016-01-15 15:15:09 -080074 return new DefaultDeviceDescription(descr.deviceUri(), type, manufacturer,
Simon Huntbc30e682017-02-15 18:39:23 -080075 hwVersion, swVersion,
76 serial, descr.chassisId(),
77 descr.isDefaultAvailable(), sa);
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070078 }
79
80 /**
81 * Generates an annotation from an existing annotation and DeviceConfig.
82 *
Simon Huntbc30e682017-02-15 18:39:23 -080083 * @param cfg the device config entity from network config
andreafe3308f2015-10-06 15:51:25 -070084 * @param an the annotation
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070085 * @return annotation combining both sources
86 */
Simon Huntbc30e682017-02-15 18:39:23 -080087 public static SparseAnnotations combine(BasicDeviceConfig cfg, SparseAnnotations an) {
88 DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
Yuta HIGUCHI9eed0b12017-06-07 11:59:18 -070089 builder.putAll(an);
Simon Huntbc30e682017-02-15 18:39:23 -080090 if (!Objects.equals(cfg.driver(), an.value(AnnotationKeys.DRIVER))) {
91 builder.set(AnnotationKeys.DRIVER, cfg.driver());
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070092 }
Simon Huntbc30e682017-02-15 18:39:23 -080093
94 combineElementAnnotations(cfg, builder);
95
96 if (cfg.managementAddress() != null) {
97 builder.set(AnnotationKeys.MANAGEMENT_ADDRESS, cfg.managementAddress());
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070098 }
Simon Huntbc30e682017-02-15 18:39:23 -080099
Yuta HIGUCHI9eed0b12017-06-07 11:59:18 -0700100 return builder.build();
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -0700101 }
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700102
Simon Huntbc30e682017-02-15 18:39:23 -0800103 /**
104 * Returns a description of the given device.
105 *
106 * @param device the device
107 * @return a description of the device
108 */
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700109 public static DeviceDescription descriptionOf(Device device) {
110 checkNotNull(device, "Must supply non-null Device");
111 return new DefaultDeviceDescription(device.id().uri(), device.type(),
Simon Huntbc30e682017-02-15 18:39:23 -0800112 device.manufacturer(), device.hwVersion(),
113 device.swVersion(), device.serialNumber(),
114 device.chassisId(), (SparseAnnotations) device.annotations());
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700115 }
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -0700116}