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 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
| 20 | import org.slf4j.Logger; |
| 21 | import org.onosproject.incubator.net.config.ConfigOperator; |
| 22 | import org.onosproject.incubator.net.config.basics.BasicHostConfig; |
| 23 | import org.onosproject.net.AnnotationKeys; |
| 24 | import org.onosproject.net.DefaultAnnotations; |
| 25 | import org.onosproject.net.SparseAnnotations; |
| 26 | import org.onosproject.net.host.DefaultHostDescription; |
| 27 | import org.onosproject.net.host.HostDescription; |
| 28 | |
| 29 | /** |
| 30 | * Implementations of merge policies for various sources of host configuration |
| 31 | * information. This includes applications, provides, and network configurations. |
| 32 | */ |
| 33 | public final class BasicHostOperator implements ConfigOperator { |
| 34 | |
| 35 | protected static final double DEFAULT_COORD = -1.0; |
| 36 | private static final Logger log = getLogger(BasicHostOperator.class); |
| 37 | |
| 38 | private BasicHostOperator() { |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Generates a HostDescription containing fields from a HostDescription and |
| 43 | * a HostConfig. |
| 44 | * |
| 45 | * @param cfg the host config entity from network config |
| 46 | * @param descr a HostDescription |
| 47 | * @return HostDescription based on both sources |
| 48 | */ |
| 49 | public static HostDescription combine(BasicHostConfig cfg, HostDescription descr) { |
| 50 | if (cfg == null) { |
| 51 | return descr; |
| 52 | } |
| 53 | SparseAnnotations sa = combine(cfg, descr.annotations()); |
| 54 | return new DefaultHostDescription(descr.hwAddress(), descr.vlan(), descr.location(), |
| 55 | descr.ipAddress(), sa); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Generates an annotation from an existing annotation and HostConfig. |
| 60 | * |
| 61 | * @param cfg the device config entity from network config |
| 62 | * @param an the annotation |
| 63 | * @return annotation combining both sources |
| 64 | */ |
| 65 | public static SparseAnnotations combine(BasicHostConfig cfg, SparseAnnotations an) { |
| 66 | DefaultAnnotations.Builder newBuilder = DefaultAnnotations.builder(); |
| 67 | if (cfg.name() != null) { |
| 68 | newBuilder.set(AnnotationKeys.NAME, cfg.name()); |
| 69 | } |
| 70 | if (cfg.latitude() != DEFAULT_COORD) { |
| 71 | newBuilder.set(AnnotationKeys.LATITUDE, Double.toString(cfg.latitude())); |
| 72 | } |
| 73 | if (cfg.longitude() != DEFAULT_COORD) { |
| 74 | newBuilder.set(AnnotationKeys.LONGITUDE, Double.toString(cfg.longitude())); |
| 75 | } |
| 76 | if (cfg.rackAddress() != null) { |
| 77 | newBuilder.set(AnnotationKeys.RACK_ADDRESS, cfg.rackAddress()); |
| 78 | } |
| 79 | if (cfg.owner() != null) { |
| 80 | newBuilder.set(AnnotationKeys.OWNER, cfg.owner()); |
| 81 | } |
| 82 | return DefaultAnnotations.union(an, newBuilder.build()); |
| 83 | } |
| 84 | } |