Clean pcep/base-parser code
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / object / PCEPExplicitRouteObjectParser.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;
9
10 import com.google.common.base.Preconditions;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14
15 import org.opendaylight.protocol.pcep.spi.EROSubobjectRegistry;
16 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
17 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.Ero;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.EroBuilder;
22
23 /**
24  * Parser for {@link Ero}.
25  */
26 public final class PCEPExplicitRouteObjectParser extends AbstractEROWithSubobjectsParser {
27
28     private static final int CLASS = 7;
29     private static final int TYPE = 1;
30
31     public PCEPExplicitRouteObjectParser(final EROSubobjectRegistry subobjReg) {
32         super(subobjReg, CLASS, TYPE);
33     }
34
35     @Override
36     public Ero parseObject(final ObjectHeader header, final ByteBuf buffer) throws PCEPDeserializerException {
37         // Explicit approval of empty ERO
38         Preconditions.checkArgument(buffer != null, "Array of bytes is mandatory. Can't be null.");
39         final EroBuilder builder = new EroBuilder()
40                 .setIgnore(header.isIgnore())
41                 .setProcessingRule(header.isProcessingRule())
42                 .setSubobject(parseSubobjects(buffer));
43         return builder.build();
44     }
45
46     @Override
47     public void serializeObject(final Object object, final ByteBuf buffer) {
48         Preconditions.checkArgument(object instanceof Ero,
49             "Wrong instance of PCEPObject. Passed %s. Needed EroObject.", object.getClass());
50         final Ero ero = ((Ero) object);
51         final ByteBuf body = Unpooled.buffer();
52         serializeSubobject(ero.getSubobject(), body);
53         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
54     }
55 }