BUG-47 : switched subobjects to generated source code.
[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.PCEPDeserializerException;
12 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
13 import org.opendaylight.protocol.pcep.PCEPErrors;
14 import org.opendaylight.protocol.pcep.impl.Util;
15 import org.opendaylight.protocol.pcep.impl.message.AbstractObjectWithTlvsParser;
16 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
17 import org.opendaylight.protocol.util.ByteArray;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.LspDbVersionTlv;
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.OfListTlv;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OpenObject;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PredundancyGroupIdTlv;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ProtocolVersion;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.StatefulCapabilityTlv;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.open.message.OpenBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Tlvs;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.TlvsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.tlvs.LspDbVersionBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.tlvs.OfListBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.tlvs.PredundancyGroupIdBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.tlvs.StatefulBuilder;
34
35 import com.google.common.primitives.UnsignedBytes;
36
37 /**
38  * Parser for {@link OpenObject}
39  */
40
41 public class PCEPOpenObjectParser extends AbstractObjectWithTlvsParser<OpenBuilder> {
42
43         public static final int CLASS = 1;
44
45         public static final int TYPE = 1;
46
47         /*
48          * lengths of fields in bytes
49          */
50         public static final int VER_FLAGS_MF_LENGTH = 1; // multi-field
51         public static final int KEEPALIVE_F_LENGTH = 1;
52         public static final int DEAD_TIMER_LENGTH = 1;
53         public static final int SID_F_LENGTH = 1;
54
55         /*
56          * lengths of subfields inside multi-field in bits
57          */
58         public static final int VERSION_SF_LENGTH = 3;
59         public static final int FLAGS_SF_LENGTH = 5;
60
61         /*
62          * offsets of field in bytes
63          */
64
65         public static final int VER_FLAGS_MF_OFFSET = 0;
66         public static final int KEEPALIVE_F_OFFSET = VER_FLAGS_MF_OFFSET + VER_FLAGS_MF_LENGTH;
67         public static final int DEAD_TIMER_OFFSET = KEEPALIVE_F_OFFSET + KEEPALIVE_F_LENGTH;
68         public static final int SID_F_OFFSET = DEAD_TIMER_OFFSET + DEAD_TIMER_LENGTH;
69         public static final int TLVS_OFFSET = SID_F_OFFSET + SID_F_LENGTH;
70
71         /*
72          * offsets of subfields inside multi-field in bits
73          */
74
75         public static final int VERSION_SF_OFFSET = 0;
76         public static final int FLAGS_SF_OFFSET = VERSION_SF_LENGTH + VERSION_SF_OFFSET;
77
78         private static final int PCEP_VERSION = 1;
79
80         public PCEPOpenObjectParser(final TlvHandlerRegistry tlvReg) {
81                 super(tlvReg);
82         }
83
84         @Override
85         public OpenObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException, PCEPDocumentedException {
86                 if (bytes == null || bytes.length == 0) {
87                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
88                 }
89
90                 final int versionValue = ByteArray.copyBitsRange(bytes[VER_FLAGS_MF_OFFSET], VERSION_SF_OFFSET, VERSION_SF_LENGTH);
91
92                 if (versionValue != PCEP_VERSION) {
93                         throw new PCEPDocumentedException("Unsupported PCEP version " + versionValue, PCEPErrors.PCEP_VERSION_NOT_SUPPORTED);
94                 }
95
96                 final OpenBuilder builder = new OpenBuilder();
97
98                 parseTlvs(builder, ByteArray.cutBytes(bytes, TLVS_OFFSET));
99
100                 builder.setVersion(new ProtocolVersion((short) versionValue));
101                 builder.setProcessingRule(header.isProcessingRule());
102                 builder.setIgnore(header.isIgnore());
103                 builder.setDeadTimer((short) UnsignedBytes.toInt(bytes[DEAD_TIMER_OFFSET]));
104                 builder.setKeepalive((short) UnsignedBytes.toInt(bytes[KEEPALIVE_F_OFFSET]));
105                 builder.setSessionId((short) UnsignedBytes.toInt(bytes[SID_F_OFFSET]));
106                 return builder.build();
107         }
108
109         @Override
110         public void addTlv(final OpenBuilder builder, final Tlv tlv) {
111                 final TlvsBuilder tbuilder = new TlvsBuilder();
112                 if (tlv instanceof OfListTlv) {
113                         tbuilder.setOfList(new OfListBuilder().setCodes(((OfListTlv) tlv).getCodes()).build());
114                 } else if (tlv instanceof StatefulCapabilityTlv) {
115                         tbuilder.setStateful(new StatefulBuilder().setFlags(((StatefulCapabilityTlv) tlv).getFlags()).build());
116                 } else if (tlv instanceof PredundancyGroupIdTlv) {
117                         tbuilder.setPredundancyGroupId(new PredundancyGroupIdBuilder().setIdentifier(((PredundancyGroupIdTlv) tlv).getIdentifier()).build());
118                 } else if (tlv instanceof LspDbVersionTlv) {
119                         tbuilder.setLspDbVersion(new LspDbVersionBuilder().build());
120                 }
121                 builder.setTlvs(tbuilder.build());
122         }
123
124         @Override
125         public byte[] serializeObject(final Object object) {
126                 if (!(object instanceof OpenObject)) {
127                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed OpenObject.");
128                 }
129                 final OpenObject open = (OpenObject) object;
130
131                 final byte versionFlagMF = (byte) (PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH));
132
133                 final byte[] tlvs = serializeTlvs(open.getTlvs());
134
135                 final byte[] bytes = new byte[TLVS_OFFSET + tlvs.length + Util.getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
136
137                 bytes[VER_FLAGS_MF_OFFSET] = versionFlagMF;
138                 bytes[KEEPALIVE_F_OFFSET] = ByteArray.shortToBytes(open.getKeepalive())[1];
139                 bytes[DEAD_TIMER_OFFSET] = ByteArray.shortToBytes(open.getDeadTimer())[1];
140                 bytes[SID_F_OFFSET] = ByteArray.shortToBytes(open.getSessionId())[1];
141                 ByteArray.copyWhole(tlvs, bytes, TLVS_OFFSET);
142
143                 return bytes;
144         }
145
146         public byte[] serializeTlvs(final Tlvs tlvs) {
147                 int finalLength = 0;
148                 if (tlvs.getLspDbVersion() != null) {
149                         final byte[] lspDbBytes = serializeTlv(new LspDbVersionBuilder().setVersion(tlvs.getLspDbVersion().getVersion()).build());
150                         finalLength = lspDbBytes.length;
151                 }
152                 if (tlvs.getOfList() != null) {
153                         final byte[] ofListBytes = serializeTlv(new OfListBuilder().setCodes(tlvs.getOfList().getCodes()).build());
154                         finalLength = ofListBytes.length;
155                 }
156
157                 // FIXME: finish
158
159                 final byte[] bytes = new byte[finalLength];
160
161                 // FIXME copy result bytes
162                 return bytes;
163         }
164
165         @Override
166         public int getObjectType() {
167                 return TYPE;
168         }
169
170         @Override
171         public int getObjectClass() {
172                 return CLASS;
173         }
174 }