Add new revision for pcep types model
[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
9 package org.opendaylight.protocol.pcep.parser.object;
10
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
12
13 import com.google.common.base.Preconditions;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.util.List;
17 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
18 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
19 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
20 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
21 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
22 import org.opendaylight.protocol.pcep.spi.UnknownObject;
23 import org.opendaylight.protocol.pcep.spi.VendorInformationTlvRegistry;
24 import org.opendaylight.protocol.util.ByteArray;
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.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         Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
66         final int versionValue = ByteArray.copyBitsRange(bytes.readByte(), VERSION_SF_OFFSET, VERSION_SF_LENGTH);
67
68         final OpenBuilder builder = new OpenBuilder();
69         builder.setVersion(new ProtocolVersion((short) versionValue));
70         builder.setProcessingRule(header.isProcessingRule());
71         builder.setIgnore(header.isIgnore());
72         final short keepalive = bytes.readUnsignedByte();
73         builder.setKeepalive(keepalive);
74         final short deadTimer = bytes.readUnsignedByte();
75         if(keepalive == 0) {
76             builder.setDeadTimer((short) 0);
77         } else {
78             builder.setDeadTimer(deadTimer);
79         }
80         builder.setSessionId(bytes.readUnsignedByte());
81
82         final TlvsBuilder tbuilder = new TlvsBuilder();
83         parseTlvs(tbuilder, bytes.slice());
84         builder.setTlvs(tbuilder.build());
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         Preconditions.checkArgument(object instanceof Open, "Wrong instance of PCEPObject. Passed %s. Needed OpenObject.", object.getClass());
106         final Open open = (Open) object;
107         final ByteBuf body = Unpooled.buffer();
108         writeUnsignedByte((short) (PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH)), body);
109         writeUnsignedByte(open.getKeepalive(), body);
110         writeUnsignedByte(open.getDeadTimer(), body);
111         Preconditions.checkArgument(open.getSessionId() != null, "SessionId is mandatory.");
112         writeUnsignedByte(open.getSessionId(), body);
113         serializeTlvs(open.getTlvs(), body);
114         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
115     }
116
117     public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
118         if (tlvs == null) {
119             return;
120         }
121         if (tlvs.getOfList() != null) {
122             serializeTlv(tlvs.getOfList(), body);
123         }
124         serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
125     }
126
127     @Override
128     protected final void addVendorInformationTlvs(final TlvsBuilder builder, final List<VendorInformationTlv> tlvs) {
129         if (!tlvs.isEmpty()) {
130             builder.setVendorInformationTlv(tlvs);
131         }
132     }
133 }