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