Mass replace CRLF->LF
[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.util.ByteBufUtils;
17 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv6AddressMatchEntry;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;
20
21 import com.google.common.collect.Lists;
22
23 /**
24  * Parent for Ipv6 address based match entry serializers
25  * @author michal.polkorab
26  */
27 public abstract class AbstractOxmIpv6AddressSerializer extends AbstractOxmMatchEntrySerializer {
28
29     @Override
30     public void serialize(final MatchEntries entry, final ByteBuf outBuffer) {
31         super.serialize(entry, outBuffer);
32         String textAddress = entry.getAugmentation(Ipv6AddressMatchEntry.class).getIpv6Address().getValue();
33         List<String> address;
34         if (textAddress.equals("::")) {
35             String[] tmp = new String[EncodeConstants.GROUPS_IN_IPV6_ADDRESS];
36             Arrays.fill(tmp, "0");
37             address = Arrays.asList(tmp);
38         } else {
39             address = parseIpv6Address(Lists.newArrayList(ByteBufUtils.COLON_SPLITTER.split(textAddress)));
40         }
41         for (String group : address) {
42             outBuffer.writeShort(Integer.parseInt(group, 16));
43         }
44         writeMask(entry, outBuffer, getValueLength());
45     }
46
47     private static List<String> parseIpv6Address(final List<String> addressGroups) {
48         int countEmpty = 0;
49         for (String group : addressGroups) {
50             if (group.equals("")) {
51                 countEmpty++;
52             }
53         }
54         List<String> ready = new ArrayList<>(EncodeConstants.GROUPS_IN_IPV6_ADDRESS);
55         switch (countEmpty) {
56         case 0:
57             ready = addressGroups;
58             break;
59         case 1:
60             int zerosToBePushed = EncodeConstants.GROUPS_IN_IPV6_ADDRESS - addressGroups.size() + 1;
61             for (String group : addressGroups) {
62                 if (group.equals("")) {
63                     for (int j = 0; j < zerosToBePushed; j++) {
64                         ready.add("0");
65                     }
66                 } else {
67                     ready.add(group);
68                 }
69             }
70             break;
71         case 2:
72             String[] tmp = new String[EncodeConstants.GROUPS_IN_IPV6_ADDRESS];
73             Arrays.fill(tmp, "0");
74             tmp[EncodeConstants.GROUPS_IN_IPV6_ADDRESS - 1] =
75                     addressGroups.get(addressGroups.size() - 1);
76             ready = Arrays.asList(tmp);
77             break;
78         default:
79             throw new IllegalStateException("Incorrect ipv6 address");
80         }
81         return ready;
82     }
83 }