Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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.ConnectPoint; |
| 20 | import org.onosproject.net.DefaultAnnotations; |
| 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; |
| 29 | |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 30 | /** |
| 31 | * Implementations of merge policies for various sources of host configuration |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 32 | * information. This includes applications, providers, and network configurations. |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 33 | */ |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 34 | public final class BasicHostOperator extends BasicElementOperator { |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 35 | |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 36 | private BasicHostOperator() { |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Generates a HostDescription containing fields from a HostDescription and |
| 41 | * a HostConfig. |
| 42 | * |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 43 | * @param cfg the host config entity from network config |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 44 | * @param descr a HostDescription |
| 45 | * @return HostDescription based on both sources |
| 46 | */ |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 47 | public static HostDescription combine(BasicHostConfig cfg, |
| 48 | HostDescription descr) { |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 49 | if (cfg == null) { |
| 50 | return descr; |
| 51 | } |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 52 | |
| 53 | HostLocation location = descr.location(); |
| 54 | ConnectPoint cfgLocation = cfg.location(); |
| 55 | if (cfgLocation != null) { |
| 56 | location = new HostLocation(cfgLocation, System.currentTimeMillis()); |
| 57 | } |
| 58 | |
| 59 | Set<IpAddress> ipAddresses = descr.ipAddress(); |
| 60 | Set<IpAddress> cfgIpAddresses = cfg.ipAddresses(); |
| 61 | if (cfgIpAddresses != null) { |
| 62 | ipAddresses = cfgIpAddresses; |
| 63 | } |
| 64 | |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 65 | SparseAnnotations sa = combine(cfg, descr.annotations()); |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 66 | return new DefaultHostDescription(descr.hwAddress(), descr.vlan(), |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 67 | location, ipAddresses, |
| 68 | descr.configured(), sa); |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Generates an annotation from an existing annotation and HostConfig. |
| 73 | * |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 74 | * @param cfg the host config entity from network config |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 75 | * @param an the annotation |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 76 | * @return annotation combining both sources |
| 77 | */ |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 78 | public static SparseAnnotations combine(BasicHostConfig cfg, SparseAnnotations an) { |
| 79 | DefaultAnnotations.Builder builder = DefaultAnnotations.builder(); |
| 80 | |
| 81 | combineElementAnnotations(cfg, builder); |
| 82 | |
| 83 | return DefaultAnnotations.union(an, builder.build()); |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 84 | } |
| 85 | } |