Extensibility support (serialization part)
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / match / AbstractOxmIpv6AddressSerializer.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 package org.opendaylight.openflowjava.protocol.impl.serialization.match;\r
9 \r
10 import io.netty.buffer.ByteBuf;\r
11 \r
12 import java.util.Arrays;\r
13 \r
14 import org.opendaylight.openflowjava.protocol.impl.util.EncodeConstants;\r
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv6AddressMatchEntry;\r
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;\r
17 \r
18 /**\r
19  * Parent for Ipv6 address based match entry serializers\r
20  * @author michal.polkorab\r
21  */\r
22 public abstract class AbstractOxmIpv6AddressSerializer extends AbstractOxmMatchEntrySerializer {\r
23 \r
24     @Override\r
25     public void serialize(MatchEntries entry, ByteBuf outBuffer) {\r
26         super.serialize(entry, outBuffer);\r
27         String textAddress = entry.getAugmentation(Ipv6AddressMatchEntry.class).getIpv6Address().getValue();\r
28         String[] address;\r
29         if (textAddress.equals("::")) {\r
30             address = new String[EncodeConstants.GROUPS_IN_IPV6_ADDRESS];\r
31             Arrays.fill(address, "0");\r
32         } else {\r
33             address = parseIpv6Address(textAddress.split(":"));\r
34         }\r
35         for (int i = 0; i < address.length; i++) {\r
36             outBuffer.writeShort(Integer.parseInt(address[i], 16));\r
37         }\r
38         writeMask(entry, outBuffer, getValueLength());\r
39     }\r
40 \r
41     private static String[] parseIpv6Address(String[] addressGroups) {\r
42         int countEmpty = 0;\r
43         for (int i = 0; i < addressGroups.length; i++) {\r
44             if (addressGroups[i].equals("")){\r
45                 countEmpty++;\r
46             }\r
47         }\r
48         String[] ready = new String[EncodeConstants.GROUPS_IN_IPV6_ADDRESS];\r
49         switch (countEmpty) {\r
50         case 0:\r
51             ready = addressGroups;\r
52             break;\r
53         case 1:\r
54             int zerosToBePushed = EncodeConstants.GROUPS_IN_IPV6_ADDRESS - addressGroups.length + 1;\r
55             int index = 0;\r
56             for (int i = 0; i < addressGroups.length; i++) {\r
57                 if (addressGroups[i].equals("")) {\r
58                     for (int j = 0; j < zerosToBePushed; j++) {\r
59                         ready[index] = "0";\r
60                         index++;\r
61                     }\r
62                 } else {\r
63                     ready[index] = addressGroups[i];\r
64                     index++;\r
65                 }\r
66             }\r
67             break;\r
68         case 2:\r
69             Arrays.fill(ready, "0");\r
70             ready[ready.length - 1] = addressGroups[addressGroups.length - 1];\r
71             break;\r
72         default:\r
73             throw new IllegalStateException("Incorrect ipv6 address");\r
74         }\r
75         return ready;\r
76     }\r
77 }\r