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