BUG-50 : added parser for Pcreq message.
[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.spi.PCEPDeserializerException;
13 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
14 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
15 import org.opendaylight.protocol.pcep.spi.UnknownObject;
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 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Parser for IPv4 {@link EndpointsObj}
32  */
33 public class PCEPEndPointsObjectParser extends AbstractObjectWithTlvsParser<EndpointsObjBuilder> {
34
35         private static final Logger LOG = LoggerFactory.getLogger(PCEPEndPointsObjectParser.class);
36
37         public static final int CLASS = 4;
38         public static final int TYPE = 1;
39
40         public static final int CLASS_6 = 4;
41         public static final int TYPE_6 = 2;
42
43         /*
44          * fields lengths and offsets for IPv4 in bytes
45          */
46         private static final int SRC4_F_LENGTH = 4;
47         private static final int DEST4_F_LENGTH = 4;
48
49         private static final int SRC4_F_OFFSET = 0;
50         private static final int DEST4_F_OFFSET = SRC4_F_OFFSET + SRC4_F_LENGTH;
51
52         private static final int SRC6_F_LENGTH = 16;
53         private static final int DEST6_F_LENGTH = 16;
54
55         private static final int SRC6_F_OFFSET = 0;
56         private static final int DEST6_F_OFFSET = SRC6_F_OFFSET + SRC6_F_LENGTH;
57
58         public PCEPEndPointsObjectParser(final TlvHandlerRegistry tlvReg) {
59                 super(tlvReg);
60         }
61
62         @Override
63         public Object parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
64                 if (bytes == null) {
65                         throw new IllegalArgumentException("Array of bytes is mandatory");
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                 if (!builder.isProcessingRule()) {
85                         LOG.debug("Processed bit not set on Endpoints OBJECT, ignoring it.");
86                         return new UnknownObject(PCEPErrors.P_FLAG_NOT_SET, builder.build());
87                 }
88                 return builder.build();
89         }
90
91         @Override
92         public void addTlv(final EndpointsObjBuilder builder, final Tlv tlv) {
93                 // No tlvs defined
94         }
95
96         @Override
97         public byte[] serializeObject(final Object object) {
98                 if (!(object instanceof EndpointsObj)) {
99                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed EndpointsObject.");
100                 }
101
102                 final EndpointsObj ePObj = (EndpointsObj) object;
103
104                 final AddressFamily afi = ePObj.getAddressFamily();
105
106                 if (afi instanceof Ipv4) {
107                         final byte[] retBytes = new byte[SRC4_F_LENGTH + DEST4_F_LENGTH];
108                         ByteArray.copyWhole(Ipv4Util.bytesForAddress(((Ipv4) afi).getSourceIpv4Address()), retBytes, SRC4_F_OFFSET);
109                         ByteArray.copyWhole(Ipv4Util.bytesForAddress(((Ipv4) afi).getDestinationIpv4Address()), retBytes, DEST4_F_OFFSET);
110                         return retBytes;
111                 } else if (afi instanceof Ipv6) {
112                         final byte[] retBytes = new byte[SRC6_F_LENGTH + DEST6_F_LENGTH];
113                         ByteArray.copyWhole(Ipv6Util.bytesForAddress(((Ipv6) afi).getSourceIpv6Address()), retBytes, SRC6_F_OFFSET);
114                         ByteArray.copyWhole(Ipv6Util.bytesForAddress(((Ipv6) afi).getDestinationIpv6Address()), retBytes, DEST6_F_OFFSET);
115                         return retBytes;
116                 } else {
117                         throw new IllegalArgumentException("Wrong instance of NetworkAddress. Passed " + afi.getClass() + ". Needed IPv4");
118                 }
119         }
120
121         @Override
122         public int getObjectType() {
123                 return TYPE;
124         }
125
126         @Override
127         public int getObjectClass() {
128                 return CLASS;
129         }
130
131         public int get6ObjectType() {
132                 return TYPE_6;
133         }
134
135         public int get6ObjectClass() {
136                 return CLASS_6;
137         }
138 }