2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.segment.routing;
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;
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;
34 public abstract class AbstractSrSubobjectParser {
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;
44 private static final int SID_TYPE_BITS_OFFSET = 4;
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;
53 SrSubobjectImpl(final boolean mflag, final boolean cflag, final SidType sidType, final Long sid,
57 this.sidType = sidType;
63 public Class<SrSubobject> implementedInterface() {
64 return SrSubobject.class;
68 public Boolean isMFlag() {
73 public Boolean isCFlag() {
78 public SidType getSidType() {
83 public Long getSid() {
93 public ByteBuf serializeSubobject(final SrSubobject srSubobject) {
94 final ByteBuf buffer = Unpooled.buffer(MINIMAL_LENGTH);
96 writeUnsignedByte((short)(srSubobject.getSidType().getIntValue() << SID_TYPE_BITS_OFFSET), buffer);
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);
104 if (srSubobject.getNai() == null) {
105 bits.set(F_FLAG_POSITION, Boolean.TRUE);
108 bits.toByteBuf(buffer);
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);
116 writeUnsignedInt(srSubobject.getSid(), buffer);
120 final Nai nai = srSubobject.getNai();
122 serializeNai(nai, srSubobject.getSidType() ,buffer);
127 private static void serializeNai(final Nai nai, final SidType sidType, final ByteBuf buffer) {
130 writeIpv4Address(((IpNodeId) nai).getIpAddress().getIpv4AddressNoZone(), buffer);
133 writeIpv6Address(((IpNodeId) nai).getIpAddress().getIpv6AddressNoZone(), buffer);
136 writeIpv4Address(((IpAdjacency) nai).getLocalIpAddress().getIpv4AddressNoZone(), buffer);
137 writeIpv4Address(((IpAdjacency) nai).getRemoteIpAddress().getIpv4AddressNoZone(), buffer);
140 writeIpv6Address(((IpAdjacency) nai).getLocalIpAddress().getIpv6AddressNoZone(), buffer);
141 writeIpv6Address(((IpAdjacency) nai).getRemoteIpAddress().getIpv6AddressNoZone(), buffer);
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);
155 private static Nai parseNai(final SidType sidType, final ByteBuf buffer) {
158 return new IpNodeIdBuilder().setIpAddress(
159 new IpAddressNoZone(Ipv4Util.noZoneAddressForByteBuf(buffer))).build();
161 return new IpNodeIdBuilder().setIpAddress(
162 new IpAddressNoZone(Ipv6Util.noZoneAddressForByteBuf(buffer))).build();
164 return new IpAdjacencyBuilder()
165 .setLocalIpAddress(new IpAddressNoZone(Ipv4Util.noZoneAddressForByteBuf(buffer)))
166 .setRemoteIpAddress(new IpAddressNoZone(Ipv4Util.noZoneAddressForByteBuf(buffer))).build();
168 return new IpAdjacencyBuilder()
169 .setLocalIpAddress(new IpAddressNoZone(Ipv6Util.noZoneAddressForByteBuf(buffer)))
170 .setRemoteIpAddress(new IpAddressNoZone(Ipv6Util.noZoneAddressForByteBuf(buffer))).build();
172 return new UnnumberedAdjacencyBuilder().setLocalNodeId(buffer.readUnsignedInt())
173 .setLocalInterfaceId(buffer.readUnsignedInt()).setRemoteNodeId(buffer.readUnsignedInt())
174 .setRemoteInterfaceId(buffer.readUnsignedInt()).build();
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);
190 throw new PCEPDeserializerException("Both SID and NAI are absent in SR subobject.");
195 tmp = buffer.readUnsignedInt() >>> MPLS_LABEL_OFFSET;
197 tmp = buffer.readUnsignedInt();
200 final Long sid = tmp;
202 if (sidType != null && !f) {
203 nai = parseNai(sidType, buffer);
207 return new SrSubobjectImpl(m, c, sidType, sid, nai);