Enable restart of CSS modules on blueprint container restart
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / tlv / OFListTlvParser.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.tlv;
9
10 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedShort;
11
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Lists;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.util.List;
17 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
18 import org.opendaylight.protocol.pcep.spi.TlvParser;
19 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
20 import org.opendaylight.protocol.pcep.spi.TlvUtil;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OfId;
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.of.list.tlv.OfListBuilder;
25
26 /**
27  * Parser for {@link OfList}
28  */
29 public class OFListTlvParser implements TlvParser, TlvSerializer {
30
31     public static final int TYPE = 4;
32
33     private static final int OF_CODE_ELEMENT_LENGTH = 2;
34
35     @Override
36     public OfList parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
37         if (buffer == null) {
38             return null;
39         }
40         if (buffer.readableBytes() % OF_CODE_ELEMENT_LENGTH != 0) {
41             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ".");
42         }
43         final List<OfId> ofCodes = Lists.newArrayList();
44         while (buffer.isReadable()) {
45             ofCodes.add(new OfId(buffer.readUnsignedShort()));
46         }
47         return new OfListBuilder().setCodes(ofCodes).build();
48     }
49
50     @Override
51     public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
52         Preconditions.checkArgument(tlv instanceof OfList, "OFListTlv is mandatory.");
53         final OfList oft = (OfList) tlv;
54         final ByteBuf body = Unpooled.buffer();
55         final List<OfId> ofCodes = oft.getCodes();
56         for (OfId id : ofCodes) {
57             writeUnsignedShort(id.getValue(), body);
58         }
59         TlvUtil.formatTlv(TYPE, body, buffer);
60     }
61 }