blob: cdf85dc42b84f3a5cff51dae09c65c287c3aab65 [file] [log] [blame]
Bri Prebilic Cole09e464b2015-01-07 17:13:54 -08001/*
2 * Copyright 2015 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
17package org.onosproject.gui;
18
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import com.google.common.collect.ImmutableSet;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.osgi.ServiceDirectory;
24import org.onlab.osgi.TestServiceDirectory;
25import org.onlab.packet.ChassisId;
26import org.onlab.packet.IpAddress;
27import org.onlab.packet.MacAddress;
28import org.onlab.packet.VlanId;
29import org.onosproject.cluster.ClusterService;
30import org.onosproject.cluster.ClusterServiceAdapter;
31import org.onosproject.core.CoreService;
32import org.onosproject.core.CoreServiceAdapter;
33import org.onosproject.core.Version;
34import org.onosproject.mastership.MastershipService;
35import org.onosproject.mastership.MastershipServiceAdapter;
36import org.onosproject.net.DefaultDevice;
37import org.onosproject.net.DefaultHost;
38import org.onosproject.net.Device;
39import org.onosproject.net.DeviceId;
40import org.onosproject.net.Host;
41import org.onosproject.net.HostId;
42import org.onosproject.net.HostLocation;
43import org.onosproject.net.Port;
44import org.onosproject.net.PortNumber;
45import org.onosproject.net.device.DeviceService;
46import org.onosproject.net.device.DeviceServiceAdapter;
47import org.onosproject.net.flow.FlowEntry;
48import org.onosproject.net.flow.FlowRuleService;
49import org.onosproject.net.flow.FlowRuleServiceAdapter;
50import org.onosproject.net.host.HostService;
51import org.onosproject.net.host.HostServiceAdapter;
52import org.onosproject.net.intent.IntentService;
53import org.onosproject.net.intent.IntentServiceAdapter;
54import org.onosproject.net.link.LinkService;
55import org.onosproject.net.link.LinkServiceAdapter;
56import org.onosproject.net.provider.ProviderId;
57import org.onosproject.net.statistic.StatisticService;
58import org.onosproject.net.statistic.StatisticServiceAdapter;
59import org.onosproject.net.topology.TopologyService;
60import org.onosproject.net.topology.TopologyServiceAdapter;
61
62import java.util.ArrayList;
63import java.util.List;
64import java.util.Set;
65
66import static org.junit.Assert.assertEquals;
67
68public class TopologyViewWebSocketTest {
69
70 private static final ProviderId PID = new ProviderId("of", "foo.bar");
71 private static final ChassisId CHID = new ChassisId(123L);
72 private static final MacAddress MAC = MacAddress.valueOf("00:00:00:00:00:19");
73 private static final DeviceId DID = DeviceId.deviceId("of:foo");
74 private static final Set<IpAddress> IPS = ImmutableSet.of(IpAddress.valueOf("1.2.3.4"));
75
76 private TestWebSocket ws;
77 private TestServiceDirectory sd;
78
79 @Before
80 public void setUp() {
81 sd = new TestServiceDirectory();
82 sd.add(DeviceService.class, new TestDeviceService());
83 sd.add(ClusterService.class, new ClusterServiceAdapter());
84 sd.add(LinkService.class, new LinkServiceAdapter());
85 sd.add(HostService.class, new TestHostService());
86 sd.add(MastershipService.class, new MastershipServiceAdapter());
87 sd.add(IntentService.class, new IntentServiceAdapter());
88 sd.add(FlowRuleService.class, new TestFlowService());
89 sd.add(StatisticService.class, new StatisticServiceAdapter());
90 sd.add(TopologyService.class, new TopologyServiceAdapter());
91 sd.add(CoreService.class, new TestCoreService());
92 ws = new TestWebSocket(sd);
93 }
94
95 @Test
96 public void requestDetailsDevice() {
97 // build the request
98 String request = "{\"event\":\"requestDetails\", \"sid\":0, "
99 + "\"payload\":{\"id\":\"of:000001\",\"class\":\"device\"}}";
100 ws.onMessage(request);
101 // look at the ws reply, and verify that it is correct
102 assertEquals("incorrect id", "of:000001", ws.reply.path("payload").path("id").asText());
103 assertEquals("incorrect mfr", "foo", ws.reply.path("payload").path("props").path("Vendor").asText());
104 }
105
106 @Test
107 public void requestDetailsHost() {
108 // build the request
109 String request = "{\"event\":\"requestDetails\", \"sid\":0, "
110 + "\"payload\":{\"id\":\"00:00:00:00:00:19/-1\",\"class\":\"host\"}}";
111 ws.onMessage(request);
112 // look at the ws reply, and verify that it is correct
113 assertEquals("incorrect id", "00:00:00:00:00:19/-1", ws.reply.path("payload").path("id").asText());
114 assertEquals("incorrect ip address", "1.2.3.4", ws.reply.path("payload").path("props").path("IP").asText());
115 }
116
117 private class TestWebSocket extends TopologyViewWebSocket {
118
119 private ObjectNode reply;
120
121 /**
122 * Creates a new web-socket for serving data to GUI topology view.
123 *
124 * @param directory service directory
125 */
126 public TestWebSocket(ServiceDirectory directory) {
127 super(directory);
128 }
129
130 @Override
131 protected synchronized void sendMessage(ObjectNode data) {
132 reply = data;
133 }
134 }
135
136 private class TestCoreService extends CoreServiceAdapter {
137 @Override
138 public Version version() {
139 return Version.version("1.2.3");
140 }
141 }
142
143 private class TestDeviceService extends DeviceServiceAdapter {
144
145 @Override
146 public Device getDevice(DeviceId deviceId) {
147 return new DefaultDevice(PID, deviceId, Device.Type.SWITCH,
148 "foo", "hw", "sw", "sn", CHID);
149 }
150
151 @Override
152 public List<Port> getPorts(DeviceId deviceId) {
153 return new ArrayList<>();
154 }
155 }
156
157 private class TestFlowService extends FlowRuleServiceAdapter {
158 @Override
159 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
160 return new ArrayList<>();
161 }
162 }
163
164 private class TestHostService extends HostServiceAdapter {
165 @Override
166 public Host getHost(HostId hostId) {
167 return new DefaultHost(PID, hostId, MAC, VlanId.NONE,
168 new HostLocation(DID, PortNumber.P0, 123L), IPS);
169 }
170 }
171}