blob: a35eecfedaa1ed47d8cb725887a99d8b7e2604a2 [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 */
Simon Hunt1e20dae2016-10-28 11:26:26 -070048 public static HostDescription combine(BasicHostConfig cfg,
49 HostDescription descr) {
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070050 if (cfg == null) {
51 return descr;
52 }
Thomas Vachuska36008462016-01-07 15:38:20 -080053
54 HostLocation location = descr.location();
55 ConnectPoint cfgLocation = cfg.location();
56 if (cfgLocation != null) {
57 location = new HostLocation(cfgLocation, System.currentTimeMillis());
58 }
59
60 Set<IpAddress> ipAddresses = descr.ipAddress();
61 Set<IpAddress> cfgIpAddresses = cfg.ipAddresses();
62 if (cfgIpAddresses != null) {
63 ipAddresses = cfgIpAddresses;
64 }
65
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070066 SparseAnnotations sa = combine(cfg, descr.annotations());
Thomas Vachuska36008462016-01-07 15:38:20 -080067 return new DefaultHostDescription(descr.hwAddress(), descr.vlan(),
Simon Hunt1e20dae2016-10-28 11:26:26 -070068 location, ipAddresses,
69 descr.configured(), 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 */
Simon Hunt1e20dae2016-10-28 11:26:26 -070079 public static SparseAnnotations combine(BasicHostConfig cfg,
80 SparseAnnotations an) {
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070081 DefaultAnnotations.Builder newBuilder = DefaultAnnotations.builder();
82 if (cfg.name() != null) {
83 newBuilder.set(AnnotationKeys.NAME, cfg.name());
84 }
Simon Hunt1e20dae2016-10-28 11:26:26 -070085 if (cfg.uiType() != null) {
86 newBuilder.set(AnnotationKeys.UI_TYPE, cfg.uiType());
87 }
Simon Huntf4fd2a22016-08-10 15:41:09 -070088 if (cfg.geoCoordsSet()) {
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070089 newBuilder.set(AnnotationKeys.LATITUDE, Double.toString(cfg.latitude()));
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070090 newBuilder.set(AnnotationKeys.LONGITUDE, Double.toString(cfg.longitude()));
91 }
92 if (cfg.rackAddress() != null) {
93 newBuilder.set(AnnotationKeys.RACK_ADDRESS, cfg.rackAddress());
94 }
95 if (cfg.owner() != null) {
96 newBuilder.set(AnnotationKeys.OWNER, cfg.owner());
97 }
98 return DefaultAnnotations.union(an, newBuilder.build());
99 }
100}