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