BUG-113: split HandlerRegistry into per-class registries
[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.impl.Util;
13 import org.opendaylight.protocol.pcep.spi.AbstractObjectParser;
14 import org.opendaylight.protocol.pcep.spi.SubobjectHandlerRegistry;
15 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
16 import org.opendaylight.protocol.util.ByteArray;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.NotificationObject;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OverloadDurationTlv;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.notification.object.Tlvs;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.notification.object.TlvsBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.notification.object.tlvs.OverloadDurationBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.NotificationsBuilder;
26
27 /**
28  * Parser for {@link NotificationObject}
29  */
30 public class PCEPNotificationObjectParser extends AbstractObjectParser<NotificationsBuilder> {
31
32         public static final int CLASS = 12;
33
34         public static final int TYPE = 1;
35
36         /*
37          * lengths of fields
38          */
39         public static final int FLAGS_F_LENGTH = 1;
40         public static final int NT_F_LENGTH = 1;
41         public static final int NV_F_LENGTH = 1;
42
43         /*
44          * offsets of fields
45          */
46         public static final int FLAGS_F_OFFSET = 1; // added reserved filed of size 1
47         public static final int NT_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
48         public static final int NV_F_OFFSET = NT_F_OFFSET + NT_F_LENGTH;
49         public static final int TLVS_OFFSET = NV_F_OFFSET + NV_F_LENGTH;
50
51         public PCEPNotificationObjectParser(final SubobjectHandlerRegistry subobjReg, final TlvHandlerRegistry tlvReg) {
52                 super(subobjReg, tlvReg);
53         }
54
55         @Override
56         public NotificationObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException,
57         PCEPDocumentedException {
58                 if (bytes == null || bytes.length == 0) {
59                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
60                 }
61
62                 final NotificationsBuilder builder = new NotificationsBuilder();
63
64                 parseTlvs(builder, ByteArray.cutBytes(bytes, TLVS_OFFSET));
65
66                 builder.setIgnore(header.isIgnore());
67                 builder.setProcessingRule(header.isProcessingRule());
68
69                 builder.setType((short) (bytes[NT_F_OFFSET] & 0xFF));
70                 builder.setValue((short) (bytes[NV_F_OFFSET] & 0xFF));
71
72                 return builder.build();
73         }
74
75         @Override
76         public void addTlv(final NotificationsBuilder builder, final Tlv tlv) {
77                 if (tlv instanceof OverloadDurationTlv && builder.getType() == 2 && builder.getValue() == 1) {
78                         builder.setTlvs(new TlvsBuilder().setOverloadDuration(
79                                         new OverloadDurationBuilder().setDuration(((OverloadDurationTlv) tlv).getDuration()).build()).build());
80                 }
81         }
82
83         @Override
84         public byte[] serializeObject(final Object object) {
85                 if (!(object instanceof NotificationObject)) {
86                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed NotificationObject.");
87                 }
88
89                 final NotificationObject notObj = (NotificationObject) object;
90
91                 final byte[] tlvs = serializeTlvs(notObj.getTlvs());
92                 int tlvsLength = 0;
93                 if (tlvs != null) {
94                         tlvsLength = tlvs.length;
95                 }
96                 final byte[] retBytes = new byte[TLVS_OFFSET + tlvsLength + Util.getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
97
98                 if (tlvs != null) {
99                         ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
100                 }
101
102                 retBytes[NT_F_OFFSET] = ByteArray.shortToBytes(notObj.getType())[1];
103                 retBytes[NV_F_OFFSET] = ByteArray.shortToBytes(notObj.getValue())[1];
104
105                 return retBytes;
106         }
107
108         public byte[] serializeTlvs(final Tlvs tlvs) {
109                 if (tlvs.getOverloadDuration() != null) {
110                         // FIXME : add
111                         // return serializeTlv(new NoPathVectorBuilder().setFlags(tlvs.getNoPathVector()).build());
112                 }
113                 return null;
114         }
115
116         @Override
117         public int getObjectType() {
118                 return TYPE;
119         }
120
121         @Override
122         public int getObjectClass() {
123                 return CLASS;
124         }
125 }