bbd1896d2a5437703e2fca5c0878656b96eb6cbc
[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 static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
12
13 import com.google.common.base.Preconditions;
14 import com.google.common.primitives.UnsignedBytes;
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import java.util.List;
18 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
19 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
20 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
21 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
22 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
23 import org.opendaylight.protocol.pcep.spi.UnknownObject;
24 import org.opendaylight.protocol.pcep.spi.VendorInformationTlvRegistry;
25 import org.opendaylight.protocol.util.ByteArray;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ProtocolVersion;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.list.tlv.OfList;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.Tlvs;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.tlvs.VendorInformationTlv;
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     public static final int CLASS = 1;
46
47     public static final int TYPE = 1;
48
49     /*
50      * lengths of subfields inside multi-field in bits
51      */
52     private static final int VERSION_SF_LENGTH = 3;
53
54     /*
55      * offsets of subfields inside multi-field in bits
56      */
57     private static final int VERSION_SF_OFFSET = 0;
58
59     private static final int PCEP_VERSION = 1;
60
61     public PCEPOpenObjectParser(TlvRegistry tlvReg, VendorInformationTlvRegistry viTlvReg) {
62         super(tlvReg, viTlvReg);
63     }
64
65     @Override
66     public Object parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
67         Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
68         final int versionValue = ByteArray.copyBitsRange(bytes.readByte(), VERSION_SF_OFFSET, VERSION_SF_LENGTH);
69
70         final OpenBuilder builder = new OpenBuilder();
71         builder.setVersion(new ProtocolVersion((short) versionValue));
72         builder.setProcessingRule(header.isProcessingRule());
73         builder.setIgnore(header.isIgnore());
74         final short keepalive = (short) UnsignedBytes.toInt(bytes.readByte());
75         builder.setKeepalive(keepalive);
76         final short deadTimer = (short) UnsignedBytes.toInt(bytes.readByte());
77         if(keepalive == 0) {
78             builder.setDeadTimer((short) 0);
79         } else {
80             builder.setDeadTimer(deadTimer);
81         }
82         builder.setSessionId((short) UnsignedBytes.toInt(bytes.readByte()));
83
84         final TlvsBuilder tbuilder = new TlvsBuilder();
85         parseTlvs(tbuilder, bytes.slice());
86         builder.setTlvs(tbuilder.build());
87
88         final Open obj = builder.build();
89         if (versionValue != PCEP_VERSION) {
90             // TODO: Should we move this check into the negotiator
91             LOG.debug("Unsupported PCEP version {}", versionValue);
92             return new UnknownObject(PCEPErrors.PCEP_VERSION_NOT_SUPPORTED, obj);
93         }
94
95         return obj;
96     }
97
98     @Override
99     public void addTlv(final TlvsBuilder tbuilder, final Tlv tlv) {
100         if (tlv instanceof OfList) {
101             tbuilder.setOfList((OfList) tlv);
102         }
103     }
104
105     @Override
106     public void serializeObject(final Object object, final ByteBuf buffer) {
107         Preconditions.checkArgument(object instanceof Open, "Wrong instance of PCEPObject. Passed %s. Needed OpenObject.", object.getClass());
108         final Open open = (Open) object;
109         final ByteBuf body = Unpooled.buffer();
110         writeUnsignedByte((short) (PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH)), body);
111         writeUnsignedByte(open.getKeepalive(), body);
112         writeUnsignedByte(open.getDeadTimer(), body);
113         Preconditions.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 }