Removing deprecated getType() method from serializers.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPEndPointsIpv6ObjectParser.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, 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.protocol.pcep.impl.object;
9
10 import org.opendaylight.protocol.concepts.Ipv6Util;
11 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
12 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
13 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
14 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
15 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
16 import org.opendaylight.protocol.pcep.spi.UnknownObject;
17 import org.opendaylight.protocol.util.ByteArray;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.AddressFamily;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv6Case;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv6CaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.ipv6._case.Ipv6Builder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.EndpointsObj;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.EndpointsObjBuilder;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Parser for IPv6 {@link EndpointsObj}
31  */
32 public class PCEPEndPointsIpv6ObjectParser extends AbstractObjectWithTlvsParser<EndpointsObjBuilder> {
33
34         private static final Logger LOG = LoggerFactory.getLogger(PCEPEndPointsIpv4ObjectParser.class);
35
36         public static final int CLASS = 4;
37         public static final int TYPE = 2;
38
39         /*
40          * fields lengths and offsets for IPv4 in bytes
41          */
42         private static final int SRC6_F_LENGTH = 16;
43         private static final int DEST6_F_LENGTH = 16;
44
45         private static final int SRC6_F_OFFSET = 0;
46         private static final int DEST6_F_OFFSET = SRC6_F_OFFSET + SRC6_F_LENGTH;
47
48         public PCEPEndPointsIpv6ObjectParser(final TlvRegistry tlvReg) {
49                 super(tlvReg);
50         }
51
52         @Override
53         public Object parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
54                 if (bytes == null) {
55                         throw new IllegalArgumentException("Array of bytes is mandatory");
56                 }
57                 final EndpointsObjBuilder builder = new EndpointsObjBuilder();
58                 builder.setIgnore(header.isIgnore());
59                 builder.setProcessingRule(header.isProcessingRule());
60                 if (!builder.isProcessingRule()) {
61                         LOG.debug("Processed bit not set on Endpoints OBJECT, ignoring it.");
62                         return new UnknownObject(PCEPErrors.P_FLAG_NOT_SET, builder.build());
63                 }
64                 if (bytes.length != SRC6_F_LENGTH + DEST6_F_LENGTH) {
65                         throw new PCEPDeserializerException("Wrong length of array of bytes.");
66                 }
67                 final Ipv6Builder b = new Ipv6Builder();
68                 b.setSourceIpv6Address(Ipv6Util.addressForBytes(ByteArray.subByte(bytes, SRC6_F_OFFSET, SRC6_F_LENGTH)));
69                 b.setDestinationIpv6Address((Ipv6Util.addressForBytes(ByteArray.subByte(bytes, DEST6_F_OFFSET, DEST6_F_LENGTH))));
70                 builder.setAddressFamily(new Ipv6CaseBuilder().setIpv6(b.build()).build());
71                 return builder.build();
72         }
73
74         @Override
75         public byte[] serializeObject(final Object object) {
76                 if (!(object instanceof EndpointsObj)) {
77                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed EndpointsObject.");
78                 }
79                 final EndpointsObj ePObj = (EndpointsObj) object;
80
81                 final AddressFamily afi = ePObj.getAddressFamily();
82
83                 if (!(afi instanceof Ipv6Case)) {
84                         throw new IllegalArgumentException("Wrong instance of NetworkAddress. Passed " + afi.getClass() + ". Needed IPv4");
85                 }
86                 final byte[] retBytes = new byte[SRC6_F_LENGTH + DEST6_F_LENGTH];
87                 ByteArray.copyWhole(Ipv6Util.bytesForAddress((((Ipv6Case) afi).getIpv6()).getSourceIpv6Address()), retBytes, SRC6_F_OFFSET);
88                 ByteArray.copyWhole(Ipv6Util.bytesForAddress((((Ipv6Case) afi).getIpv6()).getDestinationIpv6Address()), retBytes, DEST6_F_OFFSET);
89                 return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
90         }
91 }