Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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.ui.impl.topo.model; |
| 18 | |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 19 | import org.onosproject.cluster.ControllerNode; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -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.cluster.RoleInfo; |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 22 | import org.onosproject.event.EventDispatcher; |
| 23 | import org.onosproject.net.Device; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.Host; |
| 26 | import org.onosproject.net.Link; |
| 27 | import org.onosproject.net.region.Region; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame^] | 28 | import org.onosproject.ui.model.ServiceBundle; |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 29 | import org.onosproject.ui.model.topo.UiClusterMember; |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 30 | import org.onosproject.ui.model.topo.UiDevice; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 31 | import org.onosproject.ui.model.topo.UiTopology; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame^] | 32 | import org.slf4j.Logger; |
| 33 | import org.slf4j.LoggerFactory; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 34 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame^] | 35 | import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.CLUSTER_MEMBER_ADDED_OR_UPDATED; |
| 36 | import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.CLUSTER_MEMBER_REMOVED; |
| 37 | import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.DEVICE_ADDED_OR_UPDATED; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 38 | import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.DEVICE_REMOVED; |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * UI Topology Model cache. |
| 42 | */ |
| 43 | class ModelCache { |
| 44 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame^] | 45 | private static final Logger log = LoggerFactory.getLogger(ModelCache.class); |
| 46 | |
| 47 | private final ServiceBundle services; |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 48 | private final EventDispatcher dispatcher; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 49 | private final UiTopology uiTopology = new UiTopology(); |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 50 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame^] | 51 | ModelCache(ServiceBundle services, EventDispatcher eventDispatcher) { |
| 52 | this.services = services; |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 53 | this.dispatcher = eventDispatcher; |
| 54 | } |
| 55 | |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 56 | @Override |
| 57 | public String toString() { |
| 58 | return "ModelCache{" + uiTopology + "}"; |
| 59 | } |
| 60 | |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 61 | /** |
| 62 | * Clear our model. |
| 63 | */ |
| 64 | void clear() { |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 65 | uiTopology.clear(); |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Create our internal model of the global topology. |
| 70 | */ |
| 71 | void load() { |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame^] | 72 | // TODO - implement loading of initial state |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 73 | // loadClusterMembers(); |
| 74 | // loadRegions(); |
| 75 | // loadDevices(); |
| 76 | // loadHosts(); |
| 77 | // loadLinks(); |
| 78 | } |
| 79 | |
| 80 | |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 81 | /** |
| 82 | * Updates the model (adds a new instance if necessary) with the given |
| 83 | * controller node information. |
| 84 | * |
| 85 | * @param cnode controller node to be added/updated |
| 86 | */ |
| 87 | void addOrUpdateClusterMember(ControllerNode cnode) { |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame^] | 88 | NodeId id = cnode.id(); |
| 89 | UiClusterMember member = uiTopology.findClusterMember(id); |
| 90 | if (member == null) { |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 91 | member = new UiClusterMember(cnode); |
| 92 | uiTopology.add(member); |
| 93 | } |
| 94 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame^] | 95 | // inject computed data about the cluster node, into the model object |
| 96 | ControllerNode.State state = services.cluster().getState(id); |
| 97 | member.setState(state); |
| 98 | member.setDeviceCount(services.mastership().getDevicesOf(id).size()); |
| 99 | // NOTE: UI-attached is session-based data, not global |
| 100 | |
| 101 | dispatcher.post(new UiModelEvent(CLUSTER_MEMBER_ADDED_OR_UPDATED, member)); |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame^] | 104 | /** |
| 105 | * Removes from the model the specified controller node. |
| 106 | * |
| 107 | * @param cnode controller node to be removed |
| 108 | */ |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 109 | void removeClusterMember(ControllerNode cnode) { |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame^] | 110 | NodeId id = cnode.id(); |
| 111 | UiClusterMember member = uiTopology.findClusterMember(id); |
| 112 | if (member != null) { |
| 113 | uiTopology.remove(member); |
| 114 | dispatcher.post(new UiModelEvent(CLUSTER_MEMBER_REMOVED, member)); |
| 115 | } else { |
| 116 | log.warn("Tried to remove non-member cluster node {}", id); |
| 117 | } |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | void updateMasterships(DeviceId deviceId, RoleInfo roleInfo) { |
| 121 | // TODO: store the updated mastership information |
| 122 | // TODO: post event |
| 123 | } |
| 124 | |
| 125 | void addOrUpdateRegion(Region region) { |
| 126 | // TODO: find or create region assoc. with parameter |
| 127 | // TODO: post event |
| 128 | } |
| 129 | |
| 130 | void removeRegion(Region region) { |
| 131 | // TODO: find region assoc. with parameter; remove from model |
| 132 | // TODO: post event |
| 133 | } |
| 134 | |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 135 | void addOrUpdateDevice(Device device) { |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 136 | // TODO: find or create device assoc. with parameter |
| 137 | // FIXME |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 138 | UiDevice uiDevice = new UiDevice(); |
| 139 | |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 140 | // TODO: post the (correct) event |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame^] | 141 | dispatcher.post(new UiModelEvent(DEVICE_ADDED_OR_UPDATED, uiDevice)); |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void removeDevice(Device device) { |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 145 | // TODO: get UiDevice associated with the given parameter; remove from model |
| 146 | // FIXME |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 147 | UiDevice uiDevice = new UiDevice(); |
| 148 | |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 149 | // TODO: post the (correct) event |
| 150 | dispatcher.post(new UiModelEvent(DEVICE_REMOVED, uiDevice)); |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 151 | |
| 152 | } |
| 153 | |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 154 | void addOrUpdateLink(Link link) { |
| 155 | // TODO: find ui-link assoc. with parameter; create or update. |
| 156 | // TODO: post event |
| 157 | } |
| 158 | |
| 159 | void removeLink(Link link) { |
| 160 | // TODO: find ui-link assoc. with parameter; update or remove. |
| 161 | // TODO: post event |
| 162 | } |
| 163 | |
| 164 | void addOrUpdateHost(Host host) { |
| 165 | // TODO: find or create host assoc. with parameter |
| 166 | // TODO: post event |
| 167 | } |
| 168 | |
| 169 | void moveHost(Host host, Host prevHost) { |
| 170 | // TODO: process host-move |
| 171 | // TODO: post event |
| 172 | } |
| 173 | |
| 174 | void removeHost(Host host) { |
| 175 | // TODO: find host assoc. with parameter; remove from model |
| 176 | } |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 177 | |
| 178 | /** |
| 179 | * Returns the number of members in the cluster. |
| 180 | * |
| 181 | * @return number of cluster members |
| 182 | */ |
| 183 | public int clusterMemberCount() { |
| 184 | return uiTopology.clusterMemberCount(); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Returns the number of regions configured in the topology. |
| 189 | * |
| 190 | * @return number of regions |
| 191 | */ |
| 192 | public int regionCount() { |
| 193 | return uiTopology.regionCount(); |
| 194 | } |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 195 | } |