BUG-47 : unfinished PCEP migration to generated DTOs.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPEndPointsObjectParser.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.Ipv4Util;
11 import org.opendaylight.protocol.concepts.Ipv6Util;
12 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
13 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
14 import org.opendaylight.protocol.pcep.PCEPErrors;
15 import org.opendaylight.protocol.pcep.spi.AbstractObjectParser;
16 import org.opendaylight.protocol.pcep.spi.HandlerRegistry;
17 import org.opendaylight.protocol.util.ByteArray;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.EndpointsObject;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.AddressFamily;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.address.family.Ipv4;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.address.family.Ipv4Builder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.address.family.Ipv6;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.address.family.Ipv6Builder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcinitiate.message.pcinitiate.message.requests.EndpointsBuilder;
28
29 /**
30  * Parser for IPv4 {@link EndpointsObject}
31  */
32 public class PCEPEndPointsObjectParser extends AbstractObjectParser<EndpointsBuilder> {
33
34         public static final int CLASS = 4;
35
36         public static final int TYPE = 1;
37
38         /*
39          * fields lengths and offsets for IPv4 in bytes
40          */
41         public static final int SRC4_F_LENGTH = 4;
42         public static final int DEST4_F_LENGTH = 4;
43
44         public static final int SRC4_F_OFFSET = 0;
45         public static final int DEST4_F_OFFSET = SRC4_F_OFFSET + SRC4_F_LENGTH;
46         
47         public static final int CLASS_6 = 4;
48
49         public static final int TYPE_6 = 2;
50
51         public static final int SRC6_F_LENGTH = 16;
52         public static final int DEST6_F_LENGTH = 16;
53
54         public static final int SRC6_F_OFFSET = 0;
55         public static final int DEST6_F_OFFSET = SRC6_F_OFFSET + SRC6_F_LENGTH;
56
57         public PCEPEndPointsObjectParser(final HandlerRegistry registry) {
58                 super(registry);
59         }
60
61         @Override
62         public EndpointsObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException,
63                         PCEPDocumentedException {
64                 if (bytes == null)
65                         throw new IllegalArgumentException("Array of bytes is mandatory");
66                 
67                 if (!header.isProcessingRule())
68                         throw new PCEPDocumentedException("Processed flag not set", PCEPErrors.P_FLAG_NOT_SET);
69                 
70                 final EndpointsBuilder builder = new EndpointsBuilder();
71                 builder.setIgnore(header.isIgnore());
72                 builder.setProcessingRule(header.isProcessingRule());
73                 
74                 if (bytes.length == SRC4_F_LENGTH + DEST4_F_LENGTH) {
75                         final Ipv4Builder b = new Ipv4Builder();
76                         b.setSourceIpv4Address(Ipv4Util.addressForBytes(ByteArray.subByte(bytes, SRC4_F_OFFSET, SRC4_F_LENGTH)));
77                         b.setDestinationIpv4Address((Ipv4Util.addressForBytes(ByteArray.subByte(bytes, DEST4_F_OFFSET, DEST4_F_LENGTH))));
78                         builder.setAddressFamily(b.build());
79                 } else if (bytes.length == SRC6_F_LENGTH + DEST6_F_LENGTH) {
80                         final Ipv6Builder b = new Ipv6Builder();
81                         b.setSourceIpv6Address(Ipv6Util.addressForBytes(ByteArray.subByte(bytes, SRC6_F_OFFSET, SRC6_F_LENGTH)));
82                         b.setDestinationIpv6Address((Ipv6Util.addressForBytes(ByteArray.subByte(bytes, DEST6_F_OFFSET, DEST6_F_LENGTH))));
83                         builder.setAddressFamily(b.build());
84                 } else
85                         throw new PCEPDeserializerException("Wrong length of array of bytes.");
86                 return builder.build();
87         }
88
89         @Override
90         public void addTlv(final EndpointsBuilder builder, final Tlv tlv) {
91                 // No tlvs defined
92         }
93
94         @Override
95         public byte[] serializeObject(final Object object) {
96                 if (!(object instanceof EndpointsObject))
97                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed EndpointsObject.");
98
99                 final EndpointsObject ePObj = (EndpointsObject) object;
100
101                 final AddressFamily afi = ePObj.getAddressFamily();
102
103                 if (afi instanceof Ipv4) {
104                         final byte[] retBytes = new byte[SRC4_F_LENGTH + DEST4_F_LENGTH];
105                         ByteArray.copyWhole(Ipv4Util.bytesForAddress(((Ipv4) afi).getSourceIpv4Address()), retBytes, SRC4_F_OFFSET);
106                         ByteArray.copyWhole(Ipv4Util.bytesForAddress(((Ipv4) afi).getDestinationIpv4Address()), retBytes, DEST4_F_OFFSET);
107                         return retBytes;
108                 } else if (afi instanceof Ipv6) {
109                         final byte[] retBytes = new byte[SRC6_F_LENGTH + DEST6_F_LENGTH];
110                         ByteArray.copyWhole(Ipv6Util.bytesForAddress(((Ipv6) afi).getSourceIpv6Address()), retBytes, SRC6_F_OFFSET);
111                         ByteArray.copyWhole(Ipv6Util.bytesForAddress(((Ipv6) afi).getDestinationIpv6Address()), retBytes, DEST6_F_OFFSET);
112                         return retBytes;
113                 } else
114                         throw new IllegalArgumentException("Wrong instance of NetworkAddress. Passed " + afi.getClass() + ". Needed IPv4");
115         }
116
117         @Override
118         public int getObjectType() {
119                 return TYPE;
120         }
121
122         @Override
123         public int getObjectClass() {
124                 return CLASS;
125         }
126         
127         public int get6ObjectType() {
128                 return TYPE_6;
129         }
130
131         public int get6ObjectClass() {
132                 return CLASS_6;
133         }
134 }