Move pcep base parser Activator to its own bundle
[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.rev131005.Object;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv4CaseBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.ipv4._case.Ipv4Builder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.EndpointsObjBuilder;
21
22 public class PCCEndPointIpv4ObjectParser extends PCEPEndPointsIpv4ObjectParser {
23
24     @Override
25     public Object parseObject(ObjectHeader header, ByteBuf bytes) throws PCEPDeserializerException {
26         Preconditions.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.isIgnore());
32         builder.setProcessingRule(header.isProcessingRule());
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 }