BUG-50 : adjusted LSP object.
[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 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;
22
23 import com.google.common.primitives.UnsignedBytes;
24
25 /**
26  * Parser for {@link CNotification}
27  */
28 public class PCEPNotificationObjectParser extends AbstractObjectWithTlvsParser<CNotificationBuilder> {
29
30         public static final int CLASS = 12;
31
32         public static final int TYPE = 1;
33
34         /*
35          * lengths of fields
36          */
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;
40
41         /*
42          * offsets of fields
43          */
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;
48
49         public PCEPNotificationObjectParser(final TlvHandlerRegistry tlvReg) {
50                 super(tlvReg);
51         }
52
53         @Override
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.");
58                 }
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();
66         }
67
68         @Override
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());
72                 }
73         }
74
75         @Override
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.");
79                 }
80                 final CNotification notObj = (CNotification) object;
81
82                 final byte[] tlvs = serializeTlvs(notObj.getTlvs());
83
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);
87                 }
88                 retBytes[NT_F_OFFSET] = UnsignedBytes.checkedCast(notObj.getType());
89                 retBytes[NV_F_OFFSET] = UnsignedBytes.checkedCast(notObj.getValue());
90                 return retBytes;
91         }
92
93         public byte[] serializeTlvs(final Tlvs tlvs) {
94                 if (tlvs == null) {
95                         return new byte[0];
96                 } else if (tlvs.getOverloadDuration() != null) {
97                         return serializeTlv(tlvs.getOverloadDuration());
98                 }
99                 return new byte[0];
100         }
101
102         @Override
103         public int getObjectType() {
104                 return TYPE;
105         }
106
107         @Override
108         public int getObjectClass() {
109                 return CLASS;
110         }
111 }