blob: 1a3e77961d8d44c6319127ba7c694f443cc3bb1c [file] [log] [blame]
Sean Condon16df3b12017-06-09 15:14:01 +01001/*
2 * Copyright 2017-present Open Networking Foundation
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 */
16package org.onosproject.drivers.microsemi.yang;
17
18import static org.junit.Assert.assertEquals;
19import static org.junit.Assert.assertNotNull;
20import static org.junit.Assert.fail;
21
22import java.io.UncheckedIOException;
23import java.util.List;
24
25import org.junit.After;
26import org.junit.Before;
27import org.junit.Test;
28import org.onlab.packet.Ip4Address;
29import org.onosproject.drivers.microsemi.yang.impl.MseaSaFilteringManager;
30import org.onosproject.netconf.DatastoreId;
31import org.onosproject.netconf.NetconfDeviceInfo;
32import org.onosproject.netconf.NetconfException;
33import org.onosproject.netconf.NetconfSession;
34import org.onosproject.yang.gen.v1.mseasafiltering.rev20160412.MseaSaFiltering;
35import org.onosproject.yang.gen.v1.mseasafiltering.rev20160412.MseaSaFilteringOpParam;
36import org.onosproject.yang.gen.v1.mseasafiltering.rev20160412.mseasafiltering.DefaultSourceIpaddressFiltering;
37import org.onosproject.yang.gen.v1.mseasafiltering.rev20160412.mseasafiltering.SourceIpaddressFiltering;
38import org.onosproject.yang.gen.v1.mseasafiltering.rev20160412.mseasafiltering.sourceipaddressfiltering.DefaultInterfaceEth0;
39import org.onosproject.yang.gen.v1.mseasafiltering.rev20160412.mseasafiltering.sourceipaddressfiltering.InterfaceEth0;
40import org.onosproject.yang.gen.v1.mseasafiltering.rev20160412.mseasafiltering.sourceipaddressfiltering.interfaceeth0.DefaultSourceAddressRange;
41import org.onosproject.yang.gen.v1.mseasafiltering.rev20160412.mseasafiltering.sourceipaddressfiltering.interfaceeth0.SourceAddressRange;
42
43public class MseaSaFilteringManagerTest {
44
45 MseaSaFilteringManager mseaSaSvc;
46 NetconfSession session;
47
48 @Before
49 public void setUp() throws Exception {
50 try {
51 mseaSaSvc = new MockMseaSaFilteringManager();
52 mseaSaSvc.activate();
53 } catch (UncheckedIOException e) {
54 fail(e.getMessage());
55 }
56 NetconfDeviceInfo deviceInfo = new NetconfDeviceInfo("netconf", "netconf", Ip4Address.valueOf("1.2.3.4"), 830);
57 session = new MockNetconfSessionEa1000(deviceInfo);
58 }
59
60 @After
61 public void tearDown() throws Exception {
62 }
63
64 @Test
65 public void testGetMseaSaFilteringMseaSaFilteringOpParamNetconfSession() throws NetconfException {
66 SourceIpaddressFiltering sip = new DefaultSourceIpaddressFiltering();
67
68 MseaSaFilteringOpParam op = new MseaSaFilteringOpParam();
69 op.sourceIpaddressFiltering(sip);
70
71 MseaSaFiltering result = mseaSaSvc.getMseaSaFiltering(op, session);
72
73 //Results come from MockNetconfSession SAMPLE_MSEASAFILTERING_REPLY_INIT
74 assertNotNull(result.sourceIpaddressFiltering().interfaceEth0().sourceAddressRange());
75 List<SourceAddressRange> ranges = result.sourceIpaddressFiltering().interfaceEth0().sourceAddressRange();
76 assertEquals(2, ranges.size());
77
78 for (SourceAddressRange sa:ranges) {
79 if (sa.rangeId() == 1) {
80 assertEquals("10.10.10.10/16", sa.ipv4AddressPrefix());
81
82 } else if (sa.rangeId() == 2) {
83 assertEquals("20.30.40.50/18", sa.ipv4AddressPrefix());
84 }
85 }
86 }
87
88 @Test
89 public void testSetMseaSaFilteringMseaSaFilteringOpParamNetconfSessionNcDsType() {
90
91 MseaSaFilteringOpParam mseaSaFilteringConfig =
92 createConfigForEdit("192.168.60.10/27", (short) 3, "Filter3");
93
94 //Calling on the edit-config just makes the change and hopefully does not throw a Netconf Exception
95 try {
96 mseaSaSvc.setMseaSaFiltering(mseaSaFilteringConfig, session, DatastoreId.RUNNING);
97 } catch (NetconfException e) {
98 e.printStackTrace();
99 fail("NETCONF Exception: " + e.getMessage());
100 }
101 }
102
103 @Test
104 public void testDeleteMseaSaFilteringMseaSaFilteringOpParamNetconfSessionNcDsType() {
105
106 MseaSaFilteringOpParam mseaSaFilteringConfig =
107 createConfigForEdit("192.168.60.10/27", (short) 3, "Filter3");
108
109 SourceAddressRange sar2 = new DefaultSourceAddressRange();
110 sar2.ipv4AddressPrefix("10.205.86.10/27");
111 sar2.rangeId((short) 4);
112 sar2.name("Filter4");
113
114 mseaSaFilteringConfig.sourceIpaddressFiltering().interfaceEth0()
115 .addToSourceAddressRange(sar2);
116
117 //Calling on the edit-config just makes the change and hopefully does not throw a Netconf Exception
118 try {
119 mseaSaSvc.deleteMseaSaFilteringRange(mseaSaFilteringConfig, session, DatastoreId.RUNNING);
120 } catch (NetconfException e) {
121 e.printStackTrace();
122 fail("NETCONF Exception: " + e.getMessage());
123 }
124 }
125
126 /**
127 * This is also called from the test case EA1000FlowRuleProgrammableTest().
128 * In the ea1000driver project
129 * @return
130 */
131 public static MseaSaFilteringOpParam createConfigForEdit(String ipAddrPrefix, short rangeId, String rangeName) {
132 SourceAddressRange sar = new DefaultSourceAddressRange();
133 sar.ipv4AddressPrefix(ipAddrPrefix);
134 sar.rangeId(rangeId);
135 sar.name(rangeName);
136
137 InterfaceEth0 eth0 = new DefaultInterfaceEth0();
138 eth0.addToSourceAddressRange(sar);
139
140 SourceIpaddressFiltering sip = new DefaultSourceIpaddressFiltering();
141 sip.interfaceEth0(eth0);
142
143 MseaSaFilteringOpParam op = new MseaSaFilteringOpParam();
144 op.sourceIpaddressFiltering(sip);
145
146 return op;
147 }
148}