Extensibility support (deserialization part)
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / deserialization / factories / GetAsyncReplyMessageFactoryTest.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.openflowjava.protocol.impl.deserialization.factories;
10
11 import io.netty.buffer.ByteBuf;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
20 import org.opendaylight.openflowjava.protocol.api.extensibility.MessageCodeKey;
21 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
22 import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializerRegistryImpl;
23 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
24 import org.opendaylight.openflowjava.protocol.impl.util.EncodeConstants;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowRemovedReason;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PacketInReason;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortReason;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.FlowRemovedMask;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.FlowRemovedMaskBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PacketInMask;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PacketInMaskBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PortStatusMask;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PortStatusMaskBuilder;
35
36 /**
37  * @author timotej.kubas
38  *
39  */
40 public class GetAsyncReplyMessageFactoryTest {
41
42     private OFDeserializer<GetAsyncOutput> asyncFactory;
43
44     /**
45      * Initializes deserializer registry and lookups correct deserializer
46      */
47     @Before
48     public void startUp() {
49         DeserializerRegistry registry = new DeserializerRegistryImpl();
50         registry.init();
51         asyncFactory = registry.getDeserializer(
52                 new MessageCodeKey(EncodeConstants.OF13_VERSION_ID, 27, GetAsyncOutput.class));
53     }
54
55     /**
56      * Testing {@link GetAsyncReplyMessageFactory} for correct translation into POJO
57      */
58     @Test
59     public void testGetAsyncReplyMessage() {
60         ByteBuf bb = BufferHelper.buildBuffer("00 00 00 06 "+ 
61                                               "00 00 00 05 "+
62                                               "00 00 00 07 "+
63                                               "00 00 00 00 "+
64                                               "00 00 00 03 "+
65                                               "00 00 00 0A");
66         GetAsyncOutput builtByFactory = BufferHelper.deserialize(asyncFactory, bb);
67
68         BufferHelper.checkHeaderV13(builtByFactory);
69         Assert.assertEquals("Wrong packetInMask",createPacketInMask(), 
70                                                  builtByFactory.getPacketInMask());
71         Assert.assertEquals("Wrong portStatusMask",createPortStatusMask(), 
72                                                    builtByFactory.getPortStatusMask());
73         Assert.assertEquals("Wrong flowRemovedMask",createFlowRemovedMask(), 
74                                                     builtByFactory.getFlowRemovedMask());
75     }
76     
77     private static List<PacketInMask> createPacketInMask() {
78         List<PacketInMask> inMasks = new ArrayList<>();
79         PacketInMaskBuilder maskBuilder;
80         // OFPCR_ROLE_EQUAL or OFPCR_ROLE_MASTER
81         maskBuilder = new PacketInMaskBuilder();
82         List<PacketInReason> reasons = new ArrayList<>();
83         reasons.add(PacketInReason.OFPRACTION);
84         reasons.add(PacketInReason.OFPRINVALIDTTL);
85         maskBuilder.setMask(reasons);
86         inMasks.add(maskBuilder.build());
87         // OFPCR_ROLE_SLAVE
88         maskBuilder = new PacketInMaskBuilder();
89         reasons = new ArrayList<>();
90         reasons.add(PacketInReason.OFPRNOMATCH);
91         reasons.add(PacketInReason.OFPRINVALIDTTL);
92         maskBuilder.setMask(reasons);
93         inMasks.add(maskBuilder.build());
94         return inMasks;
95     }
96     
97     private static List<PortStatusMask> createPortStatusMask() {
98         List<PortStatusMask> inMasks = new ArrayList<>();
99         PortStatusMaskBuilder maskBuilder;
100         // OFPCR_ROLE_EQUAL or OFPCR_ROLE_MASTER
101         maskBuilder = new PortStatusMaskBuilder();
102         List<PortReason> reasons = new ArrayList<>();
103         reasons.add(PortReason.OFPPRADD);
104         reasons.add(PortReason.OFPPRDELETE);
105         reasons.add(PortReason.OFPPRMODIFY);
106         inMasks.add(maskBuilder.setMask(reasons).build());
107         // OFPCR_ROLE_SLAVE
108         maskBuilder = new PortStatusMaskBuilder();
109         reasons = new ArrayList<>();
110         maskBuilder.setMask(reasons);
111         inMasks.add(maskBuilder.build());
112         return inMasks;
113     }
114     
115     private static List<FlowRemovedMask> createFlowRemovedMask() {
116         List<FlowRemovedMask> inMasks = new ArrayList<>();
117         FlowRemovedMaskBuilder maskBuilder;
118         // OFPCR_ROLE_EQUAL or OFPCR_ROLE_MASTER
119         maskBuilder = new FlowRemovedMaskBuilder();
120         List<FlowRemovedReason> reasons = new ArrayList<>();
121         reasons.add(FlowRemovedReason.OFPRRIDLETIMEOUT);
122         reasons.add(FlowRemovedReason.OFPRRHARDTIMEOUT);
123         maskBuilder.setMask(reasons);
124         inMasks.add(maskBuilder.build());
125         // OFPCR_ROLE_SLAVE
126         maskBuilder = new FlowRemovedMaskBuilder();
127         reasons = new ArrayList<>();
128         reasons.add(FlowRemovedReason.OFPRRHARDTIMEOUT);
129         reasons.add(FlowRemovedReason.OFPRRGROUPDELETE);
130         maskBuilder.setMask(reasons);
131         inMasks.add(maskBuilder.build());
132         return inMasks;
133     }
134 }