blob: 255ee4685834b9c7762acf6706ba175f8b1bbcb4 [file] [log] [blame]
Ayaka Koshibef1cedf42015-08-05 18:03:56 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Ayaka Koshibef1cedf42015-08-05 18:03:56 -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.host.impl;
17
Thomas Vachuska36008462016-01-07 15:38:20 -080018import org.onlab.packet.IpAddress;
19import org.onosproject.net.AnnotationKeys;
20import org.onosproject.net.ConnectPoint;
21import org.onosproject.net.DefaultAnnotations;
22import org.onosproject.net.HostLocation;
23import org.onosproject.net.SparseAnnotations;
Ray Milkeya4122362015-08-18 15:19:08 -070024import org.onosproject.net.config.ConfigOperator;
Thomas Vachuska4998caa2015-08-26 13:28:38 -070025import org.onosproject.net.config.basics.BasicHostConfig;
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070026import org.onosproject.net.host.DefaultHostDescription;
27import org.onosproject.net.host.HostDescription;
28
Thomas Vachuska36008462016-01-07 15:38:20 -080029import java.util.Set;
30
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070031/**
32 * Implementations of merge policies for various sources of host configuration
Thomas Vachuska36008462016-01-07 15:38:20 -080033 * information. This includes applications, providers, and network configurations.
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070034 */
35public final class BasicHostOperator implements ConfigOperator {
36
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070037 private BasicHostOperator() {
38 }
39
40 /**
41 * Generates a HostDescription containing fields from a HostDescription and
42 * a HostConfig.
43 *
Thomas Vachuska36008462016-01-07 15:38:20 -080044 * @param cfg the host config entity from network config
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070045 * @param descr a HostDescription
46 * @return HostDescription based on both sources
47 */
48 public static HostDescription combine(BasicHostConfig cfg, HostDescription descr) {
49 if (cfg == null) {
50 return descr;
51 }
Thomas Vachuska36008462016-01-07 15:38:20 -080052
53 HostLocation location = descr.location();
54 ConnectPoint cfgLocation = cfg.location();
55 if (cfgLocation != null) {
56 location = new HostLocation(cfgLocation, System.currentTimeMillis());
57 }
58
59 Set<IpAddress> ipAddresses = descr.ipAddress();
60 Set<IpAddress> cfgIpAddresses = cfg.ipAddresses();
61 if (cfgIpAddresses != null) {
62 ipAddresses = cfgIpAddresses;
63 }
64
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070065 SparseAnnotations sa = combine(cfg, descr.annotations());
Thomas Vachuska36008462016-01-07 15:38:20 -080066 return new DefaultHostDescription(descr.hwAddress(), descr.vlan(),
67 location, ipAddresses, sa);
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070068 }
69
70 /**
71 * Generates an annotation from an existing annotation and HostConfig.
72 *
73 * @param cfg the device config entity from network config
Thomas Vachuska36008462016-01-07 15:38:20 -080074 * @param an the annotation
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070075 * @return annotation combining both sources
76 */
77 public static SparseAnnotations combine(BasicHostConfig cfg, SparseAnnotations an) {
78 DefaultAnnotations.Builder newBuilder = DefaultAnnotations.builder();
79 if (cfg.name() != null) {
80 newBuilder.set(AnnotationKeys.NAME, cfg.name());
81 }
Simon Huntf4fd2a22016-08-10 15:41:09 -070082 if (cfg.geoCoordsSet()) {
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070083 newBuilder.set(AnnotationKeys.LATITUDE, Double.toString(cfg.latitude()));
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070084 newBuilder.set(AnnotationKeys.LONGITUDE, Double.toString(cfg.longitude()));
85 }
86 if (cfg.rackAddress() != null) {
87 newBuilder.set(AnnotationKeys.RACK_ADDRESS, cfg.rackAddress());
88 }
89 if (cfg.owner() != null) {
90 newBuilder.set(AnnotationKeys.OWNER, cfg.owner());
91 }
92 return DefaultAnnotations.union(an, newBuilder.build());
93 }
94}