Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -0700 | [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.ui.model.topo; |
| 18 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 19 | import com.google.common.base.MoreObjects; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 20 | import org.onosproject.net.Device; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 21 | import org.onosproject.net.DeviceId; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 22 | import org.onosproject.net.region.RegionId; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 23 | |
Thomas Vachuska | b877a6f | 2017-04-14 11:43:30 -0700 | [diff] [blame] | 24 | import static com.google.common.base.Preconditions.checkNotNull; |
| 25 | |
Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -0700 | [diff] [blame] | 26 | /** |
| 27 | * Represents a device. |
| 28 | */ |
| 29 | public class UiDevice extends UiNode { |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 30 | |
Thomas Vachuska | b877a6f | 2017-04-14 11:43:30 -0700 | [diff] [blame] | 31 | private static final String DEVICE_CANNOT_BE_NULL = "Device cannot be null"; |
| 32 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 33 | private final UiTopology topology; |
Thomas Vachuska | b877a6f | 2017-04-14 11:43:30 -0700 | [diff] [blame] | 34 | private final DeviceId deviceId; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 35 | |
| 36 | private RegionId regionId; |
| 37 | |
| 38 | /** |
| 39 | * Creates a new UI device. |
| 40 | * |
| 41 | * @param topology parent topology |
| 42 | * @param device backing device |
| 43 | */ |
| 44 | public UiDevice(UiTopology topology, Device device) { |
Thomas Vachuska | b877a6f | 2017-04-14 11:43:30 -0700 | [diff] [blame] | 45 | checkNotNull(device, DEVICE_CANNOT_BE_NULL); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 46 | this.topology = topology; |
Thomas Vachuska | b877a6f | 2017-04-14 11:43:30 -0700 | [diff] [blame] | 47 | this.deviceId = device.id(); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Sets the ID of the region to which this device belongs. |
| 52 | * |
| 53 | * @param regionId region identifier |
| 54 | */ |
| 55 | public void setRegionId(RegionId regionId) { |
| 56 | this.regionId = regionId; |
| 57 | } |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 58 | |
| 59 | @Override |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 60 | public String toString() { |
| 61 | return MoreObjects.toStringHelper(this) |
| 62 | .add("id", id()) |
| 63 | .add("region", regionId) |
| 64 | .toString(); |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 65 | } |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * Returns the identity of the device. |
| 69 | * |
| 70 | * @return device ID |
| 71 | */ |
| 72 | public DeviceId id() { |
Thomas Vachuska | b877a6f | 2017-04-14 11:43:30 -0700 | [diff] [blame] | 73 | return deviceId; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public String idAsString() { |
| 78 | return id().toString(); |
| 79 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * Returns the device instance backing this UI device. |
| 83 | * |
| 84 | * @return the backing device instance |
| 85 | */ |
| 86 | public Device backingDevice() { |
Thomas Vachuska | b877a6f | 2017-04-14 11:43:30 -0700 | [diff] [blame] | 87 | return topology.services.device().getDevice(deviceId); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 91 | * Returns the identifier of the region to which this device belongs. |
| 92 | * This will be null if the device does not belong to any region. |
| 93 | * |
Simon Hunt | b058249 | 2016-09-20 18:26:38 -0700 | [diff] [blame] | 94 | * @return region ID |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 95 | */ |
| 96 | public RegionId regionId() { |
| 97 | return regionId; |
| 98 | } |
| 99 | |
| 100 | /** |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 101 | * Returns the UI region to which this device belongs. |
| 102 | * |
| 103 | * @return the UI region |
| 104 | */ |
| 105 | public UiRegion uiRegion() { |
| 106 | return topology.findRegion(regionId); |
| 107 | } |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * Returns a string representation of the type of the backing device. |
| 111 | * |
| 112 | * @return the device type |
| 113 | */ |
| 114 | public String type() { |
dvaddire | edeaf4a | 2017-06-21 00:00:30 +0530 | [diff] [blame] | 115 | Device device = backingDevice(); |
| 116 | return device == null ? null : device.type().toString().toLowerCase(); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 117 | } |
Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -0700 | [diff] [blame] | 118 | } |