Thomas Vachuska
Committed by Gerrit Code Review

Allowing driver loaders to refer to classes loaded by others.

Change-Id: Ife4e50758620d4c052a7bb81740d9b9305647317
...@@ -49,7 +49,7 @@ public abstract class AbstractDriverLoader { ...@@ -49,7 +49,7 @@ public abstract class AbstractDriverLoader {
49 @Activate 49 @Activate
50 protected void activate() { 50 protected void activate() {
51 try { 51 try {
52 - provider = new XmlDriverLoader(getClass().getClassLoader()) 52 + provider = new XmlDriverLoader(getClass().getClassLoader(), driverAdminService)
53 .loadDrivers(getClass().getResourceAsStream(path), driverAdminService); 53 .loadDrivers(getClass().getResourceAsStream(path), driverAdminService);
54 driverAdminService.registerProvider(provider); 54 driverAdminService.registerProvider(provider);
55 } catch (Exception e) { 55 } catch (Exception e) {
......
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
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 +
17 +package org.onosproject.net.driver;
18 +
19 +/**
20 + * Entity capable of resolving a class using its name.
21 + */
22 +public interface BehaviourClassResolver {
23 +
24 + /**
25 + * Returns the class corresponding to the specified class name.
26 + * @param className class className
27 + * @return
28 + */
29 + Class<? extends Behaviour> getBehaviourClass(String className);
30 +
31 +}
...@@ -20,7 +20,7 @@ import java.util.Set; ...@@ -20,7 +20,7 @@ import java.util.Set;
20 /** 20 /**
21 * Service for managing drivers and driver behaviour implementations. 21 * Service for managing drivers and driver behaviour implementations.
22 */ 22 */
23 -public interface DriverAdminService extends DriverService { 23 +public interface DriverAdminService extends DriverService, BehaviourClassResolver {
24 24
25 /** 25 /**
26 * Returns the set of driver providers currently registered. 26 * Returns the set of driver providers currently registered.
......
...@@ -64,6 +64,7 @@ public class XmlDriverLoader { ...@@ -64,6 +64,7 @@ public class XmlDriverLoader {
64 private static final String IMPL = "[@impl]"; 64 private static final String IMPL = "[@impl]";
65 65
66 private final ClassLoader classLoader; 66 private final ClassLoader classLoader;
67 + private final BehaviourClassResolver resolver;
67 68
68 private Map<String, Driver> drivers = Maps.newHashMap(); 69 private Map<String, Driver> drivers = Maps.newHashMap();
69 70
...@@ -72,9 +73,23 @@ public class XmlDriverLoader { ...@@ -72,9 +73,23 @@ public class XmlDriverLoader {
72 * class loader. 73 * class loader.
73 * 74 *
74 * @param classLoader class loader to use 75 * @param classLoader class loader to use
76 + * @deprecated since 1.7.0 (Hummingbird)
75 */ 77 */
78 + @Deprecated
76 public XmlDriverLoader(ClassLoader classLoader) { 79 public XmlDriverLoader(ClassLoader classLoader) {
80 + this(classLoader, null);
81 + }
82 +
83 + /**
84 + * Creates a new driver loader capable of loading drivers from the supplied
85 + * class loader.
86 + *
87 + * @param classLoader class loader to use
88 + * @param resolver behaviour class resolver
89 + */
90 + public XmlDriverLoader(ClassLoader classLoader, BehaviourClassResolver resolver) {
77 this.classLoader = classLoader; 91 this.classLoader = classLoader;
92 + this.resolver = resolver;
78 } 93 }
79 94
80 /** 95 /**
...@@ -182,7 +197,13 @@ public class XmlDriverLoader { ...@@ -182,7 +197,13 @@ public class XmlDriverLoader {
182 try { 197 try {
183 return (Class<? extends Behaviour>) classLoader.loadClass(className); 198 return (Class<? extends Behaviour>) classLoader.loadClass(className);
184 } catch (ClassNotFoundException e) { 199 } catch (ClassNotFoundException e) {
185 - throw new IllegalArgumentException("Unable to load class " + className, e); 200 + if (resolver != null) {
201 + Class<? extends Behaviour> cls = resolver.getBehaviourClass(className);
202 + if (cls != null) {
203 + return cls;
204 + }
205 + }
206 + throw new IllegalArgumentException("Unable to resolve class " + className, e);
186 } 207 }
187 } 208 }
188 209
......
1 package org.onosproject.net.driver; 1 package org.onosproject.net.driver;
2 2
3 -import java.util.Set;
4 -
5 import org.junit.Test; 3 import org.junit.Test;
6 -import org.onosproject.net.DeviceId; 4 +
5 +import java.util.Set;
7 6
8 /** 7 /**
9 * Base test class for driver loading. 8 * Base test class for driver loading.
10 */ 9 */
11 public abstract class AbstractDriverLoaderTest { 10 public abstract class AbstractDriverLoaderTest {
12 11
13 - private class DriverAdminServiceAdapter implements DriverAdminService { 12 + private class DriverAdminServiceAdapter extends DriverServiceAdapter implements DriverAdminService {
14 @Override 13 @Override
15 public Set<DriverProvider> getProviders() { 14 public Set<DriverProvider> getProviders() {
16 return null; 15 return null;
...@@ -18,41 +17,14 @@ public abstract class AbstractDriverLoaderTest { ...@@ -18,41 +17,14 @@ public abstract class AbstractDriverLoaderTest {
18 17
19 @Override 18 @Override
20 public void registerProvider(DriverProvider provider) { 19 public void registerProvider(DriverProvider provider) {
21 -
22 } 20 }
23 21
24 @Override 22 @Override
25 public void unregisterProvider(DriverProvider provider) { 23 public void unregisterProvider(DriverProvider provider) {
26 -
27 - }
28 -
29 - @Override
30 - public Set<Driver> getDrivers() {
31 - return null;
32 - }
33 -
34 - @Override
35 - public Set<Driver> getDrivers(Class<? extends Behaviour> withBehaviour) {
36 - return null;
37 - }
38 -
39 - @Override
40 - public Driver getDriver(String mfr, String hw, String sw) {
41 - return null;
42 - }
43 -
44 - @Override
45 - public Driver getDriver(DeviceId deviceId) {
46 - return null;
47 - }
48 -
49 - @Override
50 - public DriverHandler createHandler(DeviceId deviceId, String... credentials) {
51 - return null;
52 } 24 }
53 25
54 @Override 26 @Override
55 - public Driver getDriver(String driverName) { 27 + public Class<? extends Behaviour> getBehaviourClass(String className) {
56 return null; 28 return null;
57 } 29 }
58 } 30 }
......
...@@ -33,7 +33,7 @@ public class XmlDriverLoaderTest { ...@@ -33,7 +33,7 @@ public class XmlDriverLoaderTest {
33 33
34 @Test 34 @Test
35 public void basics() throws IOException { 35 public void basics() throws IOException {
36 - XmlDriverLoader loader = new XmlDriverLoader(getClass().getClassLoader()); 36 + XmlDriverLoader loader = new XmlDriverLoader(getClass().getClassLoader(), null);
37 InputStream stream = getClass().getResourceAsStream("drivers.1.xml"); 37 InputStream stream = getClass().getResourceAsStream("drivers.1.xml");
38 DriverProvider provider = loader.loadDrivers(stream, null); 38 DriverProvider provider = loader.loadDrivers(stream, null);
39 System.out.println(provider); 39 System.out.println(provider);
...@@ -59,19 +59,19 @@ public class XmlDriverLoaderTest { ...@@ -59,19 +59,19 @@ public class XmlDriverLoaderTest {
59 59
60 @Test(expected = IOException.class) 60 @Test(expected = IOException.class)
61 public void badXml() throws IOException { 61 public void badXml() throws IOException {
62 - XmlDriverLoader loader = new XmlDriverLoader(getClass().getClassLoader()); 62 + XmlDriverLoader loader = new XmlDriverLoader(getClass().getClassLoader(), null);
63 loader.loadDrivers(getClass().getResourceAsStream("drivers.bad.xml"), null); 63 loader.loadDrivers(getClass().getResourceAsStream("drivers.bad.xml"), null);
64 } 64 }
65 65
66 @Test(expected = IllegalArgumentException.class) 66 @Test(expected = IllegalArgumentException.class)
67 public void noClass() throws IOException { 67 public void noClass() throws IOException {
68 - XmlDriverLoader loader = new XmlDriverLoader(getClass().getClassLoader()); 68 + XmlDriverLoader loader = new XmlDriverLoader(getClass().getClassLoader(), null);
69 loader.loadDrivers(getClass().getResourceAsStream("drivers.noclass.xml"), null); 69 loader.loadDrivers(getClass().getResourceAsStream("drivers.noclass.xml"), null);
70 } 70 }
71 71
72 @Test(expected = IllegalArgumentException.class) 72 @Test(expected = IllegalArgumentException.class)
73 public void noConstructor() throws IOException { 73 public void noConstructor() throws IOException {
74 - XmlDriverLoader loader = new XmlDriverLoader(getClass().getClassLoader()); 74 + XmlDriverLoader loader = new XmlDriverLoader(getClass().getClassLoader(), null);
75 InputStream stream = getClass().getResourceAsStream("drivers.noconstructor.xml"); 75 InputStream stream = getClass().getResourceAsStream("drivers.noconstructor.xml");
76 DriverProvider provider = loader.loadDrivers(stream, null); 76 DriverProvider provider = loader.loadDrivers(stream, null);
77 Driver driver = provider.getDrivers().iterator().next(); 77 Driver driver = provider.getDrivers().iterator().next();
...@@ -80,7 +80,7 @@ public class XmlDriverLoaderTest { ...@@ -80,7 +80,7 @@ public class XmlDriverLoaderTest {
80 80
81 @Test 81 @Test
82 public void multipleDrivers() throws IOException { 82 public void multipleDrivers() throws IOException {
83 - XmlDriverLoader loader = new XmlDriverLoader(getClass().getClassLoader()); 83 + XmlDriverLoader loader = new XmlDriverLoader(getClass().getClassLoader(), null);
84 InputStream stream = getClass().getResourceAsStream("drivers.multipleInheritance.xml"); 84 InputStream stream = getClass().getResourceAsStream("drivers.multipleInheritance.xml");
85 DriverProvider provider = loader.loadDrivers(stream, null); 85 DriverProvider provider = loader.loadDrivers(stream, null);
86 Iterator<Driver> iterator = provider.getDrivers().iterator(); 86 Iterator<Driver> iterator = provider.getDrivers().iterator();
...@@ -94,7 +94,7 @@ public class XmlDriverLoaderTest { ...@@ -94,7 +94,7 @@ public class XmlDriverLoaderTest {
94 94
95 @Test 95 @Test
96 public void multipleDriversSameBehaviors() throws IOException { 96 public void multipleDriversSameBehaviors() throws IOException {
97 - XmlDriverLoader loader = new XmlDriverLoader(getClass().getClassLoader()); 97 + XmlDriverLoader loader = new XmlDriverLoader(getClass().getClassLoader(), null);
98 InputStream stream = getClass().getResourceAsStream("drivers.sameMultipleInheritance.xml"); 98 InputStream stream = getClass().getResourceAsStream("drivers.sameMultipleInheritance.xml");
99 DriverProvider provider = loader.loadDrivers(stream, null); 99 DriverProvider provider = loader.loadDrivers(stream, null);
100 Iterator<Driver> iterator = provider.getDrivers().iterator(); 100 Iterator<Driver> iterator = provider.getDrivers().iterator();
......
...@@ -70,6 +70,7 @@ public class DriverManager extends DefaultDriverProvider implements DriverAdminS ...@@ -70,6 +70,7 @@ public class DriverManager extends DefaultDriverProvider implements DriverAdminS
70 70
71 private Set<DriverProvider> providers = Sets.newConcurrentHashSet(); 71 private Set<DriverProvider> providers = Sets.newConcurrentHashSet();
72 private Map<String, Driver> driverByKey = Maps.newConcurrentMap(); 72 private Map<String, Driver> driverByKey = Maps.newConcurrentMap();
73 + private Map<String, Class<? extends Behaviour>> classes = Maps.newConcurrentMap();
73 74
74 @Activate 75 @Activate
75 protected void activate() { 76 protected void activate() {
...@@ -80,6 +81,9 @@ public class DriverManager extends DefaultDriverProvider implements DriverAdminS ...@@ -80,6 +81,9 @@ public class DriverManager extends DefaultDriverProvider implements DriverAdminS
80 @Deactivate 81 @Deactivate
81 protected void deactivate() { 82 protected void deactivate() {
82 AbstractProjectableModel.setDriverService(this, null); 83 AbstractProjectableModel.setDriverService(this, null);
84 + providers.clear();
85 + driverByKey.clear();
86 + classes.clear();
83 log.info("Stopped"); 87 log.info("Stopped");
84 } 88 }
85 89
...@@ -95,6 +99,11 @@ public class DriverManager extends DefaultDriverProvider implements DriverAdminS ...@@ -95,6 +99,11 @@ public class DriverManager extends DefaultDriverProvider implements DriverAdminS
95 driverByKey.put(key(driver.manufacturer(), 99 driverByKey.put(key(driver.manufacturer(),
96 driver.hwVersion(), 100 driver.hwVersion(),
97 driver.swVersion()), d); 101 driver.swVersion()), d);
102 + d.behaviours().forEach(b -> {
103 + Class<? extends Behaviour> implementation = d.implementation(b);
104 + classes.put(b.getName(), b);
105 + classes.put(implementation.getName(), implementation);
106 + });
98 }); 107 });
99 providers.add(provider); 108 providers.add(provider);
100 } 109 }
...@@ -111,6 +120,11 @@ public class DriverManager extends DefaultDriverProvider implements DriverAdminS ...@@ -111,6 +120,11 @@ public class DriverManager extends DefaultDriverProvider implements DriverAdminS
111 } 120 }
112 121
113 @Override 122 @Override
123 + public Class<? extends Behaviour> getBehaviourClass(String className) {
124 + return classes.get(className);
125 + }
126 +
127 + @Override
114 public Set<Driver> getDrivers() { 128 public Set<Driver> getDrivers() {
115 checkPermission(DRIVER_READ); 129 checkPermission(DRIVER_READ);
116 ImmutableSet.Builder<Driver> builder = ImmutableSet.builder(); 130 ImmutableSet.Builder<Driver> builder = ImmutableSet.builder();
......