Rework parser infrastructure to support partial message processing
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPSrpObjectParser.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.object;
9
10 import java.util.Arrays;
11
12 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
13 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
14 import org.opendaylight.protocol.util.ByteArray;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.SrpIdNumber;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.srp.object.Srp;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.srp.object.SrpBuilder;
21
22 /**
23  * Parser for {@link Srp}
24  */
25 public final class PCEPSrpObjectParser extends AbstractObjectWithTlvsParser<SrpBuilder> {
26
27         public static final int CLASS = 33;
28
29         public static final int TYPE = 1;
30
31         private static final int FLAGS_SIZE = 4;
32
33         private static final int SRP_ID_SIZE = 4;
34
35         private static final int MIN_SIZE = FLAGS_SIZE + SRP_ID_SIZE;
36
37         public PCEPSrpObjectParser(final TlvHandlerRegistry tlvReg) {
38                 super(tlvReg);
39         }
40
41         @Override
42         public Srp parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
43                 if (bytes == null || bytes.length == 0) {
44                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
45                 }
46                 if (bytes.length < MIN_SIZE) {
47                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.length + "; Expected: >=" + MIN_SIZE
48                                         + ".");
49                 }
50                 if (header.isProcessingRule()) {
51                         throw new PCEPDeserializerException("Processed flag is set");
52                 }
53                 final SrpBuilder builder = new SrpBuilder();
54                 builder.setIgnore(header.isIgnore());
55                 builder.setProcessingRule(header.isProcessingRule());
56                 final byte[] srpId = ByteArray.subByte(bytes, FLAGS_SIZE, SRP_ID_SIZE);
57                 if (Arrays.equals(srpId, new byte[] { 0, 0, 0, 0 }) || Arrays.equals(srpId, new byte[] { 0xFFFFFFFF })) {
58                         throw new PCEPDeserializerException("Min/Max values for SRP ID are reserved.");
59                 }
60                 builder.setOperationId(new SrpIdNumber(ByteArray.bytesToLong(srpId)));
61                 return builder.build();
62         }
63
64         @Override
65         public void addTlv(final SrpBuilder builder, final Tlv tlv) {
66                 // No tlvs defined
67         }
68
69         @Override
70         public byte[] serializeObject(final Object object) {
71                 if (!(object instanceof Srp)) {
72                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed SrpObject.");
73                 }
74                 final Srp srp = (Srp) object;
75                 final Long id = srp.getOperationId().getValue();
76                 if (id == 0 || id == 0xFFFFFFFFL) {
77                         throw new IllegalArgumentException("Min/Max values for SRP ID are reserved.");
78                 }
79                 final byte[] retBytes = new byte[MIN_SIZE];
80                 System.arraycopy(ByteArray.intToBytes(id.intValue()), 0, retBytes, FLAGS_SIZE, SRP_ID_SIZE);
81                 return retBytes;
82         }
83
84         @Override
85         public int getObjectType() {
86                 return TYPE;
87         }
88
89         @Override
90         public int getObjectClass() {
91                 return CLASS;
92         }
93 }