Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [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 | |
| 17 | package org.onosproject.net.device.impl; |
| 18 | |
| 19 | import org.onosproject.net.AnnotationKeys; |
| 20 | import org.onosproject.net.DefaultAnnotations; |
| 21 | import org.onosproject.net.config.ConfigOperator; |
| 22 | import org.onosproject.net.config.basics.BasicElementConfig; |
| 23 | |
Thomas Vachuska | c616e17 | 2018-04-17 16:57:12 -0700 | [diff] [blame] | 24 | import java.util.Objects; |
| 25 | |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 26 | /** |
| 27 | * Abstract base implementation for element operators. |
| 28 | */ |
| 29 | public abstract class BasicElementOperator implements ConfigOperator { |
| 30 | |
| 31 | /** |
| 32 | * Sets all defined values from the element config on the supplied |
| 33 | * annotations builder. |
| 34 | * |
| 35 | * @param cfg element configuration |
| 36 | * @param builder annotations builder |
| 37 | */ |
| 38 | protected static void combineElementAnnotations(BasicElementConfig cfg, |
| 39 | DefaultAnnotations.Builder builder) { |
| 40 | |
| 41 | if (cfg.name() != null) { |
| 42 | builder.set(AnnotationKeys.NAME, cfg.name()); |
| 43 | } |
| 44 | if (cfg.uiType() != null) { |
| 45 | builder.set(AnnotationKeys.UI_TYPE, cfg.uiType()); |
| 46 | } |
| 47 | |
| 48 | if (cfg.locType() != null) { |
| 49 | builder.set(AnnotationKeys.LOC_TYPE, cfg.locType()); |
| 50 | } |
Thomas Vachuska | c616e17 | 2018-04-17 16:57:12 -0700 | [diff] [blame] | 51 | |
| 52 | if (Objects.equals(cfg.locType(), BasicElementConfig.LOC_TYPE_NONE)) { |
Thomas Vachuska | f131e59 | 2018-05-07 11:52:14 -0700 | [diff] [blame] | 53 | // Wipe-out both grid and geo coordinates. |
Thomas Vachuska | c616e17 | 2018-04-17 16:57:12 -0700 | [diff] [blame] | 54 | builder.remove(AnnotationKeys.GRID_X).remove(AnnotationKeys.GRID_Y); |
| 55 | builder.remove(AnnotationKeys.LATITUDE).remove(AnnotationKeys.LONGITUDE); |
Thomas Vachuska | f131e59 | 2018-05-07 11:52:14 -0700 | [diff] [blame] | 56 | |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 57 | } else if (cfg.gridCoordsSet()) { |
Thomas Vachuska | f131e59 | 2018-05-07 11:52:14 -0700 | [diff] [blame] | 58 | // Give priority to coordinate-based scheme as it requires explicit |
| 59 | // location type. Set grid coordinates and wipe-out geo coordinates. |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 60 | builder.set(AnnotationKeys.GRID_Y, Double.toString(cfg.gridY())); |
| 61 | builder.set(AnnotationKeys.GRID_X, Double.toString(cfg.gridX())); |
Thomas Vachuska | c616e17 | 2018-04-17 16:57:12 -0700 | [diff] [blame] | 62 | builder.remove(AnnotationKeys.LATITUDE).remove(AnnotationKeys.LONGITUDE); |
Thomas Vachuska | f131e59 | 2018-05-07 11:52:14 -0700 | [diff] [blame] | 63 | |
| 64 | } else if (cfg.geoCoordsSet()) { |
| 65 | // Set geo coordinates and wipe-out grid coordinates. |
| 66 | builder.set(AnnotationKeys.LATITUDE, Double.toString(cfg.latitude())); |
| 67 | builder.set(AnnotationKeys.LONGITUDE, Double.toString(cfg.longitude())); |
| 68 | builder.remove(AnnotationKeys.GRID_X).remove(AnnotationKeys.GRID_Y); |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | if (cfg.rackAddress() != null) { |
| 72 | builder.set(AnnotationKeys.RACK_ADDRESS, cfg.rackAddress()); |
| 73 | } |
| 74 | if (cfg.owner() != null) { |
| 75 | builder.set(AnnotationKeys.OWNER, cfg.owner()); |
| 76 | } |
| 77 | } |
| 78 | } |