Bug-731: fixed segment-routing sonar issues.
[bgpcep.git] / pcep / segment-routing / src / main / java / org / opendaylight / protocol / pcep / segment / routing02 / SrEroSubobjectParser.java
1 /*
2  * Copyright (c) 2014 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.segment.routing02;
10
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeBitSet;
12 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv4Address;
13 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv6Address;
14 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
15 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
16
17 import com.google.common.base.Preconditions;
18 import io.netty.buffer.ByteBuf;
19 import io.netty.buffer.Unpooled;
20 import java.util.BitSet;
21 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
22 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
23 import org.opendaylight.protocol.pcep.spi.EROSubobjectUtil;
24 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
25 import org.opendaylight.protocol.util.ByteArray;
26 import org.opendaylight.protocol.util.ByteBufWriteUtil;
27 import org.opendaylight.protocol.util.Ipv4Util;
28 import org.opendaylight.protocol.util.Ipv6Util;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.SidType;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.SrEroSubobject;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.SrEroSubobject.Flags;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.add.lsp.input.arguments.ero.subobject.subobject.type.SrEroTypeBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.sr.ero.subobject.Nai;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.sr.ero.subobject.nai.IpAdjacency;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.sr.ero.subobject.nai.IpAdjacencyBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.sr.ero.subobject.nai.IpNodeId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.sr.ero.subobject.nai.IpNodeIdBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.sr.ero.subobject.nai.UnnumberedAdjacency;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.sr.ero.subobject.nai.UnnumberedAdjacencyBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
44
45 public class SrEroSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
46
47     public static final int TYPE = 5;
48
49     private static final int SID_LENGTH = 4;
50     private static final int FLAGS_OFFSET = 1;
51     private static final int HEADER_LENGTH = FLAGS_OFFSET + 1;
52     private static final int MINIMAL_LENGTH = SID_LENGTH + HEADER_LENGTH;
53     private static final int SID_TYPE_BITS_OFFSET = 4;
54
55     private static final int M_FLAG_POSITION = 7;
56     private static final int C_FLAG_POSITION = 6;
57     private static final int S_FLAG_POSITION = 5;
58     private static final int F_FLAG_POSITION = 4;
59
60     @Override
61     public void serializeSubobject(Subobject subobject, final ByteBuf buffer) {
62         Preconditions.checkArgument(subobject.getSubobjectType() instanceof SrEroSubobject,
63                 "Unknown subobject instance. Passed %s. Needed SrEroSubobject.", subobject.getSubobjectType()
64                         .getClass());
65
66         final SrEroSubobject srEroSubobject = (SrEroSubobject) subobject.getSubobjectType();
67         final ByteBuf body = Unpooled.buffer(MINIMAL_LENGTH);
68         writeUnsignedByte((short)(srEroSubobject.getSidType().getIntValue() << SID_TYPE_BITS_OFFSET), body);
69
70         final Flags flags = srEroSubobject.getFlags();
71         final BitSet bits = new BitSet();
72         if (flags != null) {
73             bits.set(M_FLAG_POSITION, flags.isM());
74             bits.set(C_FLAG_POSITION, flags.isC());
75             bits.set(S_FLAG_POSITION, flags.isS());
76             bits.set(F_FLAG_POSITION, flags.isF());
77         }
78         writeBitSet(bits, FLAGS_OFFSET, body);
79
80         writeUnsignedInt(srEroSubobject.getSid(), body);
81
82         final Nai nai = srEroSubobject.getNai();
83         if (nai != null) {
84             switch (srEroSubobject.getSidType()) {
85             case Ipv4NodeId:
86                 writeIpv4Address(((IpNodeId) nai).getIpAddress().getIpv4Address(), body);
87                 break;
88             case Ipv6NodeId:
89                 writeIpv6Address(((IpNodeId) nai).getIpAddress().getIpv6Address(), body);
90                 break;
91             case Ipv4Adjacency:
92                 writeIpv4Address(((IpAdjacency) nai).getLocalIpAddress().getIpv4Address(), body);
93                 writeIpv4Address(((IpAdjacency) nai).getRemoteIpAddress().getIpv4Address(), body);
94                 break;
95             case Ipv6Adjacency:
96                 writeIpv6Address(((IpAdjacency) nai).getLocalIpAddress().getIpv6Address(), body);
97                 writeIpv6Address(((IpAdjacency) nai).getRemoteIpAddress().getIpv6Address(), body);
98                 break;
99             case Unnumbered:
100                 final UnnumberedAdjacency unnumbered = (UnnumberedAdjacency) nai;
101                 ByteBufWriteUtil.writeUnsignedInt(unnumbered.getLocalNodeId(), body);
102                 ByteBufWriteUtil.writeUnsignedInt(unnumbered.getLocalInterfaceId(), body);
103                 ByteBufWriteUtil.writeUnsignedInt(unnumbered.getRemoteNodeId(), body);
104                 ByteBufWriteUtil.writeUnsignedInt(unnumbered.getRemoteInterfaceId(), body);
105                 break;
106             }
107         }
108         EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), body, buffer);
109     }
110
111     @Override
112     public Subobject parseSubobject(ByteBuf buffer, boolean loose) throws PCEPDeserializerException {
113         Preconditions.checkArgument(buffer != null && buffer.isReadable(),
114                 "Array of bytes is mandatory. Can't be null or empty.");
115         if (buffer.readableBytes() <= MINIMAL_LENGTH) {
116             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes()
117                     + ";");
118         }
119         final SrEroTypeBuilder srEroSubobjectBuilder = new SrEroTypeBuilder();
120         final int sidTypeByte = buffer.readByte() >> SID_TYPE_BITS_OFFSET;
121         final SidType sidType = SidType.forValue(sidTypeByte);
122         srEroSubobjectBuilder.setSidType(sidType);
123
124         BitSet bitSet = ByteArray.bytesToBitSet(new byte[] { buffer.readByte() });
125         final boolean f = bitSet.get(F_FLAG_POSITION);
126         final boolean s = bitSet.get(S_FLAG_POSITION);
127         final boolean c = bitSet.get(C_FLAG_POSITION);
128         final boolean m = bitSet.get(M_FLAG_POSITION);
129         final Flags flags = new Flags(c, f, m, s);
130         srEroSubobjectBuilder.setFlags(flags);
131
132         final long sid = buffer.readUnsignedInt();
133         srEroSubobjectBuilder.setSid(sid);
134         if (sidType != null) {
135             switch (sidType) {
136             case Ipv4NodeId:
137                 srEroSubobjectBuilder.setNai(new IpNodeIdBuilder().setIpAddress(
138                         new IpAddress(new Ipv4Address(Ipv4Util.addressForByteBuf(buffer)))).build());
139                 break;
140             case Ipv6NodeId:
141                 srEroSubobjectBuilder.setNai(new IpNodeIdBuilder().setIpAddress(
142                         new IpAddress(Ipv6Util.addressForByteBuf(buffer))).build());
143                 break;
144             case Ipv4Adjacency:
145                 srEroSubobjectBuilder.setNai(new IpAdjacencyBuilder()
146                         .setLocalIpAddress(new IpAddress(Ipv4Util.addressForByteBuf(buffer)))
147                         .setRemoteIpAddress(new IpAddress(Ipv4Util.addressForByteBuf(buffer))).build());
148                 break;
149             case Ipv6Adjacency:
150                 srEroSubobjectBuilder.setNai(new IpAdjacencyBuilder()
151                         .setLocalIpAddress(new IpAddress(Ipv6Util.addressForByteBuf(buffer)))
152                         .setRemoteIpAddress(new IpAddress(Ipv6Util.addressForByteBuf(buffer))).build());
153                 break;
154             case Unnumbered:
155                 srEroSubobjectBuilder.setNai(new UnnumberedAdjacencyBuilder().setLocalNodeId(buffer.readUnsignedInt())
156                         .setLocalInterfaceId(buffer.readUnsignedInt()).setRemoteNodeId(buffer.readUnsignedInt())
157                         .setRemoteInterfaceId(buffer.readUnsignedInt()).build());
158                 break;
159             }
160         }
161         final SubobjectBuilder subobjectBuilder = new SubobjectBuilder();
162         subobjectBuilder.setLoose(loose);
163         subobjectBuilder.setSubobjectType(srEroSubobjectBuilder.build());
164         return subobjectBuilder.build();
165     }
166
167 }