Add new revision for pcep types model
[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
9 package org.opendaylight.protocol.pcep.pcc.mock;
10
11 import com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
13 import org.opendaylight.protocol.pcep.parser.object.PCEPEndPointsIpv4ObjectParser;
14 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
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         Preconditions.checkArgument(bytes != null && bytes.isReadable(),
27                 "Array of bytes is mandatory. Can't be null or empty.");
28         final EndpointsObjBuilder builder = new EndpointsObjBuilder();
29         if (bytes.readableBytes() != Ipv4Util.IP4_LENGTH * 2) {
30             throw new PCEPDeserializerException("Wrong length of array of bytes.");
31         }
32         builder.setIgnore(header.isIgnore());
33         builder.setProcessingRule(header.isProcessingRule());
34         final Ipv4Builder b = new Ipv4Builder();
35         b.setSourceIpv4Address(Ipv4Util.noZoneAddressForByteBuf(bytes));
36         b.setDestinationIpv4Address(Ipv4Util.noZoneAddressForByteBuf(bytes));
37         builder.setAddressFamily(new Ipv4CaseBuilder().setIpv4(b.build()).build());
38         return builder.build();
39     }
40 }