Removing deprecated getType() method from serializers.
[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.ObjectUtil;
11 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
12 import org.opendaylight.protocol.pcep.spi.XROSubobjectRegistry;
13 import org.opendaylight.protocol.util.ByteArray;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.Xro;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.Xro.Flags;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.XroBuilder;
19
20 import com.google.common.primitives.UnsignedBytes;
21
22 /**
23  * Parser for {@link Xro}
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 XROSubobjectRegistry registry) {
34                 super(registry);
35         }
36
37         @Override
38         public Xro parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
39                 if (bytes == null || bytes.length == 0) {
40                         throw new IllegalArgumentException("Byte array is mandatory. Can't be null or empty.");
41                 }
42                 final XroBuilder builder = new XroBuilder();
43                 builder.setIgnore(header.isIgnore());
44                 builder.setProcessingRule(header.isProcessingRule());
45                 builder.setFlags(new Flags(UnsignedBytes.toInt(bytes[FLAGS_OFFSET]) != 0));
46                 builder.setSubobject(parseSubobjects(ByteArray.cutBytes(bytes, FLAGS_OFFSET + 1)));
47                 return builder.build();
48         }
49
50         @Override
51         public byte[] serializeObject(final Object object) {
52                 if (!(object instanceof Xro)) {
53                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed ExcludeRouteObject.");
54                 }
55                 final Xro obj = (Xro) object;
56                 assert !(obj.getSubobject().isEmpty()) : "Empty Excluded Route Object.";
57                 final byte[] bytes = serializeSubobject(obj.getSubobject());
58                 final byte[] result = new byte[FLAGS_OFFSET + 1 + bytes.length];
59                 if (obj.getFlags().isFail()) {
60                         result[FLAGS_OFFSET] = 1;
61                 }
62                 ByteArray.copyWhole(bytes, result, FLAGS_OFFSET + 1);
63                 return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), result);
64         }
65 }