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