Removed checkstyle warnings.
[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 com.google.common.base.Preconditions;
12 import com.google.common.primitives.UnsignedBytes;
13
14 import io.netty.buffer.ByteBuf;
15
16 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
17 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
19 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
20 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
21 import org.opendaylight.protocol.pcep.spi.UnknownObject;
22 import org.opendaylight.protocol.util.ByteArray;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ProtocolVersion;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.list.tlv.OfList;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.Tlvs;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Parser for {@link Open}
37  */
38 public class PCEPOpenObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
39     private static final Logger LOG = LoggerFactory.getLogger(PCEPOpenObjectParser.class);
40
41     public static final int CLASS = 1;
42
43     public static final int TYPE = 1;
44
45     /*
46      * lengths of fields in bytes
47      */
48     private static final int VER_FLAGS_MF_LENGTH = 1;
49     private static final int KEEPALIVE_F_LENGTH = 1;
50     private static final int DEAD_TIMER_LENGTH = 1;
51     private static final int SID_F_LENGTH = 1;
52
53     /*
54      * lengths of subfields inside multi-field in bits
55      */
56     private static final int VERSION_SF_LENGTH = 3;
57
58     /*
59      * offsets of field in bytes
60      */
61     private static final int VER_FLAGS_MF_OFFSET = 0;
62     private static final int KEEPALIVE_F_OFFSET = VER_FLAGS_MF_OFFSET + VER_FLAGS_MF_LENGTH;
63     private static final int DEAD_TIMER_OFFSET = KEEPALIVE_F_OFFSET + KEEPALIVE_F_LENGTH;
64     private static final int SID_F_OFFSET = DEAD_TIMER_OFFSET + DEAD_TIMER_LENGTH;
65     private static final int TLVS_OFFSET = SID_F_OFFSET + SID_F_LENGTH;
66
67     /*
68      * offsets of subfields inside multi-field in bits
69      */
70     private static final int VERSION_SF_OFFSET = 0;
71
72     private static final int PCEP_VERSION = 1;
73
74     public PCEPOpenObjectParser(final TlvRegistry tlvReg) {
75         super(tlvReg);
76     }
77
78     @Override
79     public Object parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
80         Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
81         final int versionValue = ByteArray.copyBitsRange(bytes.readByte(), VERSION_SF_OFFSET, VERSION_SF_LENGTH);
82
83         final OpenBuilder builder = new OpenBuilder();
84         builder.setVersion(new ProtocolVersion((short) versionValue));
85         builder.setProcessingRule(header.isProcessingRule());
86         builder.setIgnore(header.isIgnore());
87         builder.setKeepalive((short) UnsignedBytes.toInt(bytes.readByte()));
88         builder.setDeadTimer((short) UnsignedBytes.toInt(bytes.readByte()));
89         builder.setSessionId((short) UnsignedBytes.toInt(bytes.readByte()));
90
91         final TlvsBuilder tbuilder = new TlvsBuilder();
92         parseTlvs(tbuilder, bytes.slice());
93         builder.setTlvs(tbuilder.build());
94
95         final Open obj = builder.build();
96         if (versionValue != PCEP_VERSION) {
97             // TODO: Should we move this check into the negotiator
98             LOG.debug("Unsupported PCEP version {}", versionValue);
99             return new UnknownObject(PCEPErrors.PCEP_VERSION_NOT_SUPPORTED, obj);
100         }
101
102         return obj;
103     }
104
105     @Override
106     public void addTlv(final TlvsBuilder tbuilder, final Tlv tlv) {
107         if (tlv instanceof OfList) {
108             tbuilder.setOfList((OfList) tlv);
109         }
110     }
111
112     @Override
113     public byte[] serializeObject(final Object object) {
114         if (!(object instanceof Open)) {
115             throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed OpenObject.");
116         }
117         final Open open = (Open) object;
118
119         final byte versionFlagMF = (byte) (PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH));
120
121         final byte[] tlvs = serializeTlvs(open.getTlvs());
122
123         final byte[] bytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
124
125         bytes[VER_FLAGS_MF_OFFSET] = versionFlagMF;
126         bytes[KEEPALIVE_F_OFFSET] = UnsignedBytes.checkedCast(open.getKeepalive());
127         bytes[DEAD_TIMER_OFFSET] = UnsignedBytes.checkedCast(open.getDeadTimer());
128         bytes[SID_F_OFFSET] = UnsignedBytes.checkedCast(open.getSessionId());
129         if (tlvs.length != 0) {
130             ByteArray.copyWhole(tlvs, bytes, TLVS_OFFSET);
131         }
132         return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), bytes);
133     }
134
135     public byte[] serializeTlvs(final Tlvs tlvs) {
136         if (tlvs == null) {
137             return new byte[0];
138         }
139         byte[] ofListBytes = null;
140         if (tlvs.getOfList() != null) {
141             ofListBytes = serializeTlv(tlvs.getOfList());
142         }
143         byte[] result = new byte[0];
144         if (ofListBytes != null) {
145             result = new byte[ofListBytes.length];
146             ByteArray.copyWhole(ofListBytes, result, 0);
147         }
148         return result;
149     }
150 }