rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [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 | package org.onosproject.sfcweb; |
| 17 | |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame^] | 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import com.google.common.collect.ImmutableSet; |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 21 | import jersey.repackaged.com.google.common.collect.Lists; |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 22 | import org.onlab.osgi.DefaultServiceDirectory; |
| 23 | import org.onlab.osgi.ServiceDirectory; |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 24 | import org.onlab.packet.MacAddress; |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 25 | import org.onosproject.net.DeviceId; |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 26 | import org.onosproject.net.Element; |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 27 | import org.onosproject.net.Host; |
| 28 | import org.onosproject.net.HostId; |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 29 | import org.onosproject.net.Link; |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 30 | import org.onosproject.net.host.HostService; |
| 31 | import org.onosproject.net.link.LinkService; |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 32 | import org.onosproject.ui.RequestHandler; |
| 33 | import org.onosproject.ui.UiConnection; |
| 34 | import org.onosproject.ui.UiMessageHandler; |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 35 | import org.onosproject.ui.topo.DeviceHighlight; |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 36 | import org.onosproject.ui.topo.Highlights; |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 37 | import org.onosproject.ui.topo.HostHighlight; |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 38 | import org.onosproject.ui.topo.NodeBadge; |
| 39 | import org.onosproject.ui.topo.TopoJson; |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 40 | import org.onosproject.vtnrsc.FiveTuple; |
| 41 | import org.onosproject.vtnrsc.LoadBalanceId; |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 42 | import org.onosproject.vtnrsc.PortChain; |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 43 | import org.onosproject.vtnrsc.PortChainId; |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 44 | import org.onosproject.vtnrsc.PortPair; |
| 45 | import org.onosproject.vtnrsc.PortPairId; |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 46 | import org.onosproject.vtnrsc.VirtualPort; |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 47 | import org.onosproject.vtnrsc.VirtualPortId; |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 48 | import org.onosproject.vtnrsc.portchain.PortChainService; |
| 49 | import org.onosproject.vtnrsc.portpair.PortPairService; |
| 50 | import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService; |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 51 | import org.onosproject.vtnrsc.service.VtnRscService; |
| 52 | import org.onosproject.vtnrsc.virtualport.VirtualPortService; |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 53 | import org.slf4j.Logger; |
| 54 | import org.slf4j.LoggerFactory; |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 55 | |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame^] | 56 | import java.util.Collection; |
| 57 | import java.util.List; |
| 58 | import java.util.ListIterator; |
| 59 | import java.util.Set; |
| 60 | import java.util.TimerTask; |
| 61 | |
| 62 | import static org.onosproject.net.DefaultEdgeLink.createEdgeLink; |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * SFC web gui topology-overlay message handler. |
| 66 | */ |
| 67 | public class SfcwebUiTopovMessageHandler extends UiMessageHandler { |
| 68 | |
| 69 | private static final String SAMPLE_TOPOV_DISPLAY_START = "sfcwebTopovDisplayStart"; |
| 70 | private static final String SAMPLE_TOPOV_DISPLAY_SFC = "showSfcInfo"; |
| 71 | private static final String SAMPLE_TOPOV_DISPLAY_STOP = "sfcTopovClear"; |
| 72 | private static final String CONFIG_SFP_MSG = "configSfpMessage"; |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 73 | private static final String SAMPLE_TOPOV_SHOW_SFC_PATH = "showSfcPath"; |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 74 | private static final String ID = "id"; |
| 75 | private static final String MODE = "mode"; |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 76 | private static final String CLASSIFIER = "CLS"; |
| 77 | private static final String FORWARDER = "SFF"; |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 78 | |
| 79 | private static final Link[] EMPTY_LINK_SET = new Link[0]; |
| 80 | |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 81 | private enum Mode { |
| 82 | IDLE, MOUSE, LINK |
| 83 | } |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 84 | |
| 85 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 86 | |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 87 | private HostService hostService; |
| 88 | private LinkService linkService; |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 89 | private TimerTask demoTask = null; |
| 90 | private Mode currentMode = Mode.IDLE; |
| 91 | private Element elementOfNote; |
| 92 | private Link[] linkSet = EMPTY_LINK_SET; |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 93 | |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 94 | protected PortPairService portPairService; |
| 95 | protected VtnRscService vtnRscService; |
| 96 | protected VirtualPortService virtualPortService; |
| 97 | protected PortChainService portChainService; |
| 98 | protected PortPairGroupService portPairGroupService; |
| 99 | |
| 100 | @Override |
| 101 | public void init(UiConnection connection, ServiceDirectory directory) { |
| 102 | super.init(connection, directory); |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 103 | hostService = directory.get(HostService.class); |
| 104 | linkService = directory.get(LinkService.class); |
| 105 | portChainService = directory.get(PortChainService.class); |
| 106 | portPairService = directory.get(PortPairService.class); |
| 107 | portPairGroupService = directory.get(PortPairGroupService.class); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | protected Collection<RequestHandler> createRequestHandlers() { |
| 112 | return ImmutableSet.of( |
| 113 | new DisplayStartHandler(), |
| 114 | new DisplayStopHandler(), |
| 115 | new ConfigSfpMsg() |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Handler classes. |
| 121 | */ |
| 122 | private final class DisplayStartHandler extends RequestHandler { |
| 123 | public DisplayStartHandler() { |
| 124 | super(SAMPLE_TOPOV_DISPLAY_START); |
| 125 | } |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame^] | 126 | |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 127 | @Override |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame^] | 128 | public void process(ObjectNode payload) { |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 129 | String mode = string(payload, MODE); |
| 130 | PortChainService pcs = get(PortChainService.class); |
| 131 | Iterable<PortChain> portChains = pcs.getPortChains(); |
| 132 | ObjectNode result = objectNode(); |
| 133 | |
| 134 | ArrayNode arrayNode = arrayNode(); |
| 135 | |
| 136 | for (final PortChain portChain : portChains) { |
| 137 | arrayNode.add(portChain.portChainId().value().toString()); |
| 138 | } |
| 139 | result.putArray("a").addAll(arrayNode); |
| 140 | |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame^] | 141 | sendMessage(SAMPLE_TOPOV_DISPLAY_SFC, result); |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | private final class DisplayStopHandler extends RequestHandler { |
| 146 | public DisplayStopHandler() { |
| 147 | super(SAMPLE_TOPOV_DISPLAY_STOP); |
| 148 | } |
| 149 | |
| 150 | @Override |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame^] | 151 | public void process(ObjectNode payload) { |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 152 | log.debug("Stop Display"); |
| 153 | clearState(); |
| 154 | clearForMode(); |
| 155 | cancelTask(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | private final class ConfigSfpMsg extends RequestHandler { |
| 160 | public ConfigSfpMsg() { |
| 161 | super(CONFIG_SFP_MSG); |
| 162 | } |
| 163 | |
| 164 | @Override |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame^] | 165 | public void process(ObjectNode payload) { |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 166 | String id = string(payload, ID); |
| 167 | ServiceDirectory serviceDirectory = new DefaultServiceDirectory(); |
| 168 | vtnRscService = serviceDirectory.get(VtnRscService.class); |
| 169 | virtualPortService = serviceDirectory.get(VirtualPortService.class); |
| 170 | |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 171 | List<String> sfcPathList = Lists.newArrayList(); |
| 172 | |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 173 | Highlights highlights = new Highlights(); |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 174 | SfcLinkMap linkMap = new SfcLinkMap(); |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 175 | |
| 176 | PortChainId portChainId = PortChainId.of(id); |
| 177 | boolean portChainIdExist = portChainService.exists(portChainId); |
| 178 | if (!portChainIdExist) { |
| 179 | log.info("portchain id doesn't exist"); |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | PortChain portChain = portChainService.getPortChain(portChainId); |
| 184 | |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 185 | Set<FiveTuple> fiveTupleSet = portChain.getLoadBalanceIdMapKeys(); |
| 186 | for (FiveTuple fiveTuple : fiveTupleSet) { |
| 187 | List<PortPairId> path = portChain.getLoadBalancePath(fiveTuple); |
| 188 | LoadBalanceId lbId = portChain.getLoadBalanceId(fiveTuple); |
| 189 | ListIterator<PortPairId> pathIterator = path.listIterator(); |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 190 | |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 191 | // Add source |
| 192 | Host srcHost = hostService.getHost(HostId.hostId(fiveTuple.macSrc())); |
| 193 | |
| 194 | HostHighlight hSrc = new HostHighlight(srcHost.id().toString()); |
| 195 | hSrc.setBadge(NodeBadge.text("SRC")); |
| 196 | String sfcPath = "SRC -> "; |
| 197 | highlights.add(hSrc); |
| 198 | |
| 199 | DeviceId previousDeviceId = null; |
| 200 | while (pathIterator.hasNext()) { |
| 201 | |
| 202 | PortPairId portPairId = pathIterator.next(); |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 203 | PortPair portPair = portPairService.getPortPair(portPairId); |
| 204 | DeviceId deviceId = vtnRscService.getSfToSffMaping(VirtualPortId.portId(portPair.egress())); |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 205 | VirtualPort vPort = virtualPortService.getPort(VirtualPortId.portId(portPair.egress())); |
| 206 | MacAddress dstMacAddress = vPort.macAddress(); |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 207 | Host host = hostService.getHost(HostId.hostId(dstMacAddress)); |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 208 | |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 209 | addEdgeLinks(linkMap, host); |
| 210 | log.info("before check"); |
| 211 | if (previousDeviceId != null) { |
| 212 | log.info("pdid not null"); |
| 213 | if (!deviceId.equals(previousDeviceId)) { |
| 214 | // Highlight the link between devices. |
| 215 | |
| 216 | Link link = getLinkBetweenDevices(deviceId, previousDeviceId); |
| 217 | if (link != null) { |
| 218 | linkMap.add(link); |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | DeviceHighlight dh = new DeviceHighlight(deviceId.toString()); |
| 224 | if (portChain.getSfcClassifiers(lbId).contains(deviceId)) { |
| 225 | dh.setBadge(NodeBadge.text(CLASSIFIER)); |
| 226 | } else { |
| 227 | dh.setBadge(NodeBadge.text(FORWARDER)); |
| 228 | } |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 229 | |
| 230 | highlights.add(dh); |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 231 | |
| 232 | HostHighlight hhDst = new HostHighlight(host.id().toString()); |
| 233 | hhDst.setBadge(NodeBadge.text(portPair.name())); |
| 234 | sfcPath = sfcPath + portPair.name() + "(" + vPort.fixedIps().iterator().next().ip() + ") -> "; |
| 235 | |
| 236 | if (!portPair.ingress().equals(portPair.egress())) { |
| 237 | MacAddress srcMacAddress = virtualPortService.getPort(VirtualPortId |
| 238 | .portId(portPair.ingress())) |
| 239 | .macAddress(); |
| 240 | Host hostSrc = hostService.getHost(HostId.hostId(srcMacAddress)); |
| 241 | HostHighlight hhSrc = new HostHighlight(hostSrc.id().toString()); |
| 242 | hhSrc.setBadge(NodeBadge.text(portPair.name())); |
| 243 | highlights.add(hhSrc); |
| 244 | } |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 245 | highlights.add(hhDst); |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 246 | previousDeviceId = deviceId; |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 247 | } |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 248 | |
| 249 | // Add destination |
| 250 | Host dstHost = hostService.getHost(HostId.hostId(fiveTuple.macDst())); |
| 251 | |
| 252 | HostHighlight hDst = new HostHighlight(dstHost.id().toString()); |
| 253 | hDst.setBadge(NodeBadge.text("DST")); |
| 254 | sfcPath = sfcPath + "DST"; |
| 255 | highlights.add(hDst); |
| 256 | sfcPathList.add(sfcPath); |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 257 | } |
| 258 | |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 259 | for (SfcLink sfcLink : linkMap.biLinks()) { |
| 260 | highlights.add(sfcLink.highlight(null)); |
| 261 | } |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 262 | sendHighlights(highlights); |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 263 | |
| 264 | ObjectNode result = objectNode(); |
| 265 | ArrayNode arrayNode = arrayNode(); |
| 266 | for (String path : sfcPathList) { |
| 267 | arrayNode.add(path); |
| 268 | } |
| 269 | result.putArray("sfcPathList").addAll(arrayNode); |
| 270 | |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame^] | 271 | sendMessage(SAMPLE_TOPOV_SHOW_SFC_PATH, result); |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 275 | private Link getLinkBetweenDevices(DeviceId deviceId, DeviceId previousDeviceId) { |
| 276 | Set<Link> deviceLinks = linkService.getDeviceEgressLinks(deviceId); |
| 277 | Set<Link> previousDeviceLinks = linkService.getDeviceIngressLinks(previousDeviceId); |
| 278 | for (Link link : deviceLinks) { |
| 279 | previousDeviceLinks.contains(link); |
| 280 | return link; |
| 281 | } |
| 282 | return null; |
| 283 | } |
| 284 | |
| 285 | private void addEdgeLinks(SfcLinkMap linkMap, Host host1) { |
| 286 | linkMap.add(createEdgeLink(host1, true)); |
| 287 | linkMap.add(createEdgeLink(host1, false)); |
| 288 | } |
| 289 | |
rama-huawei | c78f309 | 2016-05-19 18:09:29 +0530 | [diff] [blame] | 290 | private synchronized void cancelTask() { |
| 291 | if (demoTask != null) { |
| 292 | demoTask.cancel(); |
| 293 | demoTask = null; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | private void clearState() { |
| 298 | currentMode = Mode.IDLE; |
| 299 | elementOfNote = null; |
| 300 | linkSet = EMPTY_LINK_SET; |
| 301 | } |
| 302 | |
| 303 | private void clearForMode() { |
| 304 | sendHighlights(new Highlights()); |
| 305 | } |
| 306 | |
| 307 | private void sendHighlights(Highlights highlights) { |
| 308 | sendMessage(TopoJson.highlightsMessage(highlights)); |
| 309 | } |
| 310 | |
| 311 | } |