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.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;
27 * Parser for IPv4 {@link EndpointsObj}
29 public class PCEPEndPointsObjectParser extends AbstractObjectWithTlvsParser<EndpointsObjBuilder> {
31 public static final int CLASS = 4;
32 public static final int TYPE = 1;
34 public static final int CLASS_6 = 4;
35 public static final int TYPE_6 = 2;
38 * fields lengths and offsets for IPv4 in bytes
40 private static final int SRC4_F_LENGTH = 4;
41 private static final int DEST4_F_LENGTH = 4;
43 private static final int SRC4_F_OFFSET = 0;
44 private static final int DEST4_F_OFFSET = SRC4_F_OFFSET + SRC4_F_LENGTH;
46 private static final int SRC6_F_LENGTH = 16;
47 private static final int DEST6_F_LENGTH = 16;
49 private static final int SRC6_F_OFFSET = 0;
50 private static final int DEST6_F_OFFSET = SRC6_F_OFFSET + SRC6_F_LENGTH;
52 public PCEPEndPointsObjectParser(final TlvHandlerRegistry tlvReg) {
57 public EndpointsObj parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
59 throw new IllegalArgumentException("Array of bytes is mandatory");
62 if (!header.isProcessingRule()) {
63 //LOG.debug("Processed bit not set on ENDPOINTS OBJECT, ignoring it");
67 final EndpointsObjBuilder builder = new EndpointsObjBuilder();
68 builder.setIgnore(header.isIgnore());
69 builder.setProcessingRule(header.isProcessingRule());
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());
82 throw new PCEPDeserializerException("Wrong length of array of bytes.");
84 return builder.build();
88 public void addTlv(final EndpointsObjBuilder builder, final Tlv tlv) {
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.");
98 final EndpointsObj ePObj = (EndpointsObj) object;
100 final AddressFamily afi = ePObj.getAddressFamily();
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);
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);
113 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() {