BUG-47 : switched subobjects to generated source code.
[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.impl.message.AbstractObjectWithTlvsParser;
14 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
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 AbstractObjectWithTlvsParser<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 TlvHandlerRegistry tlvReg) {
51                 super(tlvReg);
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
61                 final NotificationsBuilder builder = new NotificationsBuilder();
62
63                 parseTlvs(builder, ByteArray.cutBytes(bytes, TLVS_OFFSET));
64
65                 builder.setIgnore(header.isIgnore());
66                 builder.setProcessingRule(header.isProcessingRule());
67
68                 builder.setType((short) (bytes[NT_F_OFFSET] & 0xFF));
69                 builder.setValue((short) (bytes[NV_F_OFFSET] & 0xFF));
70
71                 return builder.build();
72         }
73
74         @Override
75         public void addTlv(final NotificationsBuilder builder, final Tlv tlv) {
76                 if (tlv instanceof OverloadDurationTlv && builder.getType() == 2 && builder.getValue() == 1) {
77                         builder.setTlvs(new TlvsBuilder().setOverloadDuration(
78                                         new OverloadDurationBuilder().setDuration(((OverloadDurationTlv) tlv).getDuration()).build()).build());
79                 }
80         }
81
82         @Override
83         public byte[] serializeObject(final Object object) {
84                 if (!(object instanceof NotificationObject)) {
85                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed NotificationObject.");
86                 }
87
88                 final NotificationObject notObj = (NotificationObject) object;
89
90                 final byte[] tlvs = serializeTlvs(notObj.getTlvs());
91                 int tlvsLength = 0;
92                 if (tlvs != null) {
93                         tlvsLength = tlvs.length;
94                 }
95                 final byte[] retBytes = new byte[TLVS_OFFSET + tlvsLength + Util.getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
96
97                 if (tlvs != null) {
98                         ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
99                 }
100
101                 retBytes[NT_F_OFFSET] = ByteArray.shortToBytes(notObj.getType())[1];
102                 retBytes[NV_F_OFFSET] = ByteArray.shortToBytes(notObj.getValue())[1];
103
104                 return retBytes;
105         }
106
107         public byte[] serializeTlvs(final Tlvs tlvs) {
108                 if (tlvs.getOverloadDuration() != null) {
109                         // FIXME : add
110                         // return serializeTlv(new NoPathVectorBuilder().setFlags(tlvs.getNoPathVector()).build());
111                 }
112                 return null;
113         }
114
115         @Override
116         public int getObjectType() {
117                 return TYPE;
118         }
119
120         @Override
121         public int getObjectClass() {
122                 return CLASS;
123         }
124 }