2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.ietf.stateful07;
10 import java.util.Arrays;
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;
28 * Parser for {@link Srp}
30 public final class Stateful07SrpObjectParser extends AbstractObjectWithTlvsParser<SrpBuilder> {
32 public static final int CLASS = 33;
34 public static final int TYPE = 1;
36 private static final int FLAGS_SIZE = 4;
38 private static final int SRP_ID_SIZE = 4;
40 private static final int TLVS_OFFSET = FLAGS_SIZE + SRP_ID_SIZE;
42 private static final int MIN_SIZE = FLAGS_SIZE + SRP_ID_SIZE;
44 public Stateful07SrpObjectParser(final TlvHandlerRegistry tlvReg) {
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.");
53 if (bytes.length < MIN_SIZE) {
54 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.length + "; Expected: >=" + MIN_SIZE
57 if (header.isProcessingRule()) {
58 throw new PCEPDeserializerException("Processed flag is set");
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.");
67 builder.setOperationId(new SrpIdNumber(ByteArray.bytesToLong(srpId)));
68 return builder.build();
72 public void addTlv(final SrpBuilder builder, final Tlv tlv) {
73 if (tlv instanceof SymbolicPathName) {
74 builder.setTlvs(new TlvsBuilder().setSymbolicPathName((SymbolicPathName) tlv).build());
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.");
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.");
89 final byte[] retBytes = new byte[MIN_SIZE];
91 ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
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);
98 public byte[] serializeTlvs(final Tlvs tlvs) {
101 } else if (tlvs.getSymbolicPathName() != null) {
102 return serializeTlv(tlvs.getSymbolicPathName());
108 public int getObjectType() {
113 public int getObjectClass() {