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.Ipv6Util;
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.TlvRegistry;
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.endpoints.AddressFamily;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv6Case;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv6CaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.ipv6._case.Ipv6Builder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.EndpointsObj;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.EndpointsObjBuilder;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * Parser for IPv6 {@link EndpointsObj}
32 public class PCEPEndPointsIpv6ObjectParser extends AbstractObjectWithTlvsParser<EndpointsObjBuilder> {
34 private static final Logger LOG = LoggerFactory.getLogger(PCEPEndPointsIpv4ObjectParser.class);
36 public static final int CLASS = 4;
37 public static final int TYPE = 2;
40 * fields lengths and offsets for IPv4 in bytes
42 private static final int SRC6_F_LENGTH = 16;
43 private static final int DEST6_F_LENGTH = 16;
45 private static final int SRC6_F_OFFSET = 0;
46 private static final int DEST6_F_OFFSET = SRC6_F_OFFSET + SRC6_F_LENGTH;
48 public PCEPEndPointsIpv6ObjectParser(final TlvRegistry tlvReg) {
53 public Object parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
55 throw new IllegalArgumentException("Array of bytes is mandatory");
57 final EndpointsObjBuilder builder = new EndpointsObjBuilder();
58 builder.setIgnore(header.isIgnore());
59 builder.setProcessingRule(header.isProcessingRule());
60 if (!builder.isProcessingRule()) {
61 LOG.debug("Processed bit not set on Endpoints OBJECT, ignoring it.");
62 return new UnknownObject(PCEPErrors.P_FLAG_NOT_SET, builder.build());
64 if (bytes.length != SRC6_F_LENGTH + DEST6_F_LENGTH) {
65 throw new PCEPDeserializerException("Wrong length of array of bytes.");
67 final Ipv6Builder b = new Ipv6Builder();
68 b.setSourceIpv6Address(Ipv6Util.addressForBytes(ByteArray.subByte(bytes, SRC6_F_OFFSET, SRC6_F_LENGTH)));
69 b.setDestinationIpv6Address((Ipv6Util.addressForBytes(ByteArray.subByte(bytes, DEST6_F_OFFSET, DEST6_F_LENGTH))));
70 builder.setAddressFamily(new Ipv6CaseBuilder().setIpv6(b.build()).build());
71 return builder.build();
75 public byte[] serializeObject(final Object object) {
76 if (!(object instanceof EndpointsObj)) {
77 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed EndpointsObject.");
79 final EndpointsObj ePObj = (EndpointsObj) object;
81 final AddressFamily afi = ePObj.getAddressFamily();
83 if (!(afi instanceof Ipv6Case)) {
84 throw new IllegalArgumentException("Wrong instance of NetworkAddress. Passed " + afi.getClass() + ". Needed IPv4");
86 final byte[] retBytes = new byte[SRC6_F_LENGTH + DEST6_F_LENGTH];
87 ByteArray.copyWhole(Ipv6Util.bytesForAddress((((Ipv6Case) afi).getIpv6()).getSourceIpv6Address()), retBytes, SRC6_F_OFFSET);
88 ByteArray.copyWhole(Ipv6Util.bytesForAddress((((Ipv6Case) afi).getIpv6()).getDestinationIpv6Address()), retBytes, DEST6_F_OFFSET);
89 return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
93 public int getObjectType() {
98 public int getObjectClass() {