Clean pcep/base-parser code
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / object / end / points / PCEPEndPointsIpv4ObjectParser.java
1 /*
2  * Copyright (c) 2013 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.parser.object.end.points;
9
10 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv4Address;
11
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.opendaylight.protocol.pcep.spi.CommonObjectParser;
16 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
17 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
18 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
19 import org.opendaylight.protocol.pcep.spi.UnknownObject;
20 import org.opendaylight.protocol.util.Ipv4Util;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.address.family.Ipv4CaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.address.family.ipv4._case.Ipv4;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.address.family.ipv4._case.Ipv4Builder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.object.EndpointsObj;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.object.EndpointsObjBuilder;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * Parser for IPv4 {@link EndpointsObj}.
33  */
34 public class PCEPEndPointsIpv4ObjectParser extends CommonObjectParser {
35
36     private static final int CLASS = 4;
37     private static final int TYPE = 1;
38     private static final Logger LOG = LoggerFactory.getLogger(PCEPEndPointsIpv4ObjectParser.class);
39
40     public PCEPEndPointsIpv4ObjectParser() {
41         super(CLASS, TYPE);
42     }
43
44     public static void serializeObject(
45         final Boolean processing,
46         final Boolean ignore,
47         final Ipv4 ipv4,
48         final ByteBuf buffer) {
49         final ByteBuf body = Unpooled.buffer(Ipv4Util.IP4_LENGTH + Ipv4Util.IP4_LENGTH);
50         Preconditions.checkArgument(ipv4.getSourceIpv4Address() != null,
51             "SourceIpv4Address is mandatory.");
52         writeIpv4Address(ipv4.getSourceIpv4Address(), body);
53         Preconditions.checkArgument(ipv4.getDestinationIpv4Address() != null,
54             "DestinationIpv4Address is mandatory.");
55         writeIpv4Address(ipv4.getDestinationIpv4Address(), body);
56         ObjectUtil.formatSubobject(TYPE, CLASS, processing, ignore, body, buffer);
57     }
58
59     @Override
60     public Object parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
61         Preconditions.checkArgument(bytes != null && bytes.isReadable(),
62             "Array of bytes is mandatory. Can't be null or empty.");
63         final EndpointsObjBuilder builder = new EndpointsObjBuilder();
64         if (!header.isProcessingRule()) {
65             LOG.debug("Processed bit not set on Endpoints OBJECT, ignoring it.");
66             return new UnknownObject(PCEPErrors.P_FLAG_NOT_SET, builder.build());
67         }
68         if (bytes.readableBytes() != Ipv4Util.IP4_LENGTH * 2) {
69             throw new PCEPDeserializerException("Wrong length of array of bytes.");
70         }
71         final Ipv4Builder ipv4bldr = new Ipv4Builder()
72                 .setSourceIpv4Address(Ipv4Util.noZoneAddressForByteBuf(bytes))
73                 .setDestinationIpv4Address(Ipv4Util.noZoneAddressForByteBuf(bytes));
74         builder.setIgnore(header.isIgnore())
75                 .setProcessingRule(header.isProcessingRule())
76                 .setAddressFamily(new Ipv4CaseBuilder().setIpv4(ipv4bldr.build()).build());
77         return builder.build();
78     }
79 }