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.pcep.spi.PCEPDeserializerException;
12 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
13 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
14 import org.opendaylight.protocol.pcep.spi.UnknownObject;
15 import org.opendaylight.protocol.util.ByteArray;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.AddressFamily;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv4Case;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv4CaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.ipv4._case.Ipv4Builder;
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;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * Parser for IPv4 {@link EndpointsObj}
31 public class PCEPEndPointsIpv4ObjectParser extends AbstractObjectWithTlvsParser<EndpointsObjBuilder> {
33 private static final Logger LOG = LoggerFactory.getLogger(PCEPEndPointsIpv4ObjectParser.class);
35 public static final int CLASS = 4;
36 public static final int TYPE = 1;
39 * fields lengths and offsets for IPv4 in bytes
41 private static final int SRC4_F_LENGTH = 4;
42 private static final int DEST4_F_LENGTH = 4;
44 private static final int SRC4_F_OFFSET = 0;
45 private static final int DEST4_F_OFFSET = SRC4_F_OFFSET + SRC4_F_LENGTH;
47 public PCEPEndPointsIpv4ObjectParser(final TlvHandlerRegistry tlvReg) {
52 public Object parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
54 throw new IllegalArgumentException("Array of bytes is mandatory");
56 final EndpointsObjBuilder builder = new EndpointsObjBuilder();
57 builder.setIgnore(header.isIgnore());
58 builder.setProcessingRule(header.isProcessingRule());
59 if (!builder.isProcessingRule()) {
60 LOG.debug("Processed bit not set on Endpoints OBJECT, ignoring it.");
61 return new UnknownObject(PCEPErrors.P_FLAG_NOT_SET, builder.build());
63 if (bytes.length != SRC4_F_LENGTH + DEST4_F_LENGTH) {
64 throw new PCEPDeserializerException("Wrong length of array of bytes.");
66 final Ipv4Builder b = new Ipv4Builder();
67 b.setSourceIpv4Address(Ipv4Util.addressForBytes(ByteArray.subByte(bytes, SRC4_F_OFFSET, SRC4_F_LENGTH)));
68 b.setDestinationIpv4Address((Ipv4Util.addressForBytes(ByteArray.subByte(bytes, DEST4_F_OFFSET, DEST4_F_LENGTH))));
69 builder.setAddressFamily(new Ipv4CaseBuilder().setIpv4(b.build()).build());
70 return builder.build();
74 public void addTlv(final EndpointsObjBuilder builder, final Tlv tlv) {
79 public byte[] serializeObject(final Object object) {
80 if (!(object instanceof EndpointsObj)) {
81 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed EndpointsObject.");
83 final EndpointsObj ePObj = (EndpointsObj) object;
85 final AddressFamily afi = ePObj.getAddressFamily();
87 if (!(afi instanceof Ipv4Case)) {
88 throw new IllegalArgumentException("Wrong instance of NetworkAddress. Passed " + afi.getClass() + ". Needed IPv4");
90 final byte[] retBytes = new byte[SRC4_F_LENGTH + DEST4_F_LENGTH];
91 ByteArray.copyWhole(Ipv4Util.bytesForAddress((((Ipv4Case) afi).getIpv4()).getSourceIpv4Address()), retBytes, SRC4_F_OFFSET);
92 ByteArray.copyWhole(Ipv4Util.bytesForAddress((((Ipv4Case) afi).getIpv4()).getDestinationIpv4Address()), retBytes, DEST4_F_OFFSET);
97 public int getObjectType() {
102 public int getObjectClass() {