BUG-113: split HandlerRegistry into per-class registries
[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 java.util.Map;
11
12 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
13 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
14 import org.opendaylight.protocol.pcep.spi.AbstractObjectParser;
15 import org.opendaylight.protocol.pcep.spi.SubobjectHandlerRegistry;
16 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ExplicitRouteObject;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.Subobjects;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.path.definition.ExplicitRouteBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.CSubobject;
24
25 import com.google.common.collect.Maps;
26
27 /**
28  * Parser for {@link ExplicitRouteObject}
29  */
30 public class PCEPExplicitRouteObjectParser extends AbstractObjectParser<ExplicitRouteBuilder> {
31
32         public static final int CLASS = 7;
33
34         public static final int TYPE = 1;
35
36         public PCEPExplicitRouteObjectParser(final SubobjectHandlerRegistry subobjReg, final TlvHandlerRegistry tlvReg) {
37                 super(subobjReg, tlvReg);
38         }
39
40         @Override
41         public ExplicitRouteObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException,
42         PCEPDocumentedException {
43                 if (bytes == null || bytes.length == 0) {
44                         throw new IllegalArgumentException("Byte array is mandatory. Can't be null or empty.");
45                 }
46
47                 final ExplicitRouteBuilder builder = new ExplicitRouteBuilder();
48
49                 builder.setIgnore(header.isIgnore());
50                 builder.setProcessingRule(header.isProcessingRule());
51
52                 parseSubobjects(builder, bytes);
53                 return builder.build();
54         }
55
56         @Override
57         public void addTlv(final ExplicitRouteBuilder builder, final Tlv tlv) {
58                 // No tlvs defined
59         }
60
61         @Override
62         public byte[] serializeObject(final Object object) {
63                 if (!(object instanceof ExplicitRouteObject)) {
64                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass()
65                                         + ". Needed ExplicitRouteObject.");
66                 }
67
68                 final ExplicitRouteObject ero = ((ExplicitRouteObject) object);
69
70                 assert !(ero.getSubobjects().isEmpty()) : "Empty Explicit Route Object.";
71
72                 final Map<CSubobject, Boolean> subs = Maps.newHashMap();
73                 for (final Subobjects s : ero.getSubobjects()) {
74                         subs.put((CSubobject) s, s.isLoose());
75                 }
76                 return serializeSubobject(subs);
77         }
78
79         // @Override
80         // public void addSubobject(ExplicitRouteBuilder builder, Map<CSubobject, Boolean> subobjects) {
81         // List<Subobjects> subs = Lists.newArrayList();
82         // for (Entry<CSubobject, Boolean> entry : subobjects.entrySet()) {
83         // SubobjectsBuilder b = new SubobjectsBuilder();
84         // b.setLoose(entry.getValue());
85         // CSubobject sub = entry.getKey();
86         // if (sub instanceof IpPrefixSubobject) {
87         // b.setSubobjectType(new IpPrefixBuilder().setIpPrefix(((IpPrefix)sub).getIpPrefix()).build());
88         // subs.add(b.build());
89         // } else if (sub instanceof AsNumberSubobject) {
90         // b.setSubobjectType(new AsNumberBuilder().setAsNumber((AsNumber)sub).build());
91         // subs.add(b.build());
92         // } else if (sub instanceof LabelSubobject) {
93         // b.setSubobjectType(new LabelBuilder().setLabels(((Label)sub).getLabels()).build());
94         // subs.add(b.build());
95         // } else if (sub instanceof UnnumberedSubobject) {
96         // b.setSubobjectType(new
97         // UnnumberedBuilder().setInterfaceId(((Unnumbered)sub).getInterfaceId()).setRouterId(((Unnumbered)sub).getRouterId()).build());
98         // subs.add(b.build());
99         // }
100         // }
101         // builder.setSubobjects(subs);
102         // }
103
104         @Override
105         public int getObjectType() {
106                 return TYPE;
107         }
108
109         @Override
110         public int getObjectClass() {
111                 return CLASS;
112         }
113 }