Reorganize mappingservice.implementation
[lispflowmapping.git] / mappingservice / yangmodel / src / main / java / org / opendaylight / lispflowmapping / lisp / serializer / address / LispDistinguishedNameAddressSerializer.java
1 /*
2  * Copyright (c) 2014 Contextream, Inc. 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.lispflowmapping.lisp.serializer.address;
9
10 import java.nio.ByteBuffer;
11
12 import org.opendaylight.lispflowmapping.lisp.type.AddressFamilyNumberEnum;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LispAFIAddress;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LispDistinguishedNameAddress;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.distinguishedname.DistinguishedNameBuilder;
16
17 public class LispDistinguishedNameAddressSerializer extends LispAddressSerializer {
18
19     private static final LispDistinguishedNameAddressSerializer INSTANCE = new LispDistinguishedNameAddressSerializer();
20
21     // Private constructor prevents instantiation from other classes
22     private LispDistinguishedNameAddressSerializer() {
23     }
24
25     public static LispDistinguishedNameAddressSerializer getInstance() {
26         return INSTANCE;
27     }
28
29     @Override
30     public int getAddressSize(LispAFIAddress lispAddress) {
31         return ((LispDistinguishedNameAddress) lispAddress).getDistinguishedName().length() + 1;
32
33     }
34
35     @Override
36     protected LispDistinguishedNameAddress deserializeData(ByteBuffer buffer) {
37         StringBuilder sb = new StringBuilder();
38         byte b = buffer.get();
39         while (b != 0) {
40             sb.append((char) b);
41             b = buffer.get();
42         }
43         return new DistinguishedNameBuilder().setAfi(AddressFamilyNumberEnum.DISTINGUISHED_NAME.getIanaCode()).setDistinguishedName((sb.toString()))
44                 .build();
45     }
46
47     @Override
48     protected void serializeData(ByteBuffer buffer, LispAFIAddress lispAddress) {
49         LispDistinguishedNameAddress distinguishedNameAddress = (LispDistinguishedNameAddress) lispAddress;
50         buffer.put(distinguishedNameAddress.getDistinguishedName().getBytes());
51         buffer.put((byte) 0);
52     }
53
54 }