Move pcep base parser Activator to its own bundle
[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.rev131005.Object;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ProtocolVersion;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.list.tlv.OfList;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.Tlvs;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.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     public static final int CLASS = 1;
45
46     public 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);
62     }
63
64     @Override
65     public Object parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
66         Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
67         final int versionValue = ByteArray.copyBitsRange(bytes.readByte(), VERSION_SF_OFFSET, VERSION_SF_LENGTH);
68
69         final OpenBuilder builder = new OpenBuilder();
70         builder.setVersion(new ProtocolVersion((short) versionValue));
71         builder.setProcessingRule(header.isProcessingRule());
72         builder.setIgnore(header.isIgnore());
73         final short keepalive = bytes.readUnsignedByte();
74         builder.setKeepalive(keepalive);
75         final short deadTimer = bytes.readUnsignedByte();
76         if(keepalive == 0) {
77             builder.setDeadTimer((short) 0);
78         } else {
79             builder.setDeadTimer(deadTimer);
80         }
81         builder.setSessionId(bytes.readUnsignedByte());
82
83         final TlvsBuilder tbuilder = new TlvsBuilder();
84         parseTlvs(tbuilder, bytes.slice());
85         builder.setTlvs(tbuilder.build());
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         Preconditions.checkArgument(object instanceof Open, "Wrong instance of PCEPObject. Passed %s. Needed OpenObject.", object.getClass());
107         final Open open = (Open) object;
108         final ByteBuf body = Unpooled.buffer();
109         writeUnsignedByte((short) (PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH)), body);
110         writeUnsignedByte(open.getKeepalive(), body);
111         writeUnsignedByte(open.getDeadTimer(), body);
112         Preconditions.checkArgument(open.getSessionId() != null, "SessionId is mandatory.");
113         writeUnsignedByte(open.getSessionId(), body);
114         serializeTlvs(open.getTlvs(), body);
115         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
116     }
117
118     public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
119         if (tlvs == null) {
120             return;
121         }
122         if (tlvs.getOfList() != null) {
123             serializeTlv(tlvs.getOfList(), body);
124         }
125         serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
126     }
127
128     @Override
129     protected final void addVendorInformationTlvs(final TlvsBuilder builder, final List<VendorInformationTlv> tlvs) {
130         if (!tlvs.isEmpty()) {
131             builder.setVendorInformationTlv(tlvs);
132         }
133     }
134 }