BUG-50 : adjusted ERO object.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPPathKeyObjectParser.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.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.PathKeyObject;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobjects;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectsBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobjects.subobject.type.PathKey;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.path.key.object.PathKeys;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.path.key.object.PathKeysBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.requests.path.key.expansion.PathKeyBuilder;
24
25 import com.google.common.collect.Lists;
26
27 /**
28  * Parser for {@link PathKeyObject}
29  */
30 public class PCEPPathKeyObjectParser extends AbstractEROWithSubobjectsParser {
31
32         public static final int CLASS = 16;
33
34         public static final int TYPE = 1;
35
36         public PCEPPathKeyObjectParser(final EROSubobjectHandlerRegistry subReg) {
37                 super(subReg);
38         }
39
40         @Override
41         public PathKeyObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException,
42                         PCEPDocumentedException {
43                 final PathKeyBuilder builder = new PathKeyBuilder();
44                 builder.setIgnore(header.isIgnore());
45                 builder.setProcessingRule(header.isProcessingRule());
46                 final List<PathKeys> pk = Lists.newArrayList();
47                 final List<Subobjects> subs = parseSubobjects(bytes);
48                 for (final Subobjects s : subs) {
49                         final PathKey k = (PathKey) s.getSubobjectType();
50                         pk.add(new PathKeysBuilder().setLoose(s.isLoose()).setPceId(k.getPathKey().getPceId()).setPathKey(k.getPathKey().getPathKey()).build());
51                 }
52                 builder.setPathKeys(pk);
53                 return builder.build();
54         }
55
56         @Override
57         public byte[] serializeObject(final Object object) {
58                 if (!(object instanceof PathKeyObject)) {
59                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed PathKeyObject.");
60                 }
61                 final PathKeyObject pkey = (PathKeyObject) object;
62                 final List<PathKeys> pk = pkey.getPathKeys();
63                 final List<Subobjects> subs = Lists.newArrayList();
64                 for (final PathKeys p : pk) {
65                         subs.add(new SubobjectsBuilder().setLoose(p.isLoose()).setSubobjectType(
66                                         new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobjects.subobject.type.PathKeyBuilder().setPathKey(
67                                                         new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobjects.subobject.type.path.key.PathKeyBuilder().setPathKey(
68                                                                         p.getPathKey()).setPceId(p.getPceId()).build()).build()).build());
69                 }
70                 return 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 }