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