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.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;
29 * Parser for IPv4 {@link EndpointsObj}
31 public class PCEPEndPointsObjectParser extends AbstractObjectWithTlvsParser<EndpointsObjBuilder> {
33 public static final int CLASS = 4;
34 public static final int TYPE = 1;
36 public static final int CLASS_6 = 4;
37 public static final int TYPE_6 = 2;
40 * fields lengths and offsets for IPv4 in bytes
42 private static final int SRC4_F_LENGTH = 4;
43 private static final int DEST4_F_LENGTH = 4;
45 private static final int SRC4_F_OFFSET = 0;
46 private static final int DEST4_F_OFFSET = SRC4_F_OFFSET + SRC4_F_LENGTH;
48 private static final int SRC6_F_LENGTH = 16;
49 private static final int DEST6_F_LENGTH = 16;
51 private static final int SRC6_F_OFFSET = 0;
52 private static final int DEST6_F_OFFSET = SRC6_F_OFFSET + SRC6_F_LENGTH;
54 public PCEPEndPointsObjectParser(final TlvHandlerRegistry tlvReg) {
59 public EndpointsObj parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException,
60 PCEPDocumentedException {
62 throw new IllegalArgumentException("Array of bytes is mandatory");
65 if (!header.isProcessingRule()) {
66 throw new PCEPDocumentedException("Processed flag not set", PCEPErrors.P_FLAG_NOT_SET);
69 final EndpointsObjBuilder builder = new EndpointsObjBuilder();
70 builder.setIgnore(header.isIgnore());
71 builder.setProcessingRule(header.isProcessingRule());
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());
84 throw new PCEPDeserializerException("Wrong length of array of bytes.");
86 return builder.build();
90 public void addTlv(final EndpointsObjBuilder builder, final Tlv tlv) {
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.");
100 final EndpointsObj ePObj = (EndpointsObj) object;
102 final AddressFamily afi = ePObj.getAddressFamily();
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);
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);
115 throw new IllegalArgumentException("Wrong instance of NetworkAddress. Passed " + afi.getClass() + ". Needed IPv4");
120 public int getObjectType() {
125 public int getObjectClass() {
129 public int get6ObjectType() {
133 public int get6ObjectClass() {