Rework parser infrastructure to support partial message processing
[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.spi.TlvHandlerRegistry;
14 import org.opendaylight.protocol.util.ByteArray;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.AddressFamily;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv4;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv4Builder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv6;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv6Builder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.EndpointsObj;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.EndpointsObjBuilder;
25
26 /**
27  * Parser for IPv4 {@link EndpointsObj}
28  */
29 public class PCEPEndPointsObjectParser extends AbstractObjectWithTlvsParser<EndpointsObjBuilder> {
30
31         public static final int CLASS = 4;
32         public static final int TYPE = 1;
33
34         public static final int CLASS_6 = 4;
35         public static final int TYPE_6 = 2;
36
37         /*
38          * fields lengths and offsets for IPv4 in bytes
39          */
40         private static final int SRC4_F_LENGTH = 4;
41         private static final int DEST4_F_LENGTH = 4;
42
43         private static final int SRC4_F_OFFSET = 0;
44         private static final int DEST4_F_OFFSET = SRC4_F_OFFSET + SRC4_F_LENGTH;
45
46         private static final int SRC6_F_LENGTH = 16;
47         private static final int DEST6_F_LENGTH = 16;
48
49         private static final int SRC6_F_OFFSET = 0;
50         private static final int DEST6_F_OFFSET = SRC6_F_OFFSET + SRC6_F_LENGTH;
51
52         public PCEPEndPointsObjectParser(final TlvHandlerRegistry tlvReg) {
53                 super(tlvReg);
54         }
55
56         @Override
57         public EndpointsObj parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
58                 if (bytes == null) {
59                         throw new IllegalArgumentException("Array of bytes is mandatory");
60                 }
61
62                 if (!header.isProcessingRule()) {
63                         //LOG.debug("Processed bit not set on ENDPOINTS OBJECT, ignoring it");
64                         return null;
65                 }
66
67                 final EndpointsObjBuilder builder = new EndpointsObjBuilder();
68                 builder.setIgnore(header.isIgnore());
69                 builder.setProcessingRule(header.isProcessingRule());
70
71                 if (bytes.length == SRC4_F_LENGTH + DEST4_F_LENGTH) {
72                         final Ipv4Builder b = new Ipv4Builder();
73                         b.setSourceIpv4Address(Ipv4Util.addressForBytes(ByteArray.subByte(bytes, SRC4_F_OFFSET, SRC4_F_LENGTH)));
74                         b.setDestinationIpv4Address((Ipv4Util.addressForBytes(ByteArray.subByte(bytes, DEST4_F_OFFSET, DEST4_F_LENGTH))));
75                         builder.setAddressFamily(b.build());
76                 } else if (bytes.length == SRC6_F_LENGTH + DEST6_F_LENGTH) {
77                         final Ipv6Builder b = new Ipv6Builder();
78                         b.setSourceIpv6Address(Ipv6Util.addressForBytes(ByteArray.subByte(bytes, SRC6_F_OFFSET, SRC6_F_LENGTH)));
79                         b.setDestinationIpv6Address((Ipv6Util.addressForBytes(ByteArray.subByte(bytes, DEST6_F_OFFSET, DEST6_F_LENGTH))));
80                         builder.setAddressFamily(b.build());
81                 } else {
82                         throw new PCEPDeserializerException("Wrong length of array of bytes.");
83                 }
84                 return builder.build();
85         }
86
87         @Override
88         public void addTlv(final EndpointsObjBuilder builder, final Tlv tlv) {
89                 // No tlvs defined
90         }
91
92         @Override
93         public byte[] serializeObject(final Object object) {
94                 if (!(object instanceof EndpointsObj)) {
95                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed EndpointsObject.");
96                 }
97
98                 final EndpointsObj ePObj = (EndpointsObj) object;
99
100                 final AddressFamily afi = ePObj.getAddressFamily();
101
102                 if (afi instanceof Ipv4) {
103                         final byte[] retBytes = new byte[SRC4_F_LENGTH + DEST4_F_LENGTH];
104                         ByteArray.copyWhole(Ipv4Util.bytesForAddress(((Ipv4) afi).getSourceIpv4Address()), retBytes, SRC4_F_OFFSET);
105                         ByteArray.copyWhole(Ipv4Util.bytesForAddress(((Ipv4) afi).getDestinationIpv4Address()), retBytes, DEST4_F_OFFSET);
106                         return retBytes;
107                 } else if (afi instanceof Ipv6) {
108                         final byte[] retBytes = new byte[SRC6_F_LENGTH + DEST6_F_LENGTH];
109                         ByteArray.copyWhole(Ipv6Util.bytesForAddress(((Ipv6) afi).getSourceIpv6Address()), retBytes, SRC6_F_OFFSET);
110                         ByteArray.copyWhole(Ipv6Util.bytesForAddress(((Ipv6) afi).getDestinationIpv6Address()), retBytes, DEST6_F_OFFSET);
111                         return retBytes;
112                 } else {
113                         throw new IllegalArgumentException("Wrong instance of NetworkAddress. Passed " + afi.getClass() + ". Needed IPv4");
114                 }
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 }