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.impl.Util;
13 import org.opendaylight.protocol.pcep.impl.message.AbstractObjectWithTlvsParser;
14 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
15 import org.opendaylight.protocol.util.ByteArray;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.NotificationObject;
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.OverloadDurationTlv;
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.notification.object.Tlvs;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.notification.object.TlvsBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.notification.object.tlvs.OverloadDurationBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.NotificationsBuilder;
27 * Parser for {@link NotificationObject}
29 public class PCEPNotificationObjectParser extends AbstractObjectWithTlvsParser<NotificationsBuilder> {
31 public static final int CLASS = 12;
33 public static final int TYPE = 1;
38 public static final int FLAGS_F_LENGTH = 1;
39 public static final int NT_F_LENGTH = 1;
40 public static final int NV_F_LENGTH = 1;
45 public static final int FLAGS_F_OFFSET = 1; // added reserved filed of size 1
46 public static final int NT_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
47 public static final int NV_F_OFFSET = NT_F_OFFSET + NT_F_LENGTH;
48 public static final int TLVS_OFFSET = NV_F_OFFSET + NV_F_LENGTH;
50 public PCEPNotificationObjectParser(final TlvHandlerRegistry tlvReg) {
55 public NotificationObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException,
56 PCEPDocumentedException {
57 if (bytes == null || bytes.length == 0) {
58 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
61 final NotificationsBuilder builder = new NotificationsBuilder();
63 parseTlvs(builder, ByteArray.cutBytes(bytes, TLVS_OFFSET));
65 builder.setIgnore(header.isIgnore());
66 builder.setProcessingRule(header.isProcessingRule());
68 builder.setType((short) (bytes[NT_F_OFFSET] & 0xFF));
69 builder.setValue((short) (bytes[NV_F_OFFSET] & 0xFF));
71 return builder.build();
75 public void addTlv(final NotificationsBuilder builder, final Tlv tlv) {
76 if (tlv instanceof OverloadDurationTlv && builder.getType() == 2 && builder.getValue() == 1) {
77 builder.setTlvs(new TlvsBuilder().setOverloadDuration(
78 new OverloadDurationBuilder().setDuration(((OverloadDurationTlv) tlv).getDuration()).build()).build());
83 public byte[] serializeObject(final Object object) {
84 if (!(object instanceof NotificationObject)) {
85 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed NotificationObject.");
88 final NotificationObject notObj = (NotificationObject) object;
90 final byte[] tlvs = serializeTlvs(notObj.getTlvs());
93 tlvsLength = tlvs.length;
95 final byte[] retBytes = new byte[TLVS_OFFSET + tlvsLength + Util.getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
98 ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
101 retBytes[NT_F_OFFSET] = ByteArray.shortToBytes(notObj.getType())[1];
102 retBytes[NV_F_OFFSET] = ByteArray.shortToBytes(notObj.getValue())[1];
107 public byte[] serializeTlvs(final Tlvs tlvs) {
108 if (tlvs.getOverloadDuration() != null) {
110 // return serializeTlv(new NoPathVectorBuilder().setFlags(tlvs.getNoPathVector()).build());
116 public int getObjectType() {
121 public int getObjectClass() {