BUG-64 : reformat TlvRegistry, to skip using getType method.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPBandwidthObjectParser.java
index bd7b025d5204641bef7aff33acba8cea10e71f04..0910199d789e94db2401805871980134bddaf745 100644 (file)
@@ -7,66 +7,35 @@
  */
 package org.opendaylight.protocol.pcep.impl.object;
 
-import org.opendaylight.protocol.pcep.PCEPDeserializerException;
-import org.opendaylight.protocol.pcep.PCEPDocumentedException;
-import org.opendaylight.protocol.pcep.spi.AbstractObjectParser;
-import org.opendaylight.protocol.pcep.spi.HandlerRegistry;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ieee754.rev130819.Float32;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.BandwidthObject;
+import java.util.Arrays;
+
+import org.opendaylight.protocol.pcep.spi.ObjectUtil;
+import org.opendaylight.protocol.pcep.spi.TlvRegistry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.requests.segment.computation.p2p.reported.route.BandwidthBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.bandwidth.object.Bandwidth;
 
 /**
- * Parser for {@link BandwidthObject}
+ * Parser for {@link Bandwidth}
  */
-public class PCEPBandwidthObjectParser extends AbstractObjectParser<BandwidthBuilder> {
-
-       public static final int E_CLASS = 5;
-
-       public static final int E_TYPE = 1;
+public class PCEPBandwidthObjectParser extends AbstractBandwidthParser {
 
        public static final int CLASS = 5;
 
-       public static final int TYPE = 2;
-
-       private static final int BANDWIDTH_F_LENGTH = 4;
-
-       public PCEPBandwidthObjectParser(final HandlerRegistry registry) {
-               super(registry);
-       }
-
-       @Override
-       public BandwidthObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException,
-                       PCEPDocumentedException {
-               if (bytes == null || bytes.length == 0)
-                       throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
-               if (bytes.length != BANDWIDTH_F_LENGTH)
-                       throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.length + "; Expected: "
-                                       + BANDWIDTH_F_LENGTH + ".");
-
-               final BandwidthBuilder builder = new BandwidthBuilder();
+       public static final int TYPE = 1;
 
-               builder.setIgnore(header.isIgnore());
-               builder.setProcessingRule(header.isProcessingRule());
+       private static final int BANDWIDTH_LENGTH = 4;
 
-               builder.setBandwidth(new Float32(bytes));
-
-               return builder.build();
-       }
-
-       @Override
-       public void addTlv(final BandwidthBuilder builder, final Tlv tlv) {
-               // No tlvs defined
+       public PCEPBandwidthObjectParser(final TlvRegistry tlvReg) {
+               super(tlvReg);
        }
 
        @Override
        public byte[] serializeObject(final Object object) {
-               if (!(object instanceof BandwidthObject))
+               if (!(object instanceof Bandwidth)) {
                        throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed BandwidthObject.");
-
-               return ((BandwidthObject) object).getBandwidth().getValue();
+               }
+               final byte[] retBytes = Arrays.copyOf(((Bandwidth) object).getBandwidth().getValue(), BANDWIDTH_LENGTH);
+               return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
        }
 
        @Override
@@ -78,12 +47,4 @@ public class PCEPBandwidthObjectParser extends AbstractObjectParser<BandwidthBui
        public int getObjectClass() {
                return CLASS;
        }
-
-       public int getEObjectType() {
-               return E_TYPE;
-       }
-
-       public int getEObjectClass() {
-               return E_CLASS;
-       }
 }