Sean Condon | 06613e9 | 2017-06-09 15:14:01 +0100 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Sean Condon | 06613e9 | 2017-06-09 15:14:01 +0100 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.drivers.microsemi.yang; |
| 17 | |
| 18 | import static org.junit.Assert.assertEquals; |
| 19 | import static org.junit.Assert.assertNotNull; |
| 20 | import static org.junit.Assert.fail; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame^] | 21 | import static org.junit.Assert.assertTrue; |
Sean Condon | 06613e9 | 2017-06-09 15:14:01 +0100 | [diff] [blame] | 22 | |
| 23 | import java.io.UncheckedIOException; |
| 24 | import java.util.List; |
| 25 | |
| 26 | import org.junit.After; |
| 27 | import org.junit.Before; |
| 28 | import org.junit.Test; |
| 29 | import org.onlab.packet.Ip4Address; |
| 30 | import org.onosproject.drivers.microsemi.yang.impl.MseaSaFilteringManager; |
| 31 | import org.onosproject.netconf.DatastoreId; |
| 32 | import org.onosproject.netconf.NetconfDeviceInfo; |
| 33 | import org.onosproject.netconf.NetconfException; |
| 34 | import org.onosproject.netconf.NetconfSession; |
| 35 | import org.onosproject.yang.gen.v1.mseasafiltering.rev20160412.MseaSaFiltering; |
| 36 | import org.onosproject.yang.gen.v1.mseasafiltering.rev20160412.MseaSaFilteringOpParam; |
| 37 | import org.onosproject.yang.gen.v1.mseasafiltering.rev20160412.mseasafiltering.DefaultSourceIpaddressFiltering; |
| 38 | import org.onosproject.yang.gen.v1.mseasafiltering.rev20160412.mseasafiltering.SourceIpaddressFiltering; |
| 39 | import org.onosproject.yang.gen.v1.mseasafiltering.rev20160412.mseasafiltering.sourceipaddressfiltering.DefaultInterfaceEth0; |
| 40 | import org.onosproject.yang.gen.v1.mseasafiltering.rev20160412.mseasafiltering.sourceipaddressfiltering.InterfaceEth0; |
| 41 | import org.onosproject.yang.gen.v1.mseasafiltering.rev20160412.mseasafiltering.sourceipaddressfiltering.interfaceeth0.DefaultSourceAddressRange; |
| 42 | import org.onosproject.yang.gen.v1.mseasafiltering.rev20160412.mseasafiltering.sourceipaddressfiltering.interfaceeth0.SourceAddressRange; |
| 43 | |
Sean Condon | 06613e9 | 2017-06-09 15:14:01 +0100 | [diff] [blame] | 44 | public class MseaSaFilteringManagerTest { |
| 45 | |
| 46 | MseaSaFilteringManager mseaSaSvc; |
| 47 | NetconfSession session; |
| 48 | |
| 49 | @Before |
| 50 | public void setUp() throws Exception { |
| 51 | try { |
| 52 | mseaSaSvc = new MockMseaSaFilteringManager(); |
| 53 | mseaSaSvc.activate(); |
| 54 | } catch (UncheckedIOException e) { |
| 55 | fail(e.getMessage()); |
| 56 | } |
| 57 | NetconfDeviceInfo deviceInfo = new NetconfDeviceInfo("netconf", "netconf", Ip4Address.valueOf("1.2.3.4"), 830); |
| 58 | session = new MockNetconfSessionEa1000(deviceInfo); |
| 59 | } |
| 60 | |
| 61 | @After |
| 62 | public void tearDown() throws Exception { |
| 63 | } |
| 64 | |
| 65 | @Test |
| 66 | public void testGetMseaSaFilteringMseaSaFilteringOpParamNetconfSession() throws NetconfException { |
| 67 | SourceIpaddressFiltering sip = new DefaultSourceIpaddressFiltering(); |
| 68 | |
| 69 | MseaSaFilteringOpParam op = new MseaSaFilteringOpParam(); |
| 70 | op.sourceIpaddressFiltering(sip); |
| 71 | |
| 72 | MseaSaFiltering result = mseaSaSvc.getMseaSaFiltering(op, session); |
| 73 | |
| 74 | //Results come from MockNetconfSession SAMPLE_MSEASAFILTERING_REPLY_INIT |
| 75 | assertNotNull(result.sourceIpaddressFiltering().interfaceEth0().sourceAddressRange()); |
| 76 | List<SourceAddressRange> ranges = result.sourceIpaddressFiltering().interfaceEth0().sourceAddressRange(); |
| 77 | assertEquals(2, ranges.size()); |
| 78 | |
| 79 | for (SourceAddressRange sa:ranges) { |
| 80 | if (sa.rangeId() == 1) { |
| 81 | assertEquals("10.10.10.10/16", sa.ipv4AddressPrefix()); |
| 82 | |
| 83 | } else if (sa.rangeId() == 2) { |
| 84 | assertEquals("20.30.40.50/18", sa.ipv4AddressPrefix()); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame^] | 89 | /** |
| 90 | * See sampleXmlRegexSaFilteringErrorScenario in MockNetconfSessionEa1000. |
| 91 | */ |
| 92 | @Test |
| 93 | public void testGetMseaSaFilteringMseaSaFilteringOpParamNetconfSessionError() { |
| 94 | |
| 95 | SourceAddressRange sar = new DefaultSourceAddressRange(); |
| 96 | sar.rangeId((short) 10); |
| 97 | |
| 98 | InterfaceEth0 eth0 = new DefaultInterfaceEth0(); |
| 99 | eth0.addToSourceAddressRange(sar); |
| 100 | |
| 101 | SourceIpaddressFiltering sip = new DefaultSourceIpaddressFiltering(); |
| 102 | sip.interfaceEth0(eth0); |
| 103 | |
| 104 | MseaSaFilteringOpParam mseaSaFilteringConfig = new MseaSaFilteringOpParam(); |
| 105 | mseaSaFilteringConfig.sourceIpaddressFiltering(sip); |
| 106 | |
| 107 | try { |
| 108 | MseaSaFiltering result = mseaSaSvc.getMseaSaFiltering(mseaSaFilteringConfig, session); |
| 109 | fail("Should have thrown exception"); |
| 110 | } catch (NetconfException ne) { |
| 111 | assertTrue(ne.getMessage().startsWith("NETCONF rpc-error")); |
| 112 | } |
| 113 | } |
| 114 | |
Sean Condon | 06613e9 | 2017-06-09 15:14:01 +0100 | [diff] [blame] | 115 | @Test |
| 116 | public void testSetMseaSaFilteringMseaSaFilteringOpParamNetconfSessionNcDsType() { |
| 117 | |
| 118 | MseaSaFilteringOpParam mseaSaFilteringConfig = |
| 119 | createConfigForEdit("192.168.60.10/27", (short) 3, "Filter3"); |
| 120 | |
| 121 | //Calling on the edit-config just makes the change and hopefully does not throw a Netconf Exception |
| 122 | try { |
| 123 | mseaSaSvc.setMseaSaFiltering(mseaSaFilteringConfig, session, DatastoreId.RUNNING); |
| 124 | } catch (NetconfException e) { |
| 125 | e.printStackTrace(); |
| 126 | fail("NETCONF Exception: " + e.getMessage()); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | @Test |
| 131 | public void testDeleteMseaSaFilteringMseaSaFilteringOpParamNetconfSessionNcDsType() { |
| 132 | |
| 133 | MseaSaFilteringOpParam mseaSaFilteringConfig = |
| 134 | createConfigForEdit("192.168.60.10/27", (short) 3, "Filter3"); |
| 135 | |
| 136 | SourceAddressRange sar2 = new DefaultSourceAddressRange(); |
| 137 | sar2.ipv4AddressPrefix("10.205.86.10/27"); |
| 138 | sar2.rangeId((short) 4); |
| 139 | sar2.name("Filter4"); |
| 140 | |
| 141 | mseaSaFilteringConfig.sourceIpaddressFiltering().interfaceEth0() |
| 142 | .addToSourceAddressRange(sar2); |
| 143 | |
| 144 | //Calling on the edit-config just makes the change and hopefully does not throw a Netconf Exception |
| 145 | try { |
| 146 | mseaSaSvc.deleteMseaSaFilteringRange(mseaSaFilteringConfig, session, DatastoreId.RUNNING); |
| 147 | } catch (NetconfException e) { |
| 148 | e.printStackTrace(); |
| 149 | fail("NETCONF Exception: " + e.getMessage()); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * This is also called from the test case EA1000FlowRuleProgrammableTest(). |
| 155 | * In the ea1000driver project |
| 156 | * @return |
| 157 | */ |
| 158 | public static MseaSaFilteringOpParam createConfigForEdit(String ipAddrPrefix, short rangeId, String rangeName) { |
| 159 | SourceAddressRange sar = new DefaultSourceAddressRange(); |
| 160 | sar.ipv4AddressPrefix(ipAddrPrefix); |
| 161 | sar.rangeId(rangeId); |
| 162 | sar.name(rangeName); |
| 163 | |
| 164 | InterfaceEth0 eth0 = new DefaultInterfaceEth0(); |
| 165 | eth0.addToSourceAddressRange(sar); |
| 166 | |
| 167 | SourceIpaddressFiltering sip = new DefaultSourceIpaddressFiltering(); |
| 168 | sip.interfaceEth0(eth0); |
| 169 | |
| 170 | MseaSaFilteringOpParam op = new MseaSaFilteringOpParam(); |
| 171 | op.sourceIpaddressFiltering(sip); |
| 172 | |
| 173 | return op; |
| 174 | } |
| 175 | } |