Merge "Use odlparent-lite as artifacts parent"
[lispflowmapping.git] / mappingservice / lisp-proto / src / main / java / org / opendaylight / lispflowmapping / lisp / serializer / address / Ipv4Serializer.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc.  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.lispflowmapping.lisp.serializer.address;
9
10 import java.net.Inet4Address;
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13 import java.nio.ByteBuffer;
14
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana.afn.safi.rev130704.AddressFamily;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv4Afi;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.LispAddress;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4Builder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
27
28 /**
29  * @author Lorand Jakab
30  *
31  */
32 public class Ipv4Serializer extends LispAddressSerializer {
33
34     private static final Ipv4Serializer INSTANCE = new Ipv4Serializer();
35
36     // Private constructor prevents instantiation from other classes
37     private Ipv4Serializer() {
38     }
39
40     public static Ipv4Serializer getInstance() {
41         return INSTANCE;
42     }
43
44     @Override
45     public int getAddressSize(LispAddress lispAddress) {
46         return Length.IPV4;
47     }
48
49     @Override
50     public int getAddressSize(SimpleAddress simpleAddress) {
51         return Length.IPV4;
52     }
53
54     @Override
55     protected short getAfi() {
56         return (short) AddressFamily.IpV4.getIntValue();
57     }
58
59     @Override
60     protected void serializeData(ByteBuffer buffer, LispAddress lispAddress) {
61         Ipv4 address = (Ipv4) lispAddress.getAddress();
62         try {
63             buffer.put(Inet4Address.getByName(address.getIpv4().getValue()).getAddress());
64         } catch (UnknownHostException e) {
65         }
66     }
67
68     @Override
69     protected void serializeData(ByteBuffer buffer, SimpleAddress address) {
70         try {
71             buffer.put(Inet4Address.getByName(address.getIpAddress().getIpv4Address().getValue()).getAddress());
72         } catch (UnknownHostException e) {
73         }
74     }
75
76     @Override
77     protected Eid deserializeEidData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
78         EidBuilder eb = new EidBuilder();
79         eb.setAddressType(Ipv4Afi.class);
80         eb.setVirtualNetworkId(getVni(ctx));
81         eb.setAddress(new Ipv4Builder().setIpv4(deserializeData(buffer)).build());
82         return eb.build();
83     }
84
85     @Override
86     protected Rloc deserializeRlocData(ByteBuffer buffer) {
87         RlocBuilder rb = new RlocBuilder();
88         rb.setAddressType(Ipv4Afi.class);
89         rb.setVirtualNetworkId(null);
90         rb.setAddress(new Ipv4Builder().setIpv4(deserializeData(buffer)).build());
91         return rb.build();
92     }
93
94     @Override
95     protected SimpleAddress deserializeSimpleAddressData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
96         return new SimpleAddress(new IpAddress(deserializeData(buffer)));
97     }
98
99     private Ipv4Address deserializeData(ByteBuffer buffer) {
100         byte[] ipBuffer = new byte[4];
101         InetAddress address = null;
102         buffer.get(ipBuffer);
103         try {
104             address = InetAddress.getByAddress(ipBuffer);
105         } catch (UnknownHostException e) {
106         }
107         return new Ipv4Address(address.getHostAddress());
108     }
109
110     protected interface Length {
111         int IPV4 = 4;
112     }
113
114 }