BUG-612 : switch PCEP objects to ByteBuf.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPNotificationObjectParser.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.impl.object;
9
10 import io.netty.buffer.ByteBuf;
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.TlvRegistry;
16 import org.opendaylight.protocol.util.ByteArray;
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.Tlv;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.notification.object.CNotification;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.notification.object.CNotificationBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.notification.object.c.notification.Tlvs;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.notification.object.c.notification.TlvsBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.overload.duration.tlv.OverloadDuration;
25
26 import com.google.common.base.Preconditions;
27 import com.google.common.primitives.UnsignedBytes;
28
29 /**
30  * Parser for {@link CNotification}
31  */
32 public class PCEPNotificationObjectParser extends AbstractObjectWithTlvsParser<CNotificationBuilder> {
33
34         public static final int CLASS = 12;
35
36         public static final int TYPE = 1;
37
38         /*
39          * lengths of fields
40          */
41         private static final int FLAGS_F_LENGTH = 1;
42         private static final int NT_F_LENGTH = 1;
43         private static final int NV_F_LENGTH = 1;
44
45         /*
46          * offsets of fields
47          */
48         private static final int FLAGS_F_OFFSET = 1;
49         private static final int NT_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
50         private static final int NV_F_OFFSET = NT_F_OFFSET + NT_F_LENGTH;
51         private static final int TLVS_OFFSET = NV_F_OFFSET + NV_F_LENGTH;
52
53         public PCEPNotificationObjectParser(final TlvRegistry tlvReg) {
54                 super(tlvReg);
55         }
56
57         @Override
58         public CNotification parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
59                 Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
60                 final CNotificationBuilder builder = new CNotificationBuilder();
61                 builder.setIgnore(header.isIgnore());
62                 builder.setProcessingRule(header.isProcessingRule());
63                 bytes.readerIndex(bytes.readerIndex() + NT_F_OFFSET);
64                 builder.setType((short) UnsignedBytes.toInt(bytes.readByte()));
65                 builder.setValue((short) UnsignedBytes.toInt(bytes.readByte()));
66                 parseTlvs(builder, bytes.slice());
67                 return builder.build();
68         }
69
70         @Override
71         public void addTlv(final CNotificationBuilder builder, final Tlv tlv) {
72                 if (tlv instanceof OverloadDuration && builder.getType() == 2 && builder.getValue() == 1) {
73                         builder.setTlvs(new TlvsBuilder().setOverloadDuration((OverloadDuration) tlv).build());
74                 }
75         }
76
77         @Override
78         public byte[] serializeObject(final Object object) {
79                 if (!(object instanceof CNotification)) {
80                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed NotificationObject.");
81                 }
82                 final CNotification notObj = (CNotification) object;
83
84                 final byte[] tlvs = serializeTlvs(notObj.getTlvs());
85
86                 final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
87                 if (tlvs.length != 0) {
88                         ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
89                 }
90                 retBytes[NT_F_OFFSET] = UnsignedBytes.checkedCast(notObj.getType());
91                 retBytes[NV_F_OFFSET] = UnsignedBytes.checkedCast(notObj.getValue());
92                 return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
93         }
94
95         public byte[] serializeTlvs(final Tlvs tlvs) {
96                 if (tlvs == null) {
97                         return new byte[0];
98                 } else if (tlvs.getOverloadDuration() != null) {
99                         return serializeTlv(tlvs.getOverloadDuration());
100                 }
101                 return new byte[0];
102         }
103 }