pankaj

Merge branch 'master' of ssh://gerrit.onlab.us:29418/onos-next

Showing 20 changed files with 291 additions and 19 deletions
1 package org.onlab.onos.ifwd; 1 package org.onlab.onos.ifwd;
2 2
3 +import static org.slf4j.LoggerFactory.getLogger;
4 +
3 import org.apache.felix.scr.annotations.Activate; 5 import org.apache.felix.scr.annotations.Activate;
4 import org.apache.felix.scr.annotations.Component; 6 import org.apache.felix.scr.annotations.Component;
5 import org.apache.felix.scr.annotations.Deactivate; 7 import org.apache.felix.scr.annotations.Deactivate;
...@@ -26,8 +28,6 @@ import org.onlab.onos.net.topology.TopologyService; ...@@ -26,8 +28,6 @@ import org.onlab.onos.net.topology.TopologyService;
26 import org.onlab.packet.Ethernet; 28 import org.onlab.packet.Ethernet;
27 import org.slf4j.Logger; 29 import org.slf4j.Logger;
28 30
29 -import static org.slf4j.LoggerFactory.getLogger;
30 -
31 /** 31 /**
32 * WORK-IN-PROGRESS: Sample reactive forwarding application using intent framework. 32 * WORK-IN-PROGRESS: Sample reactive forwarding application using intent framework.
33 */ 33 */
...@@ -50,7 +50,7 @@ public class IntentReactiveForwarding { ...@@ -50,7 +50,7 @@ public class IntentReactiveForwarding {
50 50
51 private ReactivePacketProcessor processor = new ReactivePacketProcessor(); 51 private ReactivePacketProcessor processor = new ReactivePacketProcessor();
52 52
53 - private static long intentId = 1; 53 + private static long intentId = 0x123000;
54 54
55 @Activate 55 @Activate
56 public void activate() { 56 public void activate() {
......
...@@ -36,6 +36,12 @@ ...@@ -36,6 +36,12 @@
36 <scope>test</scope> 36 <scope>test</scope>
37 </dependency> 37 </dependency>
38 38
39 + <dependency>
40 + <groupId>org.easymock</groupId>
41 + <artifactId>easymock</artifactId>
42 + <scope>test</scope>
43 + </dependency>
44 +
39 <!-- TODO Consider removing store dependency. 45 <!-- TODO Consider removing store dependency.
40 Currently required for DistributedDeviceManagerTest. --> 46 Currently required for DistributedDeviceManagerTest. -->
41 <dependency> 47 <dependency>
......
...@@ -60,14 +60,15 @@ public class HostMonitor implements TimerTask { ...@@ -60,14 +60,15 @@ public class HostMonitor implements TimerTask {
60 * 60 *
61 * @param deviceService device service used to find edge ports 61 * @param deviceService device service used to find edge ports
62 * @param packetService packet service used to send packets on the data plane 62 * @param packetService packet service used to send packets on the data plane
63 - * @param hostService host service used to look up host information 63 + * @param hostManager host manager used to look up host information and
64 + * probe existing hosts
64 */ 65 */
65 public HostMonitor(DeviceService deviceService, PacketService packetService, 66 public HostMonitor(DeviceService deviceService, PacketService packetService,
66 - HostManager hostService) { 67 + HostManager hostManager) {
67 68
68 this.deviceService = deviceService; 69 this.deviceService = deviceService;
69 this.packetService = packetService; 70 this.packetService = packetService;
70 - this.hostManager = hostService; 71 + this.hostManager = hostManager;
71 72
72 monitoredAddresses = Collections.newSetFromMap( 73 monitoredAddresses = Collections.newSetFromMap(
73 new ConcurrentHashMap<IpAddress, Boolean>()); 74 new ConcurrentHashMap<IpAddress, Boolean>());
......
1 +package org.onlab.onos.net.host.impl;
2 +
3 +import static org.easymock.EasyMock.createMock;
4 +import static org.easymock.EasyMock.expect;
5 +import static org.easymock.EasyMock.expectLastCall;
6 +import static org.easymock.EasyMock.replay;
7 +import static org.easymock.EasyMock.verify;
8 +import static org.junit.Assert.assertTrue;
9 +
10 +import java.util.ArrayList;
11 +import java.util.Arrays;
12 +import java.util.Collections;
13 +import java.util.List;
14 +import java.util.Set;
15 +
16 +import org.junit.Test;
17 +import org.onlab.onos.net.ConnectPoint;
18 +import org.onlab.onos.net.Device;
19 +import org.onlab.onos.net.DeviceId;
20 +import org.onlab.onos.net.Host;
21 +import org.onlab.onos.net.MastershipRole;
22 +import org.onlab.onos.net.Port;
23 +import org.onlab.onos.net.PortNumber;
24 +import org.onlab.onos.net.device.DeviceListener;
25 +import org.onlab.onos.net.device.DeviceService;
26 +import org.onlab.onos.net.flow.instructions.Instruction;
27 +import org.onlab.onos.net.flow.instructions.Instructions.OutputInstruction;
28 +import org.onlab.onos.net.host.HostProvider;
29 +import org.onlab.onos.net.host.PortAddresses;
30 +import org.onlab.onos.net.packet.OutboundPacket;
31 +import org.onlab.onos.net.packet.PacketProcessor;
32 +import org.onlab.onos.net.packet.PacketService;
33 +import org.onlab.onos.net.provider.ProviderId;
34 +import org.onlab.packet.ARP;
35 +import org.onlab.packet.Ethernet;
36 +import org.onlab.packet.IpAddress;
37 +import org.onlab.packet.IpPrefix;
38 +import org.onlab.packet.MacAddress;
39 +
40 +import com.google.common.collect.HashMultimap;
41 +import com.google.common.collect.Lists;
42 +import com.google.common.collect.Multimap;
43 +
44 +public class HostMonitorTest {
45 +
46 + private IpAddress targetIpAddress = IpAddress.valueOf("10.0.0.1");
47 + private IpPrefix targetIpPrefix = IpPrefix.valueOf(targetIpAddress.toOctets());
48 +
49 + private IpPrefix sourcePrefix = IpPrefix.valueOf("10.0.0.99/24");
50 + private MacAddress sourceMac = MacAddress.valueOf(1L);
51 +
52 + private HostMonitor hostMonitor;
53 +
54 + @Test
55 + public void testMonitorHostExists() throws Exception {
56 + ProviderId id = new ProviderId("fake://", "id");
57 +
58 + Host host = createMock(Host.class);
59 + expect(host.providerId()).andReturn(id);
60 + replay(host);
61 +
62 + HostManager hostManager = createMock(HostManager.class);
63 + expect(hostManager.getHostsByIp(targetIpPrefix))
64 + .andReturn(Collections.singleton(host));
65 + replay(hostManager);
66 +
67 + HostProvider hostProvider = createMock(HostProvider.class);
68 + expect(hostProvider.id()).andReturn(id).anyTimes();
69 + hostProvider.triggerProbe(host);
70 + expectLastCall().once();
71 + replay(hostProvider);
72 +
73 + hostMonitor = new HostMonitor(null, null, hostManager);
74 +
75 + hostMonitor.registerHostProvider(hostProvider);
76 + hostMonitor.addMonitoringFor(targetIpAddress);
77 +
78 + hostMonitor.run(null);
79 +
80 + verify(hostProvider);
81 + }
82 +
83 + @Test
84 + public void testMonitorHostDoesNotExist() throws Exception {
85 + HostManager hostManager = createMock(HostManager.class);
86 +
87 + DeviceId devId = DeviceId.deviceId("fake");
88 +
89 + Device device = createMock(Device.class);
90 + expect(device.id()).andReturn(devId).anyTimes();
91 + replay(device);
92 +
93 + PortNumber portNum = PortNumber.portNumber(1L);
94 +
95 + Port port = createMock(Port.class);
96 + expect(port.number()).andReturn(portNum).anyTimes();
97 + replay(port);
98 +
99 + TestDeviceService deviceService = new TestDeviceService();
100 + deviceService.addDevice(device, Collections.singleton(port));
101 +
102 + ConnectPoint cp = new ConnectPoint(devId, portNum);
103 + PortAddresses pa = new PortAddresses(cp, Collections.singleton(sourcePrefix),
104 + sourceMac);
105 +
106 + expect(hostManager.getHostsByIp(targetIpPrefix))
107 + .andReturn(Collections.<Host>emptySet()).anyTimes();
108 + expect(hostManager.getAddressBindingsForPort(cp))
109 + .andReturn(pa).anyTimes();
110 + replay(hostManager);
111 +
112 + TestPacketService packetService = new TestPacketService();
113 +
114 +
115 + // Run the test
116 + hostMonitor = new HostMonitor(deviceService, packetService, hostManager);
117 +
118 + hostMonitor.addMonitoringFor(targetIpAddress);
119 + hostMonitor.run(null);
120 +
121 +
122 + // Check that a packet was sent to our PacketService and that it has
123 + // the properties we expect
124 + assertTrue(packetService.packets.size() == 1);
125 + OutboundPacket packet = packetService.packets.get(0);
126 +
127 + // Check the output port is correct
128 + assertTrue(packet.treatment().instructions().size() == 1);
129 + Instruction instruction = packet.treatment().instructions().get(0);
130 + assertTrue(instruction instanceof OutputInstruction);
131 + OutputInstruction oi = (OutputInstruction) instruction;
132 + assertTrue(oi.port().equals(portNum));
133 +
134 + // Check the output packet is correct (well the important bits anyway)
135 + Ethernet eth = new Ethernet();
136 + eth.deserialize(packet.data().array(), 0, packet.data().array().length);
137 + ARP arp = (ARP) eth.getPayload();
138 + assertTrue(Arrays.equals(arp.getSenderProtocolAddress(), sourcePrefix.toOctets()));
139 + assertTrue(Arrays.equals(arp.getSenderHardwareAddress(), sourceMac.toBytes()));
140 + assertTrue(Arrays.equals(arp.getTargetProtocolAddress(), targetIpPrefix.toOctets()));
141 + }
142 +
143 + class TestPacketService implements PacketService {
144 +
145 + List<OutboundPacket> packets = new ArrayList<>();
146 +
147 + @Override
148 + public void addProcessor(PacketProcessor processor, int priority) {
149 + }
150 +
151 + @Override
152 + public void removeProcessor(PacketProcessor processor) {
153 + }
154 +
155 + @Override
156 + public void emit(OutboundPacket packet) {
157 + packets.add(packet);
158 + }
159 + }
160 +
161 + class TestDeviceService implements DeviceService {
162 +
163 + List<Device> devices = Lists.newArrayList();
164 + Multimap<DeviceId, Port> devicePorts = HashMultimap.create();
165 +
166 + void addDevice(Device device, Set<Port> ports) {
167 + devices.add(device);
168 + for (Port p : ports) {
169 + devicePorts.put(device.id(), p);
170 + }
171 + }
172 +
173 + @Override
174 + public int getDeviceCount() {
175 + return 0;
176 + }
177 +
178 + @Override
179 + public Iterable<Device> getDevices() {
180 + return devices;
181 + }
182 +
183 + @Override
184 + public Device getDevice(DeviceId deviceId) {
185 + return null;
186 + }
187 +
188 + @Override
189 + public MastershipRole getRole(DeviceId deviceId) {
190 + return null;
191 + }
192 +
193 + @Override
194 + public List<Port> getPorts(DeviceId deviceId) {
195 + List<Port> ports = Lists.newArrayList();
196 + for (Port p : devicePorts.get(deviceId)) {
197 + ports.add(p);
198 + }
199 + return ports;
200 + }
201 +
202 + @Override
203 + public Port getPort(DeviceId deviceId, PortNumber portNumber) {
204 + return null;
205 + }
206 +
207 + @Override
208 + public boolean isAvailable(DeviceId deviceId) {
209 + return false;
210 + }
211 +
212 + @Override
213 + public void addListener(DeviceListener listener) {
214 + }
215 +
216 + @Override
217 + public void removeListener(DeviceListener listener) {
218 + }
219 + }
220 +}
...@@ -3,6 +3,8 @@ package org.onlab.onos.store.cluster.messaging; ...@@ -3,6 +3,8 @@ package org.onlab.onos.store.cluster.messaging;
3 import org.onlab.onos.cluster.ControllerNode; 3 import org.onlab.onos.cluster.ControllerNode;
4 import org.onlab.onos.store.cluster.impl.ClusterNodesDelegate; 4 import org.onlab.onos.store.cluster.impl.ClusterNodesDelegate;
5 5
6 +// TODO: This service interface can be removed, once we properly start
7 +// using ClusterService
6 /** 8 /**
7 * Service for administering communications manager. 9 * Service for administering communications manager.
8 */ 10 */
......
...@@ -2,6 +2,8 @@ package org.onlab.onos.store.cluster.messaging; ...@@ -2,6 +2,8 @@ package org.onlab.onos.store.cluster.messaging;
2 2
3 import org.onlab.onos.cluster.NodeId; 3 import org.onlab.onos.cluster.NodeId;
4 4
5 +// TODO: ClusterMessage should be aware about how to serialize the payload
6 +// TODO: Should payload type be made generic?
5 /** 7 /**
6 * Base message for cluster-wide communications. 8 * Base message for cluster-wide communications.
7 */ 9 */
......
1 package org.onlab.onos.store.cluster.messaging; 1 package org.onlab.onos.store.cluster.messaging;
2 2
3 +import static com.google.common.base.Preconditions.checkNotNull;
4 +
5 +import java.util.Objects;
6 +
3 /** 7 /**
4 * Representation of a message subject. 8 * Representation of a message subject.
5 * Cluster messages have associated subjects that dictate how they get handled 9 * Cluster messages have associated subjects that dictate how they get handled
...@@ -10,7 +14,7 @@ public class MessageSubject { ...@@ -10,7 +14,7 @@ public class MessageSubject {
10 private final String value; 14 private final String value;
11 15
12 public MessageSubject(String value) { 16 public MessageSubject(String value) {
13 - this.value = value; 17 + this.value = checkNotNull(value);
14 } 18 }
15 19
16 public String value() { 20 public String value() {
...@@ -21,4 +25,24 @@ public class MessageSubject { ...@@ -21,4 +25,24 @@ public class MessageSubject {
21 public String toString() { 25 public String toString() {
22 return value; 26 return value;
23 } 27 }
28 +
29 + @Override
30 + public int hashCode() {
31 + return value.hashCode();
32 + }
33 +
34 + @Override
35 + public boolean equals(Object obj) {
36 + if (this == obj) {
37 + return true;
38 + }
39 + if (obj == null) {
40 + return false;
41 + }
42 + if (getClass() != obj.getClass()) {
43 + return false;
44 + }
45 + MessageSubject that = (MessageSubject) obj;
46 + return Objects.equals(this.value, that.value);
47 + }
24 } 48 }
......
...@@ -39,6 +39,7 @@ public class ClusterCommunicationManager ...@@ -39,6 +39,7 @@ public class ClusterCommunicationManager
39 39
40 private ControllerNode localNode; 40 private ControllerNode localNode;
41 private ClusterNodesDelegate nodesDelegate; 41 private ClusterNodesDelegate nodesDelegate;
42 + // FIXME: `members` should go away and should be using ClusterService
42 private Map<NodeId, ControllerNode> members = new HashMap<>(); 43 private Map<NodeId, ControllerNode> members = new HashMap<>();
43 private final Timer timer = new Timer("onos-controller-heatbeats"); 44 private final Timer timer = new Timer("onos-controller-heatbeats");
44 public static final long HEART_BEAT_INTERVAL_MILLIS = 1000L; 45 public static final long HEART_BEAT_INTERVAL_MILLIS = 1000L;
......
...@@ -3,6 +3,8 @@ package org.onlab.onos.store.cluster.messaging.impl; ...@@ -3,6 +3,8 @@ package org.onlab.onos.store.cluster.messaging.impl;
3 import org.onlab.onos.store.cluster.messaging.MessageSubject; 3 import org.onlab.onos.store.cluster.messaging.MessageSubject;
4 4
5 public final class ClusterMessageSubjects { 5 public final class ClusterMessageSubjects {
6 + // avoid instantiation
6 private ClusterMessageSubjects() {} 7 private ClusterMessageSubjects() {}
8 +
7 public static final MessageSubject CLUSTER_MEMBERSHIP_EVENT = new MessageSubject("CLUSTER_MEMBERSHIP_EVENT"); 9 public static final MessageSubject CLUSTER_MEMBERSHIP_EVENT = new MessageSubject("CLUSTER_MEMBERSHIP_EVENT");
8 } 10 }
......
...@@ -6,6 +6,8 @@ import java.util.Objects; ...@@ -6,6 +6,8 @@ import java.util.Objects;
6 6
7 import org.onlab.onos.store.Timestamp; 7 import org.onlab.onos.store.Timestamp;
8 8
9 +import com.google.common.base.MoreObjects;
10 +
9 /** 11 /**
10 * Wrapper class to store Timestamped value. 12 * Wrapper class to store Timestamped value.
11 * @param <T> 13 * @param <T>
...@@ -70,6 +72,14 @@ public final class Timestamped<T> { ...@@ -70,6 +72,14 @@ public final class Timestamped<T> {
70 return Objects.equals(this.timestamp, that.timestamp); 72 return Objects.equals(this.timestamp, that.timestamp);
71 } 73 }
72 74
75 + @Override
76 + public String toString() {
77 + return MoreObjects.toStringHelper(getClass())
78 + .add("timestamp", timestamp)
79 + .add("value", value)
80 + .toString();
81 + }
82 +
73 // Default constructor for serialization 83 // Default constructor for serialization
74 @Deprecated 84 @Deprecated
75 protected Timestamped() { 85 protected Timestamped() {
......
...@@ -129,6 +129,12 @@ ...@@ -129,6 +129,12 @@
129 <version>1.9.13</version> 129 <version>1.9.13</version>
130 </dependency> 130 </dependency>
131 131
132 + <dependency>
133 + <groupId>org.easymock</groupId>
134 + <artifactId>easymock</artifactId>
135 + <version>3.2</version>
136 + <scope>test</scope>
137 + </dependency>
132 138
133 <!-- Web related --> 139 <!-- Web related -->
134 <dependency> 140 <dependency>
......
...@@ -45,12 +45,12 @@ public class KryoSerializer implements Serializer { ...@@ -45,12 +45,12 @@ public class KryoSerializer implements Serializer {
45 } 45 }
46 46
47 @Override 47 @Override
48 - public <T> T deserialize(ByteBuffer buffer) { 48 + public <T> T decode(ByteBuffer buffer) {
49 return serializerPool.deserialize(buffer); 49 return serializerPool.deserialize(buffer);
50 } 50 }
51 51
52 @Override 52 @Override
53 - public void serialize(Object obj, ByteBuffer buffer) { 53 + public void encode(Object obj, ByteBuffer buffer) {
54 serializerPool.serialize(obj, buffer); 54 serializerPool.serialize(obj, buffer);
55 } 55 }
56 } 56 }
......
...@@ -48,7 +48,7 @@ public class MessageDecoder extends ReplayingDecoder<DecoderState> { ...@@ -48,7 +48,7 @@ public class MessageDecoder extends ReplayingDecoder<DecoderState> {
48 checkState(serializerVersion == MessageEncoder.SERIALIZER_VERSION, "Unexpected serializer version"); 48 checkState(serializerVersion == MessageEncoder.SERIALIZER_VERSION, "Unexpected serializer version");
49 checkpoint(DecoderState.READ_CONTENT); 49 checkpoint(DecoderState.READ_CONTENT);
50 case READ_CONTENT: 50 case READ_CONTENT:
51 - InternalMessage message = serializer.deserialize(buffer.readBytes(contentLength).nioBuffer()); 51 + InternalMessage message = serializer.decode(buffer.readBytes(contentLength).nioBuffer());
52 message.setMessagingService(messagingService); 52 message.setMessagingService(messagingService);
53 out.add(message); 53 out.add(message);
54 checkpoint(DecoderState.READ_HEADER_VERSION); 54 checkpoint(DecoderState.READ_HEADER_VERSION);
......
...@@ -24,20 +24,18 @@ public interface Serializer { ...@@ -24,20 +24,18 @@ public interface Serializer {
24 public byte[] encode(Object data); 24 public byte[] encode(Object data);
25 25
26 /** 26 /**
27 - * Serializes the specified object into bytes using one of the 27 + * Encodes the specified POJO into a byte buffer.
28 - * pre-registered serializers.
29 * 28 *
30 - * @param obj object to be serialized 29 + * @param data POJO to be encoded
31 * @param buffer to write serialized bytes 30 * @param buffer to write serialized bytes
32 */ 31 */
33 - public void serialize(final Object obj, ByteBuffer buffer); 32 + public void encode(final Object data, ByteBuffer buffer);
34 33
35 /** 34 /**
36 - * Deserializes the specified bytes into an object using one of the 35 + * Decodes the specified byte buffer to a POJO.
37 - * pre-registered serializers.
38 * 36 *
39 - * @param buffer bytes to be deserialized 37 + * @param buffer bytes to be decoded
40 - * @return deserialized object 38 + * @return POJO
41 */ 39 */
42 - public <T> T deserialize(final ByteBuffer buffer); 40 + public <T> T decode(final ByteBuffer buffer);
43 } 41 }
......