Rework PCEPDeserializerException and move it into SPI
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPExcludeRouteObjectParser.java
index e511e2f021f2e9bcad2b9ea485c097b6cc392cce..2701148272f690f07d6dc44e86094dfe5230daa6 100644 (file)
@@ -1,76 +1,74 @@
 /*
- * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
+* Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.              
+*              
+* This program and the accompanying materials are made available under the             
+* terms of the Eclipse Public License v1.0 which accompanies this distribution,                
+* and is available at http://www.eclipse.org/legal/epl-v10.html                
+*/
 package org.opendaylight.protocol.pcep.impl.object;
 
-import java.util.Arrays;
-import java.util.BitSet;
-import java.util.List;
-
+import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
+import org.opendaylight.protocol.pcep.spi.XROSubobjectHandlerRegistry;
 import org.opendaylight.protocol.util.ByteArray;
-import org.opendaylight.protocol.pcep.PCEPDeserializerException;
-import org.opendaylight.protocol.pcep.PCEPDocumentedException;
-import org.opendaylight.protocol.pcep.PCEPObject;
-import org.opendaylight.protocol.pcep.impl.PCEPObjectParser;
-import org.opendaylight.protocol.pcep.impl.PCEPXROSubobjectParser;
-import org.opendaylight.protocol.pcep.object.PCEPExcludeRouteObject;
-import org.opendaylight.protocol.pcep.subobject.ExcludeRouteSubobject;
+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.exclude.route.object.Xro;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.Xro.Flags;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.XroBuilder;
+
+import com.google.common.primitives.UnsignedBytes;
 
 /**
- * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPExcludeRouteObject
- * PCEPExcludeRouteObject}
+ * Parser for {@link Xro}
  */
-public class PCEPExcludeRouteObjectParser implements PCEPObjectParser {
+public final class PCEPExcludeRouteObjectParser extends AbstractXROWithSubobjectsParser {
 
-       /*
-        * lengths of fields in bytes
-        */
-       public final int FLAGS_F_LENGTH = 2;
+       public static final int CLASS = 7;
 
-       /*
-        * offsets of fields in bytes
-        */
-       public final int FLAGS_F_OFFSET = 2; // added reserved 2 bytes
-       public final int SO_F_OFFSET = this.FLAGS_F_OFFSET + this.FLAGS_F_LENGTH;
+       public static final int TYPE = 1;
 
-       /*
-        * Flag offsets inside flags field in bits
-        */
-       public final int F_FLAG_OFFSET = 15;
+       private static final int FLAGS_OFFSET = 3;
+
+       public PCEPExcludeRouteObjectParser(final XROSubobjectHandlerRegistry registry) {
+               super(registry);
+       }
 
        @Override
-       public PCEPObject parse(byte[] bytes, boolean processed, boolean ignored) throws PCEPDeserializerException, PCEPDocumentedException {
-               if (bytes == null || bytes.length == 0)
+       public Xro parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
+               if (bytes == null || bytes.length == 0) {
                        throw new IllegalArgumentException("Byte array is mandatory. Can't be null or empty.");
-
-               final BitSet flags = ByteArray.bytesToBitSet(Arrays.copyOfRange(bytes, this.FLAGS_F_OFFSET, this.FLAGS_F_LENGTH));
-
-               final List<ExcludeRouteSubobject> subobjects = PCEPXROSubobjectParser.parse(ByteArray.cutBytes(bytes, this.SO_F_OFFSET));
-               if (subobjects.isEmpty())
-                       throw new PCEPDeserializerException("Empty Exclude Route Object.");
-
-               return new PCEPExcludeRouteObject(subobjects, flags.get(this.F_FLAG_OFFSET), processed, ignored);
+               }
+               final XroBuilder builder = new XroBuilder();
+               builder.setIgnore(header.isIgnore());
+               builder.setProcessingRule(header.isProcessingRule());
+               builder.setFlags(new Flags(UnsignedBytes.toInt(bytes[FLAGS_OFFSET]) != 0));
+               builder.setSubobjects(parseSubobjects(ByteArray.cutBytes(bytes, FLAGS_OFFSET + 1)));
+               return builder.build();
        }
 
        @Override
-       public byte[] put(PCEPObject obj) {
-               if (!(obj instanceof PCEPExcludeRouteObject))
-                       throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + obj.getClass() + ". Needed PCEPExcludeRouteObject.");
-
-               assert !(((PCEPExcludeRouteObject) obj).getSubobjects().isEmpty()) : "Empty Exclude Route Object.";
-
-               final byte[] subObjsBytes = PCEPXROSubobjectParser.put(((PCEPExcludeRouteObject) obj).getSubobjects());
-               final byte[] retBytes = new byte[this.SO_F_OFFSET + subObjsBytes.length];
-               final BitSet flags = new BitSet(this.FLAGS_F_LENGTH * Byte.SIZE);
-               flags.set(this.F_FLAG_OFFSET, ((PCEPExcludeRouteObject) obj).isFail());
-               ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, this.FLAGS_F_LENGTH), retBytes, this.FLAGS_F_OFFSET);
-               ByteArray.copyWhole(subObjsBytes, retBytes, this.SO_F_OFFSET);
+       public byte[] serializeObject(final Object object) {
+               if (!(object instanceof Xro)) {
+                       throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed ExcludeRouteObject.");
+               }
+               final Xro obj = (Xro) object;
+               assert !(obj.getSubobjects().isEmpty()) : "Empty Excluded Route Object.";
+               final byte[] bytes = serializeSubobject(obj.getSubobjects());
+               final byte[] result = new byte[FLAGS_OFFSET + 1 + bytes.length];
+               if (obj.getFlags().isFail()) {
+                       result[FLAGS_OFFSET] = 1;
+               }
+               ByteArray.copyWhole(bytes, result, FLAGS_OFFSET + 1);
+               return result;
+       }
 
-               return retBytes;
+       @Override
+       public int getObjectType() {
+               return TYPE;
        }
 
+       @Override
+       public int getObjectClass() {
+               return CLASS;
+       }
 }