Bug 5540 - PortConvertor, MatchConvertor
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / PortConvertorTest.java
1 /**
2  * Copyright (c) 2014 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;
9
10 import java.util.Optional;
11 import org.junit.Assert;
12 import org.junit.Test;
13 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
15 import org.opendaylight.openflowplugin.api.OFConstants;
16 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionConvertorData;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortConfig;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortNumberUni;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.flow.capable.port.State;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.flow.capable.port.StateBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.port.mod.port.PortBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfigV10;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeaturesV10;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortState;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.desc._case.multipart.reply.port.desc.Ports;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.desc._case.multipart.reply.port.desc.PortsBuilder;
31
32 public class PortConvertorTest {
33
34     /** defautl mac address */
35     private static final String DEFAULT_MAC_ADDRESS = "01:02:03:04:05:06";
36
37     private PortFeatures features = new PortFeatures(true, null, null, null, null, null, null, null, null, null, null,
38             null, null, null, null, null);
39
40     private PortConfig config = new PortConfig(false, false, false, false);
41
42     private org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeatures portf31=
43             new org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeatures(
44                     null, null, null, null, null, null, null, null, null, null, true, null, null, null, null, null);
45
46     private org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfig config31 =
47             new org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfig(
48                     false, false, false, false);
49
50     /**
51      * test of {@link org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.PortConvertor#convert(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.port.mod.port.Port, org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionConvertorData)} }
52      */
53     @Test
54     public void testtoPortModInputwithAllParameters()
55     {
56
57         PortBuilder portBld = new PortBuilder();
58         portBld.setAdvertisedFeatures(features);
59         portBld.setConfiguration(config);
60         portBld.setPortNumber(new PortNumberUni(42L));
61         portBld.setHardwareAddress(new MacAddress(DEFAULT_MAC_ADDRESS));
62
63         VersionConvertorData data = new VersionConvertorData(OFConstants.OFP_VERSION_1_3);
64         Optional<PortModInput> portOutOptional = ConvertorManager.getInstance().convert(portBld.build(), data);
65         PortModInput portOut = portOutOptional.orElse(PortConvertor.defaultResult(OFConstants.OFP_VERSION_1_3));
66
67         PortConfigV10 portConfV10 = new PortConfigV10(false, false, false, false, true, true, false);
68
69         PortModInputBuilder portModInputBld = new PortModInputBuilder();
70
71         portModInputBld.setConfig(config31);
72         portModInputBld.setMask(config31);
73         portModInputBld.setPortNo(
74                 new org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber(42L));
75         portModInputBld.setHwAddress(new MacAddress(DEFAULT_MAC_ADDRESS));
76         portModInputBld.setAdvertise(portf31);
77
78         portModInputBld.setConfigV10(portConfV10);
79         portModInputBld.setMaskV10(portConfV10);
80         portModInputBld.setAdvertiseV10(
81                 new PortFeaturesV10(null, null, null, null, null, null, null, true, null, null, null, null));
82
83         portModInputBld.setVersion((short) EncodeConstants.OF13_VERSION_ID);
84
85         Assert.assertEquals(portModInputBld.build(), portOut);
86     }
87
88     /**
89      * test of {@link PortConvertor#toPortDesc(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.FlowCapablePort , short)}
90      */
91     @Test
92     public void testtoPortDescwithAllParameters(){
93
94         State state = new StateBuilder().setBlocked(false).setLinkDown(false).setLive(false).build();
95         PortState state31= new PortState(false, false, false);
96
97         FlowCapableNodeConnectorBuilder flowCapableNodeConnectorBuilder = new FlowCapableNodeConnectorBuilder();
98
99         flowCapableNodeConnectorBuilder.setAdvertisedFeatures(features);
100         flowCapableNodeConnectorBuilder.setConfiguration(config);
101         flowCapableNodeConnectorBuilder.setCurrentFeature(features);
102         flowCapableNodeConnectorBuilder.setCurrentSpeed(null);
103         flowCapableNodeConnectorBuilder.setHardwareAddress(new MacAddress(DEFAULT_MAC_ADDRESS));
104         flowCapableNodeConnectorBuilder.setMaximumSpeed(null);
105         flowCapableNodeConnectorBuilder.setName("foo");
106         flowCapableNodeConnectorBuilder.setPeerFeatures(features);
107         flowCapableNodeConnectorBuilder.setPortNumber(new PortNumberUni(42L));
108         flowCapableNodeConnectorBuilder.setState(state);
109         flowCapableNodeConnectorBuilder.setSupported(features);
110
111         Ports portsOut = PortConvertor.toPortDesc(flowCapableNodeConnectorBuilder.build(), EncodeConstants.OF13_VERSION_ID);
112
113         PortsBuilder portsB = new PortsBuilder();
114
115         portsB.setAdvertisedFeatures(portf31);
116         portsB.setConfig(config31);
117         portsB.setCurrentFeatures(portf31);
118         portsB.setCurrSpeed(null);
119         portsB.setHwAddr(new MacAddress(DEFAULT_MAC_ADDRESS));
120         portsB.setMaxSpeed(null);
121         portsB.setName("foo");
122         portsB.setPeerFeatures(portf31);
123         portsB.setPortNo(42L);
124         portsB.setState(state31);
125         portsB.setSupportedFeatures(portf31);
126
127         Assert.assertEquals(portsB.build(), portsOut);
128     }
129
130 }