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.impl.object;
10 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
11 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
12 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
13 import org.opendaylight.protocol.util.ByteArray;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.notification.object.CNotification;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.notification.object.CNotificationBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.notification.object.c.notification.Tlvs;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.notification.object.c.notification.TlvsBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.overload.duration.tlv.OverloadDuration;
23 import com.google.common.primitives.UnsignedBytes;
26 * Parser for {@link CNotification}
28 public class PCEPNotificationObjectParser extends AbstractObjectWithTlvsParser<CNotificationBuilder> {
30 public static final int CLASS = 12;
32 public static final int TYPE = 1;
37 private static final int FLAGS_F_LENGTH = 1;
38 private static final int NT_F_LENGTH = 1;
39 private static final int NV_F_LENGTH = 1;
44 private static final int FLAGS_F_OFFSET = 1;
45 private static final int NT_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
46 private static final int NV_F_OFFSET = NT_F_OFFSET + NT_F_LENGTH;
47 private static final int TLVS_OFFSET = NV_F_OFFSET + NV_F_LENGTH;
49 public PCEPNotificationObjectParser(final TlvHandlerRegistry tlvReg) {
54 public CNotification parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException,
55 PCEPDocumentedException {
56 if (bytes == null || bytes.length == 0) {
57 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
59 final CNotificationBuilder builder = new CNotificationBuilder();
60 builder.setIgnore(header.isIgnore());
61 builder.setProcessingRule(header.isProcessingRule());
62 builder.setType((short) UnsignedBytes.toInt(bytes[NT_F_OFFSET]));
63 builder.setValue((short) UnsignedBytes.toInt(bytes[NV_F_OFFSET]));
64 parseTlvs(builder, ByteArray.cutBytes(bytes, TLVS_OFFSET));
65 return builder.build();
69 public void addTlv(final CNotificationBuilder builder, final Tlv tlv) {
70 if (tlv instanceof OverloadDuration && builder.getType() == 2 && builder.getValue() == 1) {
71 builder.setTlvs(new TlvsBuilder().setOverloadDuration((OverloadDuration) tlv).build());
76 public byte[] serializeObject(final Object object) {
77 if (!(object instanceof CNotification)) {
78 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed NotificationObject.");
80 final CNotification notObj = (CNotification) object;
82 final byte[] tlvs = serializeTlvs(notObj.getTlvs());
84 final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
85 if (tlvs.length != 0) {
86 ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
88 retBytes[NT_F_OFFSET] = UnsignedBytes.checkedCast(notObj.getType());
89 retBytes[NV_F_OFFSET] = UnsignedBytes.checkedCast(notObj.getValue());
93 public byte[] serializeTlvs(final Tlvs tlvs) {
96 } else if (tlvs.getOverloadDuration() != null) {
97 return serializeTlv(tlvs.getOverloadDuration());
103 public int getObjectType() {
108 public int getObjectClass() {