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