9b21226b629e042ac18f99993796907997dc7aef
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / util / CodeKeyMakerFactory.java
1 /*\r
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 \r
9 package org.opendaylight.openflowjava.protocol.impl.util;\r
10 \r
11 import io.netty.buffer.ByteBuf;\r
12 \r
13 import org.opendaylight.openflowjava.protocol.api.extensibility.MessageCodeKey;\r
14 import org.opendaylight.openflowjava.protocol.api.keys.ActionDeserializerKey;\r
15 import org.opendaylight.openflowjava.protocol.api.keys.InstructionDeserializerKey;\r
16 import org.opendaylight.openflowjava.protocol.api.keys.MatchEntryDeserializerKey;\r
17 import org.opendaylight.openflowjava.protocol.api.keys.experimenter.ExperimenterActionDeserializerKey;\r
18 import org.opendaylight.openflowjava.protocol.api.keys.experimenter.ExperimenterInstructionDeserializerKey;\r
19 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;\r
20 \r
21 /**\r
22  * @author michal.polkorab\r
23  *\r
24  */\r
25 public abstract class CodeKeyMakerFactory {\r
26 \r
27     /**\r
28      * @param version\r
29      * @return\r
30      */\r
31     public static CodeKeyMaker createMatchEntriesKeyMaker(short version) {\r
32         return new AbstractCodeKeyMaker(version) {\r
33             @Override\r
34             public MessageCodeKey make(ByteBuf input) {\r
35                 int oxmClass = input.getUnsignedShort(input.readerIndex());\r
36                 int oxmField = input.getUnsignedByte(input.readerIndex()\r
37                         + EncodeConstants.SIZE_OF_SHORT_IN_BYTES) >>> 1;\r
38                 MatchEntryDeserializerKey key = new MatchEntryDeserializerKey(getVersion(),\r
39                         oxmClass, oxmField);\r
40                 if (oxmClass == EncodeConstants.EXPERIMENTER_VALUE) {\r
41                     long expId = input.getUnsignedInt(input.readerIndex() + EncodeConstants.SIZE_OF_SHORT_IN_BYTES\r
42                             + 2 * EncodeConstants.SIZE_OF_BYTE_IN_BYTES);\r
43                     key.setExperimenterId(expId);\r
44                     return key;\r
45                 }\r
46                 key.setExperimenterId(null);\r
47                 return key;\r
48             }\r
49         };\r
50     }\r
51 \r
52     /**\r
53      * @param version\r
54      * @return\r
55      */\r
56     public static CodeKeyMaker createActionsKeyMaker(short version) {\r
57         return new AbstractCodeKeyMaker(version) {\r
58             @Override\r
59             public MessageCodeKey make(ByteBuf input) {\r
60                 int type = input.getUnsignedShort(input.readerIndex());\r
61                 if (type == EncodeConstants.EXPERIMENTER_VALUE) {\r
62                     Long expId = input.getUnsignedInt(input.readerIndex()\r
63                             + 2 * EncodeConstants.SIZE_OF_SHORT_IN_BYTES);\r
64                     return new ExperimenterActionDeserializerKey(getVersion(), expId);\r
65                 }\r
66                 ActionDeserializerKey actionDeserializerKey = new ActionDeserializerKey(getVersion(), type, null);\r
67                 return actionDeserializerKey;\r
68             }\r
69         };\r
70     }\r
71 \r
72     /**\r
73      * @param version\r
74      * @return\r
75      */\r
76     public static CodeKeyMaker createInstructionsKeyMaker(short version) {\r
77         return new AbstractCodeKeyMaker(version) {\r
78             @Override\r
79             public MessageCodeKey make(ByteBuf input) {\r
80                 int type = input.getUnsignedShort(input.readerIndex());\r
81                 if (type == EncodeConstants.EXPERIMENTER_VALUE) {\r
82                     Long expId = input.getUnsignedInt(input.readerIndex()\r
83                             + 2 * EncodeConstants.SIZE_OF_SHORT_IN_BYTES);\r
84                     return new ExperimenterInstructionDeserializerKey(getVersion(), expId);\r
85                 }\r
86                 return new InstructionDeserializerKey(getVersion(), type, null);\r
87             }\r
88         };\r
89     }\r
90 }\r