Bumped stateful draft to 07.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPOpenObjectParser.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
9 package org.opendaylight.protocol.pcep.impl.object;
10
11 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
12 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
13 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
14 import org.opendaylight.protocol.pcep.spi.UnknownObject;
15 import org.opendaylight.protocol.util.ByteArray;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs2;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs2Builder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.stateful.capability.tlv.Stateful;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ProtocolVersion;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.list.tlv.OfList;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.Tlvs;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import com.google.common.primitives.UnsignedBytes;
32
33 /**
34  * Parser for {@link Open}
35  */
36 public class PCEPOpenObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
37         private static final Logger LOG = LoggerFactory.getLogger(PCEPOpenObjectParser.class);
38
39         public static final int CLASS = 1;
40
41         public static final int TYPE = 1;
42
43         /*
44          * lengths of fields in bytes
45          */
46         private static final int VER_FLAGS_MF_LENGTH = 1;
47         private static final int KEEPALIVE_F_LENGTH = 1;
48         private static final int DEAD_TIMER_LENGTH = 1;
49         private static final int SID_F_LENGTH = 1;
50
51         /*
52          * lengths of subfields inside multi-field in bits
53          */
54         private static final int VERSION_SF_LENGTH = 3;
55
56         /*
57          * offsets of field in bytes
58          */
59         private static final int VER_FLAGS_MF_OFFSET = 0;
60         private static final int KEEPALIVE_F_OFFSET = VER_FLAGS_MF_OFFSET + VER_FLAGS_MF_LENGTH;
61         private static final int DEAD_TIMER_OFFSET = KEEPALIVE_F_OFFSET + KEEPALIVE_F_LENGTH;
62         private static final int SID_F_OFFSET = DEAD_TIMER_OFFSET + DEAD_TIMER_LENGTH;
63         private static final int TLVS_OFFSET = SID_F_OFFSET + SID_F_LENGTH;
64
65         /*
66          * offsets of subfields inside multi-field in bits
67          */
68         private static final int VERSION_SF_OFFSET = 0;
69
70         private static final int PCEP_VERSION = 1;
71
72         public PCEPOpenObjectParser(final TlvHandlerRegistry tlvReg) {
73                 super(tlvReg);
74         }
75
76         @Override
77         public Object parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
78                 if (bytes == null || bytes.length == 0) {
79                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
80                 }
81                 final int versionValue = ByteArray.copyBitsRange(bytes[VER_FLAGS_MF_OFFSET], VERSION_SF_OFFSET, VERSION_SF_LENGTH);
82
83                 final OpenBuilder builder = new OpenBuilder();
84                 builder.setVersion(new ProtocolVersion((short) versionValue));
85                 builder.setProcessingRule(header.isProcessingRule());
86                 builder.setIgnore(header.isIgnore());
87                 builder.setDeadTimer((short) UnsignedBytes.toInt(bytes[DEAD_TIMER_OFFSET]));
88                 builder.setKeepalive((short) UnsignedBytes.toInt(bytes[KEEPALIVE_F_OFFSET]));
89                 builder.setSessionId((short) UnsignedBytes.toInt(bytes[SID_F_OFFSET]));
90
91                 final TlvsBuilder tbuilder = new TlvsBuilder();
92                 parseTlvs(tbuilder, ByteArray.cutBytes(bytes, TLVS_OFFSET));
93                 builder.setTlvs(tbuilder.build());
94
95                 final Open obj = builder.build();
96                 if (versionValue != PCEP_VERSION) {
97                         // TODO: Should we move this check into the negotiator
98                         LOG.debug("Unsupported PCEP version {}", versionValue);
99                         return new UnknownObject(PCEPErrors.PCEP_VERSION_NOT_SUPPORTED, obj);
100                 }
101
102                 return obj;
103         }
104
105         @Override
106         public void addTlv(final TlvsBuilder tbuilder, final Tlv tlv) {
107                 final Tlvs2Builder statefulBuilder = new Tlvs2Builder();
108                 if (tbuilder.getAugmentation(Tlvs2.class) != null) {
109                         final Tlvs2 t = tbuilder.getAugmentation(Tlvs2.class);
110                         if (t.getClass() != null) {
111                                 statefulBuilder.setStateful(t.getStateful());
112                         }
113                 }
114                 if (tlv instanceof OfList) {
115                         tbuilder.setOfList((OfList) tlv);
116                 } else if (tlv instanceof Stateful) {
117                         statefulBuilder.setStateful((Stateful) tlv);
118                 }
119                 tbuilder.addAugmentation(Tlvs2.class, statefulBuilder.build());
120         }
121
122         @Override
123         public byte[] serializeObject(final Object object) {
124                 if (!(object instanceof Open)) {
125                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed OpenObject.");
126                 }
127                 final Open open = (Open) object;
128
129                 final byte versionFlagMF = (byte) (PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH));
130
131                 final byte[] tlvs = serializeTlvs(open.getTlvs());
132
133                 final byte[] bytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
134
135                 bytes[VER_FLAGS_MF_OFFSET] = versionFlagMF;
136                 bytes[KEEPALIVE_F_OFFSET] = UnsignedBytes.checkedCast(open.getKeepalive());
137                 bytes[DEAD_TIMER_OFFSET] = UnsignedBytes.checkedCast(open.getDeadTimer());
138                 bytes[SID_F_OFFSET] = UnsignedBytes.checkedCast(open.getSessionId());
139                 if (tlvs.length != 0) {
140                         ByteArray.copyWhole(tlvs, bytes, TLVS_OFFSET);
141                 }
142                 return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), bytes);
143         }
144
145         public byte[] serializeTlvs(final Tlvs tlvs) {
146                 if (tlvs == null) {
147                         return new byte[0];
148                 }
149                 int finalLength = 0;
150                 byte[] ofListBytes = null;
151                 byte[] statefulBytes = null;
152                 if (tlvs.getOfList() != null) {
153                         ofListBytes = serializeTlv(tlvs.getOfList());
154                         finalLength += ofListBytes.length;
155                 }
156                 if (tlvs.getAugmentation(Tlvs2.class) != null) {
157                         final Tlvs2 statefulTlvs = tlvs.getAugmentation(Tlvs2.class);
158                         if (statefulTlvs.getStateful() != null) {
159                                 statefulBytes = serializeTlv(statefulTlvs.getStateful());
160                                 finalLength += statefulBytes.length;
161                         }
162                 }
163
164                 int offset = 0;
165                 final byte[] result = new byte[finalLength];
166                 if (ofListBytes != null) {
167                         ByteArray.copyWhole(ofListBytes, result, offset);
168                         offset += ofListBytes.length;
169                 }
170                 if (statefulBytes != null) {
171                         ByteArray.copyWhole(statefulBytes, result, offset);
172                         offset += statefulBytes.length;
173                 }
174                 return result;
175         }
176
177         @Override
178         public int getObjectType() {
179                 return TYPE;
180         }
181
182         @Override
183         public int getObjectClass() {
184                 return CLASS;
185         }
186 }