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.unreach;
10 import static com.google.common.base.Preconditions.checkArgument;
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.Ipv4Util;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
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.Ipv4DestinationCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.unreach.destination.object.unreach.destination.obj.destination.Ipv4DestinationCaseBuilder;
27 public final class PCEPIpv4UnreachDestinationParser extends CommonObjectParser {
28 private static final int CLASS = 28;
29 private static final int TYPE = 1;
31 public PCEPIpv4UnreachDestinationParser() {
35 public static void serializeObject(
36 final Boolean processing,
38 final Ipv4DestinationCase ipv4Case,
39 final ByteBuf buffer) {
40 final List<Ipv4AddressNoZone> dest = ipv4Case.getDestinationIpv4Address();
41 checkArgument(dest != null, "DestinationIpv4Address is mandatory.");
42 final ByteBuf body = Unpooled.buffer(Ipv4Util.IP4_LENGTH * dest.size());
43 dest.forEach(ipv4 -> Ipv4Util.writeIpv4Address(ipv4, body));
44 ObjectUtil.formatSubobject(TYPE, CLASS, processing, ignore, body, buffer);
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() % Ipv4Util.IP4_LENGTH != 0) {
53 throw new PCEPDeserializerException("Wrong length of array of bytes.");
55 builder.setIgnore(header.isIgnore());
56 builder.setProcessingRule(header.isProcessingRule());
57 List<Ipv4AddressNoZone> dest = new ArrayList<>();
58 while (bytes.isReadable()) {
59 dest.add(Ipv4Util.noZoneAddressForByteBuf(bytes));
61 builder.setDestination(new Ipv4DestinationCaseBuilder().setDestinationIpv4Address(dest).build());
62 return builder.build();