2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.impl.object;
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;
30 * Parser for IPv4 {@link EndpointsObject}
32 public class PCEPEndPointsObjectParser extends AbstractObjectParser<EndpointsBuilder> {
34 public static final int CLASS = 4;
36 public static final int TYPE = 1;
39 * fields lengths and offsets for IPv4 in bytes
41 public static final int SRC4_F_LENGTH = 4;
42 public static final int DEST4_F_LENGTH = 4;
44 public static final int SRC4_F_OFFSET = 0;
45 public static final int DEST4_F_OFFSET = SRC4_F_OFFSET + SRC4_F_LENGTH;
47 public static final int CLASS_6 = 4;
49 public static final int TYPE_6 = 2;
51 public static final int SRC6_F_LENGTH = 16;
52 public static final int DEST6_F_LENGTH = 16;
54 public static final int SRC6_F_OFFSET = 0;
55 public static final int DEST6_F_OFFSET = SRC6_F_OFFSET + SRC6_F_LENGTH;
57 public PCEPEndPointsObjectParser(final HandlerRegistry registry) {
62 public EndpointsObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException,
63 PCEPDocumentedException {
65 throw new IllegalArgumentException("Array of bytes is mandatory");
67 if (!header.isProcessingRule())
68 throw new PCEPDocumentedException("Processed flag not set", PCEPErrors.P_FLAG_NOT_SET);
70 final EndpointsBuilder builder = new EndpointsBuilder();
71 builder.setIgnore(header.isIgnore());
72 builder.setProcessingRule(header.isProcessingRule());
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());
85 throw new PCEPDeserializerException("Wrong length of array of bytes.");
86 return builder.build();
90 public void addTlv(final EndpointsBuilder builder, final Tlv tlv) {
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.");
99 final EndpointsObject ePObj = (EndpointsObject) object;
101 final AddressFamily afi = ePObj.getAddressFamily();
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);
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);
114 throw new IllegalArgumentException("Wrong instance of NetworkAddress. Passed " + afi.getClass() + ". Needed IPv4");
118 public int getObjectType() {
123 public int getObjectClass() {
127 public int get6ObjectType() {
131 public int get6ObjectClass() {