2 * Copyright (c) 2014 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
9 package org.opendaylight.protocol.bgp.parser.spi.pojo;
11 import com.google.common.collect.Lists;
12 import io.netty.buffer.ByteBuf;
13 import java.util.List;
14 import org.junit.Assert;
15 import org.mockito.Mock;
16 import org.mockito.Mockito;
17 import org.mockito.MockitoAnnotations;
18 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
19 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
20 import org.opendaylight.protocol.bgp.parser.spi.AbstractBGPExtensionProviderActivator;
21 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
22 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
23 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
24 import org.opendaylight.protocol.bgp.parser.spi.BgpPrefixSidTlvParser;
25 import org.opendaylight.protocol.bgp.parser.spi.BgpPrefixSidTlvSerializer;
26 import org.opendaylight.protocol.bgp.parser.spi.CapabilityParser;
27 import org.opendaylight.protocol.bgp.parser.spi.CapabilitySerializer;
28 import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
29 import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
30 import org.opendaylight.protocol.bgp.parser.spi.NextHopParserSerializer;
31 import org.opendaylight.protocol.bgp.parser.spi.NlriParser;
32 import org.opendaylight.protocol.bgp.parser.spi.NlriSerializer;
33 import org.opendaylight.protocol.bgp.parser.spi.ParameterParser;
34 import org.opendaylight.protocol.bgp.parser.spi.ParameterSerializer;
35 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
36 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunityParser;
37 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunitySerializer;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.BgpParameters;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.optional.capabilities.CParameters;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.bgp.prefix.sid.bgp.prefix.sid.tlvs.BgpPrefixSidTlv;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlriBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlriBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv6AddressFamily;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.UnicastSubsequentAddressFamily;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.RouteTargetIpv4Case;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.CNextHop;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv4NextHopCase;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv6NextHopCase;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.ipv4.next.hop._case.Ipv4NextHopBuilder;
54 import org.opendaylight.yangtools.yang.binding.DataObject;
55 import org.opendaylight.yangtools.yang.binding.Notification;
57 public class BgpTestActivator extends AbstractBGPExtensionProviderActivator {
59 protected static final int TYPE = 0;
60 private static final String EMPTY = "";
63 protected AttributeSerializer attrSerializer;
65 protected AttributeParser attrParser;
68 protected ParameterParser paramParser;
70 protected ParameterSerializer paramSerializer;
73 protected CapabilityParser capaParser;
75 protected CapabilitySerializer capaSerializer;
78 protected MessageParser msgParser;
80 protected MessageSerializer msgSerializer;
83 protected NlriParser nlriParser;
85 protected NlriSerializer nlriSerializer;
88 protected ExtendedCommunityParser exParser;
90 protected ExtendedCommunitySerializer exSerializer;
92 protected NextHopParserSerializer nextHopParserSerializer;
95 protected BgpPrefixSidTlvParser sidTlvParser;
97 protected BgpPrefixSidTlvSerializer sidTlvSerializer;
100 protected List<AutoCloseable> startImpl(final BGPExtensionProviderContext context) {
102 final List<AutoCloseable> regs = Lists.newArrayList();
103 regs.add(context.registerAttributeParser(TYPE, this.attrParser));
104 regs.add(context.registerAttributeSerializer(DataObject.class, this.attrSerializer));
106 regs.add(context.registerParameterParser(TYPE, this.paramParser));
107 regs.add(context.registerParameterSerializer(BgpParameters.class, this.paramSerializer));
109 regs.add(context.registerCapabilityParser(TYPE, this.capaParser));
110 regs.add(context.registerCapabilitySerializer(CParameters.class, this.capaSerializer));
112 regs.add(context.registerBgpPrefixSidTlvParser(TYPE, this.sidTlvParser));
113 regs.add(context.registerBgpPrefixSidTlvSerializer(BgpPrefixSidTlv.class, this.sidTlvSerializer));
115 regs.add(context.registerMessageParser(TYPE, this.msgParser));
116 regs.add(context.registerMessageSerializer(Notification.class, this.msgSerializer));
118 regs.add(context.registerAddressFamily(Ipv4AddressFamily.class, 1));
119 regs.add(context.registerAddressFamily(Ipv6AddressFamily.class, 2));
120 regs.add(context.registerSubsequentAddressFamily(UnicastSubsequentAddressFamily.class, 1));
122 this.nextHopParserSerializer = new NextHopParserSerializer() {
124 public CNextHop parseNextHop(final ByteBuf buffer) throws BGPParsingException {
125 return new Ipv4NextHopCaseBuilder().setIpv4NextHop(new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("127.0.0.1")).build()).build();
128 public void serializeNextHop(final CNextHop cNextHop, final ByteBuf byteAggregator) {
129 final byte[] mpReachBytes = {
130 0x7f, 0x00, 0x00, 0x01
132 byteAggregator.writeBytes(mpReachBytes);
136 regs.add(context.registerNlriParser(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class, this
137 .nlriParser, this.nextHopParserSerializer, Ipv4NextHopCase.class));
138 regs.add(context.registerNlriParser(Ipv6AddressFamily.class, UnicastSubsequentAddressFamily.class, this
139 .nlriParser, this.nextHopParserSerializer, Ipv6NextHopCase.class));
140 regs.add(context.registerNlriSerializer(DataObject.class, this.nlriSerializer));
142 regs.add(context.registerExtendedCommunityParser(0, 0, this.exParser));
143 regs.add(context.registerExtendedCommunitySerializer(RouteTargetIpv4Case.class, this.exSerializer));
148 private void initMock() {
149 MockitoAnnotations.initMocks(this);
151 Mockito.doNothing().when(this.attrParser).parseAttribute(Mockito.any(ByteBuf.class), Mockito.any(AttributesBuilder.class),
152 Mockito.any(PeerSpecificParserConstraint.class));
153 Mockito.doReturn(EMPTY).when(this.attrParser).toString();
154 Mockito.doNothing().when(this.attrSerializer).serializeAttribute(Mockito.any(DataObject.class), Mockito.any(ByteBuf.class));
155 Mockito.doReturn(EMPTY).when(this.attrSerializer).toString();
157 Mockito.doReturn(null).when(this.paramParser).parseParameter(Mockito.any(ByteBuf.class));
158 Mockito.doReturn(EMPTY).when(this.paramParser).toString();
159 Mockito.doNothing().when(this.paramSerializer).serializeParameter(Mockito.any(BgpParameters.class), Mockito.any(ByteBuf.class));
160 Mockito.doReturn(EMPTY).when(this.paramSerializer).toString();
162 Mockito.doReturn(null).when(this.capaParser).parseCapability(Mockito.any(ByteBuf.class));
163 Mockito.doReturn(EMPTY).when(this.capaParser).toString();
164 Mockito.doNothing().when(this.capaSerializer).serializeCapability(Mockito.any(CParameters.class), Mockito.any(ByteBuf.class));
165 Mockito.doReturn(EMPTY).when(this.capaSerializer).toString();
167 Mockito.doReturn(null).when(this.sidTlvParser).parseBgpPrefixSidTlv(Mockito.any(ByteBuf.class));
168 Mockito.doReturn(EMPTY).when(this.sidTlvParser).toString();
169 Mockito.doNothing().when(this.sidTlvSerializer).serializeBgpPrefixSidTlv(Mockito.any(BgpPrefixSidTlv.class), Mockito.any(ByteBuf.class));
170 Mockito.doReturn(EMPTY).when(this.sidTlvSerializer).toString();
171 Mockito.doReturn(0).when(this.sidTlvSerializer).getType();
173 Mockito.doReturn(Mockito.mock(Notification.class)).when(this.msgParser).parseMessageBody(Mockito.any(ByteBuf.class), Mockito.anyInt(),
174 Mockito.any(PeerSpecificParserConstraint.class));
175 Mockito.doReturn(EMPTY).when(this.msgParser).toString();
176 Mockito.doNothing().when(this.msgSerializer).serializeMessage(Mockito.any(Notification.class), Mockito.any(ByteBuf.class));
177 Mockito.doReturn(EMPTY).when(this.msgSerializer).toString();
179 Mockito.doNothing().when(this.nlriParser).parseNlri(Mockito.any(ByteBuf.class), Mockito.any(MpUnreachNlriBuilder.class), Mockito.any());
180 Mockito.doNothing().when(this.nlriParser).parseNlri(Mockito.any(ByteBuf.class), Mockito.any(MpReachNlriBuilder.class), Mockito.any());
181 Mockito.doReturn(EMPTY).when(this.nlriParser).toString();
183 } catch (BGPDocumentedException | BGPParsingException e) {