2 * Copyright (c) 2017 AT&T Intellectual Property. 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.bgp.cli.utils;
10 import static org.junit.Assert.assertEquals;
11 import static org.opendaylight.protocol.bgp.cli.utils.BGPOperationalStateUtils.PROTOCOLS_IID;
12 import static org.opendaylight.protocol.bgp.cli.utils.NeighborStateCliUtilsTest.NEIGHBOR_ADDRESS;
13 import static org.opendaylight.protocol.bgp.cli.utils.PeerGroupStateCliUtilsTest.UTF8;
15 import java.io.ByteArrayOutputStream;
16 import java.io.IOException;
17 import java.io.PrintStream;
18 import java.util.Collections;
19 import java.util.concurrent.ExecutionException;
20 import org.apache.commons.io.IOUtils;
21 import org.junit.Test;
22 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
23 import org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.Bgp;
26 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.BgpBuilder;
27 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.bgp.NeighborsBuilder;
28 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.network.instance.protocols.Protocol;
29 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.network.instance.protocols.ProtocolKey;
30 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.policy.types.rev151009.BGP;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.NetworkInstanceProtocol;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 public class BGPOperationalStateUtilsTest extends AbstractConcurrentDataBrokerTest {
35 static final String RIB_ID = "test-rib";
36 private static final String RIB_NOT_FOUND = "RIB not found for [test-rib]\n";
37 private final ByteArrayOutputStream output = new ByteArrayOutputStream();
38 private final PrintStream stream = new PrintStream(this.output);
41 public void testDisplayBgpOperationalStateNotFound() {
42 BGPOperationalStateUtils.displayBgpOperationalState(getDataBroker(), this.stream, RIB_ID, null, null);
43 assertEquals(RIB_NOT_FOUND, this.output.toString());
47 public void testDisplayBgpOperationalStateFound() throws IOException, ExecutionException, InterruptedException {
48 createDefaultProtocol();
49 BGPOperationalStateUtils.displayBgpOperationalState(getDataBroker(), this.stream, RIB_ID, null, null);
50 final String expected = IOUtils.toString(
51 getClass().getClassLoader().getResourceAsStream("global.txt"), UTF8);
52 assertEquals(expected, this.output.toString());
55 private void createDefaultProtocol() throws ExecutionException, InterruptedException {
56 final WriteTransaction wt = getDataBroker().newWriteOnlyTransaction();
57 final Bgp bgp = new BgpBuilder()
58 .setGlobal(GlobalStateCliUtilsTest.buildGlobal(true).build())
59 .setNeighbors(new NeighborsBuilder().setNeighbor(
60 Collections.singletonList(NeighborStateCliUtilsTest.createBasicNeighbor())).build())
62 GlobalStateCliUtilsTest.buildGlobal(true);
63 final InstanceIdentifier<Bgp> bgpIID = PROTOCOLS_IID
64 .child(Protocol.class, new ProtocolKey(BGP.class, RIB_ID))
65 .augmentation(NetworkInstanceProtocol.class).child(Bgp.class);
66 wt.put(LogicalDatastoreType.OPERATIONAL, bgpIID, bgp, true);
71 public void testDisplayNeighborOperationalState() throws IOException, ExecutionException, InterruptedException {
72 createDefaultProtocol();
73 BGPOperationalStateUtils.displayBgpOperationalState(getDataBroker(), this.stream, RIB_ID, null,
75 final String expected = IOUtils.toString(
76 getClass().getClassLoader().getResourceAsStream("empty-neighbor.txt"), UTF8);
77 assertEquals(expected, this.output.toString());