blob: 9645c5fba9a1376fb81eac8be38a9c57bee0177b [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
37 protected static final double DEFAULT_COORD = -1.0;
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070038
39 private BasicHostOperator() {
40 }
41
42 /**
43 * Generates a HostDescription containing fields from a HostDescription and
44 * a HostConfig.
45 *
Thomas Vachuska36008462016-01-07 15:38:20 -080046 * @param cfg the host config entity from network config
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070047 * @param descr a HostDescription
48 * @return HostDescription based on both sources
49 */
50 public static HostDescription combine(BasicHostConfig cfg, HostDescription descr) {
51 if (cfg == null) {
52 return descr;
53 }
Thomas Vachuska36008462016-01-07 15:38:20 -080054
55 HostLocation location = descr.location();
56 ConnectPoint cfgLocation = cfg.location();
57 if (cfgLocation != null) {
58 location = new HostLocation(cfgLocation, System.currentTimeMillis());
59 }
60
61 Set<IpAddress> ipAddresses = descr.ipAddress();
62 Set<IpAddress> cfgIpAddresses = cfg.ipAddresses();
63 if (cfgIpAddresses != null) {
64 ipAddresses = cfgIpAddresses;
65 }
66
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070067 SparseAnnotations sa = combine(cfg, descr.annotations());
Thomas Vachuska36008462016-01-07 15:38:20 -080068 return new DefaultHostDescription(descr.hwAddress(), descr.vlan(),
69 location, ipAddresses, sa);
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070070 }
71
72 /**
73 * Generates an annotation from an existing annotation and HostConfig.
74 *
75 * @param cfg the device config entity from network config
Thomas Vachuska36008462016-01-07 15:38:20 -080076 * @param an the annotation
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070077 * @return annotation combining both sources
78 */
79 public static SparseAnnotations combine(BasicHostConfig cfg, SparseAnnotations an) {
80 DefaultAnnotations.Builder newBuilder = DefaultAnnotations.builder();
81 if (cfg.name() != null) {
82 newBuilder.set(AnnotationKeys.NAME, cfg.name());
83 }
84 if (cfg.latitude() != DEFAULT_COORD) {
85 newBuilder.set(AnnotationKeys.LATITUDE, Double.toString(cfg.latitude()));
86 }
87 if (cfg.longitude() != DEFAULT_COORD) {
88 newBuilder.set(AnnotationKeys.LONGITUDE, Double.toString(cfg.longitude()));
89 }
90 if (cfg.rackAddress() != null) {
91 newBuilder.set(AnnotationKeys.RACK_ADDRESS, cfg.rackAddress());
92 }
93 if (cfg.owner() != null) {
94 newBuilder.set(AnnotationKeys.OWNER, cfg.owner());
95 }
96 return DefaultAnnotations.union(an, newBuilder.build());
97 }
98}