Bug 2756 - Match model update
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / match / AbstractOxmIpv6AddressSerializer.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 package org.opendaylight.openflowjava.protocol.impl.serialization.match;
9
10 import io.netty.buffer.ByteBuf;
11
12 import java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.List;
15
16 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
17 import org.opendaylight.openflowjava.util.ByteBufUtils;
18
19 import com.google.common.collect.Lists;
20
21 /**
22  * Parent for Ipv6 address based match entry serializers
23  * @author michal.polkorab
24  */
25 public abstract class AbstractOxmIpv6AddressSerializer extends AbstractOxmMatchEntrySerializer {
26
27     protected void writeIpv6Address(String textAddress, final ByteBuf outBuffer) {
28         List<String> address;
29         if (textAddress.equals("::")) {
30             String[] tmp = new String[EncodeConstants.GROUPS_IN_IPV6_ADDRESS];
31             Arrays.fill(tmp, "0");
32             address = Arrays.asList(tmp);
33         } else {
34             address = parseIpv6Address(Lists.newArrayList(ByteBufUtils.COLON_SPLITTER.split(textAddress)));
35         }
36         for (String group : address) {
37             outBuffer.writeShort(Integer.parseInt(group, 16));
38         }
39     }
40
41     private static List<String> parseIpv6Address(final List<String> addressGroups) {
42         int countEmpty = 0;
43         for (String group : addressGroups) {
44             if (group.equals("")) {
45                 countEmpty++;
46             }
47         }
48         List<String> ready = new ArrayList<>(EncodeConstants.GROUPS_IN_IPV6_ADDRESS);
49         switch (countEmpty) {
50         case 0:
51             ready = addressGroups;
52             break;
53         case 1:
54             int zerosToBePushed = EncodeConstants.GROUPS_IN_IPV6_ADDRESS - addressGroups.size() + 1;
55             for (String group : addressGroups) {
56                 if (group.equals("")) {
57                     for (int j = 0; j < zerosToBePushed; j++) {
58                         ready.add("0");
59                     }
60                 } else {
61                     ready.add(group);
62                 }
63             }
64             break;
65         case 2:
66             String[] tmp = new String[EncodeConstants.GROUPS_IN_IPV6_ADDRESS];
67             Arrays.fill(tmp, "0");
68             tmp[EncodeConstants.GROUPS_IN_IPV6_ADDRESS - 1] =
69                     addressGroups.get(addressGroups.size() - 1);
70             ready = Arrays.asList(tmp);
71             break;
72         default:
73             throw new IllegalStateException("Incorrect ipv6 address");
74         }
75         return ready;
76     }
77 }