Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | |
| 17 | package org.onosproject.net.config.basics; |
| 18 | |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 19 | import com.google.common.base.MoreObjects; |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 20 | import org.onosproject.net.DeviceId; |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 21 | import org.onosproject.net.region.Region; |
| 22 | import org.onosproject.net.region.RegionId; |
| 23 | |
| 24 | import java.util.List; |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 25 | import java.util.Set; |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Basic configuration for network regions. |
| 29 | */ |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 30 | public final class BasicRegionConfig extends BasicElementConfig<RegionId> { |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 31 | |
| 32 | private static final String TYPE = "type"; |
| 33 | private static final String DEVICES = "devices"; |
| 34 | |
| 35 | @Override |
| 36 | public boolean isValid() { |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 37 | return hasOnlyFields(ALLOWED, NAME, LATITUDE, LONGITUDE, UI_TYPE, |
| 38 | RACK_ADDRESS, OWNER, TYPE, DEVICES); |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | @Override |
| 42 | public String toString() { |
| 43 | return MoreObjects.toStringHelper(this) |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 44 | .add(NAME, name()) |
| 45 | .add(TYPE, type()) |
| 46 | .add(UI_TYPE, uiType()) |
| 47 | .add(LATITUDE, latitude()) |
| 48 | .add(LONGITUDE, longitude()) |
| 49 | .add(DEVICES, devices()) |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 50 | .toString(); |
| 51 | } |
| 52 | |
| 53 | /** |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 54 | * Returns the region type. |
| 55 | * |
| 56 | * @return the region type |
| 57 | */ |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 58 | public Region.Type type() { |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 59 | String t = get(TYPE, null); |
| 60 | return t == null ? null : regionTypeFor(t); |
| 61 | } |
| 62 | |
| 63 | private Region.Type regionTypeFor(String t) { |
| 64 | try { |
| 65 | return Region.Type.valueOf(t.toUpperCase()); |
| 66 | } catch (IllegalArgumentException ignored) { |
| 67 | } |
| 68 | return null; |
| 69 | } |
| 70 | |
| 71 | /** |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 72 | * Sets the region type. |
| 73 | * |
| 74 | * @param type the region type, or null to unset |
| 75 | * @return the config of the region |
| 76 | */ |
| 77 | public BasicRegionConfig type(Region.Type type) { |
| 78 | String t = type == null ? null : type.name().toLowerCase(); |
| 79 | return (BasicRegionConfig) setOrClear(TYPE, t); |
| 80 | } |
| 81 | |
| 82 | /** |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 83 | * Returns the identities of the devices in this region. |
| 84 | * |
| 85 | * @return list of device identifiers |
| 86 | */ |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 87 | public List<DeviceId> devices() { |
| 88 | return object.has(DEVICES) ? getList(DEVICES, DeviceId::deviceId) : null; |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 91 | /** |
| 92 | * Sets the devices of this region. |
| 93 | * |
| 94 | * @param devices the device identifiers, or null to unset |
| 95 | * @return the config of the region |
| 96 | */ |
| 97 | public BasicRegionConfig devices(Set<DeviceId> devices) { |
| 98 | return (BasicRegionConfig) setOrClear(DEVICES, devices); |
| 99 | } |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 100 | } |