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