Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
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 | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame^] | 20 | import org.onosproject.cluster.NodeId; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 21 | import org.onosproject.net.Device; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 22 | import org.onosproject.net.DeviceId; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 23 | import org.onosproject.net.region.RegionId; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 24 | |
Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -0700 | [diff] [blame] | 25 | /** |
| 26 | * Represents a device. |
| 27 | */ |
| 28 | public class UiDevice extends UiNode { |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 29 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 30 | private final UiTopology topology; |
| 31 | private final Device device; |
| 32 | |
| 33 | private RegionId regionId; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame^] | 34 | private NodeId masterId; |
| 35 | private boolean online; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Creates a new UI device. |
| 39 | * |
| 40 | * @param topology parent topology |
| 41 | * @param device backing device |
| 42 | */ |
| 43 | public UiDevice(UiTopology topology, Device device) { |
| 44 | this.topology = topology; |
| 45 | this.device = device; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Sets the ID of the region to which this device belongs. |
| 50 | * |
| 51 | * @param regionId region identifier |
| 52 | */ |
| 53 | public void setRegionId(RegionId regionId) { |
| 54 | this.regionId = regionId; |
| 55 | } |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 56 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame^] | 57 | /** |
| 58 | * Sets the ID of the controller node that holds mastership for this device. |
| 59 | * |
| 60 | * @param masterId master identifier |
| 61 | */ |
| 62 | public void setMasterId(NodeId masterId) { |
| 63 | this.masterId = masterId; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Sets a flag indicating whether the backing device is online. |
| 68 | * |
| 69 | * @param online boolen flag |
| 70 | */ |
| 71 | public void setOnline(boolean online) { |
| 72 | this.online = online; |
| 73 | } |
| 74 | |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 75 | @Override |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 76 | public String toString() { |
| 77 | return MoreObjects.toStringHelper(this) |
| 78 | .add("id", id()) |
| 79 | .add("region", regionId) |
| 80 | .toString(); |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 81 | } |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 82 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 83 | // @Override |
| 84 | // protected void destroy() { |
| 85 | // } |
| 86 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 87 | /** |
| 88 | * Returns the identity of the device. |
| 89 | * |
| 90 | * @return device ID |
| 91 | */ |
| 92 | public DeviceId id() { |
| 93 | return device.id(); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public String idAsString() { |
| 98 | return id().toString(); |
| 99 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * Returns the device instance backing this UI device. |
| 103 | * |
| 104 | * @return the backing device instance |
| 105 | */ |
| 106 | public Device backingDevice() { |
| 107 | return device; |
| 108 | } |
| 109 | |
| 110 | /** |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 111 | * Returns the identifier of the region to which this device belongs. |
| 112 | * This will be null if the device does not belong to any region. |
| 113 | * |
| 114 | * @return region identity |
| 115 | */ |
| 116 | public RegionId regionId() { |
| 117 | return regionId; |
| 118 | } |
| 119 | |
| 120 | /** |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 121 | * Returns the UI region to which this device belongs. |
| 122 | * |
| 123 | * @return the UI region |
| 124 | */ |
| 125 | public UiRegion uiRegion() { |
| 126 | return topology.findRegion(regionId); |
| 127 | } |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame^] | 128 | |
| 129 | /** |
| 130 | * Returns a string representation of the type of the backing device. |
| 131 | * |
| 132 | * @return the device type |
| 133 | */ |
| 134 | public String type() { |
| 135 | return device.type().toString().toLowerCase(); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Returns a boolean indicating whether the backing device is online. |
| 140 | * |
| 141 | * @return true if device is online, false otherwise |
| 142 | */ |
| 143 | public boolean isOnline() { |
| 144 | return online; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Returns the identifier for the cluster member that has |
| 149 | * mastership over this device. |
| 150 | * |
| 151 | * @return master cluster member identifier |
| 152 | */ |
| 153 | public NodeId master() { |
| 154 | return masterId; |
| 155 | } |
Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -0700 | [diff] [blame] | 156 | } |