blob: df541b38f5c9b177693b7303e2af6a6edd139b49 [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
18import org.onlab.osgi.DefaultServiceDirectory;
19import org.onlab.osgi.ServiceDirectory;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import com.fasterxml.jackson.databind.node.ArrayNode;
22import com.google.common.collect.ImmutableSet;
23
24import org.onlab.packet.MacAddress;
25import org.onosproject.net.Device;
26import org.onosproject.net.Element;
27import org.onosproject.net.Link;
28import org.onosproject.net.device.DeviceService;
29import org.onosproject.net.host.HostService;
30import org.onosproject.net.link.LinkService;
31import org.onosproject.ui.topo.HostHighlight;
32import org.onosproject.ui.RequestHandler;
33import org.onosproject.ui.UiConnection;
34import org.onosproject.ui.UiMessageHandler;
35import org.onosproject.ui.topo.Highlights;
36import org.onosproject.ui.topo.NodeBadge;
37import org.onosproject.ui.topo.TopoJson;
38import org.onosproject.ui.topo.DeviceHighlight;
39import org.slf4j.Logger;
40import org.slf4j.LoggerFactory;
41import org.onosproject.vtnrsc.PortChain;
42import org.onosproject.vtnrsc.portchain.PortChainService;
43import org.onosproject.vtnrsc.portpair.PortPairService;
44import org.onosproject.vtnrsc.PortChainId;
45import org.onosproject.net.DeviceId;
46import org.onosproject.net.Host;
47import org.onosproject.net.HostId;
48import org.onosproject.vtnrsc.PortPair;
49import org.onosproject.vtnrsc.PortPairId;
50import org.onosproject.vtnrsc.VirtualPortId;
51import org.onosproject.vtnrsc.service.VtnRscService;
52import org.onosproject.vtnrsc.virtualport.VirtualPortService;
53import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService;
54import org.onosproject.vtnrsc.PortPairGroupId;
55import org.onosproject.vtnrsc.PortPairGroup;
56
57import java.util.Collection;
58import java.util.Timer;
59import java.util.TimerTask;
60import java.util.List;
61import java.util.ListIterator;
62
63/**
64 * SFC web gui topology-overlay message handler.
65 */
66public class SfcwebUiTopovMessageHandler extends UiMessageHandler {
67
68 private static final String SAMPLE_TOPOV_DISPLAY_START = "sfcwebTopovDisplayStart";
69 private static final String SAMPLE_TOPOV_DISPLAY_SFC = "showSfcInfo";
70 private static final String SAMPLE_TOPOV_DISPLAY_STOP = "sfcTopovClear";
71 private static final String CONFIG_SFP_MSG = "configSfpMessage";
72
73 private static final String ID = "id";
74 private static final String MODE = "mode";
75 private static final String SFC_ID = "SFC";
76
77 private static final long UPDATE_PERIOD_MS = 1000;
78
79 private static final Link[] EMPTY_LINK_SET = new Link[0];
80
81 private enum Mode { IDLE, MOUSE, LINK }
82
83 private final Logger log = LoggerFactory.getLogger(getClass());
84
85 private DeviceService deviceService;
86 private HostService hostService;
87 private LinkService linkService;
88
89 private final Timer timer = new Timer("sfcweb-overlay");
90 private TimerTask demoTask = null;
91 private Mode currentMode = Mode.IDLE;
92 private Element elementOfNote;
93 private Link[] linkSet = EMPTY_LINK_SET;
94 private int linkIndex;
95
96 private long someNumber = 1;
97 private long someIncrement = 1;
98 protected PortPairService portPairService;
99 protected VtnRscService vtnRscService;
100 protected VirtualPortService virtualPortService;
101 protected PortChainService portChainService;
102 protected PortPairGroupService portPairGroupService;
103
104 @Override
105 public void init(UiConnection connection, ServiceDirectory directory) {
106 super.init(connection, directory);
107 deviceService = directory.get(DeviceService.class);
108 hostService = directory.get(HostService.class);
109 linkService = directory.get(LinkService.class);
110 portChainService = directory.get(PortChainService.class);
111 portPairService = directory.get(PortPairService.class);
112 portPairGroupService = directory.get(PortPairGroupService.class);
113 }
114
115 @Override
116 protected Collection<RequestHandler> createRequestHandlers() {
117 return ImmutableSet.of(
118 new DisplayStartHandler(),
119 new DisplayStopHandler(),
120 new ConfigSfpMsg()
121 );
122 }
123
124 /**
125 * Handler classes.
126 */
127 private final class DisplayStartHandler extends RequestHandler {
128 public DisplayStartHandler() {
129 super(SAMPLE_TOPOV_DISPLAY_START);
130 }
131 @Override
132 public void process(long sid, ObjectNode payload) {
133 String mode = string(payload, MODE);
134 PortChainService pcs = get(PortChainService.class);
135 Iterable<PortChain> portChains = pcs.getPortChains();
136 ObjectNode result = objectNode();
137
138 ArrayNode arrayNode = arrayNode();
139
140 for (final PortChain portChain : portChains) {
141 arrayNode.add(portChain.portChainId().value().toString());
142 }
143 result.putArray("a").addAll(arrayNode);
144
145 sendMessage(SAMPLE_TOPOV_DISPLAY_SFC, sid, result);
146 }
147 }
148
149 private final class DisplayStopHandler extends RequestHandler {
150 public DisplayStopHandler() {
151 super(SAMPLE_TOPOV_DISPLAY_STOP);
152 }
153
154 @Override
155 public void process(long sid, ObjectNode payload) {
156 log.debug("Stop Display");
157 clearState();
158 clearForMode();
159 cancelTask();
160 }
161 }
162
163 private final class ConfigSfpMsg extends RequestHandler {
164 public ConfigSfpMsg() {
165 super(CONFIG_SFP_MSG);
166 }
167
168 @Override
169 public void process(long sid, ObjectNode payload) {
170 String id = string(payload, ID);
171 ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
172 vtnRscService = serviceDirectory.get(VtnRscService.class);
173 virtualPortService = serviceDirectory.get(VirtualPortService.class);
174
175 Highlights highlights = new Highlights();
176
177 PortChainId portChainId = PortChainId.of(id);
178 boolean portChainIdExist = portChainService.exists(portChainId);
179 if (!portChainIdExist) {
180 log.info("portchain id doesn't exist");
181 return;
182 }
183
184 PortChain portChain = portChainService.getPortChain(portChainId);
185
186 List<PortPairGroupId> llPortPairGroupIdList = portChain.portPairGroups();
187 ListIterator<PortPairGroupId> portPairGroupIdListIterator = llPortPairGroupIdList.listIterator();
188 while (portPairGroupIdListIterator.hasNext()) {
189 PortPairGroupId portPairGroupId = portPairGroupIdListIterator.next();
190 PortPairGroup portPairGroup = portPairGroupService.getPortPairGroup(portPairGroupId);
191 List<PortPairId> llPortPairIdList = portPairGroup.portPairs();
192 ListIterator<PortPairId> portPairListIterator = llPortPairIdList.listIterator();
193
194 while (portPairListIterator.hasNext()) {
195 PortPairId portPairId = portPairListIterator.next();
196 PortPair portPair = portPairService.getPortPair(portPairId);
197 DeviceId deviceId = vtnRscService.getSfToSffMaping(VirtualPortId.portId(portPair.egress()));
198 Device device = deviceService.getDevice(deviceId);
199 DeviceHighlight dh = new DeviceHighlight(device.id().toString());
200 dh.setBadge(NodeBadge.text(SFC_ID));
201
202 MacAddress dstMacAddress = virtualPortService.getPort(VirtualPortId
203 .portId(portPair.egress())).macAddress();
204 Host host = hostService.getHost(HostId.hostId(dstMacAddress));
205 HostHighlight hhDst = new HostHighlight(host.id().toString());
206 hhDst.setBadge(NodeBadge.text(SFC_ID));
207
208 MacAddress srcMacAddress = virtualPortService.getPort(VirtualPortId
209 .portId(portPair.ingress())).macAddress();
210 Host hostSrc = hostService.getHost(HostId.hostId(srcMacAddress));
211 HostHighlight hhSrc = new HostHighlight(hostSrc.id().toString());
212 hhSrc.setBadge(NodeBadge.text(SFC_ID));
213
214 highlights.add(dh);
215 highlights.add(hhSrc);
216 highlights.add(hhDst);
217 }
218 }
219
220 sendHighlights(highlights);
221 }
222 }
223
224 private synchronized void cancelTask() {
225 if (demoTask != null) {
226 demoTask.cancel();
227 demoTask = null;
228 }
229 }
230
231 private void clearState() {
232 currentMode = Mode.IDLE;
233 elementOfNote = null;
234 linkSet = EMPTY_LINK_SET;
235 }
236
237 private void clearForMode() {
238 sendHighlights(new Highlights());
239 }
240
241 private void sendHighlights(Highlights highlights) {
242 sendMessage(TopoJson.highlightsMessage(highlights));
243 }
244
245}