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