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