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; |
| 19 | import org.onosproject.net.AnnotationKeys; |
| 20 | import org.onosproject.net.ConnectPoint; |
| 21 | import org.onosproject.net.DefaultAnnotations; |
| 22 | import org.onosproject.net.HostLocation; |
| 23 | import org.onosproject.net.SparseAnnotations; |
Ray Milkey | a412236 | 2015-08-18 15:19:08 -0700 | [diff] [blame] | 24 | import org.onosproject.net.config.ConfigOperator; |
Thomas Vachuska | 4998caa | 2015-08-26 13:28:38 -0700 | [diff] [blame] | 25 | import org.onosproject.net.config.basics.BasicHostConfig; |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 26 | import org.onosproject.net.host.DefaultHostDescription; |
| 27 | import org.onosproject.net.host.HostDescription; |
| 28 | |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 29 | import java.util.Set; |
| 30 | |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 31 | /** |
| 32 | * Implementations of merge policies for various sources of host configuration |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 33 | * information. This includes applications, providers, and network configurations. |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 34 | */ |
| 35 | public final class BasicHostOperator implements ConfigOperator { |
| 36 | |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 37 | private BasicHostOperator() { |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Generates a HostDescription containing fields from a HostDescription and |
| 42 | * a HostConfig. |
| 43 | * |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 44 | * @param cfg the host config entity from network config |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 45 | * @param descr a HostDescription |
| 46 | * @return HostDescription based on both sources |
| 47 | */ |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 48 | public static HostDescription combine(BasicHostConfig cfg, |
| 49 | HostDescription descr) { |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 50 | if (cfg == null) { |
| 51 | return descr; |
| 52 | } |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 53 | |
| 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 Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 66 | SparseAnnotations sa = combine(cfg, descr.annotations()); |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 67 | return new DefaultHostDescription(descr.hwAddress(), descr.vlan(), |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 68 | location, ipAddresses, |
| 69 | descr.configured(), sa); |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Generates an annotation from an existing annotation and HostConfig. |
| 74 | * |
| 75 | * @param cfg the device config entity from network config |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 76 | * @param an the annotation |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 77 | * @return annotation combining both sources |
| 78 | */ |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 79 | public static SparseAnnotations combine(BasicHostConfig cfg, |
| 80 | SparseAnnotations an) { |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 81 | DefaultAnnotations.Builder newBuilder = DefaultAnnotations.builder(); |
| 82 | if (cfg.name() != null) { |
| 83 | newBuilder.set(AnnotationKeys.NAME, cfg.name()); |
| 84 | } |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 85 | if (cfg.uiType() != null) { |
| 86 | newBuilder.set(AnnotationKeys.UI_TYPE, cfg.uiType()); |
| 87 | } |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 88 | if (cfg.geoCoordsSet()) { |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 89 | newBuilder.set(AnnotationKeys.LATITUDE, Double.toString(cfg.latitude())); |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 90 | 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 | } |