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