Merge "BUG-130 : introduced XROSubobjectUtil."
[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
29 public class EROExplicitExclusionRouteSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
30
31         public static final int TYPE = 33;
32
33         private static final int SUB_TYPE_FLAG_F_LENGTH = 1;
34         private static final int SUB_LENGTH_F_LENGTH = 1;
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.xro.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._case.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.xro.Subobjects s : list) {
56                         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();
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 ExrsCaseBuilder().setExrs(new ExrsBuilder().setExrs(exrss).build()).build());
63                 return builder.build();
64         }
65
66         @Override
67         public byte[] serializeSubobject(final Subobjects subobject) {
68                 if (!(subobject.getSubobjectType() instanceof ExrsCase)) {
69                         throw new IllegalArgumentException("Unknown subobject instance. Passed " + subobject.getSubobjectType().getClass()
70                                         + ". Needed Exrs.");
71                 }
72                 final Exrs e = ((ExrsCase) subobject.getSubobjectType()).getExrs();
73                 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobjects> list = Lists.newArrayList();
74                 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()) {
75                         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();
76                         b.setAttribute(ex.getAttribute());
77                         b.setMandatory(ex.isMandatory());
78                         b.setSubobjectType(ex.getSubobjectType());
79                         list.add(b.build());
80                 }
81                 return EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), serializeSubobject(list));
82         }
83
84         private List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobjects> parseSubobjects(
85                         final byte[] bytes) throws PCEPDeserializerException {
86                 if (bytes == null) {
87                         throw new IllegalArgumentException("Byte array is mandatory.");
88                 }
89
90                 int type;
91
92                 byte[] soContentsBytes;
93                 int length;
94                 int offset = 0;
95
96                 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobjects> subs = Lists.newArrayList();
97
98                 while (offset < bytes.length) {
99
100                         length = ByteArray.bytesToInt(ByteArray.subByte(bytes, offset + LENGTH_F_OFFSET, SUB_LENGTH_F_LENGTH));
101
102                         final boolean mandatory = ((bytes[offset + TYPE_FLAG_F_OFFSET] & (1 << 7)) != 0) ? true : false;
103                         type = (bytes[offset + TYPE_FLAG_F_OFFSET] & 0xff) & ~(1 << 7);
104                         if (length > bytes.length - offset) {
105                                 throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= "
106                                                 + (bytes.length - offset));
107                         }
108
109                         soContentsBytes = new byte[length - SO_CONTENTS_OFFSET];
110                         System.arraycopy(bytes, offset + SO_CONTENTS_OFFSET, soContentsBytes, 0, length - SO_CONTENTS_OFFSET);
111
112                         final XROSubobjectParser parser = this.registry.getSubobjectParser(type);
113
114                         subs.add(parser.parseSubobject(soContentsBytes, mandatory));
115
116                         offset += length;
117                 }
118                 return subs;
119         }
120
121         private byte[] serializeSubobject(
122                         final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobjects> subobjects) {
123
124                 final List<byte[]> result = Lists.newArrayList();
125
126                 int finalLength = 0;
127
128                 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobjects subobject : subobjects) {
129
130                         final XROSubobjectSerializer serializer = this.registry.getSubobjectSerializer(subobject.getSubobjectType());
131
132                         final byte[] bytes = serializer.serializeSubobject(subobject);
133                         finalLength += bytes.length;
134                         result.add(bytes);
135                 }
136
137                 final byte[] resultBytes = new byte[finalLength];
138                 int byteOffset = 0;
139                 for (final byte[] b : result) {
140                         System.arraycopy(b, 0, resultBytes, byteOffset, b.length);
141                         byteOffset += b.length;
142                 }
143                 return resultBytes;
144         }
145
146         @Override
147         public int getType() {
148                 return TYPE;
149         }
150 }