Clean pcep/base-parser code
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / 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 package org.opendaylight.protocol.pcep.parser.object;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.util.List;
16 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
17 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
19 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
20 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
21 import org.opendaylight.protocol.pcep.spi.UnknownObject;
22 import org.opendaylight.protocol.pcep.spi.VendorInformationTlvRegistry;
23 import org.opendaylight.protocol.util.ByteArray;
24 import org.opendaylight.protocol.util.ByteBufUtils;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ProtocolVersion;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Tlv;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.of.list.tlv.OfList;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.object.Open;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.object.OpenBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.object.open.Tlvs;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.object.open.TlvsBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.tlvs.VendorInformationTlv;
35 import org.opendaylight.yangtools.yang.common.Uint8;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * Parser for {@link Open}.
41  */
42 public class PCEPOpenObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
43     private static final Logger LOG = LoggerFactory.getLogger(PCEPOpenObjectParser.class);
44
45     private static final int CLASS = 1;
46     private static final int TYPE = 1;
47
48     /*
49      * lengths of subfields inside multi-field in bits
50      */
51     private static final int VERSION_SF_LENGTH = 3;
52
53     /*
54      * offsets of subfields inside multi-field in bits
55      */
56     private static final int VERSION_SF_OFFSET = 0;
57
58     private static final int PCEP_VERSION = 1;
59
60     public PCEPOpenObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
61         super(tlvReg, viTlvReg, CLASS, TYPE);
62     }
63
64     @Override
65     public Object parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
66         checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
67         final int versionValue = ByteArray.copyBitsRange(bytes.readByte(), VERSION_SF_OFFSET, VERSION_SF_LENGTH);
68
69         final short keepalive = bytes.readUnsignedByte();
70         final short deadTimer = bytes.readUnsignedByte();
71         final Uint8 sessionId = ByteBufUtils.readUint8(bytes);
72         final TlvsBuilder tbuilder = new TlvsBuilder();
73         parseTlvs(tbuilder, bytes.slice());
74         final OpenBuilder builder = new OpenBuilder()
75                 .setVersion(new ProtocolVersion(Uint8.valueOf(versionValue)))
76                 .setProcessingRule(header.isProcessingRule())
77                 .setIgnore(header.isIgnore())
78                 .setKeepalive(Uint8.valueOf(keepalive))
79                 .setSessionId(sessionId)
80                 .setTlvs(tbuilder.build());
81         if (keepalive == 0) {
82             builder.setDeadTimer(Uint8.ZERO);
83         } else {
84             builder.setDeadTimer(Uint8.valueOf(deadTimer));
85         }
86
87         final Open obj = builder.build();
88         if (versionValue != PCEP_VERSION) {
89             // TODO: Should we move this check into the negotiator
90             LOG.debug("Unsupported PCEP version {}", versionValue);
91             return new UnknownObject(PCEPErrors.PCEP_VERSION_NOT_SUPPORTED, obj);
92         }
93
94         return obj;
95     }
96
97     @Override
98     public void addTlv(final TlvsBuilder tbuilder, final Tlv tlv) {
99         if (tlv instanceof OfList) {
100             tbuilder.setOfList((OfList) tlv);
101         }
102     }
103
104     @Override
105     public void serializeObject(final Object object, final ByteBuf buffer) {
106         checkArgument(object instanceof Open, "Wrong instance of PCEPObject. Passed %s. Needed OpenObject.",
107             object.getClass());
108         final Open open = (Open) object;
109         final ByteBuf body = Unpooled.buffer();
110         writeUnsignedByte(Uint8.valueOf(PCEP_VERSION << Byte.SIZE - VERSION_SF_LENGTH), body);
111         writeUnsignedByte(open.getKeepalive(), body);
112         writeUnsignedByte(open.getDeadTimer(), body);
113         checkArgument(open.getSessionId() != null, "SessionId is mandatory.");
114         writeUnsignedByte(open.getSessionId(), body);
115         serializeTlvs(open.getTlvs(), body);
116         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
117     }
118
119     public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
120         if (tlvs == null) {
121             return;
122         }
123         if (tlvs.getOfList() != null) {
124             serializeTlv(tlvs.getOfList(), body);
125         }
126         serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
127     }
128
129     @Override
130     protected final void addVendorInformationTlvs(final TlvsBuilder builder, final List<VendorInformationTlv> tlvs) {
131         if (!tlvs.isEmpty()) {
132             builder.setVendorInformationTlv(tlvs);
133         }
134     }
135 }