blob: 6c7a0653f40df746ade572e1884ed12cf6a86e10 [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 */
33public final class BasicDeviceOperator extends BasicDeviceConfig {
34
35 private static final Logger log = getLogger(BasicDeviceOperator.class);
36
37 private BasicDeviceOperator() {
38 }
39
40 /**
41 * Generates a DeviceDescription containing fields from a DeviceDescription and
42 * a DeviceConfig.
43 *
44 * @param bdc the device config entity from network config
45 * @param descr a DeviceDescription
46 * @return DeviceDescription based on both sources
47 */
48 public static DeviceDescription combine(BasicDeviceConfig bdc, DeviceDescription descr) {
49 if (bdc == null) {
50 return descr;
51 }
52
53 Device.Type type = descr.type();
54 if (bdc.type() != null && bdc.type() != type) {
55 type = bdc.type();
56 }
57
58 SparseAnnotations sa = combine(bdc, descr.annotations());
59 return new DefaultDeviceDescription(descr.deviceURI(), type, descr.manufacturer(),
60 descr.hwVersion(), descr.swVersion(),
61 descr.serialNumber(), descr.chassisId(), sa);
62 }
63
64 /**
65 * Generates an annotation from an existing annotation and DeviceConfig.
66 *
67 * @param bdc the device config entity from network config
68 * @param an the annotation
69 * @return annotation combining both sources
70 */
71 public static SparseAnnotations combine(BasicDeviceConfig bdc, SparseAnnotations an) {
72 DefaultAnnotations.Builder newBuilder = DefaultAnnotations.builder();
73 if (bdc.driver() != an.value(DRIVER)) {
74 newBuilder.set(AnnotationKeys.DRIVER, bdc.driver());
75 }
76 if (bdc.name() != null) {
77 newBuilder.set(AnnotationKeys.NAME, bdc.name());
78 }
79 if (bdc.latitude() != DEFAULT_COORD) {
80 newBuilder.set(AnnotationKeys.LATITUDE, Double.toString(bdc.latitude()));
81 }
82 if (bdc.longitude() != DEFAULT_COORD) {
83 newBuilder.set(AnnotationKeys.LONGITUDE, Double.toString(bdc.longitude()));
84 }
85 if (bdc.rackAddress() != null) {
86 newBuilder.set(RACK_ADDRESS, bdc.rackAddress());
87 }
88 if (bdc.owner() != null) {
89 newBuilder.set(OWNER, bdc.owner());
90 }
91 DefaultAnnotations newAnnotations = newBuilder.build();
92 return DefaultAnnotations.union(an, newAnnotations);
93 }
94}