blob: 26bfdeb614c01683c53d5f4860392e673ae67534 [file] [log] [blame]
Ayaka Koshibef1cedf42015-08-05 18:03:56 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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.DefaultAnnotations;
Simon Huntffbad3b2017-05-16 15:37:51 -070020import org.onosproject.net.Host;
Thomas Vachuska36008462016-01-07 15:38:20 -080021import org.onosproject.net.HostLocation;
22import org.onosproject.net.SparseAnnotations;
Thomas Vachuska4998caa2015-08-26 13:28:38 -070023import org.onosproject.net.config.basics.BasicHostConfig;
Simon Huntbc30e682017-02-15 18:39:23 -080024import org.onosproject.net.device.impl.BasicElementOperator;
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070025import org.onosproject.net.host.DefaultHostDescription;
26import org.onosproject.net.host.HostDescription;
27
Thomas Vachuska36008462016-01-07 15:38:20 -080028import java.util.Set;
Charles Chan61fc0d82017-04-28 14:13:36 -070029import java.util.stream.Collectors;
Thomas Vachuska36008462016-01-07 15:38:20 -080030
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
Charles Chan61fc0d82017-04-28 14:13:36 -070056 Set<HostLocation> locations = descr.locations();
57 Set<HostLocation> cfgLocations = cfg.locations();
58 if (cfgLocations != null) {
59 locations = cfgLocations.stream()
60 .map(hostLocation -> new HostLocation(hostLocation, System.currentTimeMillis()))
61 .collect(Collectors.toSet());
Thomas Vachuska36008462016-01-07 15:38:20 -080062 }
63
Charles Chaneb5bd492019-12-18 15:49:39 -080064 Set<HostLocation> auxLocations = descr.auxLocations();
65 Set<HostLocation> cfgAuxLocations = cfg.auxLocations();
66 if (cfgAuxLocations != null) {
67 auxLocations = cfgAuxLocations.stream()
68 .map(hostLocation -> new HostLocation(hostLocation, System.currentTimeMillis()))
69 .collect(Collectors.toSet());
70 }
71
Thomas Vachuska36008462016-01-07 15:38:20 -080072 Set<IpAddress> ipAddresses = descr.ipAddress();
73 Set<IpAddress> cfgIpAddresses = cfg.ipAddresses();
74 if (cfgIpAddresses != null) {
75 ipAddresses = cfgIpAddresses;
76 }
77
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070078 SparseAnnotations sa = combine(cfg, descr.annotations());
Thomas Vachuska36008462016-01-07 15:38:20 -080079 return new DefaultHostDescription(descr.hwAddress(), descr.vlan(),
Charles Chaneb5bd492019-12-18 15:49:39 -080080 locations, auxLocations, ipAddresses, descr.innerVlan(),
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -070081 descr.tpid(), descr.configured(), sa);
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070082 }
83
84 /**
85 * Generates an annotation from an existing annotation and HostConfig.
86 *
Simon Huntbc30e682017-02-15 18:39:23 -080087 * @param cfg the host config entity from network config
Thomas Vachuska36008462016-01-07 15:38:20 -080088 * @param an the annotation
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070089 * @return annotation combining both sources
90 */
Simon Huntbc30e682017-02-15 18:39:23 -080091 public static SparseAnnotations combine(BasicHostConfig cfg, SparseAnnotations an) {
92 DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
Yuta HIGUCHI9eed0b12017-06-07 11:59:18 -070093 builder.putAll(an);
Simon Huntbc30e682017-02-15 18:39:23 -080094 combineElementAnnotations(cfg, builder);
95
Yuta HIGUCHI9eed0b12017-06-07 11:59:18 -070096 return builder.build();
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070097 }
Simon Huntffbad3b2017-05-16 15:37:51 -070098
99 /**
100 * Returns a description of the given host.
101 *
102 * @param host the host
103 * @return a description of the host
104 */
105 public static HostDescription descriptionOf(Host host) {
106 checkNotNull(host, "Must supply a non-null Host");
Charles Chanf32d8542017-11-30 13:52:37 -0800107 return new DefaultHostDescription(host.mac(), host.vlan(), host.locations(),
Simon Huntffbad3b2017-05-16 15:37:51 -0700108 host.ipAddresses(), host.configured(),
109 (SparseAnnotations) host.annotations());
110 }
Ayaka Koshibef1cedf42015-08-05 18:03:56 -0700111}