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