Merge "Change the type of some leafs to uint32"
[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.spi.EROSubobjectHandlerRegistry;
13 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
14 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.include.route.object.Iro;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.include.route.object.IroBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.include.route.object.iro.Subobject;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.include.route.object.iro.SubobjectBuilder;
21
22 import com.google.common.collect.Lists;
23
24 /**
25  * Parser for {@link Iro}
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 Iro 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
43                 final IroBuilder builder = new IroBuilder();
44
45                 builder.setIgnore(header.isIgnore());
46                 builder.setProcessingRule(header.isProcessingRule());
47
48                 final List<Subobject> subs = Lists.newArrayList();
49                 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject s : parseSubobjects(bytes)) {
50                         subs.add(new SubobjectBuilder().setSubobjectType(s.getSubobjectType()).build());
51                 }
52                 builder.setSubobject(subs);
53                 return builder.build();
54         }
55
56         @Override
57         public byte[] serializeObject(final Object object) {
58                 if (!(object instanceof Iro)) {
59                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed IncludeRouteObject.");
60                 }
61                 final Iro iro = ((Iro) object);
62
63                 assert !(iro.getSubobject().isEmpty()) : "Empty Include Route Object.";
64
65                 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject> subs = Lists.newArrayList();
66
67                 for (final Subobject s : iro.getSubobject()) {
68                         subs.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder().setLoose(
69                                         false).setSubobjectType(s.getSubobjectType()).build());
70                 }
71                 return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), serializeSubobject(subs));
72         }
73
74         @Override
75         public int getObjectType() {
76                 return TYPE;
77         }
78
79         @Override
80         public int getObjectClass() {
81                 return CLASS;
82         }
83 }