BUG-47 : switched subobjects to generated source code.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPIncludeRouteObjectParser.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 java.util.List;
11
12 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
13 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
14 import org.opendaylight.protocol.pcep.spi.EROSubobjectHandlerRegistry;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.IncludeRouteObject;
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.include.route.object.Subobjects;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.include.route.object.SubobjectsBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.attributes.IncludeRouteBuilder;
21
22 import com.google.common.collect.Lists;
23
24 /**
25  * Parser for {@link IncludeRouteObject}
26  */
27 public class PCEPIncludeRouteObjectParser extends AbstractEROWithSubobjectsParser {
28
29         public static final int CLASS = 10;
30
31         public static final int TYPE = 1;
32
33         public PCEPIncludeRouteObjectParser(final EROSubobjectHandlerRegistry subobjReg) {
34                 super(subobjReg);
35         }
36
37         @Override
38         public IncludeRouteObject 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
44                 final IncludeRouteBuilder builder = new IncludeRouteBuilder();
45
46                 builder.setIgnore(header.isIgnore());
47                 builder.setProcessingRule(header.isProcessingRule());
48
49                 final List<Subobjects> subs = Lists.newArrayList();
50                 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.Subobjects s : parseSubobjects(bytes)) {
51                         subs.add(new SubobjectsBuilder().setSubobjectType(s.getSubobjectType()).build());
52                 }
53                 builder.setSubobjects(subs);
54                 return builder.build();
55         }
56
57         @Override
58         public byte[] serializeObject(final Object object) {
59                 if (!(object instanceof IncludeRouteObject)) {
60                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed IncludeRouteObject.");
61                 }
62                 final IncludeRouteObject iro = ((IncludeRouteObject) object);
63
64                 assert !(iro.getSubobjects().isEmpty()) : "Empty Include Route Object.";
65
66                 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.Subobjects> subs = Lists.newArrayList();
67
68                 for (final Subobjects s : iro.getSubobjects()) {
69                         subs.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.SubobjectsBuilder().setLoose(
70                                         false).setSubobjectType(s.getSubobjectType()).build());
71                 }
72
73                 return serializeSubobject(subs);
74         }
75
76         @Override
77         public int getObjectType() {
78                 return TYPE;
79         }
80
81         @Override
82         public int getObjectClass() {
83                 return CLASS;
84         }
85 }