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
9 package org.opendaylight.protocol.pcep.impl.object;
11 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
12 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
13 import org.opendaylight.protocol.pcep.PCEPErrors;
14 import org.opendaylight.protocol.pcep.impl.Util;
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.LspDbVersionTlv;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OfListTlv;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OpenObject;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PredundancyGroupIdTlv;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ProtocolVersion;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.StatefulCapabilityTlv;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.open.message.OpenBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Tlvs;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.TlvsBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.tlvs.LspDbVersionBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.tlvs.OfListBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.tlvs.PredundancyGroupIdBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.tlvs.StatefulBuilder;
34 import com.google.common.primitives.UnsignedBytes;
37 * Parser for {@link OpenObject}
40 public class PCEPOpenObjectParser extends AbstractObjectWithTlvsParser<OpenBuilder> {
42 public static final int CLASS = 1;
44 public static final int TYPE = 1;
47 * lengths of fields in bytes
49 public static final int VER_FLAGS_MF_LENGTH = 1; // multi-field
50 public static final int KEEPALIVE_F_LENGTH = 1;
51 public static final int DEAD_TIMER_LENGTH = 1;
52 public static final int SID_F_LENGTH = 1;
55 * lengths of subfields inside multi-field in bits
57 public static final int VERSION_SF_LENGTH = 3;
58 public static final int FLAGS_SF_LENGTH = 5;
61 * offsets of field in bytes
64 public static final int VER_FLAGS_MF_OFFSET = 0;
65 public static final int KEEPALIVE_F_OFFSET = VER_FLAGS_MF_OFFSET + VER_FLAGS_MF_LENGTH;
66 public static final int DEAD_TIMER_OFFSET = KEEPALIVE_F_OFFSET + KEEPALIVE_F_LENGTH;
67 public static final int SID_F_OFFSET = DEAD_TIMER_OFFSET + DEAD_TIMER_LENGTH;
68 public static final int TLVS_OFFSET = SID_F_OFFSET + SID_F_LENGTH;
71 * offsets of subfields inside multi-field in bits
74 public static final int VERSION_SF_OFFSET = 0;
75 public static final int FLAGS_SF_OFFSET = VERSION_SF_LENGTH + VERSION_SF_OFFSET;
77 private static final int PCEP_VERSION = 1;
79 public PCEPOpenObjectParser(final TlvHandlerRegistry tlvReg) {
84 public OpenObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException, PCEPDocumentedException {
85 if (bytes == null || bytes.length == 0) {
86 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
89 final int versionValue = ByteArray.copyBitsRange(bytes[VER_FLAGS_MF_OFFSET], VERSION_SF_OFFSET, VERSION_SF_LENGTH);
91 if (versionValue != PCEP_VERSION) {
92 throw new PCEPDocumentedException("Unsupported PCEP version " + versionValue, PCEPErrors.PCEP_VERSION_NOT_SUPPORTED);
95 final OpenBuilder builder = new OpenBuilder();
97 parseTlvs(builder, ByteArray.cutBytes(bytes, TLVS_OFFSET));
99 builder.setVersion(new ProtocolVersion((short) versionValue));
100 builder.setProcessingRule(header.isProcessingRule());
101 builder.setIgnore(header.isIgnore());
102 builder.setDeadTimer((short) UnsignedBytes.toInt(bytes[DEAD_TIMER_OFFSET]));
103 builder.setKeepalive((short) UnsignedBytes.toInt(bytes[KEEPALIVE_F_OFFSET]));
104 builder.setSessionId((short) UnsignedBytes.toInt(bytes[SID_F_OFFSET]));
105 return builder.build();
109 public void addTlv(final OpenBuilder builder, final Tlv tlv) {
110 final TlvsBuilder tbuilder = new TlvsBuilder();
111 if (tlv instanceof OfListTlv) {
112 tbuilder.setOfList(new OfListBuilder().setCodes(((OfListTlv) tlv).getCodes()).build());
113 } else if (tlv instanceof StatefulCapabilityTlv) {
114 tbuilder.setStateful(new StatefulBuilder().setFlags(((StatefulCapabilityTlv) tlv).getFlags()).build());
115 } else if (tlv instanceof PredundancyGroupIdTlv) {
116 tbuilder.setPredundancyGroupId(new PredundancyGroupIdBuilder().setIdentifier(((PredundancyGroupIdTlv) tlv).getIdentifier()).build());
117 } else if (tlv instanceof LspDbVersionTlv) {
118 tbuilder.setLspDbVersion(new LspDbVersionBuilder().build());
120 builder.setTlvs(tbuilder.build());
124 public byte[] serializeObject(final Object object) {
125 if (!(object instanceof OpenObject)) {
126 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed OpenObject.");
128 final OpenObject open = (OpenObject) object;
130 final byte versionFlagMF = (byte) (PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH));
132 final byte[] tlvs = serializeTlvs(open.getTlvs());
134 final byte[] bytes = new byte[TLVS_OFFSET + tlvs.length + Util.getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
136 bytes[VER_FLAGS_MF_OFFSET] = versionFlagMF;
137 bytes[KEEPALIVE_F_OFFSET] = ByteArray.shortToBytes(open.getKeepalive())[1];
138 bytes[DEAD_TIMER_OFFSET] = ByteArray.shortToBytes(open.getDeadTimer())[1];
139 bytes[SID_F_OFFSET] = ByteArray.shortToBytes(open.getSessionId())[1];
140 ByteArray.copyWhole(tlvs, bytes, TLVS_OFFSET);
145 public byte[] serializeTlvs(final Tlvs tlvs) {
147 if (tlvs.getLspDbVersion() != null) {
148 final byte[] lspDbBytes = serializeTlv(new LspDbVersionBuilder().setVersion(tlvs.getLspDbVersion().getVersion()).build());
149 finalLength = lspDbBytes.length;
151 if (tlvs.getOfList() != null) {
152 final byte[] ofListBytes = serializeTlv(new OfListBuilder().setCodes(tlvs.getOfList().getCodes()).build());
153 finalLength = ofListBytes.length;
158 final byte[] bytes = new byte[finalLength];
160 // FIXME copy result bytes
165 public int getObjectType() {
170 public int getObjectClass() {