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.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.Ipv4Case;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv4CaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv6Case;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv6CaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.ipv4._case.Ipv4Builder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.ipv6._case.Ipv6Builder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.EndpointsObj;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.EndpointsObjBuilder;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Parser for IPv4 {@link EndpointsObj}
35 public class PCEPEndPointsObjectParser extends AbstractObjectWithTlvsParser<EndpointsObjBuilder> {
37 private static final Logger LOG = LoggerFactory.getLogger(PCEPEndPointsObjectParser.class);
39 public static final int CLASS = 4;
40 public static final int TYPE = 1;
42 public static final int CLASS_6 = 4;
43 public static final int TYPE_6 = 2;
46 * fields lengths and offsets for IPv4 in bytes
48 private static final int SRC4_F_LENGTH = 4;
49 private static final int DEST4_F_LENGTH = 4;
51 private static final int SRC4_F_OFFSET = 0;
52 private static final int DEST4_F_OFFSET = SRC4_F_OFFSET + SRC4_F_LENGTH;
54 private static final int SRC6_F_LENGTH = 16;
55 private static final int DEST6_F_LENGTH = 16;
57 private static final int SRC6_F_OFFSET = 0;
58 private static final int DEST6_F_OFFSET = SRC6_F_OFFSET + SRC6_F_LENGTH;
60 public PCEPEndPointsObjectParser(final TlvHandlerRegistry tlvReg) {
65 public Object parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
67 throw new IllegalArgumentException("Array of bytes is mandatory");
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(new Ipv4CaseBuilder().setIpv4(b.build()).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(new Ipv6CaseBuilder().setIpv6(b.build()).build());
84 throw new PCEPDeserializerException("Wrong length of array of bytes.");
86 if (!builder.isProcessingRule()) {
87 LOG.debug("Processed bit not set on Endpoints OBJECT, ignoring it.");
88 return new UnknownObject(PCEPErrors.P_FLAG_NOT_SET, builder.build());
90 return builder.build();
94 public void addTlv(final EndpointsObjBuilder builder, final Tlv tlv) {
99 public byte[] serializeObject(final Object object) {
100 if (!(object instanceof EndpointsObj)) {
101 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed EndpointsObject.");
104 final EndpointsObj ePObj = (EndpointsObj) object;
106 final AddressFamily afi = ePObj.getAddressFamily();
108 if (afi instanceof Ipv4Case) {
109 final byte[] retBytes = new byte[SRC4_F_LENGTH + DEST4_F_LENGTH];
110 ByteArray.copyWhole(Ipv4Util.bytesForAddress((((Ipv4Case) afi).getIpv4()).getSourceIpv4Address()), retBytes, SRC4_F_OFFSET);
111 ByteArray.copyWhole(Ipv4Util.bytesForAddress((((Ipv4Case) afi).getIpv4()).getDestinationIpv4Address()), retBytes,
114 } else if (afi instanceof Ipv6Case) {
115 final byte[] retBytes = new byte[SRC6_F_LENGTH + DEST6_F_LENGTH];
116 ByteArray.copyWhole(Ipv6Util.bytesForAddress((((Ipv6Case) afi).getIpv6()).getSourceIpv6Address()), retBytes, SRC6_F_OFFSET);
117 ByteArray.copyWhole(Ipv6Util.bytesForAddress((((Ipv6Case) afi).getIpv6()).getDestinationIpv6Address()), retBytes,
121 throw new IllegalArgumentException("Wrong instance of NetworkAddress. Passed " + afi.getClass() + ". Needed IPv4");
126 public int getObjectType() {
131 public int getObjectClass() {
135 public int get6ObjectType() {
139 public int get6ObjectClass() {