0bc2280446ca2da6411e9d3f8b0fd8418df3ce7e
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / object / bnc / BranchNodeListObjectParser.java
1 /*
2  * Copyright (c) 2018 AT&T Intellectual Property. 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.bnc;
9
10 import com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import org.opendaylight.protocol.pcep.parser.object.AbstractEROWithSubobjectsParser;
14 import org.opendaylight.protocol.pcep.spi.EROSubobjectRegistry;
15 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
16 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.branch.node.object.BranchNodeList;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.branch.node.object.BranchNodeListBuilder;
21
22 public final class BranchNodeListObjectParser extends AbstractEROWithSubobjectsParser {
23
24     private static final int CLASS = 31;
25     private static final int TYPE = 1;
26
27     public BranchNodeListObjectParser(final EROSubobjectRegistry subobjReg) {
28         super(subobjReg, CLASS, TYPE);
29     }
30
31     @Override
32     public BranchNodeList parseObject(final ObjectHeader header, final ByteBuf bytes)
33         throws PCEPDeserializerException {
34         Preconditions.checkArgument(bytes != null && bytes.isReadable(),
35             "Array of bytes is mandatory. Can't be null or empty.");
36         return new BranchNodeListBuilder()
37             .setIgnore(header.getIgnore())
38             .setProcessingRule(header.getProcessingRule())
39             .setSubobject(BNCUtil.toBncSubobject(parseSubobjects(bytes)))
40             .build();
41     }
42
43     @Override
44     public void serializeObject(final Object object, final ByteBuf buffer) {
45         Preconditions.checkArgument(object instanceof BranchNodeList,
46             "Wrong instance of PCEPObject. Passed %s. Needed BranchNodeList.", object.getClass());
47         final BranchNodeList nbnc = (BranchNodeList) object;
48         final ByteBuf body = Unpooled.buffer();
49         serializeSubobject(BNCUtil.toIroSubject(nbnc.getSubobject()), body);
50         ObjectUtil.formatSubobject(TYPE, CLASS, object.getProcessingRule(), object.getIgnore(), body, buffer);
51     }
52 }