2 * Copyright (c) 2018 AT&T Intellectual Property. 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.parser.object.end.points;
10 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv6Address;
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.util.ArrayList;
16 import java.util.List;
17 import org.opendaylight.protocol.pcep.spi.CommonObjectParser;
18 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
19 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
20 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
21 import org.opendaylight.protocol.pcep.spi.UnknownObject;
22 import org.opendaylight.protocol.util.Ipv6Util;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.P2mpLeaves;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.address.family.P2mpIpv6CaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.address.family.p2mp.ipv6._case.P2mpIpv6;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.address.family.p2mp.ipv6._case.P2mpIpv6Builder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.object.EndpointsObjBuilder;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class PCEPP2MPEndPointsIpv6ObjectParser extends CommonObjectParser {
35 private static final int CLASS = 4;
36 private static final int TYPE = 4;
37 private static final int LEAF_TYPE_SIZE = 32;
38 private static final Logger LOG = LoggerFactory.getLogger(PCEPP2MPEndPointsIpv6ObjectParser.class);
40 public PCEPP2MPEndPointsIpv6ObjectParser() {
44 public static void serializeObject(
45 final Boolean processing,
47 final P2mpIpv6 p2mpIpv6,
48 final ByteBuf buffer) {
49 final List<Ipv6AddressNoZone> dest = p2mpIpv6.getDestinationIpv6Address();
50 Preconditions.checkArgument(dest != null, "DestinationIpv6Address is mandatory.");
52 Unpooled.buffer(LEAF_TYPE_SIZE + Ipv6Util.IPV6_LENGTH + Ipv6Util.IPV6_LENGTH * dest.size());
53 Preconditions.checkArgument(p2mpIpv6.getSourceIpv6Address() != null,
54 "SourceIpv6Address is mandatory.");
55 body.writeInt(p2mpIpv6.getP2mpLeaves().getIntValue());
56 writeIpv6Address(p2mpIpv6.getSourceIpv6Address(), body);
57 dest.forEach(ipv6 -> writeIpv6Address(ipv6, body));
58 ObjectUtil.formatSubobject(TYPE, CLASS, processing, ignore, body, buffer);
62 public Object parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
63 Preconditions.checkArgument(bytes != null && bytes.isReadable(),
64 "Array of bytes is mandatory. Can't be null or empty.");
65 if (!header.isProcessingRule()) {
66 LOG.debug("Processed bit not set on Endpoints OBJECT, ignoring it.");
67 return new UnknownObject(PCEPErrors.P_FLAG_NOT_SET, new EndpointsObjBuilder().build());
69 if (bytes.readableBytes() % Ipv6Util.IPV6_LENGTH != 4) {
70 throw new PCEPDeserializerException("Wrong length of array of bytes.");
73 final P2mpIpv6Builder p2mpIpv6Builder = new P2mpIpv6Builder()
74 .setP2mpLeaves(P2mpLeaves.forValue(bytes.readInt()))
75 .setSourceIpv6Address(Ipv6Util.noZoneAddressForByteBuf(bytes));
76 List<Ipv6AddressNoZone> dest = new ArrayList<>();
77 while (bytes.isReadable()) {
78 dest.add(Ipv6Util.noZoneAddressForByteBuf(bytes));
80 p2mpIpv6Builder.setDestinationIpv6Address(dest);
81 final EndpointsObjBuilder builder = new EndpointsObjBuilder()
82 .setIgnore(header.isIgnore())
83 .setProcessingRule(header.isProcessingRule())
84 .setAddressFamily(new P2mpIpv6CaseBuilder().setP2mpIpv6(p2mpIpv6Builder.build()).build());
85 return builder.build();