BUG-47 : PCEP migration to generated DTOs.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPObjectiveFunctionObjectParser.java
index d82f06067d2362d6e3a047a40a5dfc7220d2d527..b37523999128aa08e23875274849ec4227a65a18 100644 (file)
@@ -7,22 +7,26 @@
  */
 package org.opendaylight.protocol.pcep.impl.object;
 
-import java.util.NoSuchElementException;
-
 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
-import org.opendaylight.protocol.pcep.PCEPObject;
-import org.opendaylight.protocol.pcep.impl.PCEPOFCodesMapping;
-import org.opendaylight.protocol.pcep.impl.PCEPObjectParser;
-import org.opendaylight.protocol.pcep.impl.PCEPTlvParser;
-import org.opendaylight.protocol.pcep.object.PCEPObjectiveFunctionObject;
+import org.opendaylight.protocol.pcep.PCEPDocumentedException;
+import org.opendaylight.protocol.pcep.spi.AbstractObjectParser;
+import org.opendaylight.protocol.pcep.spi.HandlerRegistry;
 import org.opendaylight.protocol.util.ByteArray;
+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.OfId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OfObject;
+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.lsp.attributes.OfBuilder;
 
 /**
- * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPObjectiveFunctionObject
- * PCEPObjectiveFunctionObject}
+ * Parser for {@link OfObject}
  */
-public class PCEPObjectiveFunctionObjectParser implements PCEPObjectParser {
+public class PCEPObjectiveFunctionObjectParser extends AbstractObjectParser<OfBuilder> {
+
+       public static final int CLASS = 21;
 
+       public static final int TYPE = 1;
        /*
         * lengths of fields
         */
@@ -34,37 +38,56 @@ public class PCEPObjectiveFunctionObjectParser implements PCEPObjectParser {
        public static final int OF_CODE_F_OFFSET = 0;
        public static final int TLVS_OFFSET = OF_CODE_F_OFFSET + OF_CODE_F_LENGTH + 2; // added reserved field of size 2
 
+       public PCEPObjectiveFunctionObjectParser(final HandlerRegistry registry) {
+               super(registry);
+       }
+
        @Override
-       public PCEPObject parse(byte[] bytes, boolean processed, boolean ignored) throws PCEPDeserializerException {
+       public OfObject 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 < TLVS_OFFSET)
-                       throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.length + "; Expected: >=" + TLVS_OFFSET + ".");
-               try {
-                       return new PCEPObjectiveFunctionObject(PCEPOFCodesMapping.getInstance().getFromCodeIdentifier(
-                                       ByteArray.bytesToShort(ByteArray.subByte(bytes, OF_CODE_F_OFFSET, OF_CODE_F_LENGTH)) & 0xFFFF), PCEPTlvParser.parse(ByteArray.cutBytes(
-                                       bytes, TLVS_OFFSET)), processed, ignored);
-               } catch (final NoSuchElementException e) {
-                       throw new PCEPDeserializerException(e, "Objective function object has unknown identifier.");
-               }
+               final OfBuilder builder = new OfBuilder();
+
+               parseTlvs(builder, ByteArray.cutBytes(bytes, TLVS_OFFSET));
+
+               builder.setIgnore(header.isIgnore());
+               builder.setProcessingRule(header.isProcessingRule());
+               builder.setCode(new OfId(ByteArray.bytesToInt(ByteArray.subByte(bytes, OF_CODE_F_OFFSET, OF_CODE_F_LENGTH)) & 0xFFFF));
+
+               return builder.build();
        }
 
        @Override
-       public byte[] put(PCEPObject obj) {
-               if (!(obj instanceof PCEPObjectiveFunctionObject))
-                       throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + obj.getClass() + ". Needed PCEPObjectiveFunction.");
+       public void addTlv(final OfBuilder builder, final Tlv tlv) {
+               // No tlvs defined
+       }
 
-               final PCEPObjectiveFunctionObject specObj = (PCEPObjectiveFunctionObject) obj;
+       @Override
+       public byte[] serializeObject(final Object object) {
+               if (!(object instanceof OfObject))
+                       throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass()
+                                       + ". Needed PCEPObjectiveFunction.");
 
-               final byte[] tlvs = PCEPTlvParser.put(specObj.getTlvs());
-               final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length];
+               final OfObject specObj = (OfObject) object;
+               // FIXME
+               // final byte[] tlvs = PCEPTlvParser.put(specObj.getTlvs());
+               final byte[] retBytes = new byte[TLVS_OFFSET + 0];
 
-               ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
+               // ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
 
-               ByteArray.copyWhole(ByteArray.shortToBytes((short) PCEPOFCodesMapping.getInstance().getFromOFCodesEnum(specObj.getCode())), retBytes, OF_CODE_F_OFFSET);
+               ByteArray.copyWhole(ByteArray.shortToBytes(specObj.getCode().getValue().shortValue()), retBytes, OF_CODE_F_OFFSET);
 
                return retBytes;
        }
 
+       @Override
+       public int getObjectType() {
+               return TYPE;
+       }
+
+       @Override
+       public int getObjectClass() {
+               return CLASS;
+       }
 }