Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [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 | fae8e66 | 2016-12-15 10:25:13 +0000 | [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.netconf; |
| 17 | |
David K. Bainbridge | 1a10d62 | 2018-05-07 12:32:27 -0700 | [diff] [blame] | 18 | import static org.junit.Assert.fail; |
| 19 | |
| 20 | import java.io.InputStream; |
| 21 | import java.nio.charset.StandardCharsets; |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 22 | import java.util.HashMap; |
| 23 | import java.util.Map; |
| 24 | |
David K. Bainbridge | 1a10d62 | 2018-05-07 12:32:27 -0700 | [diff] [blame] | 25 | import javax.xml.parsers.DocumentBuilder; |
| 26 | import javax.xml.parsers.DocumentBuilderFactory; |
| 27 | import javax.xml.xpath.XPath; |
| 28 | import javax.xml.xpath.XPathConstants; |
| 29 | import javax.xml.xpath.XPathFactory; |
| 30 | |
| 31 | import org.apache.commons.io.IOUtils; |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 32 | import org.onosproject.core.CoreService; |
| 33 | import org.onosproject.mastership.MastershipService; |
| 34 | import org.onosproject.net.DeviceId; |
David K. Bainbridge | 1a10d62 | 2018-05-07 12:32:27 -0700 | [diff] [blame] | 35 | import org.onosproject.net.device.DeviceService; |
| 36 | import org.onosproject.net.driver.AbstractDriverLoader; |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 37 | import org.onosproject.net.driver.Behaviour; |
| 38 | import org.onosproject.net.driver.DefaultDriver; |
| 39 | import org.onosproject.net.driver.DefaultDriverData; |
| 40 | import org.onosproject.net.driver.Driver; |
| 41 | import org.onosproject.net.driver.DriverData; |
| 42 | import org.onosproject.net.driver.DriverHandler; |
| 43 | import org.onosproject.net.flow.FlowRuleProgrammable; |
| 44 | import org.onosproject.netconf.NetconfController; |
| 45 | import org.onosproject.netconf.NetconfException; |
David K. Bainbridge | 1a10d62 | 2018-05-07 12:32:27 -0700 | [diff] [blame] | 46 | import org.w3c.dom.Document; |
| 47 | import org.w3c.dom.NamedNodeMap; |
| 48 | import org.w3c.dom.Node; |
| 49 | import org.w3c.dom.NodeList; |
| 50 | |
| 51 | import com.google.common.io.Resources; |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 52 | |
| 53 | public class MockDriverHandler implements DriverHandler { |
| 54 | |
| 55 | private DriverData mockDriverData; |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 56 | private NetconfController ncc; |
| 57 | private CoreService coreService; |
David K. Bainbridge | 1a10d62 | 2018-05-07 12:32:27 -0700 | [diff] [blame] | 58 | private DeviceService deviceService; |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 59 | private MastershipService mastershipService; |
| 60 | |
David K. Bainbridge | 1a10d62 | 2018-05-07 12:32:27 -0700 | [diff] [blame] | 61 | // Centralize some initialization. |
| 62 | private void init(Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours, DeviceId mockDeviceId, |
| 63 | CoreService mockCoreService, DeviceService mockDeviceService) throws NetconfException { |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 64 | Map<String, String> properties = new HashMap<String, String>(); |
| 65 | |
David K. Bainbridge | 1a10d62 | 2018-05-07 12:32:27 -0700 | [diff] [blame] | 66 | Driver mockDriver = new DefaultDriver("mockDriver", null, "ONOSProject", "1.0.0", "1.0.0", behaviours, |
| 67 | properties); |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 68 | mockDriverData = new DefaultDriverData(mockDriver, mockDeviceId); |
| 69 | |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 70 | ncc = new MockNetconfController(); |
| 71 | ncc.connectDevice(mockDeviceId); |
David K. Bainbridge | 1a10d62 | 2018-05-07 12:32:27 -0700 | [diff] [blame] | 72 | coreService = mockCoreService; |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 73 | mastershipService = new MockMastershipService(); |
David K. Bainbridge | 1a10d62 | 2018-05-07 12:32:27 -0700 | [diff] [blame] | 74 | deviceService = mockDeviceService; |
| 75 | } |
| 76 | |
| 77 | @SuppressWarnings("unchecked") |
| 78 | public MockDriverHandler(Class<? extends AbstractDriverLoader> loaderClass, String behaviorSpec, |
| 79 | DeviceId mockDeviceId, CoreService mockCoreService, DeviceService mockDeviceService) { |
| 80 | |
| 81 | // Had to split into declaration and initialization to make stylecheck happy |
| 82 | // else line was considered too long |
| 83 | // and auto format couldn't be tweak to make it correct |
| 84 | Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours; |
| 85 | behaviours = new HashMap<Class<? extends Behaviour>, Class<? extends Behaviour>>(); |
| 86 | |
| 87 | try { |
| 88 | String data = Resources.toString(Resources.getResource(loaderClass, behaviorSpec), StandardCharsets.UTF_8); |
| 89 | InputStream resp = IOUtils.toInputStream(data, StandardCharsets.UTF_8); |
| 90 | DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); |
| 91 | DocumentBuilder builder = builderFactory.newDocumentBuilder(); |
| 92 | Document document = builder.parse(resp); |
| 93 | |
| 94 | XPath xp = XPathFactory.newInstance().newXPath(); |
| 95 | NodeList list = (NodeList) xp.evaluate("//behaviour", document, XPathConstants.NODESET); |
| 96 | for (int i = 0; i < list.getLength(); i += 1) { |
| 97 | Node node = list.item(i); |
| 98 | NamedNodeMap attrs = node.getAttributes(); |
| 99 | Class<? extends Behaviour> api = (Class<? extends Behaviour>) Class |
| 100 | .forName(attrs.getNamedItem("api").getNodeValue()); |
| 101 | Class<? extends Behaviour> impl = (Class<? extends Behaviour>) Class |
| 102 | .forName(attrs.getNamedItem("impl").getNodeValue()); |
| 103 | behaviours.put(api, impl); |
| 104 | } |
| 105 | init(behaviours, mockDeviceId, mockCoreService, mockDeviceService); |
| 106 | } catch (Exception e) { |
| 107 | fail(e.toString()); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | public MockDriverHandler() throws NetconfException { |
| 112 | Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours; |
| 113 | behaviours = new HashMap<Class<? extends Behaviour>, Class<? extends Behaviour>>(); |
| 114 | behaviours.put(FlowRuleProgrammable.class, FlowRuleProgrammable.class); |
| 115 | DeviceId mockDeviceId = DeviceId.deviceId("netconf:1.2.3.4:830"); |
| 116 | coreService = new MockCoreService(); |
| 117 | init(behaviours, mockDeviceId, coreService, null); |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public Driver driver() { |
| 122 | return mockDriverData.driver(); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public DriverData data() { |
| 127 | return mockDriverData; |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public <T extends Behaviour> T behaviour(Class<T> behaviourClass) { |
| 132 | // TODO Auto-generated method stub |
| 133 | return null; |
| 134 | } |
| 135 | |
David K. Bainbridge | 1a10d62 | 2018-05-07 12:32:27 -0700 | [diff] [blame] | 136 | @SuppressWarnings("unchecked") |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 137 | @Override |
| 138 | public <T> T get(Class<T> serviceClass) { |
| 139 | if (serviceClass.equals(NetconfController.class)) { |
| 140 | return (T) ncc; |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 141 | } else if (serviceClass.equals(CoreService.class)) { |
| 142 | return (T) coreService; |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 143 | } else if (serviceClass.equals(MastershipService.class)) { |
| 144 | return (T) mastershipService; |
David K. Bainbridge | 1a10d62 | 2018-05-07 12:32:27 -0700 | [diff] [blame] | 145 | } else if (serviceClass.equals(DeviceService.class)) { |
| 146 | return (T) deviceService; |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | return null; |
| 150 | } |
| 151 | |
| 152 | } |