Rework PCEPDeserializerException and move it into SPI
[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 org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
11 import org.opendaylight.protocol.pcep.spi.XROSubobjectHandlerRegistry;
12 import org.opendaylight.protocol.util.ByteArray;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.Xro;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.Xro.Flags;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.XroBuilder;
18
19 import com.google.common.primitives.UnsignedBytes;
20
21 /**
22  * Parser for {@link Xro}
23  */
24 public final class PCEPExcludeRouteObjectParser extends AbstractXROWithSubobjectsParser {
25
26         public static final int CLASS = 7;
27
28         public static final int TYPE = 1;
29
30         private static final int FLAGS_OFFSET = 3;
31
32         public PCEPExcludeRouteObjectParser(final XROSubobjectHandlerRegistry registry) {
33                 super(registry);
34         }
35
36         @Override
37         public Xro parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
38                 if (bytes == null || bytes.length == 0) {
39                         throw new IllegalArgumentException("Byte array is mandatory. Can't be null or empty.");
40                 }
41                 final XroBuilder builder = new XroBuilder();
42                 builder.setIgnore(header.isIgnore());
43                 builder.setProcessingRule(header.isProcessingRule());
44                 builder.setFlags(new Flags(UnsignedBytes.toInt(bytes[FLAGS_OFFSET]) != 0));
45                 builder.setSubobjects(parseSubobjects(ByteArray.cutBytes(bytes, FLAGS_OFFSET + 1)));
46                 return builder.build();
47         }
48
49         @Override
50         public byte[] serializeObject(final Object object) {
51                 if (!(object instanceof Xro)) {
52                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed ExcludeRouteObject.");
53                 }
54                 final Xro obj = (Xro) object;
55                 assert !(obj.getSubobjects().isEmpty()) : "Empty Excluded Route Object.";
56                 final byte[] bytes = serializeSubobject(obj.getSubobjects());
57                 final byte[] result = new byte[FLAGS_OFFSET + 1 + bytes.length];
58                 if (obj.getFlags().isFail()) {
59                         result[FLAGS_OFFSET] = 1;
60                 }
61                 ByteArray.copyWhole(bytes, result, FLAGS_OFFSET + 1);
62                 return result;
63         }
64
65         @Override
66         public int getObjectType() {
67                 return TYPE;
68         }
69
70         @Override
71         public int getObjectClass() {
72                 return CLASS;
73         }
74 }