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