Promote MessageRegistry to pcep-api
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / object / unreach / PCEPIpv4UnreachDestinationParser.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 com.google.common.collect.ImmutableSet;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.util.Set;
16 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
17 import org.opendaylight.protocol.pcep.spi.CommonObjectParser;
18 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
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;
26
27 public final class PCEPIpv4UnreachDestinationParser extends CommonObjectParser {
28     private static final int CLASS = 28;
29     private static final int TYPE = 1;
30
31     public PCEPIpv4UnreachDestinationParser() {
32         super(CLASS, TYPE);
33     }
34
35     public static void serializeObject(
36         final Boolean processing,
37         final Boolean ignore,
38         final Ipv4DestinationCase ipv4Case,
39         final ByteBuf buffer) {
40         final Set<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);
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         if (bytes.readableBytes() % Ipv4Util.IP4_LENGTH != 0) {
52             throw new PCEPDeserializerException("Wrong length of array of bytes.");
53         }
54         final var dest = ImmutableSet.<Ipv4AddressNoZone>builder();
55         while (bytes.isReadable()) {
56             dest.add(Ipv4Util.addressForByteBuf(bytes));
57         }
58         return new UnreachDestinationObjBuilder()
59             .setIgnore(header.getIgnore())
60             .setProcessingRule(header.getProcessingRule())
61             .setDestination(new Ipv4DestinationCaseBuilder().setDestinationIpv4Address(dest.build()).build())
62             .build();
63     }
64 }