2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
\r
4 * This program and the accompanying materials are made available under the
\r
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
\r
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
\r
9 package org.opendaylight.openflowplugin.openflow.md.core.translator;
\r
11 import io.netty.channel.ChannelFuture;
\r
12 import io.netty.channel.EventLoopGroup;
\r
13 import io.netty.channel.nio.NioEventLoopGroup;
\r
14 import io.netty.channel.socket.nio.NioSocketChannel;
\r
16 import java.io.IOException;
\r
17 import java.math.BigInteger;
\r
18 import java.net.InetSocketAddress;
\r
19 import java.util.ArrayList;
\r
20 import java.util.Collection;
\r
21 import java.util.HashMap;
\r
22 import java.util.List;
\r
23 import java.util.Map;
\r
25 import org.junit.Assert;
\r
26 import org.junit.Test;
\r
27 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
\r
28 import org.opendaylight.openflowjava.protocol.impl.connection.ConnectionAdapterImpl;
\r
29 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
\r
30 import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
\r
31 import org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductorImpl;
\r
32 import org.opendaylight.openflowplugin.openflow.md.core.IMDMessageTranslator;
\r
33 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
\r
34 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContextOFImpl;
\r
35 import org.opendaylight.openflowplugin.openflow.md.core.session.SwitchConnectionCookieOFImpl;
\r
36 import org.opendaylight.openflowplugin.openflow.md.queue.QueueProcessorLightImpl;
\r
37 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
\r
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PacketInReason;
\r
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
\r
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
\r
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
\r
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
\r
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessageBuilder;
\r
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInput;
\r
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInputBuilder;
\r
46 import org.opendaylight.yangtools.yang.binding.DataObject;
\r
47 import org.slf4j.Logger;
\r
48 import org.slf4j.LoggerFactory;
\r
51 * Created by Jakub Toth jatoth@cisco.com on 3/10/14.
\r
54 public class PacketInV10TranslatorTest{
\r
55 private static final Logger log = LoggerFactory
\r
56 .getLogger(PacketInV10TranslatorTest.class);
\r
60 * {@link PacketInV10Translator#translate(SwitchConnectionDistinguisher, SessionContext, OfHeader)}
\r
61 * - all parameteres are null - translates packetIn from OF-API model to
\r
62 * MD-SAL model, supports OF-1.0
\r
65 public void testTranslateWithAllNullParam(){
\r
66 SwitchConnectionDistinguisher cookie = null;
\r
67 SessionContext sessionContext = null;
\r
68 OfHeader msg = null;
\r
70 PacketInV10Translator packetInV10Translator = new PacketInV10Translator();
\r
72 List<DataObject> salPacketIn = packetInV10Translator.translate(cookie,
\r
73 sessionContext, msg);
\r
75 Assert.assertEquals(true, salPacketIn.isEmpty());
\r
77 log.info("Test with all null parameters done.");
\r
82 * {@link PacketInV10Translator#translate(SwitchConnectionDistinguisher, SessionContext, OfHeader)}
\r
83 * - DPID is null- translates packetIn from OF-API model to MD-SAL model,
\r
87 public void testTranslateDPIDNull(){
\r
88 SessionContextOFImpl sessionContextOFImpl = new SessionContextOFImpl();
\r
90 SwitchConnectionDistinguisher cookie = this.settingCookie();
\r
92 byte[] data = this.messageData();
\r
93 PacketInMessage message = this.createPacketInMessage(data, null);
\r
95 PacketInV10Translator packetInV10Translator = new PacketInV10Translator();
\r
97 List<DataObject> salPacketIn = packetInV10Translator.translate(cookie,
\r
98 sessionContextOFImpl, message);
\r
100 Assert.assertEquals(true, salPacketIn.isEmpty());
\r
101 log.info("Test with null DPID parameter done.");
\r
106 * {@link PacketInV10Translator#translate(SwitchConnectionDistinguisher, SessionContext, OfHeader)}
\r
107 * - inPort is null- translates packetIn from OF-API model to MD-SAL model,
\r
111 public void testTranslateInPortNull(){
\r
112 BigInteger datapathId = this.dataPathId();
\r
114 GetFeaturesOutputBuilder featuresBuilder = new GetFeaturesOutputBuilder();
\r
115 featuresBuilder.setDatapathId(datapathId);
\r
117 SessionContextOFImpl sessionContextOFImpl = new SessionContextOFImpl();
\r
118 sessionContextOFImpl.setFeatures(featuresBuilder.build());
\r
120 SwitchConnectionDistinguisher cookie = this.settingCookie();
\r
122 byte[] data = this.messageData();
\r
124 PacketInMessage message = this.createPacketInMessage(data, null);
\r
126 PacketInV10Translator packetInV10Translator = new PacketInV10Translator();
\r
128 List<DataObject> salPacketIn = packetInV10Translator.translate(cookie,
\r
129 sessionContextOFImpl, message);
\r
131 Assert.assertEquals(true, salPacketIn.isEmpty());
\r
133 log.info("Test with null inPort parameter done.");
\r
138 * {@link PacketInV10Translator#translate(SwitchConnectionDistinguisher, SessionContext, OfHeader)}
\r
139 * - translates packetIn from OF-API model to MD-SAL model, supports OF-1.0
\r
141 * @throws IOException
\r
144 public void testTranslate(){
\r
145 BigInteger datapathId = this.dataPathId();
\r
147 byte[] data = this.messageData();
\r
149 PacketInMessage message = this.createPacketInMessage(data, 5);
\r
151 GetFeaturesOutput featuresOutput = this
\r
152 .createGetFeatureOutput(datapathId);
\r
154 NioSocketChannel nioSocketChannel = new NioSocketChannel();
\r
155 this.registerChannel(nioSocketChannel);
\r
157 SetConfigInput setConfigInput = this.createSetConfigInput();
\r
159 ConnectionAdapterImpl connectionAdapter = new ConnectionAdapterImpl(
\r
160 nioSocketChannel, new InetSocketAddress(51));
\r
161 connectionAdapter.setConfig(setConfigInput);
\r
163 ConnectionConductorImpl connectionConductor = new ConnectionConductorImpl(
\r
164 connectionAdapter);
\r
166 this.initConnectionConductor(connectionConductor, featuresOutput);
\r
168 SessionContextOFImpl sessionContextOFImpl = new SessionContextOFImpl();
\r
169 sessionContextOFImpl.setFeatures(featuresOutput);
\r
170 sessionContextOFImpl.setPrimaryConductor(connectionConductor);
\r
172 SwitchConnectionDistinguisher cookie = this.settingCookie();
\r
174 PacketInV10Translator packetInV10Translator = new PacketInV10Translator();
\r
176 OpenflowPortsUtil.init();
\r
178 List<DataObject> salPacketIn = packetInV10Translator.translate(cookie,
\r
179 sessionContextOFImpl, message);
\r
181 String expectedString = "[PacketReceived [_ingress=NodeConnectorRef [_value=KeyedInstanceIdentifier"
\r
182 + "{targetType=interface org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector,"
\r
183 + " path=[org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes,"
\r
184 + " org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node["
\r
185 + "key=NodeKey [_id=Uri [_value=openflow:"
\r
186 + datapathId.toString()
\r
188 + " org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector["
\r
189 + "key=NodeConnectorKey [_id=Uri [_value=openflow:"
\r
190 + datapathId.toString()
\r
192 + message.getInPort().toString()
\r
193 + "]]]]}], _packetInReason=class org.opendaylight.yang.gen.v1.urn.opendaylight."
\r
194 + "packet.service.rev130709.SendToController, _payload=[115, 101, 110, 100, 79, 117,"
\r
195 + " 116, 112, 117, 116, 77, 115, 103, 95, 84, 69, 83, 84], augmentation=[]]]";
\r
196 Assert.assertEquals(expectedString, salPacketIn.toString());
\r
198 log.info("Test translate done.");
\r
202 * create datapathID
\r
204 * @return BigInteger
\r
206 private BigInteger dataPathId(){
\r
207 byte[] datapathIdByte = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
\r
208 for(int i = 0; i < datapathIdByte.length; i++){
\r
209 datapathIdByte[i] = 1;
\r
211 return new BigInteger(1, datapathIdByte);
\r
215 * generate message from string to byte[]
\r
219 private byte[] messageData(){
\r
220 String string = new String("sendOutputMsg_TEST");
\r
221 return string.getBytes();
\r
225 * create PacketInMessage with setting Version, InPort, Data, Reason
\r
229 * @return PacketInMessage
\r
231 private PacketInMessage createPacketInMessage(final byte[] data,
\r
232 final java.lang.Integer port){
\r
233 PacketInReason reason = PacketInReason.OFPRACTION;
\r
234 return new PacketInMessageBuilder()
\r
235 .setVersion((short) EncodeConstants.OF10_VERSION_ID)
\r
236 .setInPort(port).setData(data).setReason(reason).build();
\r
243 * @return SwitchConnectionDistinguisher
\r
245 private SwitchConnectionDistinguisher settingCookie(){
\r
246 SwitchConnectionCookieOFImpl key = new SwitchConnectionCookieOFImpl();
\r
247 key.setAuxiliaryId((short) 1);
\r
253 * create GetFeatureOutput
\r
255 * @param datapathId
\r
256 * @return GetFeaturesOutput
\r
258 private GetFeaturesOutput createGetFeatureOutput(final BigInteger datapathId){
\r
259 return new GetFeaturesOutputBuilder().setDatapathId(datapathId)
\r
260 .setVersion((short) 0x01).build();
\r
264 * register eventLop
\r
268 private void registerChannel(final NioSocketChannel channel){
\r
269 EventLoopGroup eventLop = new NioEventLoopGroup();
\r
270 ChannelFuture regFuture = eventLop.register(channel);
\r
274 * build SetConfigInput with setting Version and Xid
\r
276 * @return SetConfigInput
\r
278 private SetConfigInput createSetConfigInput(){
\r
279 return new SetConfigInputBuilder().setVersion((short) 0x01)
\r
280 .setXid(0xfffffL).build();
\r
284 * init connectionConductor
\r
286 * @param connectionConductor
\r
287 * @param featuresOutput
\r
289 private void initConnectionConductor(
\r
290 final ConnectionConductorImpl connectionConductor,
\r
291 final GetFeaturesOutput featuresOutput){
\r
292 TranslatorKey paramK = new TranslatorKey(1, "PacketInMessage.class");
\r
293 Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> coll = new ArrayList<>();
\r
294 coll.add(new PacketInV10Translator());
\r
295 Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> translatorMapping = new HashMap<>();
\r
296 translatorMapping.put(paramK, coll);
\r
298 QueueProcessorLightImpl queueProcessor = new QueueProcessorLightImpl();
\r
299 queueProcessor.setTranslatorMapping(translatorMapping);
\r
300 queueProcessor.init();
\r
301 connectionConductor.setQueueProcessor(queueProcessor);
\r
302 connectionConductor.init();
\r
303 connectionConductor
\r
304 .onHandshakeSuccessfull(featuresOutput, (short) 0x01);
\r