Defined Symbolic path name TLV to LSPA.
[bgpcep.git] / pcep / ietf-stateful07 / src / main / java / org / opendaylight / protocol / pcep / ietf / stateful07 / Stateful07SrpObjectParser.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.ietf.stateful07;
9
10 import java.util.Arrays;
11
12 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
13 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
14 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
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.ietf.stateful.rev131222.SrpIdNumber;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.Srp;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.SrpBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.srp.Tlvs;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.srp.TlvsBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.symbolic.path.name.tlv.SymbolicPathName;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
26
27 /**
28  * Parser for {@link Srp}
29  */
30 public final class Stateful07SrpObjectParser extends AbstractObjectWithTlvsParser<SrpBuilder> {
31
32         public static final int CLASS = 33;
33
34         public static final int TYPE = 1;
35
36         private static final int FLAGS_SIZE = 4;
37
38         private static final int SRP_ID_SIZE = 4;
39
40         private static final int TLVS_OFFSET = FLAGS_SIZE + SRP_ID_SIZE;
41
42         private static final int MIN_SIZE = FLAGS_SIZE + SRP_ID_SIZE;
43
44         public Stateful07SrpObjectParser(final TlvHandlerRegistry tlvReg) {
45                 super(tlvReg);
46         }
47
48         @Override
49         public Srp parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
50                 if (bytes == null || bytes.length == 0) {
51                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
52                 }
53                 if (bytes.length < MIN_SIZE) {
54                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.length + "; Expected: >=" + MIN_SIZE
55                                         + ".");
56                 }
57                 if (header.isProcessingRule()) {
58                         throw new PCEPDeserializerException("Processed flag is set");
59                 }
60                 final SrpBuilder builder = new SrpBuilder();
61                 builder.setIgnore(header.isIgnore());
62                 builder.setProcessingRule(header.isProcessingRule());
63                 final byte[] srpId = ByteArray.subByte(bytes, FLAGS_SIZE, SRP_ID_SIZE);
64                 if (Arrays.equals(srpId, new byte[] { 0, 0, 0, 0 }) || Arrays.equals(srpId, new byte[] { 0xFFFFFFFF })) {
65                         throw new PCEPDeserializerException("Min/Max values for SRP ID are reserved.");
66                 }
67                 builder.setOperationId(new SrpIdNumber(ByteArray.bytesToLong(srpId)));
68                 return builder.build();
69         }
70
71         @Override
72         public void addTlv(final SrpBuilder builder, final Tlv tlv) {
73                 if (tlv instanceof SymbolicPathName) {
74                         builder.setTlvs(new TlvsBuilder().setSymbolicPathName((SymbolicPathName) tlv).build());
75                 }
76         }
77
78         @Override
79         public byte[] serializeObject(final Object object) {
80                 if (!(object instanceof Srp)) {
81                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed SrpObject.");
82                 }
83                 final Srp srp = (Srp) object;
84                 final byte[] tlvs = serializeTlvs(srp.getTlvs());
85                 final Long id = srp.getOperationId().getValue();
86                 if (id == 0 || id == 0xFFFFFFFFL) {
87                         throw new IllegalArgumentException("Min/Max values for SRP ID are reserved.");
88                 }
89                 final byte[] retBytes = new byte[MIN_SIZE];
90                 if (tlvs != null) {
91                         ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
92                 }
93                 System.arraycopy(ByteArray.intToBytes(id.intValue(), SRP_ID_SIZE), 0, retBytes, FLAGS_SIZE, SRP_ID_SIZE);
94                 ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
95                 return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
96         }
97
98         public byte[] serializeTlvs(final Tlvs tlvs) {
99                 if (tlvs == null) {
100                         return new byte[0];
101                 } else if (tlvs.getSymbolicPathName() != null) {
102                         return serializeTlv(tlvs.getSymbolicPathName());
103                 }
104                 return new byte[0];
105         }
106
107         @Override
108         public int getObjectType() {
109                 return TYPE;
110         }
111
112         @Override
113         public int getObjectClass() {
114                 return CLASS;
115         }
116 }