Bump MDSAL to 4.0.0
[bgpcep.git] / pcep / segment-routing / src / main / java / org / opendaylight / protocol / pcep / segment / routing / AbstractSrSubobjectParser.java
1 /*
2  * Copyright (c) 2015 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 package org.opendaylight.protocol.pcep.segment.routing;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv4Address;
12 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv6Address;
13 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
14 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
15
16 import io.netty.buffer.ByteBuf;
17 import io.netty.buffer.Unpooled;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
19 import org.opendaylight.protocol.util.BitArray;
20 import org.opendaylight.protocol.util.ByteBufWriteUtil;
21 import org.opendaylight.protocol.util.Ipv4Util;
22 import org.opendaylight.protocol.util.Ipv6Util;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressNoZone;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev181109.SidType;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev181109.SrSubobject;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev181109.sr.subobject.Nai;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev181109.sr.subobject.nai.IpAdjacency;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev181109.sr.subobject.nai.IpAdjacencyBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev181109.sr.subobject.nai.IpNodeId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev181109.sr.subobject.nai.IpNodeIdBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev181109.sr.subobject.nai.UnnumberedAdjacency;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev181109.sr.subobject.nai.UnnumberedAdjacencyBuilder;
33
34 public abstract class AbstractSrSubobjectParser   {
35
36     protected static final int MINIMAL_LENGTH = 4;
37     protected static final int BITSET_LENGTH = 8;
38     protected static final int M_FLAG_POSITION = 7;
39     protected static final int C_FLAG_POSITION = 6;
40     protected static final int S_FLAG_POSITION = 5;
41     protected static final int F_FLAG_POSITION = 4;
42     protected static final int MPLS_LABEL_OFFSET = 12;
43
44     private static final int SID_TYPE_BITS_OFFSET = 4;
45
46     private static class SrSubobjectImpl implements SrSubobject {
47         private final boolean mflag;
48         private final boolean cflag;
49         private final SidType sidType;
50         private final Long sid;
51         private final Nai nai;
52
53         SrSubobjectImpl(final boolean mflag, final boolean cflag, final SidType sidType, final Long sid,
54                 final Nai nai) {
55             this.mflag = mflag;
56             this.cflag = cflag;
57             this.sidType = sidType;
58             this.sid = sid;
59             this.nai = nai;
60         }
61
62         @Override
63         public Class<SrSubobject> implementedInterface() {
64             return SrSubobject.class;
65         }
66
67         @Override
68         public Boolean isMFlag() {
69             return this.mflag;
70         }
71
72         @Override
73         public Boolean isCFlag() {
74             return this.cflag;
75         }
76
77         @Override
78         public SidType getSidType() {
79             return this.sidType;
80         }
81
82         @Override
83         public Long getSid() {
84             return this.sid;
85         }
86
87         @Override
88         public Nai getNai() {
89             return this.nai;
90         }
91     }
92
93     public ByteBuf serializeSubobject(final SrSubobject srSubobject) {
94         final ByteBuf buffer = Unpooled.buffer(MINIMAL_LENGTH);
95         // sid type
96         writeUnsignedByte((short)(srSubobject.getSidType().getIntValue() << SID_TYPE_BITS_OFFSET), buffer);
97
98         final BitArray bits = new BitArray(BITSET_LENGTH);
99         bits.set(M_FLAG_POSITION, srSubobject.isMFlag());
100         bits.set(C_FLAG_POSITION, srSubobject.isCFlag());
101         if (srSubobject.getSid() == null) {
102             bits.set(S_FLAG_POSITION, Boolean.TRUE);
103         }
104         if (srSubobject.getNai() == null) {
105             bits.set(F_FLAG_POSITION, Boolean.TRUE);
106         }
107         // bits
108         bits.toByteBuf(buffer);
109         // sid
110         checkArgument(srSubobject.getNai() != null || srSubobject.getSid() != null,
111                 "Both SID and NAI are absent in SR subobject.");
112         if (srSubobject.getSid() != null) {
113             if (srSubobject.isMFlag()) {
114                 writeUnsignedInt(srSubobject.getSid() << MPLS_LABEL_OFFSET, buffer);
115             } else {
116                 writeUnsignedInt(srSubobject.getSid(), buffer);
117             }
118         }
119         // nai
120         final Nai nai = srSubobject.getNai();
121         if (nai != null) {
122             serializeNai(nai, srSubobject.getSidType() ,buffer);
123         }
124         return buffer;
125     }
126
127     private static void serializeNai(final Nai nai, final SidType sidType, final ByteBuf buffer) {
128         switch (sidType) {
129             case Ipv4NodeId:
130                 writeIpv4Address(((IpNodeId) nai).getIpAddress().getIpv4AddressNoZone(), buffer);
131                 break;
132             case Ipv6NodeId:
133                 writeIpv6Address(((IpNodeId) nai).getIpAddress().getIpv6AddressNoZone(), buffer);
134                 break;
135             case Ipv4Adjacency:
136                 writeIpv4Address(((IpAdjacency) nai).getLocalIpAddress().getIpv4AddressNoZone(), buffer);
137                 writeIpv4Address(((IpAdjacency) nai).getRemoteIpAddress().getIpv4AddressNoZone(), buffer);
138                 break;
139             case Ipv6Adjacency:
140                 writeIpv6Address(((IpAdjacency) nai).getLocalIpAddress().getIpv6AddressNoZone(), buffer);
141                 writeIpv6Address(((IpAdjacency) nai).getRemoteIpAddress().getIpv6AddressNoZone(), buffer);
142                 break;
143             case Unnumbered:
144                 final UnnumberedAdjacency unnumbered = (UnnumberedAdjacency) nai;
145                 ByteBufWriteUtil.writeUnsignedInt(unnumbered.getLocalNodeId(), buffer);
146                 ByteBufWriteUtil.writeUnsignedInt(unnumbered.getLocalInterfaceId(), buffer);
147                 ByteBufWriteUtil.writeUnsignedInt(unnumbered.getRemoteNodeId(), buffer);
148                 ByteBufWriteUtil.writeUnsignedInt(unnumbered.getRemoteInterfaceId(), buffer);
149                 break;
150             default:
151                 break;
152         }
153     }
154
155     private static Nai parseNai(final SidType sidType, final ByteBuf buffer) {
156         switch (sidType) {
157             case Ipv4NodeId:
158                 return new IpNodeIdBuilder().setIpAddress(
159                     new IpAddressNoZone(Ipv4Util.noZoneAddressForByteBuf(buffer))).build();
160             case Ipv6NodeId:
161                 return new IpNodeIdBuilder().setIpAddress(
162                     new IpAddressNoZone(Ipv6Util.noZoneAddressForByteBuf(buffer))).build();
163             case Ipv4Adjacency:
164                 return new IpAdjacencyBuilder()
165                         .setLocalIpAddress(new IpAddressNoZone(Ipv4Util.noZoneAddressForByteBuf(buffer)))
166                         .setRemoteIpAddress(new IpAddressNoZone(Ipv4Util.noZoneAddressForByteBuf(buffer))).build();
167             case Ipv6Adjacency:
168                 return new IpAdjacencyBuilder()
169                         .setLocalIpAddress(new IpAddressNoZone(Ipv6Util.noZoneAddressForByteBuf(buffer)))
170                         .setRemoteIpAddress(new IpAddressNoZone(Ipv6Util.noZoneAddressForByteBuf(buffer))).build();
171             case Unnumbered:
172                 return new UnnumberedAdjacencyBuilder().setLocalNodeId(buffer.readUnsignedInt())
173                         .setLocalInterfaceId(buffer.readUnsignedInt()).setRemoteNodeId(buffer.readUnsignedInt())
174                         .setRemoteInterfaceId(buffer.readUnsignedInt()).build();
175             default:
176                 return null;
177         }
178     }
179
180     protected static SrSubobject parseSrSubobject(final ByteBuf buffer) throws PCEPDeserializerException {
181         final int sidTypeByte = buffer.readByte() >> SID_TYPE_BITS_OFFSET;
182         final SidType sidType = SidType.forValue(sidTypeByte);
183         final BitArray bitSet = BitArray.valueOf(buffer.readByte());
184         final boolean f = bitSet.get(F_FLAG_POSITION);
185         final boolean s = bitSet.get(S_FLAG_POSITION);
186         final boolean c = bitSet.get(C_FLAG_POSITION);
187         final boolean m = bitSet.get(M_FLAG_POSITION);
188
189         if (f && s) {
190             throw new PCEPDeserializerException("Both SID and NAI are absent in SR subobject.");
191         }
192         Long tmp = null;
193         if (!s) {
194             if (m) {
195                 tmp = buffer.readUnsignedInt() >>> MPLS_LABEL_OFFSET;
196             } else {
197                 tmp = buffer.readUnsignedInt();
198             }
199         }
200         final Long sid = tmp;
201         final Nai nai;
202         if (sidType != null && !f) {
203             nai = parseNai(sidType, buffer);
204         } else {
205             nai = null;
206         }
207         return new SrSubobjectImpl(m, c, sidType, sid, nai);
208     }
209 }