Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-2015 Open Networking Laboratory |
| 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 | |
| 37 | protected static final double DEFAULT_COORD = -1.0; |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 38 | |
| 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 | */ |
| 50 | public static HostDescription combine(BasicHostConfig cfg, HostDescription descr) { |
| 51 | if (cfg == null) { |
| 52 | return descr; |
| 53 | } |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 54 | |
| 55 | HostLocation location = descr.location(); |
| 56 | ConnectPoint cfgLocation = cfg.location(); |
| 57 | if (cfgLocation != null) { |
| 58 | location = new HostLocation(cfgLocation, System.currentTimeMillis()); |
| 59 | } |
| 60 | |
| 61 | Set<IpAddress> ipAddresses = descr.ipAddress(); |
| 62 | Set<IpAddress> cfgIpAddresses = cfg.ipAddresses(); |
| 63 | if (cfgIpAddresses != null) { |
| 64 | ipAddresses = cfgIpAddresses; |
| 65 | } |
| 66 | |
Ayaka Koshibe | f1cedf4 | 2015-08-05 18:03:56 -0700 | [diff] [blame] | 67 | SparseAnnotations sa = combine(cfg, descr.annotations()); |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 68 | return new DefaultHostDescription(descr.hwAddress(), descr.vlan(), |
| 69 | location, ipAddresses, 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 | */ |
| 79 | public static SparseAnnotations combine(BasicHostConfig cfg, SparseAnnotations an) { |
| 80 | DefaultAnnotations.Builder newBuilder = DefaultAnnotations.builder(); |
| 81 | if (cfg.name() != null) { |
| 82 | newBuilder.set(AnnotationKeys.NAME, cfg.name()); |
| 83 | } |
| 84 | if (cfg.latitude() != DEFAULT_COORD) { |
| 85 | newBuilder.set(AnnotationKeys.LATITUDE, Double.toString(cfg.latitude())); |
| 86 | } |
| 87 | if (cfg.longitude() != DEFAULT_COORD) { |
| 88 | newBuilder.set(AnnotationKeys.LONGITUDE, Double.toString(cfg.longitude())); |
| 89 | } |
| 90 | if (cfg.rackAddress() != null) { |
| 91 | newBuilder.set(AnnotationKeys.RACK_ADDRESS, cfg.rackAddress()); |
| 92 | } |
| 93 | if (cfg.owner() != null) { |
| 94 | newBuilder.set(AnnotationKeys.OWNER, cfg.owner()); |
| 95 | } |
| 96 | return DefaultAnnotations.union(an, newBuilder.build()); |
| 97 | } |
| 98 | } |