Clean pcep/base-parser code
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / object / AbstractPccIdReqObjectParser.java
1 /*
2  * Copyright (c) 2014 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.parser.object;
10
11 import com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import org.opendaylight.protocol.pcep.spi.CommonObjectParser;
15 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
16 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
17 import org.opendaylight.protocol.util.ByteBufWriteUtil;
18 import org.opendaylight.protocol.util.Ipv4Util;
19 import org.opendaylight.protocol.util.Ipv6Util;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcc.id.req.object.PccIdReq;
22
23 public abstract class AbstractPccIdReqObjectParser extends CommonObjectParser implements ObjectSerializer {
24     private static final int CLASS = 20;
25
26     public AbstractPccIdReqObjectParser(final int objectType) {
27         super(CLASS, objectType);
28     }
29
30     @Override
31     public void serializeObject(final Object object, final ByteBuf buffer) {
32         Preconditions.checkArgument(object instanceof PccIdReq,
33                 "Wrong instance of PCEPObject. Passed %s. Needed PccIdReqObject.", object.getClass());
34         final PccIdReq pccIdReq = (PccIdReq) object;
35         if (pccIdReq.getIpAddress().getIpv4AddressNoZone() != null) {
36             final ByteBuf body = Unpooled.buffer(Ipv4Util.IP4_LENGTH);
37             ByteBufWriteUtil.writeIpv4Address(pccIdReq.getIpAddress().getIpv4AddressNoZone(), body);
38             ObjectUtil.formatSubobject(getObjectType(), getObjectClass(), object.isProcessingRule(), object.isIgnore(),
39                     body, buffer);
40         } else if (pccIdReq.getIpAddress().getIpv6AddressNoZone() != null) {
41             final ByteBuf body = Unpooled.buffer(Ipv6Util.IPV6_LENGTH);
42             ByteBufWriteUtil.writeIpv6Address(pccIdReq.getIpAddress().getIpv6AddressNoZone(), body);
43             ObjectUtil.formatSubobject(getObjectType(), getObjectClass(), object.isProcessingRule(), object.isIgnore(),
44                     body, buffer);
45         }
46     }
47 }