Promote MessageRegistry to pcep-api
[bgpcep.git] / pcep / pcc-mock / src / main / java / org / opendaylight / protocol / pcep / pcc / mock / PCCEndPointIpv4ObjectParser.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  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.pcc.mock;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import io.netty.buffer.ByteBuf;
13 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
14 import org.opendaylight.protocol.pcep.parser.object.end.points.PCEPEndPointsIpv4ObjectParser;
15 import org.opendaylight.protocol.util.Ipv4Util;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.address.family.Ipv4CaseBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.address.family.ipv4._case.Ipv4Builder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.object.EndpointsObjBuilder;
21
22 public class PCCEndPointIpv4ObjectParser extends PCEPEndPointsIpv4ObjectParser {
23
24     @Override
25     public Object parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
26         checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
27         final EndpointsObjBuilder builder = new EndpointsObjBuilder();
28         if (bytes.readableBytes() != Ipv4Util.IP4_LENGTH * 2) {
29             throw new PCEPDeserializerException("Wrong length of array of bytes.");
30         }
31         builder.setIgnore(header.getIgnore());
32         builder.setProcessingRule(header.getProcessingRule());
33         final Ipv4Builder b = new Ipv4Builder();
34         b.setSourceIpv4Address(Ipv4Util.addressForByteBuf(bytes));
35         b.setDestinationIpv4Address(Ipv4Util.addressForByteBuf(bytes));
36         builder.setAddressFamily(new Ipv4CaseBuilder().setIpv4(b.build()).build());
37         return builder.build();
38     }
39 }