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