Merge "BUG-130: introduced EROSubobjectUtil."
[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 java.util.List;
11
12 import org.opendaylight.protocol.pcep.impl.object.EROSubobjectUtil;
13 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
14 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
15 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.spi.XROSubobjectHandlerRegistry;
17 import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
18 import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
19 import org.opendaylight.protocol.util.ByteArray;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobjects;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectsBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.ExrsCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.ExrsCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.Exrs;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.ExrsBuilder;
26
27 import com.google.common.collect.Lists;
28 import com.google.common.primitives.UnsignedBytes;
29
30 public class EROExplicitExclusionRouteSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
31
32         public static final int TYPE = 33;
33
34         private static final int SUB_TYPE_FLAG_F_LENGTH = 1;
35         private static final int SUB_LENGTH_F_LENGTH = 1;
36         private static final int SUB_HEADER_LENGTH = SUB_TYPE_FLAG_F_LENGTH + SUB_LENGTH_F_LENGTH;
37
38         private static final int TYPE_FLAG_F_OFFSET = 0;
39         private static final int LENGTH_F_OFFSET = TYPE_FLAG_F_OFFSET + SUB_TYPE_FLAG_F_LENGTH;
40         private static final int SO_CONTENTS_OFFSET = LENGTH_F_OFFSET + SUB_LENGTH_F_LENGTH;
41
42         private final XROSubobjectHandlerRegistry registry;
43
44         public EROExplicitExclusionRouteSubobjectParser(final XROSubobjectHandlerRegistry registry) {
45                 this.registry = registry;
46         }
47
48         @Override
49         public Subobjects parseSubobject(final byte[] buffer, final boolean loose) throws PCEPDeserializerException {
50                 if (buffer == null || buffer.length == 0) {
51                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
52                 }
53                 final SubobjectsBuilder builder = new SubobjectsBuilder();
54                 builder.setLoose(loose);
55                 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobjects> list = parseSubobjects(buffer);
56                 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();
57                 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobjects s : list) {
58                         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();
59                         b.setAttribute(s.getAttribute());
60                         b.setMandatory(s.isMandatory());
61                         b.setSubobjectType(s.getSubobjectType());
62                         exrss.add(b.build());
63                 }
64                 builder.setSubobjectType(new ExrsCaseBuilder().setExrs(new ExrsBuilder().setExrs(exrss).build()).build());
65                 return builder.build();
66         }
67
68         @Override
69         public byte[] serializeSubobject(final Subobjects subobject) {
70                 if (!(subobject.getSubobjectType() instanceof ExrsCase)) {
71                         throw new IllegalArgumentException("Unknown subobject instance. Passed " + subobject.getSubobjectType().getClass()
72                                         + ". Needed Exrs.");
73                 }
74                 final Exrs e = ((ExrsCase) subobject.getSubobjectType()).getExrs();
75                 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobjects> list = Lists.newArrayList();
76                 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()) {
77                         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.SubobjectsBuilder b = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.SubobjectsBuilder();
78                         b.setAttribute(ex.getAttribute());
79                         b.setMandatory(ex.isMandatory());
80                         b.setSubobjectType(ex.getSubobjectType());
81                         list.add(b.build());
82                 }
83                 return EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), serializeSubobject(list));
84         }
85
86         private List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobjects> parseSubobjects(
87                         final byte[] bytes) throws PCEPDeserializerException {
88                 if (bytes == null) {
89                         throw new IllegalArgumentException("Byte array is mandatory.");
90                 }
91
92                 int type;
93
94                 byte[] soContentsBytes;
95                 int length;
96                 int offset = 0;
97
98                 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobjects> subs = Lists.newArrayList();
99
100                 while (offset < bytes.length) {
101
102                         length = ByteArray.bytesToInt(ByteArray.subByte(bytes, offset + LENGTH_F_OFFSET, SUB_LENGTH_F_LENGTH));
103
104                         final boolean mandatory = ((bytes[offset + TYPE_FLAG_F_OFFSET] & (1 << 7)) != 0) ? true : false;
105                         type = (bytes[offset + TYPE_FLAG_F_OFFSET] & 0xff) & ~(1 << 7);
106                         if (length > bytes.length - offset) {
107                                 throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= "
108                                                 + (bytes.length - offset));
109                         }
110
111                         soContentsBytes = new byte[length - SO_CONTENTS_OFFSET];
112                         System.arraycopy(bytes, offset + SO_CONTENTS_OFFSET, soContentsBytes, 0, length - SO_CONTENTS_OFFSET);
113
114                         final XROSubobjectParser parser = this.registry.getSubobjectParser(type);
115
116                         subs.add(parser.parseSubobject(soContentsBytes, mandatory));
117
118                         offset += length;
119                 }
120                 return subs;
121         }
122
123         private byte[] serializeSubobject(
124                         final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobjects> subobjects) {
125
126                 final List<byte[]> result = Lists.newArrayList();
127
128                 int finalLength = 0;
129
130                 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobjects subobject : subobjects) {
131
132                         final XROSubobjectSerializer serializer = this.registry.getSubobjectSerializer(subobject.getSubobjectType());
133
134                         final byte[] valueBytes = serializer.serializeSubobject(subobject);
135
136                         final byte[] bytes = new byte[SUB_HEADER_LENGTH + valueBytes.length];
137
138                         final byte typeBytes = (byte) (UnsignedBytes.checkedCast(serializer.getType()) | (subobject.isMandatory() ? 1 << 7 : 0));
139
140                         final byte lengthBytes = UnsignedBytes.checkedCast(valueBytes.length + SUB_HEADER_LENGTH);
141
142                         bytes[0] = typeBytes;
143                         bytes[1] = lengthBytes;
144                         System.arraycopy(valueBytes, 0, bytes, SUB_HEADER_LENGTH, valueBytes.length);
145
146                         finalLength += bytes.length;
147                         result.add(bytes);
148                 }
149
150                 final byte[] resultBytes = new byte[finalLength];
151                 int byteOffset = 0;
152                 for (final byte[] b : result) {
153                         System.arraycopy(b, 0, resultBytes, byteOffset, b.length);
154                         byteOffset += b.length;
155                 }
156                 return resultBytes;
157         }
158
159         @Override
160         public int getType() {
161                 return TYPE;
162         }
163 }