Merge "BUG-223: Support having multiple RIB instances"
[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.PCEPDeserializerException;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.include.route.object.Iro;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.include.route.object.IroBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.include.route.object.iro.Subobjects;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.include.route.object.iro.SubobjectsBuilder;
20
21 import com.google.common.collect.Lists;
22
23 /**
24  * Parser for {@link Iro}
25  */
26 public class PCEPIncludeRouteObjectParser extends AbstractEROWithSubobjectsParser {
27
28         public static final int CLASS = 10;
29
30         public static final int TYPE = 1;
31
32         public PCEPIncludeRouteObjectParser(final EROSubobjectHandlerRegistry subobjReg) {
33                 super(subobjReg);
34         }
35
36         @Override
37         public Iro parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
38                 if (bytes == null || bytes.length == 0) {
39                         throw new IllegalArgumentException("Byte array is mandatory. Can't be null or empty.");
40                 }
41
42                 final IroBuilder builder = new IroBuilder();
43
44                 builder.setIgnore(header.isIgnore());
45                 builder.setProcessingRule(header.isProcessingRule());
46
47                 final List<Subobjects> subs = Lists.newArrayList();
48                 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobjects s : parseSubobjects(bytes)) {
49                         subs.add(new SubobjectsBuilder().setSubobjectType(s.getSubobjectType()).build());
50                 }
51                 builder.setSubobjects(subs);
52                 return builder.build();
53         }
54
55         @Override
56         public byte[] serializeObject(final Object object) {
57                 if (!(object instanceof Iro)) {
58                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed IncludeRouteObject.");
59                 }
60                 final Iro iro = ((Iro) object);
61
62                 assert !(iro.getSubobjects().isEmpty()) : "Empty Include Route Object.";
63
64                 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobjects> subs = Lists.newArrayList();
65
66                 for (final Subobjects s : iro.getSubobjects()) {
67                         subs.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectsBuilder().setLoose(
68                                         false).setSubobjectType(s.getSubobjectType()).build());
69                 }
70                 return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), serializeSubobject(subs));
71         }
72
73         @Override
74         public int getObjectType() {
75                 return TYPE;
76         }
77
78         @Override
79         public int getObjectClass() {
80                 return CLASS;
81         }
82 }