Bug 2245 Fixed Avoid cycle between java packages
[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.keys.ActionDeserializerKey;
14 import org.opendaylight.openflowjava.protocol.api.keys.ExperimenterActionDeserializerKey;
15 import org.opendaylight.openflowjava.protocol.api.keys.ExperimenterInstructionDeserializerKey;
16 import org.opendaylight.openflowjava.protocol.api.keys.InstructionDeserializerKey;
17 import org.opendaylight.openflowjava.protocol.api.keys.MatchEntryDeserializerKey;
18 import org.opendaylight.openflowjava.protocol.api.keys.MessageCodeKey;
19 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
20
21 /**
22  * @author michal.polkorab
23  *
24  */
25 public abstract class CodeKeyMakerFactory {
26
27     private CodeKeyMakerFactory() {
28         //not called
29     }
30     /**
31      * @param version
32      * @return
33      */
34     public static CodeKeyMaker createMatchEntriesKeyMaker(short version) {
35         return new AbstractCodeKeyMaker(version) {
36             @Override
37             public MessageCodeKey make(ByteBuf input) {
38                 int oxmClass = input.getUnsignedShort(input.readerIndex());
39                 int oxmField = input.getUnsignedByte(input.readerIndex()
40                         + EncodeConstants.SIZE_OF_SHORT_IN_BYTES) >>> 1;
41                 MatchEntryDeserializerKey key = new MatchEntryDeserializerKey(getVersion(),
42                         oxmClass, oxmField);
43                 if (oxmClass == EncodeConstants.EXPERIMENTER_VALUE) {
44                     long expId = input.getUnsignedInt(input.readerIndex() + EncodeConstants.SIZE_OF_SHORT_IN_BYTES
45                             + 2 * EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
46                     key.setExperimenterId(expId);
47                     return key;
48                 }
49                 key.setExperimenterId(null);
50                 return key;
51             }
52         };
53     }
54
55     /**
56      * @param version
57      * @return
58      */
59     public static CodeKeyMaker createActionsKeyMaker(short version) {
60         return new AbstractCodeKeyMaker(version) {
61             @Override
62             public MessageCodeKey make(ByteBuf input) {
63                 int type = input.getUnsignedShort(input.readerIndex());
64                 if (type == EncodeConstants.EXPERIMENTER_VALUE) {
65                     Long expId = input.getUnsignedInt(input.readerIndex()
66                             + 2 * EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
67                     return new ExperimenterActionDeserializerKey(getVersion(), expId);
68                 }
69                 ActionDeserializerKey actionDeserializerKey = new ActionDeserializerKey(getVersion(), type, null);
70                 return actionDeserializerKey;
71             }
72         };
73     }
74
75     /**
76      * @param version
77      * @return
78      */
79     public static CodeKeyMaker createInstructionsKeyMaker(short version) {
80         return new AbstractCodeKeyMaker(version) {
81             @Override
82             public MessageCodeKey make(ByteBuf input) {
83                 int type = input.getUnsignedShort(input.readerIndex());
84                 if (type == EncodeConstants.EXPERIMENTER_VALUE) {
85                     Long expId = input.getUnsignedInt(input.readerIndex()
86                             + 2 * EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
87                     return new ExperimenterInstructionDeserializerKey(getVersion(), expId);
88                 }
89                 return new InstructionDeserializerKey(getVersion(), type, null);
90             }
91         };
92     }
93 }