blob: 29333e696fddd0d8b26c6613f62cf92bd2725602 [file] [log] [blame]
Simon Huntbc30e682017-02-15 18:39:23 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Simon Huntbc30e682017-02-15 18:39:23 -08003 *
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
17package org.onosproject.net.device.impl;
18
19import org.onosproject.net.AnnotationKeys;
20import org.onosproject.net.DefaultAnnotations;
21import org.onosproject.net.config.ConfigOperator;
22import org.onosproject.net.config.basics.BasicElementConfig;
23
Thomas Vachuskac616e172018-04-17 16:57:12 -070024import java.util.Objects;
25
Simon Huntbc30e682017-02-15 18:39:23 -080026/**
27 * Abstract base implementation for element operators.
28 */
29public 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 Vachuskac616e172018-04-17 16:57:12 -070051
52 if (Objects.equals(cfg.locType(), BasicElementConfig.LOC_TYPE_NONE)) {
53 builder.remove(AnnotationKeys.GRID_X).remove(AnnotationKeys.GRID_Y);
54 builder.remove(AnnotationKeys.LATITUDE).remove(AnnotationKeys.LONGITUDE);
55 } else if (cfg.geoCoordsSet()) {
Simon Huntbc30e682017-02-15 18:39:23 -080056 builder.set(AnnotationKeys.LATITUDE, Double.toString(cfg.latitude()));
57 builder.set(AnnotationKeys.LONGITUDE, Double.toString(cfg.longitude()));
Thomas Vachuskac616e172018-04-17 16:57:12 -070058 builder.remove(AnnotationKeys.GRID_X).remove(AnnotationKeys.GRID_Y);
Simon Huntbc30e682017-02-15 18:39:23 -080059 } else if (cfg.gridCoordsSet()) {
60 builder.set(AnnotationKeys.GRID_Y, Double.toString(cfg.gridY()));
61 builder.set(AnnotationKeys.GRID_X, Double.toString(cfg.gridX()));
Thomas Vachuskac616e172018-04-17 16:57:12 -070062 builder.remove(AnnotationKeys.LATITUDE).remove(AnnotationKeys.LONGITUDE);
Simon Huntbc30e682017-02-15 18:39:23 -080063 }
64
65 if (cfg.rackAddress() != null) {
66 builder.set(AnnotationKeys.RACK_ADDRESS, cfg.rackAddress());
67 }
68 if (cfg.owner() != null) {
69 builder.set(AnnotationKeys.OWNER, cfg.owner());
70 }
71 }
72}