BUG-612 : switched ERO 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 io.netty.buffer.ByteBuf;
11
12 import java.util.ArrayList;
13 import java.util.List;
14
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;
27
28 import com.google.common.base.Preconditions;
29 import com.google.common.collect.Lists;
30 import com.google.common.primitives.UnsignedBytes;
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 byte[] serializeSubobject(final Subobject subobject) {
64                 if (!(subobject.getSubobjectType() instanceof ExrsCase)) {
65                         throw new IllegalArgumentException("Unknown subobject instance. Passed " + subobject.getSubobjectType().getClass()
66                                         + ". Needed Exrs.");
67                 }
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());
75                         list.add(b.build());
76                 }
77                 return EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), serializeSubobject(list));
78         }
79
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());
91                         }
92                         //FIXME: switch to ByteBuf
93                         subs.add(this.registry.parseSubobject(type, ByteArray.readBytes(buffer, length), mandatory));
94                 }
95                 return subs;
96         }
97
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) {
100
101                 final List<byte[]> result = Lists.newArrayList();
102
103                 int finalLength = 0;
104
105                 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject subobject : subobjects) {
106
107                         final byte[] bytes = this.registry.serializeSubobject(subobject);
108                         finalLength += bytes.length;
109                         result.add(bytes);
110                 }
111
112                 final byte[] resultBytes = new byte[finalLength];
113                 int byteOffset = 0;
114                 for (final byte[] b : result) {
115                         System.arraycopy(b, 0, resultBytes, byteOffset, b.length);
116                         byteOffset += b.length;
117                 }
118                 return resultBytes;
119         }
120 }