blob: daedb142cb5da831e0d1730629efe4e6e7f37ead [file] [log] [blame]
rama-huaweic78f3092016-05-19 18:09:29 +05301/*
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 */
16package org.onosproject.sfcweb;
17
Simon Hunt8a0429a2017-01-06 16:52:47 -080018import com.fasterxml.jackson.databind.node.ArrayNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import com.google.common.collect.ImmutableSet;
Phaneendra Mandab212bc92016-07-08 16:50:11 +053021import jersey.repackaged.com.google.common.collect.Lists;
rama-huaweic78f3092016-05-19 18:09:29 +053022import org.onlab.osgi.DefaultServiceDirectory;
23import org.onlab.osgi.ServiceDirectory;
rama-huaweic78f3092016-05-19 18:09:29 +053024import org.onlab.packet.MacAddress;
Phaneendra Mandab212bc92016-07-08 16:50:11 +053025import org.onosproject.net.DeviceId;
rama-huaweic78f3092016-05-19 18:09:29 +053026import org.onosproject.net.Element;
Phaneendra Mandab212bc92016-07-08 16:50:11 +053027import org.onosproject.net.Host;
28import org.onosproject.net.HostId;
rama-huaweic78f3092016-05-19 18:09:29 +053029import org.onosproject.net.Link;
rama-huaweic78f3092016-05-19 18:09:29 +053030import org.onosproject.net.host.HostService;
31import org.onosproject.net.link.LinkService;
rama-huaweic78f3092016-05-19 18:09:29 +053032import org.onosproject.ui.RequestHandler;
33import org.onosproject.ui.UiConnection;
34import org.onosproject.ui.UiMessageHandler;
Phaneendra Mandab212bc92016-07-08 16:50:11 +053035import org.onosproject.ui.topo.DeviceHighlight;
rama-huaweic78f3092016-05-19 18:09:29 +053036import org.onosproject.ui.topo.Highlights;
Phaneendra Mandab212bc92016-07-08 16:50:11 +053037import org.onosproject.ui.topo.HostHighlight;
rama-huaweic78f3092016-05-19 18:09:29 +053038import org.onosproject.ui.topo.NodeBadge;
39import org.onosproject.ui.topo.TopoJson;
Phaneendra Mandab212bc92016-07-08 16:50:11 +053040import org.onosproject.vtnrsc.FiveTuple;
41import org.onosproject.vtnrsc.LoadBalanceId;
rama-huaweic78f3092016-05-19 18:09:29 +053042import org.onosproject.vtnrsc.PortChain;
rama-huaweic78f3092016-05-19 18:09:29 +053043import org.onosproject.vtnrsc.PortChainId;
rama-huaweic78f3092016-05-19 18:09:29 +053044import org.onosproject.vtnrsc.PortPair;
45import org.onosproject.vtnrsc.PortPairId;
Phaneendra Mandab212bc92016-07-08 16:50:11 +053046import org.onosproject.vtnrsc.VirtualPort;
rama-huaweic78f3092016-05-19 18:09:29 +053047import org.onosproject.vtnrsc.VirtualPortId;
Phaneendra Mandab212bc92016-07-08 16:50:11 +053048import org.onosproject.vtnrsc.portchain.PortChainService;
49import org.onosproject.vtnrsc.portpair.PortPairService;
50import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService;
rama-huaweic78f3092016-05-19 18:09:29 +053051import org.onosproject.vtnrsc.service.VtnRscService;
52import org.onosproject.vtnrsc.virtualport.VirtualPortService;
Phaneendra Mandab212bc92016-07-08 16:50:11 +053053import org.slf4j.Logger;
54import org.slf4j.LoggerFactory;
rama-huaweic78f3092016-05-19 18:09:29 +053055
Simon Hunt8a0429a2017-01-06 16:52:47 -080056import java.util.Collection;
57import java.util.List;
58import java.util.ListIterator;
59import java.util.Set;
60import java.util.TimerTask;
61
62import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
rama-huaweic78f3092016-05-19 18:09:29 +053063
64/**
65 * SFC web gui topology-overlay message handler.
66 */
67public 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 Mandab212bc92016-07-08 16:50:11 +053073 private static final String SAMPLE_TOPOV_SHOW_SFC_PATH = "showSfcPath";
rama-huaweic78f3092016-05-19 18:09:29 +053074 private static final String ID = "id";
75 private static final String MODE = "mode";
Phaneendra Mandab212bc92016-07-08 16:50:11 +053076 private static final String CLASSIFIER = "CLS";
77 private static final String FORWARDER = "SFF";
rama-huaweic78f3092016-05-19 18:09:29 +053078
79 private static final Link[] EMPTY_LINK_SET = new Link[0];
80
Phaneendra Mandab212bc92016-07-08 16:50:11 +053081 private enum Mode {
82 IDLE, MOUSE, LINK
83 }
rama-huaweic78f3092016-05-19 18:09:29 +053084
85 private final Logger log = LoggerFactory.getLogger(getClass());
86
rama-huaweic78f3092016-05-19 18:09:29 +053087 private HostService hostService;
88 private LinkService linkService;
rama-huaweic78f3092016-05-19 18:09:29 +053089 private TimerTask demoTask = null;
90 private Mode currentMode = Mode.IDLE;
91 private Element elementOfNote;
92 private Link[] linkSet = EMPTY_LINK_SET;
rama-huaweic78f3092016-05-19 18:09:29 +053093
rama-huaweic78f3092016-05-19 18:09:29 +053094 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-huaweic78f3092016-05-19 18:09:29 +0530103 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 Hunt8a0429a2017-01-06 16:52:47 -0800126
rama-huaweic78f3092016-05-19 18:09:29 +0530127 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800128 public void process(ObjectNode payload) {
rama-huaweic78f3092016-05-19 18:09:29 +0530129 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 Hunt8a0429a2017-01-06 16:52:47 -0800141 sendMessage(SAMPLE_TOPOV_DISPLAY_SFC, result);
rama-huaweic78f3092016-05-19 18:09:29 +0530142 }
143 }
144
145 private final class DisplayStopHandler extends RequestHandler {
146 public DisplayStopHandler() {
147 super(SAMPLE_TOPOV_DISPLAY_STOP);
148 }
149
150 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800151 public void process(ObjectNode payload) {
rama-huaweic78f3092016-05-19 18:09:29 +0530152 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 Hunt8a0429a2017-01-06 16:52:47 -0800165 public void process(ObjectNode payload) {
rama-huaweic78f3092016-05-19 18:09:29 +0530166 String id = string(payload, ID);
167 ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
168 vtnRscService = serviceDirectory.get(VtnRscService.class);
169 virtualPortService = serviceDirectory.get(VirtualPortService.class);
170
Phaneendra Mandab212bc92016-07-08 16:50:11 +0530171 List<String> sfcPathList = Lists.newArrayList();
172
rama-huaweic78f3092016-05-19 18:09:29 +0530173 Highlights highlights = new Highlights();
Phaneendra Mandab212bc92016-07-08 16:50:11 +0530174 SfcLinkMap linkMap = new SfcLinkMap();
rama-huaweic78f3092016-05-19 18:09:29 +0530175
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 Mandab212bc92016-07-08 16:50:11 +0530185 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-huaweic78f3092016-05-19 18:09:29 +0530190
Phaneendra Mandab212bc92016-07-08 16:50:11 +0530191 // 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-huaweic78f3092016-05-19 18:09:29 +0530203 PortPair portPair = portPairService.getPortPair(portPairId);
204 DeviceId deviceId = vtnRscService.getSfToSffMaping(VirtualPortId.portId(portPair.egress()));
Phaneendra Mandab212bc92016-07-08 16:50:11 +0530205 VirtualPort vPort = virtualPortService.getPort(VirtualPortId.portId(portPair.egress()));
206 MacAddress dstMacAddress = vPort.macAddress();
rama-huaweic78f3092016-05-19 18:09:29 +0530207 Host host = hostService.getHost(HostId.hostId(dstMacAddress));
rama-huaweic78f3092016-05-19 18:09:29 +0530208
Phaneendra Mandab212bc92016-07-08 16:50:11 +0530209 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-huaweic78f3092016-05-19 18:09:29 +0530229
230 highlights.add(dh);
Phaneendra Mandab212bc92016-07-08 16:50:11 +0530231
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-huaweic78f3092016-05-19 18:09:29 +0530245 highlights.add(hhDst);
Phaneendra Mandab212bc92016-07-08 16:50:11 +0530246 previousDeviceId = deviceId;
rama-huaweic78f3092016-05-19 18:09:29 +0530247 }
Phaneendra Mandab212bc92016-07-08 16:50:11 +0530248
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-huaweic78f3092016-05-19 18:09:29 +0530257 }
258
Phaneendra Mandab212bc92016-07-08 16:50:11 +0530259 for (SfcLink sfcLink : linkMap.biLinks()) {
260 highlights.add(sfcLink.highlight(null));
261 }
rama-huaweic78f3092016-05-19 18:09:29 +0530262 sendHighlights(highlights);
Phaneendra Mandab212bc92016-07-08 16:50:11 +0530263
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 Hunt8a0429a2017-01-06 16:52:47 -0800271 sendMessage(SAMPLE_TOPOV_SHOW_SFC_PATH, result);
rama-huaweic78f3092016-05-19 18:09:29 +0530272 }
273 }
274
Phaneendra Mandab212bc92016-07-08 16:50:11 +0530275 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-huaweic78f3092016-05-19 18:09:29 +0530290 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}