bfee4bbbd321f9e6e4a92b2a538b2e68409aa617
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPReportedRouteObjectParser.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.RROSubobjectHandlerRegistry;
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.reported.route.object.Rro;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.RroBuilder;
16
17 /**
18  * Parser for {@link Rro}
19  */
20 public class PCEPReportedRouteObjectParser extends AbstractRROWithSubobjectsParser {
21
22         public static final int CLASS = 8;
23
24         public static final int TYPE = 1;
25
26         public PCEPReportedRouteObjectParser(final RROSubobjectHandlerRegistry subobjReg) {
27                 super(subobjReg);
28         }
29
30         @Override
31         public Rro 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 RroBuilder builder = new RroBuilder();
36
37                 builder.setIgnore(header.isIgnore());
38                 builder.setProcessingRule(header.isProcessingRule());
39                 builder.setSubobjects(parseSubobjects(bytes));
40                 return builder.build();
41         }
42
43         @Override
44         public byte[] serializeObject(final Object object) {
45                 if (!(object instanceof Rro)) {
46                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass()
47                                         + ". Needed ReportedRouteObject.");
48                 }
49                 final Rro obj = (Rro) object;
50                 assert !(obj.getSubobjects().isEmpty()) : "Empty Reported Route Object.";
51                 return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(),
52                                 serializeSubobject(obj.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 }