BUG-612 : switched PCEP XRO subobject serializers to ByteBuf
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / EROExplicitExclusionRouteSubobjectParser.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.subobject;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.Lists;
12 import com.google.common.primitives.UnsignedBytes;
13
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
21 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
22 import org.opendaylight.protocol.pcep.spi.EROSubobjectUtil;
23 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
24 import org.opendaylight.protocol.pcep.spi.XROSubobjectRegistry;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.ExrsCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.ExrsCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.Exrs;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.ExrsBuilder;
31
32 public class EROExplicitExclusionRouteSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
33
34     public static final int TYPE = 33;
35
36     private static final int HEADER_LENGTH = 2;
37
38     private final XROSubobjectRegistry registry;
39
40     public EROExplicitExclusionRouteSubobjectParser(final XROSubobjectRegistry registry) {
41         this.registry = registry;
42     }
43
44     @Override
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());
56             exrss.add(b.build());
57         }
58         builder.setSubobjectType(new ExrsCaseBuilder().setExrs(new ExrsBuilder().setExrs(exrss).build()).build());
59         return builder.build();
60     }
61
62     @Override
63     public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
64         Preconditions.checkArgument(subobject.getSubobjectType() instanceof ExrsCase, "Unknown subobject instance. Passed %s. Needed Exrs.", subobject.getSubobjectType().getClass());
65         final Exrs e = ((ExrsCase) subobject.getSubobjectType()).getExrs();
66         final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject> list = new ArrayList<>();
67         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()) {
68             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();
69             b.setAttribute(ex.getAttribute());
70             b.setMandatory(ex.isMandatory());
71             b.setSubobjectType(ex.getSubobjectType());
72             list.add(b.build());
73         }
74         final ByteBuf body = Unpooled.buffer();
75         serializeSubobject(list, body);
76         EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), body, buffer);
77     }
78
79     private List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject> parseSubobject(
80             final ByteBuf buffer) throws PCEPDeserializerException {
81         Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
82         final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject> subs = new ArrayList<>();
83         while (buffer.isReadable()) {
84             final boolean mandatory = ((buffer.getByte(buffer.readerIndex()) & (1 << 7)) != 0) ? true : false;
85             int type = (buffer.readByte() & 0xff) & ~(1 << 7);
86             int length = UnsignedBytes.toInt(buffer.readByte()) - HEADER_LENGTH;
87             if (length > buffer.readableBytes()) {
88                 throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= "
89                         + buffer.readableBytes());
90             }
91             subs.add(this.registry.parseSubobject(type, buffer.slice(buffer.readerIndex(), length), mandatory));
92             buffer.readerIndex(buffer.readerIndex() + length);
93         }
94         return subs;
95     }
96
97     private void serializeSubobject(
98             final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject> subobjects, final ByteBuf body) {
99         for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject subobject : subobjects) {
100             this.registry.serializeSubobject(subobject, body);
101         }
102     }
103 }