0c8be58cb3aa3046439a410fe6a9aeec11dcfa98
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / object / unreach / PCEPIpv6UnreachDestinationParser.java
1 /*
2  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.pcep.parser.object.unreach;
9
10 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv6Address;
11
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.util.Ipv6Util;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.unreach.destination.object.UnreachDestinationObj;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.unreach.destination.object.UnreachDestinationObjBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.unreach.destination.object.unreach.destination.obj.destination.Ipv6DestinationCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.unreach.destination.object.unreach.destination.obj.destination.Ipv6DestinationCaseBuilder;
27
28 public final class PCEPIpv6UnreachDestinationParser extends CommonObjectParser {
29     private static final int CLASS = 28;
30     private static final int TYPE = 2;
31
32     public PCEPIpv6UnreachDestinationParser() {
33         super(CLASS, TYPE);
34     }
35
36     public static void serializeObject(
37         final Boolean processing,
38         final Boolean ignore,
39         final Ipv6DestinationCase ipv6Case,
40         final ByteBuf buffer) {
41         final List<Ipv6AddressNoZone> dest = ipv6Case.getDestinationIpv6Address();
42         Preconditions.checkArgument(dest != null, "Destinationipv6Address is mandatory.");
43         final ByteBuf body = Unpooled.buffer(Ipv6Util.IPV6_LENGTH * dest.size());
44         dest.forEach(ipv6 -> writeIpv6Address(ipv6, body));
45         ObjectUtil.formatSubobject(TYPE, CLASS, processing, ignore, body, buffer);
46     }
47
48     @Override
49     public UnreachDestinationObj parseObject(final ObjectHeader header, final ByteBuf bytes)
50         throws PCEPDeserializerException {
51         Preconditions.checkArgument(bytes != null && bytes.isReadable(),
52             "Array of bytes is mandatory. Can't be null or empty.");
53         final UnreachDestinationObjBuilder builder = new UnreachDestinationObjBuilder();
54         if (bytes.readableBytes() % Ipv6Util.IPV6_LENGTH != 0) {
55             throw new PCEPDeserializerException("Wrong length of array of bytes.");
56         }
57         builder.setIgnore(header.isIgnore());
58         builder.setProcessingRule(header.isProcessingRule());
59         List<Ipv6AddressNoZone> dest = new ArrayList<>();
60         while (bytes.isReadable()) {
61             dest.add(Ipv6Util.noZoneAddressForByteBuf(bytes));
62         }
63         builder.setDestination(new Ipv6DestinationCaseBuilder().setDestinationIpv6Address(dest).build());
64         return builder.build();
65     }
66 }