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