InstructionDeserializer split into separate deserializers
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / util / MatchDeserializer.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.util;
10
11 import io.netty.buffer.ByteBuf;
12
13 import java.util.List;
14
15 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
16 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistryInjector;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.StandardMatchType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OxmMatchType;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.grouping.Match;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.grouping.MatchBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;
23
24 /**
25  * Deserializes ofp_match (OpenFlow v1.3) and its oxm_fields structures
26  * @author timotej.kubas
27  * @author michal.polkorab
28  */
29 public class MatchDeserializer implements OFDeserializer<Match>,
30         DeserializerRegistryInjector {
31
32     private DeserializerRegistry registry;
33
34     @Override
35     public Match deserialize(ByteBuf input) {
36         if (input.readableBytes() > 0) {
37             MatchBuilder builder = new MatchBuilder();
38             int type = input.readUnsignedShort();
39             int length = input.readUnsignedShort();
40             switch (type) {
41             case 0:
42                 builder.setType(StandardMatchType.class);
43                 break;
44             case 1:
45                 builder.setType(OxmMatchType.class);
46                 break;
47             default:
48                 break;
49             }
50             CodeKeyMaker keyMaker = CodeKeyMakerFactory
51                     .createMatchEntriesKeyMaker(EncodeConstants.OF13_VERSION_ID);
52             List<MatchEntries> entries = ListDeserializer.deserializeList(EncodeConstants.OF13_VERSION_ID,
53                     length - 2 * EncodeConstants.SIZE_OF_SHORT_IN_BYTES, input, keyMaker, registry);
54             builder.setMatchEntries(entries);
55             int paddingRemainder = length % EncodeConstants.PADDING;
56             if (paddingRemainder != 0) {
57                 input.skipBytes(EncodeConstants.PADDING - paddingRemainder);
58             }
59             return builder.build();
60         }
61         return null;
62     }
63
64     @Override
65     public void injectDeserializerRegistry(DeserializerRegistry deserializerRegistry) {
66         this.registry = deserializerRegistry;
67     }
68 }