BUG-47 : switched subobjects to generated source code.
[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.impl.message.AbstractObjectWithTlvsParser;
16 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
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 AbstractObjectWithTlvsParser<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 TlvHandlerRegistry tlvReg) {
58                 super(tlvReg);
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
68                 if (!header.isProcessingRule()) {
69                         throw new PCEPDocumentedException("Processed flag not set", PCEPErrors.P_FLAG_NOT_SET);
70                 }
71
72                 final EndpointsBuilder builder = new EndpointsBuilder();
73                 builder.setIgnore(header.isIgnore());
74                 builder.setProcessingRule(header.isProcessingRule());
75
76                 if (bytes.length == SRC4_F_LENGTH + DEST4_F_LENGTH) {
77                         final Ipv4Builder b = new Ipv4Builder();
78                         b.setSourceIpv4Address(Ipv4Util.addressForBytes(ByteArray.subByte(bytes, SRC4_F_OFFSET, SRC4_F_LENGTH)));
79                         b.setDestinationIpv4Address((Ipv4Util.addressForBytes(ByteArray.subByte(bytes, DEST4_F_OFFSET, DEST4_F_LENGTH))));
80                         builder.setAddressFamily(b.build());
81                 } else if (bytes.length == SRC6_F_LENGTH + DEST6_F_LENGTH) {
82                         final Ipv6Builder b = new Ipv6Builder();
83                         b.setSourceIpv6Address(Ipv6Util.addressForBytes(ByteArray.subByte(bytes, SRC6_F_OFFSET, SRC6_F_LENGTH)));
84                         b.setDestinationIpv6Address((Ipv6Util.addressForBytes(ByteArray.subByte(bytes, DEST6_F_OFFSET, DEST6_F_LENGTH))));
85                         builder.setAddressFamily(b.build());
86                 } else {
87                         throw new PCEPDeserializerException("Wrong length of array of bytes.");
88                 }
89                 return builder.build();
90         }
91
92         @Override
93         public void addTlv(final EndpointsBuilder builder, final Tlv tlv) {
94                 // No tlvs defined
95         }
96
97         @Override
98         public byte[] serializeObject(final Object object) {
99                 if (!(object instanceof EndpointsObject)) {
100                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed EndpointsObject.");
101                 }
102
103                 final EndpointsObject ePObj = (EndpointsObject) object;
104
105                 final AddressFamily afi = ePObj.getAddressFamily();
106
107                 if (afi instanceof Ipv4) {
108                         final byte[] retBytes = new byte[SRC4_F_LENGTH + DEST4_F_LENGTH];
109                         ByteArray.copyWhole(Ipv4Util.bytesForAddress(((Ipv4) afi).getSourceIpv4Address()), retBytes, SRC4_F_OFFSET);
110                         ByteArray.copyWhole(Ipv4Util.bytesForAddress(((Ipv4) afi).getDestinationIpv4Address()), retBytes, DEST4_F_OFFSET);
111                         return retBytes;
112                 } else if (afi instanceof Ipv6) {
113                         final byte[] retBytes = new byte[SRC6_F_LENGTH + DEST6_F_LENGTH];
114                         ByteArray.copyWhole(Ipv6Util.bytesForAddress(((Ipv6) afi).getSourceIpv6Address()), retBytes, SRC6_F_OFFSET);
115                         ByteArray.copyWhole(Ipv6Util.bytesForAddress(((Ipv6) afi).getDestinationIpv6Address()), retBytes, DEST6_F_OFFSET);
116                         return retBytes;
117                 } else {
118                         throw new IllegalArgumentException("Wrong instance of NetworkAddress. Passed " + afi.getClass() + ". Needed IPv4");
119                 }
120         }
121
122         @Override
123         public int getObjectType() {
124                 return TYPE;
125         }
126
127         @Override
128         public int getObjectClass() {
129                 return CLASS;
130         }
131
132         public int get6ObjectType() {
133                 return TYPE_6;
134         }
135
136         public int get6ObjectClass() {
137                 return CLASS_6;
138         }
139 }