937f7d8c1f54460937f051cb3e78b2dbe920d1ae
[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.api.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 07 "+ 
61                                               "00 00 00 00 "+
62                                               "00 00 00 07 "+
63                                               "00 00 00 00 "+
64                                               "00 00 00 0F "+
65                                               "00 00 00 00");
66         GetAsyncOutput builtByFactory = BufferHelper.deserialize(asyncFactory, bb);
67
68         BufferHelper.checkHeaderV13(builtByFactory);
69         Assert.assertEquals("Wrong packetInMask",createPacketInMask(), builtByFactory.getPacketInMask());
70         Assert.assertEquals("Wrong portStatusMask",createPortStatusMask(), builtByFactory.getPortStatusMask());
71         Assert.assertEquals("Wrong flowRemovedMask",createFlowRemovedMask(), builtByFactory.getFlowRemovedMask());
72     }
73
74     private static List<PacketInMask> createPacketInMask() {
75         List<PacketInMask> inMasks = new ArrayList<>();
76         PacketInMaskBuilder maskBuilder;
77         // OFPCR_ROLE_EQUAL or OFPCR_ROLE_MASTER
78         maskBuilder = new PacketInMaskBuilder();
79         List<PacketInReason> reasons = new ArrayList<>();
80         reasons.add(PacketInReason.OFPRNOMATCH);
81         reasons.add(PacketInReason.OFPRACTION);
82         reasons.add(PacketInReason.OFPRINVALIDTTL);
83         maskBuilder.setMask(reasons);
84         inMasks.add(maskBuilder.build());
85         // OFPCR_ROLE_SLAVE
86         maskBuilder = new PacketInMaskBuilder();
87         reasons = new ArrayList<>();
88         maskBuilder.setMask(reasons);
89         inMasks.add(maskBuilder.build());
90         return inMasks;
91     }
92
93     private static List<PortStatusMask> createPortStatusMask() {
94         List<PortStatusMask> inMasks = new ArrayList<>();
95         PortStatusMaskBuilder maskBuilder;
96         // OFPCR_ROLE_EQUAL or OFPCR_ROLE_MASTER
97         maskBuilder = new PortStatusMaskBuilder();
98         List<PortReason> reasons = new ArrayList<>();
99         reasons.add(PortReason.OFPPRADD);
100         reasons.add(PortReason.OFPPRDELETE);
101         reasons.add(PortReason.OFPPRMODIFY);
102         inMasks.add(maskBuilder.setMask(reasons).build());
103         // OFPCR_ROLE_SLAVE
104         maskBuilder = new PortStatusMaskBuilder();
105         reasons = new ArrayList<>();
106         maskBuilder.setMask(reasons);
107         inMasks.add(maskBuilder.build());
108         return inMasks;
109     }
110
111     private static List<FlowRemovedMask> createFlowRemovedMask() {
112         List<FlowRemovedMask> inMasks = new ArrayList<>();
113         FlowRemovedMaskBuilder maskBuilder;
114         // OFPCR_ROLE_EQUAL or OFPCR_ROLE_MASTER
115         maskBuilder = new FlowRemovedMaskBuilder();
116         List<FlowRemovedReason> reasons = new ArrayList<>();
117         reasons.add(FlowRemovedReason.OFPRRIDLETIMEOUT);
118         reasons.add(FlowRemovedReason.OFPRRHARDTIMEOUT);
119         reasons.add(FlowRemovedReason.OFPRRDELETE);
120         reasons.add(FlowRemovedReason.OFPRRGROUPDELETE);
121         maskBuilder.setMask(reasons);
122         inMasks.add(maskBuilder.build());
123         // OFPCR_ROLE_SLAVE
124         maskBuilder = new FlowRemovedMaskBuilder();
125         reasons = new ArrayList<>();
126         maskBuilder.setMask(reasons);
127         inMasks.add(maskBuilder.build());
128         return inMasks;
129     }
130 }