2ca3d85d451375017543aea5de1c9552209e4cb1
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPExcludeRouteObjectParser.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.ObjectUtil;
13 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
14 import org.opendaylight.protocol.pcep.spi.XROSubobjectRegistry;
15 import org.opendaylight.protocol.util.ByteArray;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.Xro;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.Xro.Flags;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.XroBuilder;
21
22 import com.google.common.base.Preconditions;
23
24 /**
25  * Parser for {@link Xro}
26  */
27 public final class PCEPExcludeRouteObjectParser extends AbstractXROWithSubobjectsParser {
28
29         public static final int CLASS = 7;
30
31         public static final int TYPE = 1;
32
33         private static final int FLAGS_OFFSET = 3;
34
35         public PCEPExcludeRouteObjectParser(final XROSubobjectRegistry registry) {
36                 super(registry);
37         }
38
39         @Override
40         public Xro parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
41                 Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
42                 final XroBuilder builder = new XroBuilder();
43                 builder.setIgnore(header.isIgnore());
44                 builder.setProcessingRule(header.isProcessingRule());
45                 bytes.readerIndex(bytes.readerIndex() + FLAGS_OFFSET);
46                 builder.setFlags(new Flags(bytes.readBoolean()));
47                 //FIXME: switch to ByteBuf
48                 builder.setSubobject(parseSubobjects(ByteArray.readAllBytes(bytes)));
49                 return builder.build();
50         }
51
52         @Override
53         public byte[] serializeObject(final Object object) {
54                 if (!(object instanceof Xro)) {
55                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed ExcludeRouteObject.");
56                 }
57                 final Xro obj = (Xro) object;
58                 assert !(obj.getSubobject().isEmpty()) : "Empty Excluded Route Object.";
59                 final byte[] bytes = serializeSubobject(obj.getSubobject());
60                 final byte[] result = new byte[FLAGS_OFFSET + 1 + bytes.length];
61                 if (obj.getFlags().isFail()) {
62                         result[FLAGS_OFFSET] = 1;
63                 }
64                 ByteArray.copyWhole(bytes, result, FLAGS_OFFSET + 1);
65                 return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), result);
66         }
67 }