BUG-612 : switched ERO to ByteBuf.
[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 io.netty.buffer.ByteBuf;
11
12 import java.util.ArrayList;
13 import java.util.List;
14
15 import org.opendaylight.protocol.pcep.spi.EROSubobjectRegistry;
16 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
17 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
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.explicit.route.object.ero.Subobject;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.path.key.object.PathKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.path.key.object.PathKeyBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.path.key.object.path.key.PathKeys;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.path.key.object.path.key.PathKeysBuilder;
26
27 import com.google.common.base.Preconditions;
28 import com.google.common.collect.Lists;
29
30 /**
31  * Parser for {@link PathKey}
32  */
33 public class PCEPPathKeyObjectParser extends AbstractEROWithSubobjectsParser {
34
35         public static final int CLASS = 16;
36
37         public static final int TYPE = 1;
38
39         public PCEPPathKeyObjectParser(final EROSubobjectRegistry subReg) {
40                 super(subReg);
41         }
42
43         @Override
44         public PathKey parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
45                 Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
46                 final PathKeyBuilder builder = new PathKeyBuilder();
47                 builder.setIgnore(header.isIgnore());
48                 builder.setProcessingRule(header.isProcessingRule());
49                 final List<PathKeys> pk = new ArrayList<>();
50                 final List<Subobject> subs = parseSubobjects(bytes);
51                 for (final Subobject s : subs) {
52                         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();
53                         pk.add(new PathKeysBuilder().setLoose(s.isLoose()).setPceId(k.getPathKey().getPceId()).setPathKey(k.getPathKey().getPathKey()).build());
54                 }
55                 builder.setPathKeys(pk);
56                 return builder.build();
57         }
58
59         @Override
60         public byte[] serializeObject(final Object object) {
61                 if (!(object instanceof PathKey)) {
62                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed PathKeyObject.");
63                 }
64                 final PathKey pkey = (PathKey) object;
65                 final List<PathKeys> pk = pkey.getPathKeys();
66                 final List<Subobject> subs = Lists.newArrayList();
67                 for (final PathKeys p : pk) {
68                         subs.add(new SubobjectBuilder().setLoose(p.isLoose()).setSubobjectType(
69                                         new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobject.subobject.type.PathKeyCaseBuilder().setPathKey(
70                                                         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(
71                                                                         p.getPathKey()).setPceId(p.getPceId()).build()).build()).build());
72                 }
73                 return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), serializeSubobject(subs));
74         }
75 }