Committed by
Gerrit Code Review
Add LispMessageEncoder and LispMessageDecoder with unit tests
Change-Id: If73a41687a9c2400de23bbde6179a63ac7f75d15
Showing
10 changed files
with
886 additions
and
4 deletions
| 1 | COMPILE_DEPS = [ | 1 | COMPILE_DEPS = [ |
| 2 | '//lib:CORE_DEPS', | 2 | '//lib:CORE_DEPS', |
| 3 | - '//protocols/lisp/api:onos-protocols-lisp-api' | 3 | + '//protocols/lisp/api:onos-protocols-lisp-api', |
| 4 | + '//protocols/lisp/msg:onos-protocols-lisp-msg', | ||
| 5 | + '//lib:netty-buffer', | ||
| 6 | + '//lib:netty-codec', | ||
| 7 | + '//lib:netty-transport' | ||
| 4 | ] | 8 | ] |
| 5 | 9 | ||
| 6 | TEST_DEPS = [ | 10 | TEST_DEPS = [ | ... | ... |
| ... | @@ -41,6 +41,11 @@ | ... | @@ -41,6 +41,11 @@ |
| 41 | </dependency> | 41 | </dependency> |
| 42 | <dependency> | 42 | <dependency> |
| 43 | <groupId>org.onosproject</groupId> | 43 | <groupId>org.onosproject</groupId> |
| 44 | + <artifactId>onos-lisp-msg</artifactId> | ||
| 45 | + <version>${project.version}</version> | ||
| 46 | + </dependency> | ||
| 47 | + <dependency> | ||
| 48 | + <groupId>org.onosproject</groupId> | ||
| 44 | <artifactId>onos-api</artifactId> | 49 | <artifactId>onos-api</artifactId> |
| 45 | <classifier>tests</classifier> | 50 | <classifier>tests</classifier> |
| 46 | <scope>test</scope> | 51 | <scope>test</scope> | ... | ... |
| ... | @@ -15,10 +15,26 @@ | ... | @@ -15,10 +15,26 @@ |
| 15 | */ | 15 | */ |
| 16 | package org.onosproject.lisp.ctl; | 16 | package org.onosproject.lisp.ctl; |
| 17 | 17 | ||
| 18 | +import io.netty.buffer.ByteBuf; | ||
| 19 | +import io.netty.channel.ChannelHandlerContext; | ||
| 20 | +import io.netty.handler.codec.ByteToMessageDecoder; | ||
| 21 | +import org.onosproject.lisp.msg.protocols.LispMessage; | ||
| 22 | +import org.onosproject.lisp.msg.protocols.LispMessageReader; | ||
| 23 | +import org.onosproject.lisp.msg.protocols.LispMessageReaderFactory; | ||
| 24 | + | ||
| 25 | +import java.util.List; | ||
| 26 | + | ||
| 18 | /** | 27 | /** |
| 19 | - * Remove me. | 28 | + * Decode a LISP message from a ByteBuffer, for use in a netty pipeline. |
| 20 | */ | 29 | */ |
| 21 | -@Deprecated | 30 | +public class LispMessageDecoder extends ByteToMessageDecoder { |
| 22 | -public abstract class PlaceHolder { | 31 | + |
| 32 | + @Override | ||
| 33 | + protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, | ||
| 34 | + List<Object> list) throws Exception { | ||
| 23 | 35 | ||
| 36 | + LispMessageReader reader = LispMessageReaderFactory.getReader(byteBuf); | ||
| 37 | + LispMessage message = (LispMessage) reader.readFrom(byteBuf); | ||
| 38 | + list.add(message); | ||
| 39 | + } | ||
| 24 | } | 40 | } | ... | ... |
| 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.lisp.ctl; | ||
| 17 | + | ||
| 18 | + | ||
| 19 | +import io.netty.buffer.ByteBuf; | ||
| 20 | +import io.netty.channel.ChannelHandlerContext; | ||
| 21 | +import io.netty.handler.codec.MessageToByteEncoder; | ||
| 22 | +import org.onosproject.lisp.msg.protocols.LispMessage; | ||
| 23 | + | ||
| 24 | +import java.util.List; | ||
| 25 | + | ||
| 26 | +/** | ||
| 27 | + * Encode a LISP message for output into a ByteBuffer, | ||
| 28 | + * for use in a netty pipeline. | ||
| 29 | + */ | ||
| 30 | +public class LispMessageEncoder extends MessageToByteEncoder { | ||
| 31 | + | ||
| 32 | + @Override | ||
| 33 | + protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception { | ||
| 34 | + if (!(msg instanceof List)) { | ||
| 35 | + ((LispMessage) msg).writeTo(out); | ||
| 36 | + return; | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + List<LispMessage> msgList = (List<LispMessage>) msg; | ||
| 40 | + | ||
| 41 | + for (LispMessage message : msgList) { | ||
| 42 | + if (message != null) { | ||
| 43 | + message.writeTo(out); | ||
| 44 | + } | ||
| 45 | + } | ||
| 46 | + } | ||
| 47 | +} |
| 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.lisp.ctl; | ||
| 17 | + | ||
| 18 | +import io.netty.buffer.ByteBufAllocator; | ||
| 19 | +import io.netty.channel.Channel; | ||
| 20 | +import io.netty.channel.ChannelConfig; | ||
| 21 | +import io.netty.channel.ChannelFuture; | ||
| 22 | +import io.netty.channel.ChannelPipeline; | ||
| 23 | +import io.netty.channel.ChannelPromise; | ||
| 24 | +import io.netty.channel.ChannelProgressivePromise; | ||
| 25 | +import io.netty.channel.ChannelMetadata; | ||
| 26 | +import io.netty.channel.EventLoop; | ||
| 27 | +import io.netty.util.Attribute; | ||
| 28 | +import io.netty.util.AttributeKey; | ||
| 29 | + | ||
| 30 | +import java.net.SocketAddress; | ||
| 31 | + | ||
| 32 | +/** | ||
| 33 | + * Adapter for testing against a netty channel. | ||
| 34 | + */ | ||
| 35 | +public class ChannelAdapter implements Channel { | ||
| 36 | + @Override | ||
| 37 | + public EventLoop eventLoop() { | ||
| 38 | + return null; | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + @Override | ||
| 42 | + public Channel parent() { | ||
| 43 | + return null; | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + @Override | ||
| 47 | + public ChannelConfig config() { | ||
| 48 | + return null; | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + @Override | ||
| 52 | + public boolean isOpen() { | ||
| 53 | + return false; | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + @Override | ||
| 57 | + public boolean isRegistered() { | ||
| 58 | + return false; | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + @Override | ||
| 62 | + public boolean isActive() { | ||
| 63 | + return false; | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + @Override | ||
| 67 | + public ChannelMetadata metadata() { | ||
| 68 | + return null; | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + @Override | ||
| 72 | + public SocketAddress localAddress() { | ||
| 73 | + return null; | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + @Override | ||
| 77 | + public SocketAddress remoteAddress() { | ||
| 78 | + return null; | ||
| 79 | + } | ||
| 80 | + | ||
| 81 | + @Override | ||
| 82 | + public ChannelFuture closeFuture() { | ||
| 83 | + return null; | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + @Override | ||
| 87 | + public boolean isWritable() { | ||
| 88 | + return false; | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + @Override | ||
| 92 | + public Unsafe unsafe() { | ||
| 93 | + return null; | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + @Override | ||
| 97 | + public ChannelPipeline pipeline() { | ||
| 98 | + return null; | ||
| 99 | + } | ||
| 100 | + | ||
| 101 | + @Override | ||
| 102 | + public ByteBufAllocator alloc() { | ||
| 103 | + return null; | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | + @Override | ||
| 107 | + public ChannelPromise newPromise() { | ||
| 108 | + return null; | ||
| 109 | + } | ||
| 110 | + | ||
| 111 | + @Override | ||
| 112 | + public ChannelProgressivePromise newProgressivePromise() { | ||
| 113 | + return null; | ||
| 114 | + } | ||
| 115 | + | ||
| 116 | + @Override | ||
| 117 | + public ChannelFuture newSucceededFuture() { | ||
| 118 | + return null; | ||
| 119 | + } | ||
| 120 | + | ||
| 121 | + @Override | ||
| 122 | + public ChannelFuture newFailedFuture(Throwable cause) { | ||
| 123 | + return null; | ||
| 124 | + } | ||
| 125 | + | ||
| 126 | + @Override | ||
| 127 | + public ChannelPromise voidPromise() { | ||
| 128 | + return null; | ||
| 129 | + } | ||
| 130 | + | ||
| 131 | + @Override | ||
| 132 | + public ChannelFuture bind(SocketAddress localAddress) { | ||
| 133 | + return null; | ||
| 134 | + } | ||
| 135 | + | ||
| 136 | + @Override | ||
| 137 | + public ChannelFuture connect(SocketAddress remoteAddress) { | ||
| 138 | + return null; | ||
| 139 | + } | ||
| 140 | + | ||
| 141 | + @Override | ||
| 142 | + public ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress) { | ||
| 143 | + return null; | ||
| 144 | + } | ||
| 145 | + | ||
| 146 | + @Override | ||
| 147 | + public ChannelFuture disconnect() { | ||
| 148 | + return null; | ||
| 149 | + } | ||
| 150 | + | ||
| 151 | + @Override | ||
| 152 | + public ChannelFuture close() { | ||
| 153 | + return null; | ||
| 154 | + } | ||
| 155 | + | ||
| 156 | + @Override | ||
| 157 | + public ChannelFuture deregister() { | ||
| 158 | + return null; | ||
| 159 | + } | ||
| 160 | + | ||
| 161 | + @Override | ||
| 162 | + public ChannelFuture bind(SocketAddress localAddress, ChannelPromise promise) { | ||
| 163 | + return null; | ||
| 164 | + } | ||
| 165 | + | ||
| 166 | + @Override | ||
| 167 | + public ChannelFuture connect(SocketAddress remoteAddress, ChannelPromise promise) { | ||
| 168 | + return null; | ||
| 169 | + } | ||
| 170 | + | ||
| 171 | + @Override | ||
| 172 | + public ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) { | ||
| 173 | + return null; | ||
| 174 | + } | ||
| 175 | + | ||
| 176 | + @Override | ||
| 177 | + public ChannelFuture disconnect(ChannelPromise promise) { | ||
| 178 | + return null; | ||
| 179 | + } | ||
| 180 | + | ||
| 181 | + @Override | ||
| 182 | + public ChannelFuture close(ChannelPromise promise) { | ||
| 183 | + return null; | ||
| 184 | + } | ||
| 185 | + | ||
| 186 | + @Override | ||
| 187 | + public ChannelFuture deregister(ChannelPromise promise) { | ||
| 188 | + return null; | ||
| 189 | + } | ||
| 190 | + | ||
| 191 | + @Override | ||
| 192 | + public Channel read() { | ||
| 193 | + return null; | ||
| 194 | + } | ||
| 195 | + | ||
| 196 | + @Override | ||
| 197 | + public ChannelFuture write(Object msg) { | ||
| 198 | + return null; | ||
| 199 | + } | ||
| 200 | + | ||
| 201 | + @Override | ||
| 202 | + public ChannelFuture write(Object msg, ChannelPromise promise) { | ||
| 203 | + return null; | ||
| 204 | + } | ||
| 205 | + | ||
| 206 | + @Override | ||
| 207 | + public Channel flush() { | ||
| 208 | + return null; | ||
| 209 | + } | ||
| 210 | + | ||
| 211 | + @Override | ||
| 212 | + public ChannelFuture writeAndFlush(Object msg, ChannelPromise promise) { | ||
| 213 | + return null; | ||
| 214 | + } | ||
| 215 | + | ||
| 216 | + @Override | ||
| 217 | + public ChannelFuture writeAndFlush(Object msg) { | ||
| 218 | + return null; | ||
| 219 | + } | ||
| 220 | + | ||
| 221 | + @Override | ||
| 222 | + public <T> Attribute<T> attr(AttributeKey<T> attributeKey) { | ||
| 223 | + return null; | ||
| 224 | + } | ||
| 225 | + | ||
| 226 | + @Override | ||
| 227 | + public int compareTo(Channel o) { | ||
| 228 | + return 0; | ||
| 229 | + } | ||
| 230 | +} |
protocols/lisp/ctl/src/test/java/org/onosproject/lisp/ctl/ChannelHandlerContextAdapter.java
0 → 100644
| 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.lisp.ctl; | ||
| 17 | + | ||
| 18 | + | ||
| 19 | +import io.netty.buffer.ByteBufAllocator; | ||
| 20 | +import io.netty.channel.Channel; | ||
| 21 | +import io.netty.channel.ChannelFuture; | ||
| 22 | +import io.netty.channel.ChannelHandler; | ||
| 23 | +import io.netty.channel.ChannelHandlerContext; | ||
| 24 | +import io.netty.channel.ChannelPromise; | ||
| 25 | +import io.netty.channel.ChannelPipeline; | ||
| 26 | +import io.netty.channel.ChannelProgressivePromise; | ||
| 27 | +import io.netty.util.Attribute; | ||
| 28 | +import io.netty.util.AttributeKey; | ||
| 29 | +import io.netty.util.concurrent.EventExecutor; | ||
| 30 | + | ||
| 31 | +import java.net.SocketAddress; | ||
| 32 | + | ||
| 33 | +/** | ||
| 34 | + * Adapter for testing against a netty channel handler context. | ||
| 35 | + */ | ||
| 36 | +public class ChannelHandlerContextAdapter implements ChannelHandlerContext { | ||
| 37 | + | ||
| 38 | + @Override | ||
| 39 | + public Channel channel() { | ||
| 40 | + return null; | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + @Override | ||
| 44 | + public EventExecutor executor() { | ||
| 45 | + return null; | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + @Override | ||
| 49 | + public String name() { | ||
| 50 | + return null; | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + @Override | ||
| 54 | + public ChannelHandler handler() { | ||
| 55 | + return null; | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + @Override | ||
| 59 | + public boolean isRemoved() { | ||
| 60 | + return false; | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + @Override | ||
| 64 | + public ChannelHandlerContext fireChannelRegistered() { | ||
| 65 | + return null; | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + @Override | ||
| 69 | + public ChannelHandlerContext fireChannelUnregistered() { | ||
| 70 | + return null; | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + @Override | ||
| 74 | + public ChannelHandlerContext fireChannelActive() { | ||
| 75 | + return null; | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + @Override | ||
| 79 | + public ChannelHandlerContext fireChannelInactive() { | ||
| 80 | + return null; | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + @Override | ||
| 84 | + public ChannelHandlerContext fireExceptionCaught(Throwable cause) { | ||
| 85 | + return null; | ||
| 86 | + } | ||
| 87 | + | ||
| 88 | + @Override | ||
| 89 | + public ChannelHandlerContext fireUserEventTriggered(Object evt) { | ||
| 90 | + return null; | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + @Override | ||
| 94 | + public ChannelHandlerContext fireChannelRead(Object msg) { | ||
| 95 | + return null; | ||
| 96 | + } | ||
| 97 | + | ||
| 98 | + @Override | ||
| 99 | + public ChannelHandlerContext fireChannelReadComplete() { | ||
| 100 | + return null; | ||
| 101 | + } | ||
| 102 | + | ||
| 103 | + @Override | ||
| 104 | + public ChannelHandlerContext fireChannelWritabilityChanged() { | ||
| 105 | + return null; | ||
| 106 | + } | ||
| 107 | + | ||
| 108 | + @Override | ||
| 109 | + public ChannelFuture bind(SocketAddress localAddress) { | ||
| 110 | + return null; | ||
| 111 | + } | ||
| 112 | + | ||
| 113 | + @Override | ||
| 114 | + public ChannelFuture connect(SocketAddress remoteAddress) { | ||
| 115 | + return null; | ||
| 116 | + } | ||
| 117 | + | ||
| 118 | + @Override | ||
| 119 | + public ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress) { | ||
| 120 | + return null; | ||
| 121 | + } | ||
| 122 | + | ||
| 123 | + @Override | ||
| 124 | + public ChannelFuture disconnect() { | ||
| 125 | + return null; | ||
| 126 | + } | ||
| 127 | + | ||
| 128 | + @Override | ||
| 129 | + public ChannelFuture close() { | ||
| 130 | + return null; | ||
| 131 | + } | ||
| 132 | + | ||
| 133 | + @Override | ||
| 134 | + public ChannelFuture deregister() { | ||
| 135 | + return null; | ||
| 136 | + } | ||
| 137 | + | ||
| 138 | + @Override | ||
| 139 | + public ChannelFuture bind(SocketAddress localAddress, ChannelPromise promise) { | ||
| 140 | + return null; | ||
| 141 | + } | ||
| 142 | + | ||
| 143 | + @Override | ||
| 144 | + public ChannelFuture connect(SocketAddress remoteAddress, ChannelPromise promise) { | ||
| 145 | + return null; | ||
| 146 | + } | ||
| 147 | + | ||
| 148 | + @Override | ||
| 149 | + public ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) { | ||
| 150 | + return null; | ||
| 151 | + } | ||
| 152 | + | ||
| 153 | + @Override | ||
| 154 | + public ChannelFuture disconnect(ChannelPromise promise) { | ||
| 155 | + return null; | ||
| 156 | + } | ||
| 157 | + | ||
| 158 | + @Override | ||
| 159 | + public ChannelFuture close(ChannelPromise promise) { | ||
| 160 | + return null; | ||
| 161 | + } | ||
| 162 | + | ||
| 163 | + @Override | ||
| 164 | + public ChannelFuture deregister(ChannelPromise promise) { | ||
| 165 | + return null; | ||
| 166 | + } | ||
| 167 | + | ||
| 168 | + @Override | ||
| 169 | + public ChannelHandlerContext read() { | ||
| 170 | + return null; | ||
| 171 | + } | ||
| 172 | + | ||
| 173 | + @Override | ||
| 174 | + public ChannelFuture write(Object msg) { | ||
| 175 | + return null; | ||
| 176 | + } | ||
| 177 | + | ||
| 178 | + @Override | ||
| 179 | + public ChannelFuture write(Object msg, ChannelPromise promise) { | ||
| 180 | + return null; | ||
| 181 | + } | ||
| 182 | + | ||
| 183 | + @Override | ||
| 184 | + public ChannelHandlerContext flush() { | ||
| 185 | + return null; | ||
| 186 | + } | ||
| 187 | + | ||
| 188 | + @Override | ||
| 189 | + public ChannelFuture writeAndFlush(Object msg, ChannelPromise promise) { | ||
| 190 | + return null; | ||
| 191 | + } | ||
| 192 | + | ||
| 193 | + @Override | ||
| 194 | + public ChannelFuture writeAndFlush(Object msg) { | ||
| 195 | + return null; | ||
| 196 | + } | ||
| 197 | + | ||
| 198 | + @Override | ||
| 199 | + public ChannelPipeline pipeline() { | ||
| 200 | + return null; | ||
| 201 | + } | ||
| 202 | + | ||
| 203 | + @Override | ||
| 204 | + public ByteBufAllocator alloc() { | ||
| 205 | + return null; | ||
| 206 | + } | ||
| 207 | + | ||
| 208 | + @Override | ||
| 209 | + public ChannelPromise newPromise() { | ||
| 210 | + return null; | ||
| 211 | + } | ||
| 212 | + | ||
| 213 | + @Override | ||
| 214 | + public ChannelProgressivePromise newProgressivePromise() { | ||
| 215 | + return null; | ||
| 216 | + } | ||
| 217 | + | ||
| 218 | + @Override | ||
| 219 | + public ChannelFuture newSucceededFuture() { | ||
| 220 | + return null; | ||
| 221 | + } | ||
| 222 | + | ||
| 223 | + @Override | ||
| 224 | + public ChannelFuture newFailedFuture(Throwable cause) { | ||
| 225 | + return null; | ||
| 226 | + } | ||
| 227 | + | ||
| 228 | + @Override | ||
| 229 | + public ChannelPromise voidPromise() { | ||
| 230 | + return null; | ||
| 231 | + } | ||
| 232 | + | ||
| 233 | + @Override | ||
| 234 | + public <T> Attribute<T> attr(AttributeKey<T> attributeKey) { | ||
| 235 | + return null; | ||
| 236 | + } | ||
| 237 | +} |
| 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.lisp.ctl; | ||
| 17 | + | ||
| 18 | +import com.google.common.collect.ImmutableList; | ||
| 19 | +import io.netty.buffer.ByteBuf; | ||
| 20 | +import io.netty.buffer.Unpooled; | ||
| 21 | +import org.junit.Test; | ||
| 22 | +import org.onosproject.lisp.msg.protocols.LispType; | ||
| 23 | + | ||
| 24 | +import java.nio.charset.StandardCharsets; | ||
| 25 | +import java.util.List; | ||
| 26 | + | ||
| 27 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
| 28 | +import static org.hamcrest.Matchers.is; | ||
| 29 | +import static org.hamcrest.Matchers.notNullValue; | ||
| 30 | + | ||
| 31 | +/** | ||
| 32 | + * Tests for LISP message encoder. | ||
| 33 | + */ | ||
| 34 | +public class LIspMessageEncoderTest { | ||
| 35 | + | ||
| 36 | + static class MockLispMessage extends LispMessageAdapter { | ||
| 37 | + LispType type; | ||
| 38 | + | ||
| 39 | + public MockLispMessage(LispType type) { | ||
| 40 | + super(type); | ||
| 41 | + this.type = type; | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + @Override | ||
| 45 | + public void writeTo(ByteBuf buffer) { | ||
| 46 | + String message = "LISP message [" + type.toString() + "] "; | ||
| 47 | + buffer.writeBytes(message.getBytes(StandardCharsets.UTF_8)); | ||
| 48 | + } | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + @Test | ||
| 52 | + public void testEncodeOneEntry() throws Exception { | ||
| 53 | + LispMessageEncoder encoder = new LispMessageEncoder(); | ||
| 54 | + MockLispMessage message = new MockLispMessage(LispType.LISP_MAP_REQUEST); | ||
| 55 | + ByteBuf buff = Unpooled.buffer(); | ||
| 56 | + encoder.encode(null, message, buff); | ||
| 57 | + | ||
| 58 | + assertThat(buff, notNullValue()); | ||
| 59 | + | ||
| 60 | + String expected = "LISP message [LISP_MAP_REQUEST] "; | ||
| 61 | + String returned = new String(buff.array(), StandardCharsets.UTF_8).substring(0, expected.length()); | ||
| 62 | + assertThat(returned, is(expected)); | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + @Test | ||
| 66 | + public void testEncode() throws Exception { | ||
| 67 | + LispMessageEncoder encoder = new LispMessageEncoder(); | ||
| 68 | + MockLispMessage request = new MockLispMessage(LispType.LISP_MAP_REQUEST); | ||
| 69 | + MockLispMessage reply = new MockLispMessage(LispType.LISP_MAP_REPLY); | ||
| 70 | + MockLispMessage register = new MockLispMessage(LispType.LISP_MAP_REGISTER); | ||
| 71 | + MockLispMessage notify = new MockLispMessage(LispType.LISP_MAP_NOTIFY); | ||
| 72 | + | ||
| 73 | + ByteBuf buff = Unpooled.buffer(); | ||
| 74 | + List<MockLispMessage> messages = ImmutableList.of(request, reply, register, notify); | ||
| 75 | + encoder.encode(null, messages, buff); | ||
| 76 | + | ||
| 77 | + assertThat(buff, notNullValue()); | ||
| 78 | + | ||
| 79 | + StringBuilder expBuilder = new StringBuilder(); | ||
| 80 | + expBuilder.append("LISP message [LISP_MAP_REQUEST] "); | ||
| 81 | + expBuilder.append("LISP message [LISP_MAP_REPLY] "); | ||
| 82 | + expBuilder.append("LISP message [LISP_MAP_REGISTER] "); | ||
| 83 | + expBuilder.append("LISP message [LISP_MAP_NOTIFY] "); | ||
| 84 | + | ||
| 85 | + String expected = expBuilder.toString(); | ||
| 86 | + String returned = new String(buff.array(), StandardCharsets.UTF_8).substring(0, expected.length()); | ||
| 87 | + assertThat(returned, is(expected)); | ||
| 88 | + } | ||
| 89 | +} |
| 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.lisp.ctl; | ||
| 17 | + | ||
| 18 | +import io.netty.buffer.ByteBuf; | ||
| 19 | +import org.onosproject.lisp.msg.protocols.LispMessage; | ||
| 20 | +import org.onosproject.lisp.msg.protocols.LispType; | ||
| 21 | + | ||
| 22 | +/** | ||
| 23 | + * Adapter for testing against a LISP message. | ||
| 24 | + */ | ||
| 25 | +public class LispMessageAdapter implements LispMessage { | ||
| 26 | + LispType type; | ||
| 27 | + | ||
| 28 | + private LispMessageAdapter() {} | ||
| 29 | + | ||
| 30 | + public LispMessageAdapter(LispType type) { | ||
| 31 | + this.type = type; | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + @Override | ||
| 35 | + public LispType getType() { | ||
| 36 | + return type; | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + @Override | ||
| 40 | + public void writeTo(ByteBuf byteBuf) { | ||
| 41 | + | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + @Override | ||
| 45 | + public Builder createBuilder() { | ||
| 46 | + return null; | ||
| 47 | + } | ||
| 48 | +} |
| 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.lisp.ctl; | ||
| 17 | + | ||
| 18 | +import com.google.common.collect.Lists; | ||
| 19 | +import io.netty.buffer.ByteBuf; | ||
| 20 | +import io.netty.buffer.Unpooled; | ||
| 21 | +import org.junit.Test; | ||
| 22 | +import org.onosproject.lisp.msg.protocols.LispMapNotify; | ||
| 23 | +import org.onosproject.lisp.msg.protocols.LispMapRegister; | ||
| 24 | +import org.onosproject.lisp.msg.protocols.LispMapReply; | ||
| 25 | +import org.onosproject.lisp.msg.protocols.LispMapRequest; | ||
| 26 | + | ||
| 27 | +import java.util.List; | ||
| 28 | + | ||
| 29 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
| 30 | +import static org.hamcrest.Matchers.instanceOf; | ||
| 31 | +import static org.hamcrest.Matchers.is; | ||
| 32 | + | ||
| 33 | +/** | ||
| 34 | + * Tests for LISP message decoder. | ||
| 35 | + */ | ||
| 36 | +public class LispMessageDecoderTest { | ||
| 37 | + | ||
| 38 | + private static final int TYPE_SHIFT_BIT = 4; | ||
| 39 | + private static final byte MAP_REQUEST = 1; | ||
| 40 | + private static final byte MAP_REPLY = 2; | ||
| 41 | + private static final byte MAP_REGISTER = 3; | ||
| 42 | + private static final byte MAP_NOTIFY = 4; | ||
| 43 | + | ||
| 44 | + | ||
| 45 | + private ByteBuf getLispMapRequestBuffer() { | ||
| 46 | + ByteBuf buffer = Unpooled.buffer(); | ||
| 47 | + | ||
| 48 | + // specify message type | ||
| 49 | + buffer.writeByte(MAP_REQUEST << TYPE_SHIFT_BIT); | ||
| 50 | + | ||
| 51 | + // fill up message payload | ||
| 52 | + // second byte denotes the number of RLOCs | ||
| 53 | + byte[] messageData = {0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; | ||
| 54 | + byte[] eidData = {0x0, 0x1, 0x0, 0x0, 0x0, 0x0}; | ||
| 55 | + byte[] rlocData = {0x0, 0x1, 0x0, 0x0, 0x0, 0x0}; | ||
| 56 | + buffer.writeBytes(messageData); | ||
| 57 | + buffer.writeBytes(eidData); | ||
| 58 | + buffer.writeBytes(rlocData); | ||
| 59 | + return buffer; | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + private ByteBuf getLispMapReplyBuffer() { | ||
| 63 | + ByteBuf buffer = Unpooled.buffer(); | ||
| 64 | + | ||
| 65 | + // specify message type | ||
| 66 | + buffer.writeByte(MAP_REPLY << TYPE_SHIFT_BIT); | ||
| 67 | + | ||
| 68 | + // fill up message payload | ||
| 69 | + byte[] messageData = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; | ||
| 70 | + buffer.writeBytes(messageData); | ||
| 71 | + return buffer; | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + private ByteBuf getLispMapRegisterBuffer() { | ||
| 75 | + ByteBuf buffer = Unpooled.buffer(); | ||
| 76 | + | ||
| 77 | + // specify message type | ||
| 78 | + buffer.writeByte(MAP_REGISTER << TYPE_SHIFT_BIT); | ||
| 79 | + | ||
| 80 | + // fill up message payload | ||
| 81 | + byte[] messageData = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; | ||
| 82 | + byte[] keyId = {0x0, 0x1}; | ||
| 83 | + | ||
| 84 | + // assume that we have auth data which has 2 bytes size | ||
| 85 | + byte[] authDataLength = {0x0, 0x2}; | ||
| 86 | + byte[] authData = {0x0, 0x0}; | ||
| 87 | + | ||
| 88 | + buffer.writeBytes(messageData); | ||
| 89 | + buffer.writeBytes(keyId); | ||
| 90 | + buffer.writeBytes(authDataLength); | ||
| 91 | + buffer.writeBytes(authData); | ||
| 92 | + return buffer; | ||
| 93 | + } | ||
| 94 | + | ||
| 95 | + private ByteBuf getLispMapNotifyBuffer() { | ||
| 96 | + ByteBuf buffer = Unpooled.buffer(); | ||
| 97 | + | ||
| 98 | + // specify message type | ||
| 99 | + buffer.writeByte(MAP_NOTIFY << TYPE_SHIFT_BIT); | ||
| 100 | + | ||
| 101 | + // fill up message payload | ||
| 102 | + byte[] messageData = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; | ||
| 103 | + byte[] keyId = {0x0, 0x1}; | ||
| 104 | + | ||
| 105 | + // assume that we have auth data which has 2 bytes size | ||
| 106 | + byte[] authDataLength = {0x0, 0x2}; | ||
| 107 | + byte[] authData = {0x0, 0x0}; | ||
| 108 | + | ||
| 109 | + buffer.writeBytes(messageData); | ||
| 110 | + buffer.writeBytes(keyId); | ||
| 111 | + buffer.writeBytes(authDataLength); | ||
| 112 | + buffer.writeBytes(authData); | ||
| 113 | + | ||
| 114 | + return buffer; | ||
| 115 | + } | ||
| 116 | + | ||
| 117 | + @Test(expected = IllegalArgumentException.class) | ||
| 118 | + public void testDecodeNoChannel() throws Exception { | ||
| 119 | + LispMessageDecoder decoder = new LispMessageDecoder(); | ||
| 120 | + | ||
| 121 | + List<Object> list = Lists.newArrayList(); | ||
| 122 | + decoder.decode(new ChannelHandlerContextAdapter(), Unpooled.buffer(), list); | ||
| 123 | + } | ||
| 124 | + | ||
| 125 | + @Test | ||
| 126 | + public void testDecode() throws Exception { | ||
| 127 | + LispMessageDecoder decoder = new LispMessageDecoder(); | ||
| 128 | + ByteBuf requestBuff = getLispMapRequestBuffer(); | ||
| 129 | + ByteBuf replyBuff = getLispMapReplyBuffer(); | ||
| 130 | + ByteBuf registerBuff = getLispMapRegisterBuffer(); | ||
| 131 | + ByteBuf notifyBuff = getLispMapNotifyBuffer(); | ||
| 132 | + | ||
| 133 | + List<Object> list = Lists.newArrayList(); | ||
| 134 | + decoder.decode(new ChannelHandlerContextAdapter(), requestBuff, list); | ||
| 135 | + decoder.decode(new ChannelHandlerContextAdapter(), replyBuff, list); | ||
| 136 | + decoder.decode(new ChannelHandlerContextAdapter(), registerBuff, list); | ||
| 137 | + decoder.decode(new ChannelHandlerContextAdapter(), notifyBuff, list); | ||
| 138 | + | ||
| 139 | + assertThat(list.size(), is(4)); | ||
| 140 | + assertThat(list.get(0), is(instanceOf(LispMapRequest.class))); | ||
| 141 | + assertThat(list.get(1), is(instanceOf(LispMapReply.class))); | ||
| 142 | + assertThat(list.get(2), is(instanceOf(LispMapRegister.class))); | ||
| 143 | + assertThat(list.get(3), is(instanceOf(LispMapNotify.class))); | ||
| 144 | + } | ||
| 145 | +} |
protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/LispMessageReaderFactory.java
0 → 100644
| 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.lisp.msg.protocols; | ||
| 17 | + | ||
| 18 | +import io.netty.buffer.ByteBuf; | ||
| 19 | + | ||
| 20 | +import static org.onosproject.lisp.msg.protocols.DefaultLispMapReply.ReplyReader; | ||
| 21 | +import static org.onosproject.lisp.msg.protocols.DefaultLispMapNotify.NotifyReader; | ||
| 22 | +import static org.onosproject.lisp.msg.protocols.DefaultLispMapRegister.RegisterReader; | ||
| 23 | +import static org.onosproject.lisp.msg.protocols.DefaultLispMapRequest.RequestReader; | ||
| 24 | + | ||
| 25 | +/** | ||
| 26 | + * A factory class which helps to instantiate LISP reader class. | ||
| 27 | + */ | ||
| 28 | +public final class LispMessageReaderFactory { | ||
| 29 | + private static final int TYPE_SHIFT_BIT = 4; | ||
| 30 | + | ||
| 31 | + private LispMessageReaderFactory() {} | ||
| 32 | + | ||
| 33 | + /** | ||
| 34 | + * Obtains corresponding LISP message reader. | ||
| 35 | + * | ||
| 36 | + * @param buffer netty byte buffer | ||
| 37 | + * @return LISP message reader | ||
| 38 | + */ | ||
| 39 | + public static LispMessageReader getReader(ByteBuf buffer) { | ||
| 40 | + LispMessageReader reader; | ||
| 41 | + | ||
| 42 | + int type = buffer.getByte(0) >> TYPE_SHIFT_BIT; | ||
| 43 | + switch (type) { | ||
| 44 | + case 1: | ||
| 45 | + reader = new RequestReader(); | ||
| 46 | + break; | ||
| 47 | + case 2: | ||
| 48 | + reader = new ReplyReader(); | ||
| 49 | + break; | ||
| 50 | + case 3: | ||
| 51 | + reader = new RegisterReader(); | ||
| 52 | + break; | ||
| 53 | + case 4: | ||
| 54 | + reader = new NotifyReader(); | ||
| 55 | + break; | ||
| 56 | + default: | ||
| 57 | + throw new IllegalArgumentException("Unknown LISP message type: " + type); | ||
| 58 | + } | ||
| 59 | + return reader; | ||
| 60 | + } | ||
| 61 | +} |
-
Please register or login to post a comment