BUG-47 : PCEP migration to generated DTOs.
[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.HandlerRegistry;
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;
25
26 /**
27  * Parser for {@link NotificationObject}
28  */
29 public class PCEPNotificationObjectParser extends AbstractObjectParser<NotificationsBuilder> {
30
31         public static final int CLASS = 12;
32
33         public static final int TYPE = 1;
34
35         /*
36          * lengths of fields
37          */
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;
41
42         /*
43          * offsets of fields
44          */
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;
49
50         public PCEPNotificationObjectParser(final HandlerRegistry registry) {
51                 super(registry);
52         }
53
54         @Override
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.");
59
60                 final NotificationsBuilder builder = new NotificationsBuilder();
61
62                 parseTlvs(builder, ByteArray.cutBytes(bytes, TLVS_OFFSET));
63
64                 builder.setIgnore(header.isIgnore());
65                 builder.setProcessingRule(header.isProcessingRule());
66
67                 builder.setType((short) (bytes[NT_F_OFFSET] & 0xFF));
68                 builder.setValue((short) (bytes[NV_F_OFFSET] & 0xFF));
69
70                 return builder.build();
71         }
72
73         @Override
74         public void addTlv(final NotificationsBuilder builder, final Tlv tlv) {
75                 if (tlv instanceof OverloadDurationTlv && builder.getType() == 2 && builder.getValue() == 1)
76                         builder.setTlvs(new TlvsBuilder().setOverloadDuration(
77                                         new OverloadDurationBuilder().setDuration(((OverloadDurationTlv) tlv).getDuration()).build()).build());
78         }
79
80         @Override
81         public byte[] serializeObject(final Object object) {
82                 if (!(object instanceof NotificationObject))
83                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed NotificationObject.");
84
85                 final NotificationObject notObj = (NotificationObject) object;
86
87                 final byte[] tlvs = serializeTlvs(notObj.getTlvs());
88                 int tlvsLength = 0;
89                 if (tlvs != null)
90                         tlvsLength = tlvs.length;
91                 final byte[] retBytes = new byte[TLVS_OFFSET + tlvsLength + Util.getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
92
93                 if (tlvs != null)
94                         ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
95
96                 retBytes[NT_F_OFFSET] = ByteArray.shortToBytes(notObj.getType())[1];
97                 retBytes[NV_F_OFFSET] = ByteArray.shortToBytes(notObj.getValue())[1];
98
99                 return retBytes;
100         }
101
102         public byte[] serializeTlvs(final Tlvs tlvs) {
103                 if (tlvs.getOverloadDuration() != null) {
104                         // FIXME : add
105                         // return serializeTlv(new NoPathVectorBuilder().setFlags(tlvs.getNoPathVector()).build());
106                 }
107                 return null;
108         }
109
110         @Override
111         public int getObjectType() {
112                 return TYPE;
113         }
114
115         @Override
116         public int getObjectClass() {
117                 return CLASS;
118         }
119 }