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