blob: d898158d68f2a173b41a040e14d3f4a0ffce0685 [file] [log] [blame]
Sean Condonfae8e662016-12-15 10:25:13 +00001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Sean Condonfae8e662016-12-15 10:25:13 +00003 *
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.netconf;
17
David K. Bainbridge1a10d622018-05-07 12:32:27 -070018import static org.junit.Assert.fail;
19
20import java.io.InputStream;
21import java.nio.charset.StandardCharsets;
Sean Condonfae8e662016-12-15 10:25:13 +000022import java.util.HashMap;
23import java.util.Map;
24
David K. Bainbridge1a10d622018-05-07 12:32:27 -070025import javax.xml.parsers.DocumentBuilder;
26import javax.xml.parsers.DocumentBuilderFactory;
27import javax.xml.xpath.XPath;
28import javax.xml.xpath.XPathConstants;
29import javax.xml.xpath.XPathFactory;
30
31import org.apache.commons.io.IOUtils;
Sean Condonfae8e662016-12-15 10:25:13 +000032import org.onosproject.core.CoreService;
33import org.onosproject.mastership.MastershipService;
34import org.onosproject.net.DeviceId;
David K. Bainbridge1a10d622018-05-07 12:32:27 -070035import org.onosproject.net.device.DeviceService;
36import org.onosproject.net.driver.AbstractDriverLoader;
Sean Condonfae8e662016-12-15 10:25:13 +000037import org.onosproject.net.driver.Behaviour;
38import org.onosproject.net.driver.DefaultDriver;
39import org.onosproject.net.driver.DefaultDriverData;
40import org.onosproject.net.driver.Driver;
41import org.onosproject.net.driver.DriverData;
42import org.onosproject.net.driver.DriverHandler;
43import org.onosproject.net.flow.FlowRuleProgrammable;
44import org.onosproject.netconf.NetconfController;
45import org.onosproject.netconf.NetconfException;
David K. Bainbridge1a10d622018-05-07 12:32:27 -070046import org.w3c.dom.Document;
47import org.w3c.dom.NamedNodeMap;
48import org.w3c.dom.Node;
49import org.w3c.dom.NodeList;
50
51import com.google.common.io.Resources;
Sean Condonfae8e662016-12-15 10:25:13 +000052
53public class MockDriverHandler implements DriverHandler {
54
55 private DriverData mockDriverData;
Sean Condonfae8e662016-12-15 10:25:13 +000056 private NetconfController ncc;
57 private CoreService coreService;
David K. Bainbridge1a10d622018-05-07 12:32:27 -070058 private DeviceService deviceService;
Sean Condonfae8e662016-12-15 10:25:13 +000059 private MastershipService mastershipService;
60
David K. Bainbridge1a10d622018-05-07 12:32:27 -070061 // 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 Condonfae8e662016-12-15 10:25:13 +000064 Map<String, String> properties = new HashMap<String, String>();
65
David K. Bainbridge1a10d622018-05-07 12:32:27 -070066 Driver mockDriver = new DefaultDriver("mockDriver", null, "ONOSProject", "1.0.0", "1.0.0", behaviours,
67 properties);
Sean Condonfae8e662016-12-15 10:25:13 +000068 mockDriverData = new DefaultDriverData(mockDriver, mockDeviceId);
69
Sean Condonfae8e662016-12-15 10:25:13 +000070 ncc = new MockNetconfController();
71 ncc.connectDevice(mockDeviceId);
David K. Bainbridge1a10d622018-05-07 12:32:27 -070072 coreService = mockCoreService;
Sean Condonfae8e662016-12-15 10:25:13 +000073 mastershipService = new MockMastershipService();
David K. Bainbridge1a10d622018-05-07 12:32:27 -070074 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 Condonfae8e662016-12-15 10:25:13 +0000118 }
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. Bainbridge1a10d622018-05-07 12:32:27 -0700136 @SuppressWarnings("unchecked")
Sean Condonfae8e662016-12-15 10:25:13 +0000137 @Override
138 public <T> T get(Class<T> serviceClass) {
139 if (serviceClass.equals(NetconfController.class)) {
140 return (T) ncc;
Sean Condonfae8e662016-12-15 10:25:13 +0000141 } else if (serviceClass.equals(CoreService.class)) {
142 return (T) coreService;
Sean Condonfae8e662016-12-15 10:25:13 +0000143 } else if (serviceClass.equals(MastershipService.class)) {
144 return (T) mastershipService;
David K. Bainbridge1a10d622018-05-07 12:32:27 -0700145 } else if (serviceClass.equals(DeviceService.class)) {
146 return (T) deviceService;
Sean Condonfae8e662016-12-15 10:25:13 +0000147 }
148
149 return null;
150 }
151
152}