Rework PCEPDeserializerException and move it into SPI
[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 org.opendaylight.protocol.pcep.spi.EROSubobjectHandlerRegistry;
11 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.Ero;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.EroBuilder;
16
17 /**
18  * Parser for {@link Ero}
19  */
20 public class PCEPExplicitRouteObjectParser extends AbstractEROWithSubobjectsParser {
21
22         public static final int CLASS = 7;
23
24         public static final int TYPE = 1;
25
26         public PCEPExplicitRouteObjectParser(final EROSubobjectHandlerRegistry subobjReg) {
27                 super(subobjReg);
28         }
29
30         @Override
31         public Ero parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
32                 if (bytes == null || bytes.length == 0) {
33                         throw new IllegalArgumentException("Byte array is mandatory. Can't be null or empty.");
34                 }
35                 final EroBuilder builder = new EroBuilder();
36                 builder.setIgnore(header.isIgnore());
37                 builder.setProcessingRule(header.isProcessingRule());
38                 builder.setSubobjects(parseSubobjects(bytes));
39                 return builder.build();
40         }
41
42         @Override
43         public byte[] serializeObject(final Object object) {
44                 if (!(object instanceof Ero)) {
45                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass()
46                                         + ". Needed ExplicitRouteObject.");
47                 }
48                 final Ero ero = ((Ero) object);
49
50                 assert !(ero.getSubobjects().isEmpty()) : "Empty Explicit Route Object.";
51
52                 return serializeSubobject(ero.getSubobjects());
53         }
54
55         @Override
56         public int getObjectType() {
57                 return TYPE;
58         }
59
60         @Override
61         public int getObjectClass() {
62                 return CLASS;
63         }
64 }