Merge "BUG-1287: migrate tests to new APIs"
[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 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.util.ByteArray;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ProtocolVersion;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.list.tlv.OfList;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.Tlvs;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Parser for {@link Open}
38  */
39 public class PCEPOpenObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
40     private static final Logger LOG = LoggerFactory.getLogger(PCEPOpenObjectParser.class);
41
42     public static final int CLASS = 1;
43
44     public static final int TYPE = 1;
45
46     /*
47      * lengths of subfields inside multi-field in bits
48      */
49     private static final int VERSION_SF_LENGTH = 3;
50
51     /*
52      * offsets of subfields inside multi-field in bits
53      */
54     private static final int VERSION_SF_OFFSET = 0;
55
56     private static final int PCEP_VERSION = 1;
57
58     public PCEPOpenObjectParser(final TlvRegistry tlvReg) {
59         super(tlvReg);
60     }
61
62     @Override
63     public Object parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
64         Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
65         final int versionValue = ByteArray.copyBitsRange(bytes.readByte(), VERSION_SF_OFFSET, VERSION_SF_LENGTH);
66
67         final OpenBuilder builder = new OpenBuilder();
68         builder.setVersion(new ProtocolVersion((short) versionValue));
69         builder.setProcessingRule(header.isProcessingRule());
70         builder.setIgnore(header.isIgnore());
71         final short keepalive = (short) UnsignedBytes.toInt(bytes.readByte());
72         builder.setKeepalive(keepalive);
73         final short deadTimer = (short) UnsignedBytes.toInt(bytes.readByte());
74         if(keepalive == 0) {
75             builder.setDeadTimer((short) 0);
76         } else {
77             builder.setDeadTimer(deadTimer);
78         }
79         builder.setSessionId((short) UnsignedBytes.toInt(bytes.readByte()));
80
81         final TlvsBuilder tbuilder = new TlvsBuilder();
82         parseTlvs(tbuilder, bytes.slice());
83         builder.setTlvs(tbuilder.build());
84
85         final Open obj = builder.build();
86         if (versionValue != PCEP_VERSION) {
87             // TODO: Should we move this check into the negotiator
88             LOG.debug("Unsupported PCEP version {}", versionValue);
89             return new UnknownObject(PCEPErrors.PCEP_VERSION_NOT_SUPPORTED, obj);
90         }
91
92         return obj;
93     }
94
95     @Override
96     public void addTlv(final TlvsBuilder tbuilder, final Tlv tlv) {
97         if (tlv instanceof OfList) {
98             tbuilder.setOfList((OfList) tlv);
99         }
100     }
101
102     @Override
103     public void serializeObject(final Object object, final ByteBuf buffer) {
104         Preconditions.checkArgument(object instanceof Open, "Wrong instance of PCEPObject. Passed %s. Needed OpenObject.", object.getClass());
105         final Open open = (Open) object;
106         final ByteBuf body = Unpooled.buffer();
107         writeUnsignedByte((short) (PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH)), body);
108         writeUnsignedByte(open.getKeepalive(), body);
109         writeUnsignedByte(open.getDeadTimer(), body);
110         Preconditions.checkArgument(open.getSessionId() != null, "SessionId is mandatory.");
111         writeUnsignedByte(open.getSessionId(), body);
112         serializeTlvs(open.getTlvs(), body);
113         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
114     }
115
116     public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
117         if (tlvs == null) {
118             return;
119         }
120         if (tlvs.getOfList() != null) {
121             serializeTlv(tlvs.getOfList(), body);
122         }
123     }
124 }