blob: dd562175caee78daacd295f7d12127718f302071 [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;
Thomas Vachuska36008462016-01-07 15:38:20 -080019import org.onosproject.net.ConnectPoint;
20import org.onosproject.net.DefaultAnnotations;
Simon Huntffbad3b2017-05-16 15:37:51 -070021import org.onosproject.net.Host;
Thomas Vachuska36008462016-01-07 15:38:20 -080022import org.onosproject.net.HostLocation;
23import org.onosproject.net.SparseAnnotations;
Thomas Vachuska4998caa2015-08-26 13:28:38 -070024import org.onosproject.net.config.basics.BasicHostConfig;
Simon Huntbc30e682017-02-15 18:39:23 -080025import org.onosproject.net.device.impl.BasicElementOperator;
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
Simon Huntffbad3b2017-05-16 15:37:51 -070031import static com.google.common.base.Preconditions.checkNotNull;
32
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070033/**
34 * Implementations of merge policies for various sources of host configuration
Thomas Vachuska36008462016-01-07 15:38:20 -080035 * information. This includes applications, providers, and network configurations.
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070036 */
Simon Huntbc30e682017-02-15 18:39:23 -080037public final class BasicHostOperator extends BasicElementOperator {
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070038
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070039 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 */
Simon Hunt1e20dae2016-10-28 11:26:26 -070050 public static HostDescription combine(BasicHostConfig cfg,
51 HostDescription descr) {
Simon Huntffbad3b2017-05-16 15:37:51 -070052 if (cfg == null || descr == null) {
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070053 return descr;
54 }
Thomas Vachuska36008462016-01-07 15:38:20 -080055
56 HostLocation location = descr.location();
57 ConnectPoint cfgLocation = cfg.location();
58 if (cfgLocation != null) {
59 location = new HostLocation(cfgLocation, System.currentTimeMillis());
60 }
61
62 Set<IpAddress> ipAddresses = descr.ipAddress();
63 Set<IpAddress> cfgIpAddresses = cfg.ipAddresses();
64 if (cfgIpAddresses != null) {
65 ipAddresses = cfgIpAddresses;
66 }
67
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070068 SparseAnnotations sa = combine(cfg, descr.annotations());
Thomas Vachuska36008462016-01-07 15:38:20 -080069 return new DefaultHostDescription(descr.hwAddress(), descr.vlan(),
Simon Huntffbad3b2017-05-16 15:37:51 -070070 location, ipAddresses,
71 descr.configured(), sa);
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070072 }
73
74 /**
75 * Generates an annotation from an existing annotation and HostConfig.
76 *
Simon Huntbc30e682017-02-15 18:39:23 -080077 * @param cfg the host config entity from network config
Thomas Vachuska36008462016-01-07 15:38:20 -080078 * @param an the annotation
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070079 * @return annotation combining both sources
80 */
Simon Huntbc30e682017-02-15 18:39:23 -080081 public static SparseAnnotations combine(BasicHostConfig cfg, SparseAnnotations an) {
82 DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
83
84 combineElementAnnotations(cfg, builder);
85
86 return DefaultAnnotations.union(an, builder.build());
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070087 }
Simon Huntffbad3b2017-05-16 15:37:51 -070088
89 /**
90 * Returns a description of the given host.
91 *
92 * @param host the host
93 * @return a description of the host
94 */
95 public static HostDescription descriptionOf(Host host) {
96 checkNotNull(host, "Must supply a non-null Host");
97 return new DefaultHostDescription(host.mac(), host.vlan(), host.location(),
98 host.ipAddresses(), host.configured(),
99 (SparseAnnotations) host.annotations());
100 }
Ayaka Koshibef1cedf42015-08-05 18:03:56 -0700101}