2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.impl.object;
10 import com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
15 import java.util.ArrayList;
16 import java.util.List;
18 import org.opendaylight.protocol.pcep.spi.EROSubobjectRegistry;
19 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
20 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.path.key.object.PathKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.path.key.object.PathKeyBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.path.key.object.path.key.PathKeys;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.path.key.object.path.key.PathKeysBuilder;
31 * Parser for {@link PathKey}
33 public class PCEPPathKeyObjectParser extends AbstractEROWithSubobjectsParser {
35 public static final int CLASS = 16;
37 public static final int TYPE = 1;
39 public PCEPPathKeyObjectParser(final EROSubobjectRegistry subReg) {
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());
55 builder.setPathKeys(pk);
56 return builder.build();
60 public void serializeObject(final Object object, final ByteBuf buffer) {
61 Preconditions.checkArgument(object instanceof PathKey, "Wrong instance of PCEPObject. Passed %s. Needed PathKeyObject.", object.getClass());
62 final PathKey pkey = (PathKey) object;
63 final ByteBuf body = Unpooled.buffer();
64 final List<PathKeys> pk = pkey.getPathKeys();
65 final List<Subobject> subs = new ArrayList<>();
66 for (final PathKeys p : pk) {
67 subs.add(new SubobjectBuilder().setLoose(p.isLoose()).setSubobjectType(
68 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobject.subobject.type.PathKeyCaseBuilder().setPathKey(
69 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(
70 p.getPathKey()).setPceId(p.getPceId()).build()).build()).build());
72 // FIXME: switch to ByteBuf
73 body.writeBytes(serializeSubobject(subs));
74 ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);