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