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.subobject;
10 import io.netty.buffer.ByteBuf;
12 import java.util.ArrayList;
13 import java.util.List;
15 import org.opendaylight.protocol.pcep.impl.object.EROSubobjectUtil;
16 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
17 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
19 import org.opendaylight.protocol.pcep.spi.XROSubobjectRegistry;
20 import org.opendaylight.protocol.util.ByteArray;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.ExrsCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.ExrsCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.Exrs;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.ExrsBuilder;
28 import com.google.common.base.Preconditions;
29 import com.google.common.collect.Lists;
30 import com.google.common.primitives.UnsignedBytes;
32 public class EROExplicitExclusionRouteSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
34 public static final int TYPE = 33;
36 private static final int HEADER_LENGTH = 2;
38 private final XROSubobjectRegistry registry;
40 public EROExplicitExclusionRouteSubobjectParser(final XROSubobjectRegistry registry) {
41 this.registry = registry;
45 public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException {
46 Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
47 final SubobjectBuilder builder = new SubobjectBuilder();
48 builder.setLoose(loose);
49 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject> list = parseSubobject(buffer);
50 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.exrs.Exrs> exrss = Lists.newArrayList();
51 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject s : list) {
52 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.exrs.ExrsBuilder b = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.exrs.ExrsBuilder();
53 b.setAttribute(s.getAttribute());
54 b.setMandatory(s.isMandatory());
55 b.setSubobjectType(s.getSubobjectType());
58 builder.setSubobjectType(new ExrsCaseBuilder().setExrs(new ExrsBuilder().setExrs(exrss).build()).build());
59 return builder.build();
63 public byte[] serializeSubobject(final Subobject subobject) {
64 if (!(subobject.getSubobjectType() instanceof ExrsCase)) {
65 throw new IllegalArgumentException("Unknown subobject instance. Passed " + subobject.getSubobjectType().getClass()
68 final Exrs e = ((ExrsCase) subobject.getSubobjectType()).getExrs();
69 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject> list = new ArrayList<>();
70 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.exrs.Exrs ex : e.getExrs()) {
71 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.SubobjectBuilder b = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.SubobjectBuilder();
72 b.setAttribute(ex.getAttribute());
73 b.setMandatory(ex.isMandatory());
74 b.setSubobjectType(ex.getSubobjectType());
77 return EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), serializeSubobject(list));
80 private List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject> parseSubobject(
81 final ByteBuf buffer) throws PCEPDeserializerException {
82 Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
83 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject> subs = new ArrayList<>();
84 while (buffer.isReadable()) {
85 final boolean mandatory = ((buffer.getByte(buffer.readerIndex()) & (1 << 7)) != 0) ? true : false;
86 int type = (buffer.readByte() & 0xff) & ~(1 << 7);
87 int length = UnsignedBytes.toInt(buffer.readByte()) - HEADER_LENGTH;
88 if (length > buffer.readableBytes()) {
89 throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= "
90 + buffer.readableBytes());
92 //FIXME: switch to ByteBuf
93 subs.add(this.registry.parseSubobject(type, ByteArray.readBytes(buffer, length), mandatory));
98 private byte[] serializeSubobject(
99 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject> subobjects) {
101 final List<byte[]> result = Lists.newArrayList();
105 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject subobject : subobjects) {
107 final byte[] bytes = this.registry.serializeSubobject(subobject);
108 finalLength += bytes.length;
112 final byte[] resultBytes = new byte[finalLength];
114 for (final byte[] b : result) {
115 System.arraycopy(b, 0, resultBytes, byteOffset, b.length);
116 byteOffset += b.length;