Merge "BUG-218: expose instruction status in MD-SAL"
[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.types.rev131005.Object;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ProtocolVersion;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.db.version.tlv.LspDbVersion;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.list.tlv.OfList;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.Tlvs;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.predundancy.group.id.tlv.PredundancyGroupId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.stateful.capability.tlv.Stateful;
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                 if (tlv instanceof OfList) {
108                         tbuilder.setOfList((OfList) tlv);
109                 } else if (tlv instanceof Stateful) {
110                         tbuilder.setStateful((Stateful) tlv);
111                 } else if (tlv instanceof PredundancyGroupId) {
112                         tbuilder.setPredundancyGroupId((PredundancyGroupId) tlv);
113                 } else if (tlv instanceof LspDbVersion) {
114                         tbuilder.setLspDbVersion((LspDbVersion) tlv);
115                 }
116         }
117
118         @Override
119         public byte[] serializeObject(final Object object) {
120                 if (!(object instanceof Open)) {
121                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed OpenObject.");
122                 }
123                 final Open open = (Open) object;
124
125                 final byte versionFlagMF = (byte) (PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH));
126
127                 final byte[] tlvs = serializeTlvs(open.getTlvs());
128
129                 final byte[] bytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
130
131                 bytes[VER_FLAGS_MF_OFFSET] = versionFlagMF;
132                 bytes[KEEPALIVE_F_OFFSET] = UnsignedBytes.checkedCast(open.getKeepalive());
133                 bytes[DEAD_TIMER_OFFSET] = UnsignedBytes.checkedCast(open.getDeadTimer());
134                 bytes[SID_F_OFFSET] = UnsignedBytes.checkedCast(open.getSessionId());
135                 if (tlvs.length != 0) {
136                         ByteArray.copyWhole(tlvs, bytes, TLVS_OFFSET);
137                 }
138                 return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), bytes);
139         }
140
141         public byte[] serializeTlvs(final Tlvs tlvs) {
142                 if (tlvs == null) {
143                         return new byte[0];
144                 }
145                 int finalLength = 0;
146                 byte[] ofListBytes = null;
147                 byte[] statefulBytes = null;
148                 byte[] predundancyBytes = null;
149                 byte[] lspDbBytes = null;
150                 if (tlvs.getOfList() != null) {
151                         ofListBytes = serializeTlv(tlvs.getOfList());
152                         finalLength += ofListBytes.length;
153                 }
154                 if (tlvs.getStateful() != null) {
155                         statefulBytes = serializeTlv(tlvs.getStateful());
156                         finalLength += statefulBytes.length;
157                 }
158                 if (tlvs.getPredundancyGroupId() != null) {
159                         predundancyBytes = serializeTlv(tlvs.getPredundancyGroupId());
160                         finalLength += predundancyBytes.length;
161                 }
162                 if (tlvs.getLspDbVersion() != null) {
163                         lspDbBytes = serializeTlv(tlvs.getLspDbVersion());
164                         finalLength += lspDbBytes.length;
165                 }
166                 int offset = 0;
167                 final byte[] result = new byte[finalLength];
168                 if (ofListBytes != null) {
169                         ByteArray.copyWhole(ofListBytes, result, offset);
170                         offset += ofListBytes.length;
171                 }
172                 if (statefulBytes != null) {
173                         ByteArray.copyWhole(statefulBytes, result, offset);
174                         offset += statefulBytes.length;
175                 }
176                 if (lspDbBytes != null) {
177                         ByteArray.copyWhole(lspDbBytes, result, offset);
178                         offset += lspDbBytes.length;
179                 }
180                 if (predundancyBytes != null) {
181                         ByteArray.copyWhole(predundancyBytes, result, offset);
182                         offset += predundancyBytes.length;
183                 }
184                 return result;
185         }
186
187         @Override
188         public int getObjectType() {
189                 return TYPE;
190         }
191
192         @Override
193         public int getObjectClass() {
194                 return CLASS;
195         }
196 }