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