Adapt to IetfInetUtil change
[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.UnknownHostException;
12 import java.nio.ByteBuffer;
13
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana.afn.safi.rev130704.AddressFamily;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.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 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Class to (de)serialize IPv4 addresses from/to String representation.
32  *
33  * @author Lorand Jakab
34  *
35  */
36 public final class Ipv4Serializer extends LispAddressSerializer {
37
38     protected static final Logger LOG = LoggerFactory.getLogger(Ipv4Serializer.class);
39
40     private static final Ipv4Serializer INSTANCE = new Ipv4Serializer();
41
42     // Private constructor prevents instantiation from other classes
43     private Ipv4Serializer() {
44     }
45
46     public static Ipv4Serializer getInstance() {
47         return INSTANCE;
48     }
49
50     @Override
51     public int getAddressSize(LispAddress lispAddress) {
52         return Length.IPV4;
53     }
54
55     @Override
56     public int getAddressSize(SimpleAddress simpleAddress) {
57         return Length.IPV4;
58     }
59
60     @Override
61     protected short getAfi() {
62         return (short) AddressFamily.IpV4.getIntValue();
63     }
64
65     @Override
66     protected void serializeData(ByteBuffer buffer, LispAddress lispAddress) {
67         Ipv4 address = (Ipv4) lispAddress.getAddress();
68         try {
69             buffer.put(Inet4Address.getByName(address.getIpv4().getValue()).getAddress());
70         } catch (UnknownHostException e) {
71             LOG.debug("Unknown host {}", address.getIpv4().getValue(), e);
72         }
73     }
74
75     @Override
76     protected void serializeData(ByteBuffer buffer, SimpleAddress address) {
77         try {
78             buffer.put(Inet4Address.getByName(address.getIpAddress().getIpv4Address().getValue()).getAddress());
79         } catch (UnknownHostException e) {
80             LOG.debug("Unknown host {}", address.getIpAddress().getIpv4Address().getValue(), e);
81         }
82     }
83
84     @Override
85     protected Eid deserializeEidData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
86         EidBuilder eb = new EidBuilder();
87         eb.setAddressType(Ipv4Afi.class);
88         eb.setVirtualNetworkId(getVni(ctx));
89         eb.setAddress(new Ipv4Builder().setIpv4(deserializeData(buffer)).build());
90         return eb.build();
91     }
92
93     @Override
94     protected Rloc deserializeRlocData(ByteBuffer buffer) {
95         RlocBuilder rb = new RlocBuilder();
96         rb.setAddressType(Ipv4Afi.class);
97         rb.setVirtualNetworkId(null);
98         rb.setAddress(new Ipv4Builder().setIpv4(deserializeData(buffer)).build());
99         return rb.build();
100     }
101
102     @Override
103     protected SimpleAddress deserializeSimpleAddressData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
104         return new SimpleAddress(new IpAddress(deserializeData(buffer)));
105     }
106
107     private static Ipv4Address deserializeData(ByteBuffer buffer) {
108         byte[] ipBuffer = new byte[4];
109         buffer.get(ipBuffer);
110         return new Ipv4Address(IetfInetUtil.INSTANCE.ipv4AddressFor(ipBuffer));
111     }
112
113     protected interface Length {
114         int IPV4 = 4;
115     }
116
117 }