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