Do not instantiate Joiner/Splitter
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / util / OF10MatchSerializer.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 java.util.HashMap;
14 import java.util.Map;
15
16 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowWildcardsV10;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.v10.grouping.MatchV10;
19
20 /**
21  * Serializes ofp_match (OpenFlow v1.0) structure
22  * @author michal.polkorab
23  */
24 public class OF10MatchSerializer implements OFSerializer<MatchV10> {
25
26     private static final byte PADDING_IN_MATCH = 1;
27     private static final byte PADDING_IN_MATCH_2 = 2;
28     private static final byte NW_SRC_SHIFT = 8;
29     private static final byte NW_DST_SHIFT = 14;
30
31     /**
32      * Serializes ofp_match (OpenFlow v1.0)
33      * @param outBuffer output ByteBuf
34      * @param match match to be serialized
35      */
36     @Override
37     public void serialize(final MatchV10 match, final ByteBuf outBuffer) {
38         outBuffer.writeInt(encodeWildcards(match.getWildcards(), match.getNwSrcMask(), match.getNwDstMask()));
39         outBuffer.writeShort(match.getInPort());
40         outBuffer.writeBytes(ByteBufUtils.macAddressToBytes(match.getDlSrc().getValue()));
41         outBuffer.writeBytes(ByteBufUtils.macAddressToBytes(match.getDlDst().getValue()));
42         outBuffer.writeShort(match.getDlVlan());
43         outBuffer.writeByte(match.getDlVlanPcp());
44         ByteBufUtils.padBuffer(PADDING_IN_MATCH, outBuffer);
45         outBuffer.writeShort(match.getDlType());
46         outBuffer.writeByte(match.getNwTos());
47         outBuffer.writeByte(match.getNwProto());
48         ByteBufUtils.padBuffer(PADDING_IN_MATCH_2, outBuffer);
49         Iterable<String> srcGroups = ByteBufUtils.DOT_SPLITTER.split(match.getNwSrc().getValue());
50         for (String group : srcGroups) {
51             outBuffer.writeByte(Short.parseShort(group));
52         }
53         Iterable<String> dstGroups = ByteBufUtils.DOT_SPLITTER.split(match.getNwDst().getValue());
54         for (String group : dstGroups) {
55             outBuffer.writeByte(Short.parseShort(group));
56         }
57         outBuffer.writeShort(match.getTpSrc());
58         outBuffer.writeShort(match.getTpDst());
59     }
60
61     private static int encodeWildcards(final FlowWildcardsV10 wildcards, final short srcMask, final short dstMask) {
62         int bitmask = 0;
63         Map<Integer, Boolean> wildcardsMap = new HashMap<>();
64         wildcardsMap.put(0, wildcards.isINPORT());
65         wildcardsMap.put(1, wildcards.isDLVLAN());
66         wildcardsMap.put(2, wildcards.isDLSRC());
67         wildcardsMap.put(3, wildcards.isDLDST());
68         wildcardsMap.put(4, wildcards.isDLTYPE());
69         wildcardsMap.put(5, wildcards.isNWPROTO());
70         wildcardsMap.put(6, wildcards.isTPSRC());
71         wildcardsMap.put(7, wildcards.isTPDST());
72         wildcardsMap.put(20, wildcards.isDLVLANPCP());
73         wildcardsMap.put(21, wildcards.isNWTOS());
74         bitmask = ByteBufUtils.fillBitMaskFromMap(wildcardsMap);
75         bitmask |= ((32 - srcMask) << NW_SRC_SHIFT);
76         bitmask |= ((32 - dstMask) << NW_DST_SHIFT);
77         return bitmask;
78     }
79
80 }