Merge "OPNFLWPLUG-972: Point to openflowplugin liblldp"
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / util / CodeKeyMakerFactory.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 import org.opendaylight.openflowjava.protocol.api.keys.ActionDeserializerKey;
13 import org.opendaylight.openflowjava.protocol.api.keys.ExperimenterActionDeserializerKey;
14 import org.opendaylight.openflowjava.protocol.api.keys.ExperimenterInstructionDeserializerKey;
15 import org.opendaylight.openflowjava.protocol.api.keys.InstructionDeserializerKey;
16 import org.opendaylight.openflowjava.protocol.api.keys.MatchEntryDeserializerKey;
17 import org.opendaylight.openflowjava.protocol.api.keys.MessageCodeKey;
18 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
19
20 /**
21  * Factory for creating CodeKeyMaker instances.
22  *
23  * @author michal.polkorab
24  */
25 public final class CodeKeyMakerFactory {
26
27     private CodeKeyMakerFactory() {
28         //not called
29     }
30
31     public static CodeKeyMaker createMatchEntriesKeyMaker(short version) {
32         return new AbstractCodeKeyMaker(version) {
33             @Override
34             public MessageCodeKey make(ByteBuf input) {
35                 int oxmClass = input.getUnsignedShort(input.readerIndex());
36                 int oxmField = input.getUnsignedByte(input.readerIndex()
37                         + EncodeConstants.SIZE_OF_SHORT_IN_BYTES) >>> 1;
38                 MatchEntryDeserializerKey key = new MatchEntryDeserializerKey(getVersion(),
39                         oxmClass, oxmField);
40                 if (oxmClass == EncodeConstants.EXPERIMENTER_VALUE) {
41                     long expId = input.getUnsignedInt(input.readerIndex() + EncodeConstants.SIZE_OF_SHORT_IN_BYTES
42                             + 2 * EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
43                     key.setExperimenterId(expId);
44                     return key;
45                 }
46                 key.setExperimenterId(null);
47                 return key;
48             }
49         };
50     }
51
52     public static CodeKeyMaker createActionsKeyMaker(short version) {
53         return new AbstractCodeKeyMaker(version) {
54             @Override
55             public MessageCodeKey make(ByteBuf input) {
56                 int type = input.getUnsignedShort(input.readerIndex());
57                 if (type == EncodeConstants.EXPERIMENTER_VALUE) {
58                     Long expId = input.getUnsignedInt(input.readerIndex()
59                             + 2 * EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
60                     return new ExperimenterActionDeserializerKey(getVersion(), expId);
61                 }
62                 ActionDeserializerKey actionDeserializerKey = new ActionDeserializerKey(getVersion(), type, null);
63                 return actionDeserializerKey;
64             }
65         };
66     }
67
68     public static CodeKeyMaker createInstructionsKeyMaker(short version) {
69         return new AbstractCodeKeyMaker(version) {
70             @Override
71             public MessageCodeKey make(ByteBuf input) {
72                 int type = input.getUnsignedShort(input.readerIndex());
73                 if (type == EncodeConstants.EXPERIMENTER_VALUE) {
74                     Long expId = input.getUnsignedInt(input.readerIndex()
75                             + 2 * EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
76                     return new ExperimenterInstructionDeserializerKey(getVersion(), expId);
77                 }
78                 return new InstructionDeserializerKey(getVersion(), type, null);
79             }
80         };
81     }
82 }