Removed checkstyle warnings.
[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
16 import java.util.ArrayList;
17 import java.util.List;
18
19 import org.opendaylight.protocol.pcep.impl.object.EROSubobjectUtil;
20 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
21 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
22 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
23 import org.opendaylight.protocol.pcep.spi.XROSubobjectRegistry;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.ExrsCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.ExrsCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.Exrs;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.ExrsBuilder;
30
31 public class EROExplicitExclusionRouteSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
32
33     public static final int TYPE = 33;
34
35     private static final int HEADER_LENGTH = 2;
36
37     private final XROSubobjectRegistry registry;
38
39     public EROExplicitExclusionRouteSubobjectParser(final XROSubobjectRegistry registry) {
40         this.registry = registry;
41     }
42
43     @Override
44     public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException {
45         Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
46         final SubobjectBuilder builder = new SubobjectBuilder();
47         builder.setLoose(loose);
48         final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject> list = parseSubobject(buffer);
49         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();
50         for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject s : list) {
51             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();
52             b.setAttribute(s.getAttribute());
53             b.setMandatory(s.isMandatory());
54             b.setSubobjectType(s.getSubobjectType());
55             exrss.add(b.build());
56         }
57         builder.setSubobjectType(new ExrsCaseBuilder().setExrs(new ExrsBuilder().setExrs(exrss).build()).build());
58         return builder.build();
59     }
60
61     @Override
62     public byte[] serializeSubobject(final Subobject subobject) {
63         if (!(subobject.getSubobjectType() instanceof ExrsCase)) {
64             throw new IllegalArgumentException("Unknown subobject instance. Passed " + subobject.getSubobjectType().getClass()
65                     + ". Needed Exrs.");
66         }
67         final Exrs e = ((ExrsCase) subobject.getSubobjectType()).getExrs();
68         final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject> list = new ArrayList<>();
69         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()) {
70             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();
71             b.setAttribute(ex.getAttribute());
72             b.setMandatory(ex.isMandatory());
73             b.setSubobjectType(ex.getSubobjectType());
74             list.add(b.build());
75         }
76         return EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), serializeSubobject(list));
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 byte[] serializeSubobject(
98             final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject> subobjects) {
99
100         final List<byte[]> result = Lists.newArrayList();
101
102         int finalLength = 0;
103
104         for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject subobject : subobjects) {
105
106             final byte[] bytes = this.registry.serializeSubobject(subobject);
107             finalLength += bytes.length;
108             result.add(bytes);
109         }
110
111         final byte[] resultBytes = new byte[finalLength];
112         int byteOffset = 0;
113         for (final byte[] b : result) {
114             System.arraycopy(b, 0, resultBytes, byteOffset, b.length);
115             byteOffset += b.length;
116         }
117         return resultBytes;
118     }
119 }