Mass replace CRLF->LF
[openflowjava.git] / 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
13 import org.opendaylight.openflowjava.protocol.api.extensibility.MessageCodeKey;
14 import org.opendaylight.openflowjava.protocol.api.keys.ActionDeserializerKey;
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.experimenter.ExperimenterActionDeserializerKey;
18 import org.opendaylight.openflowjava.protocol.api.keys.experimenter.ExperimenterInstructionDeserializerKey;
19 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
20
21 /**
22  * @author michal.polkorab
23  *
24  */
25 public abstract class CodeKeyMakerFactory {
26
27     /**
28      * @param version
29      * @return
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     /**
53      * @param version
54      * @return
55      */
56     public static CodeKeyMaker createActionsKeyMaker(short version) {
57         return new AbstractCodeKeyMaker(version) {
58             @Override
59             public MessageCodeKey make(ByteBuf input) {
60                 int type = input.getUnsignedShort(input.readerIndex());
61                 if (type == EncodeConstants.EXPERIMENTER_VALUE) {
62                     Long expId = input.getUnsignedInt(input.readerIndex()
63                             + 2 * EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
64                     return new ExperimenterActionDeserializerKey(getVersion(), expId);
65                 }
66                 ActionDeserializerKey actionDeserializerKey = new ActionDeserializerKey(getVersion(), type, null);
67                 return actionDeserializerKey;
68             }
69         };
70     }
71
72     /**
73      * @param version
74      * @return
75      */
76     public static CodeKeyMaker createInstructionsKeyMaker(short version) {
77         return new AbstractCodeKeyMaker(version) {
78             @Override
79             public MessageCodeKey make(ByteBuf input) {
80                 int type = input.getUnsignedShort(input.readerIndex());
81                 if (type == EncodeConstants.EXPERIMENTER_VALUE) {
82                     Long expId = input.getUnsignedInt(input.readerIndex()
83                             + 2 * EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
84                     return new ExperimenterInstructionDeserializerKey(getVersion(), expId);
85                 }
86                 return new InstructionDeserializerKey(getVersion(), type, null);
87             }
88         };
89     }
90 }