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.parser.subobject;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.util.ArrayList;
15 import java.util.List;
16 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
17 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
18 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
19 import org.opendaylight.protocol.pcep.spi.EROSubobjectUtil;
20 import org.opendaylight.protocol.pcep.spi.XROSubobjectRegistry;
21 import org.opendaylight.protocol.util.Values;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.Subobject;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.SubobjectBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.ExrsCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.ExrsCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.exrs._case.Exrs;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.exrs._case.ExrsBuilder;
29 public class EROExplicitExclusionRouteSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
31 public static final int TYPE = 33;
33 private static final int HEADER_LENGTH = 2;
35 private final XROSubobjectRegistry registry;
37 public EROExplicitExclusionRouteSubobjectParser(final XROSubobjectRegistry registry) {
38 this.registry = registry;
42 public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException {
43 checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
44 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude
45 .route.object.xro.Subobject> xros = parseSubobject(buffer);
46 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit
47 .route.subobjects.subobject.type.exrs._case.exrs.Exrs> exrss = new ArrayList<>();
48 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude
49 .route.object.xro.Subobject xro : xros) {
50 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820
51 .explicit.route.subobjects.subobject.type.exrs._case.exrs.ExrsBuilder exrsBuilder =
52 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820
53 .explicit.route.subobjects.subobject.type.exrs._case.exrs.ExrsBuilder()
54 .setAttribute(xro.getAttribute())
55 .setMandatory(xro.getMandatory())
56 .setSubobjectType(xro.getSubobjectType());
57 exrss.add(exrsBuilder.build());
59 final SubobjectBuilder builder = new SubobjectBuilder()
61 .setSubobjectType(new ExrsCaseBuilder().setExrs(new ExrsBuilder().setExrs(exrss).build()).build());
62 return builder.build();
65 private List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude
66 .route.object.xro.Subobject> parseSubobject(final ByteBuf buffer) throws PCEPDeserializerException {
67 checkArgument(buffer != null && buffer.isReadable(),
68 "Array of bytes is mandatory. Can't be null or empty.");
69 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude
70 .route.object.xro.Subobject> xros = new ArrayList<>();
71 while (buffer.isReadable()) {
72 final boolean mandatory =
73 (buffer.getByte(buffer.readerIndex()) & 1 << Values.FIRST_BIT_OFFSET) != 0;
75 buffer.readUnsignedByte() & Values.BYTE_MAX_VALUE_BYTES & ~(1 << Values.FIRST_BIT_OFFSET);
76 final int length = buffer.readUnsignedByte() - HEADER_LENGTH;
77 if (length > buffer.readableBytes()) {
78 throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= "
79 + buffer.readableBytes());
81 xros.add(registry.parseSubobject(type, buffer.readSlice(length), mandatory));
87 public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
88 checkArgument(subobject.getSubobjectType() instanceof ExrsCase,
89 "Unknown subobject instance. Passed %s. Needed Exrs.", subobject.getSubobjectType().getClass());
90 final Exrs exrs = ((ExrsCase) subobject.getSubobjectType()).getExrs();
91 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude
92 .route.object.xro.Subobject> xros = new ArrayList<>();
93 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit
94 .route.subobjects.subobject.type.exrs._case.exrs.Exrs exr : exrs.nonnullExrs()) {
95 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109
96 .exclude.route.object.xro.SubobjectBuilder xroBuilder =
97 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109
98 .exclude.route.object.xro.SubobjectBuilder()
99 .setAttribute(exr.getAttribute())
100 .setMandatory(exr.getMandatory())
101 .setSubobjectType(exr.getSubobjectType());
102 xros.add(xroBuilder.build());
104 final ByteBuf body = Unpooled.buffer();
105 serializeSubobject(xros, body);
106 EROSubobjectUtil.formatSubobject(TYPE, subobject.getLoose(), body, buffer);
109 private void serializeSubobject(
110 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude
111 .route.object.xro.Subobject> subobjects, final ByteBuf body) {
112 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude
113 .route.object.xro.Subobject subobject : subobjects) {
114 registry.serializeSubobject(subobject, body);