Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.net.host.impl; |
| 17 | |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 18 | import org.onlab.packet.IpAddress; |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 19 | import org.onosproject.net.DefaultAnnotations; |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 20 | import org.onosproject.net.Host; |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 21 | import org.onosproject.net.HostLocation; |
| 22 | import org.onosproject.net.SparseAnnotations; |
Thomas Vachuska | 4998caa | 2015-08-26 13:28:38 -0700 | [diff] [blame] | 23 | import org.onosproject.net.config.basics.BasicHostConfig; |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 24 | import org.onosproject.net.device.impl.BasicElementOperator; |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 25 | import org.onosproject.net.host.DefaultHostDescription; |
| 26 | import org.onosproject.net.host.HostDescription; |
| 27 | |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 28 | import java.util.Set; |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 29 | import java.util.stream.Collectors; |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 30 | |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 31 | import static com.google.common.base.Preconditions.checkNotNull; |
| 32 | |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 33 | /** |
| 34 | * Implementations of merge policies for various sources of host configuration |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 35 | * information. This includes applications, providers, and network configurations. |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 36 | */ |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 37 | public final class BasicHostOperator extends BasicElementOperator { |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 38 | |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 39 | private BasicHostOperator() { |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Generates a HostDescription containing fields from a HostDescription and |
| 44 | * a HostConfig. |
| 45 | * |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 46 | * @param cfg the host config entity from network config |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 47 | * @param descr a HostDescription |
| 48 | * @return HostDescription based on both sources |
| 49 | */ |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 50 | public static HostDescription combine(BasicHostConfig cfg, |
| 51 | HostDescription descr) { |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 52 | if (cfg == null || descr == null) { |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 53 | return descr; |
| 54 | } |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 55 | |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 56 | 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 Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | Set<IpAddress> ipAddresses = descr.ipAddress(); |
| 65 | Set<IpAddress> cfgIpAddresses = cfg.ipAddresses(); |
| 66 | if (cfgIpAddresses != null) { |
| 67 | ipAddresses = cfgIpAddresses; |
| 68 | } |
| 69 | |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 70 | SparseAnnotations sa = combine(cfg, descr.annotations()); |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 71 | return new DefaultHostDescription(descr.hwAddress(), descr.vlan(), |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 72 | locations, ipAddresses, descr.innerVlan(), |
| 73 | descr.tpid(), descr.configured(), sa); |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Generates an annotation from an existing annotation and HostConfig. |
| 78 | * |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 79 | * @param cfg the host config entity from network config |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 80 | * @param an the annotation |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 81 | * @return annotation combining both sources |
| 82 | */ |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 83 | public static SparseAnnotations combine(BasicHostConfig cfg, SparseAnnotations an) { |
| 84 | DefaultAnnotations.Builder builder = DefaultAnnotations.builder(); |
Yuta HIGUCHI | 9eed0b1 | 2017-06-07 11:59:18 -0700 | [diff] [blame] | 85 | builder.putAll(an); |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 86 | combineElementAnnotations(cfg, builder); |
| 87 | |
Yuta HIGUCHI | 9eed0b1 | 2017-06-07 11:59:18 -0700 | [diff] [blame] | 88 | return builder.build(); |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 89 | } |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 90 | |
| 91 | /** |
| 92 | * Returns a description of the given host. |
| 93 | * |
| 94 | * @param host the host |
| 95 | * @return a description of the host |
| 96 | */ |
| 97 | public static HostDescription descriptionOf(Host host) { |
| 98 | checkNotNull(host, "Must supply a non-null Host"); |
Charles Chan | f32d854 | 2017-11-30 13:52:37 -0800 | [diff] [blame] | 99 | return new DefaultHostDescription(host.mac(), host.vlan(), host.locations(), |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 100 | host.ipAddresses(), host.configured(), |
| 101 | (SparseAnnotations) host.annotations()); |
| 102 | } |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 103 | } |