Thomas Vachuska
Committed by Ray Milkey

Sketching out device driver subsystem to allow extensible device management func…

…tions to be made available to applications.
Revising some implementations and adding tests.
Removing fingerprint and extension concept for now.

Change-Id: Id50faf29e4590b08e0c2b385ae8d4bb0c453f37e
Showing 36 changed files with 1682 additions and 20 deletions
...@@ -37,6 +37,15 @@ ...@@ -37,6 +37,15 @@
37 <artifactId>joda-time</artifactId> 37 <artifactId>joda-time</artifactId>
38 </dependency> 38 </dependency>
39 <dependency> 39 <dependency>
40 + <groupId>commons-configuration</groupId>
41 + <artifactId>commons-configuration</artifactId>
42 + </dependency>
43 + <dependency>
44 + <groupId>commons-collections</groupId>
45 + <artifactId>commons-collections</artifactId>
46 + <version>3.2.1</version>
47 + </dependency>
48 + <dependency>
40 <groupId>com.google.guava</groupId> 49 <groupId>com.google.guava</groupId>
41 <artifactId>guava-testlib</artifactId> 50 <artifactId>guava-testlib</artifactId>
42 <scope>test</scope> 51 <scope>test</scope>
......
...@@ -16,14 +16,13 @@ ...@@ -16,14 +16,13 @@
16 package org.onosproject.net; 16 package org.onosproject.net;
17 17
18 import static com.google.common.base.Preconditions.checkArgument; 18 import static com.google.common.base.Preconditions.checkArgument;
19 +import static org.onosproject.net.DefaultAnnotations.EMPTY;
19 20
20 /** 21 /**
21 * Base abstraction of an annotated entity. 22 * Base abstraction of an annotated entity.
22 */ 23 */
23 public abstract class AbstractAnnotated implements Annotated { 24 public abstract class AbstractAnnotated implements Annotated {
24 25
25 - private static final Annotations EMPTY = DefaultAnnotations.builder().build();
26 -
27 private final Annotations annotations; 26 private final Annotations annotations;
28 27
29 // For serialization 28 // For serialization
......
...@@ -29,6 +29,8 @@ import static com.google.common.base.Preconditions.checkNotNull; ...@@ -29,6 +29,8 @@ import static com.google.common.base.Preconditions.checkNotNull;
29 */ 29 */
30 public final class DefaultAnnotations implements SparseAnnotations { 30 public final class DefaultAnnotations implements SparseAnnotations {
31 31
32 + public static final Annotations EMPTY = DefaultAnnotations.builder().build();
33 +
32 private final Map<String, String> map; 34 private final Map<String, String> map;
33 35
34 // For serialization 36 // For serialization
......
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.net;
17 +
18 +/**
19 + * Represents an mutable set of simple key/value string annotations.
20 + */
21 +public interface MutableAnnotations extends Annotations {
22 +
23 + /**
24 + * Returns the value of the specified annotation.
25 + *
26 + * @param key annotation key
27 + * @param value annotation value
28 + * @return self
29 + */
30 + public MutableAnnotations set(String key, String value);
31 +
32 + /**
33 + * Clears the specified keys or the all keys if none were specified.
34 + *
35 + * @param keys keys to be cleared
36 + * @return self
37 + */
38 + public MutableAnnotations clear(String... keys);
39 +
40 +}
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.net.driver;
17 +
18 +/**
19 + * Base implementation of device driver behaviour.
20 + */
21 +public class AbstractBehaviour implements Behaviour {
22 +
23 + private DriverData data;
24 +
25 + @Override
26 + public void setData(DriverData data) {
27 + this.data = data;
28 + }
29 +}
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.net.driver;
17 +
18 +/**
19 + * Representation of a facet of device behaviour that can be used to talk about
20 + * a device (in context of {@link DriverData}) or to a device (in context of
21 + * {@link DriverHandler}).
22 + */
23 +public interface Behaviour {
24 +
25 + /**
26 + * Sets the driver data context on this this behaviour should operate.
27 + *
28 + * @param data driver data
29 + */
30 + void setData(DriverData data);
31 +
32 +}
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.net.driver;
17 +
18 +import com.google.common.collect.ImmutableMap;
19 +
20 +import java.util.Map;
21 +import java.util.Set;
22 +
23 +import static com.google.common.base.MoreObjects.toStringHelper;
24 +import static com.google.common.base.Preconditions.checkArgument;
25 +import static com.google.common.base.Preconditions.checkNotNull;
26 +import static com.google.common.collect.ImmutableMap.copyOf;
27 +
28 +/**
29 + * Default implementation of extensible driver.
30 + */
31 +public class DefaultDriver implements Driver {
32 +
33 + private final String name;
34 +
35 + private final String manufacturer;
36 + private final String hwVersion;
37 + private final String swVersion;
38 +
39 + private final Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours;
40 + private final Map<String, String> properties;
41 +
42 +
43 + /**
44 + * Creates a driver with the specified name.
45 + *
46 + * @param name driver name
47 + * @param manufacturer device manufacturer
48 + * @param hwVersion device hardware version
49 + * @param swVersion device software version
50 + * @param behaviours device behaviour classes
51 + * @param properties properties for configuration of device behaviour classes
52 + */
53 + public DefaultDriver(String name, String manufacturer,
54 + String hwVersion, String swVersion,
55 + Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours,
56 + Map<String, String> properties) {
57 + this.name = checkNotNull(name, "Name cannot be null");
58 + this.manufacturer = checkNotNull(manufacturer, "Manufacturer cannot be null");
59 + this.hwVersion = checkNotNull(hwVersion, "HW version cannot be null");
60 + this.swVersion = checkNotNull(swVersion, "SW version cannot be null");
61 + this.behaviours = copyOf(checkNotNull(behaviours, "Behaviours cannot be null"));
62 + this.properties = copyOf(checkNotNull(properties, "Properties cannot be null"));
63 + }
64 +
65 + /**
66 + * Merges the two drivers while giving preference to this driver when
67 + * dealing with conflicts.
68 + *
69 + * @param other other driver
70 + * @return new driver
71 + */
72 + DefaultDriver merge(DefaultDriver other) {
73 + // Merge the behaviours.
74 + ImmutableMap.Builder<Class<? extends Behaviour>, Class<? extends Behaviour>>
75 + behaviours = ImmutableMap.builder();
76 + behaviours.putAll(other.behaviours).putAll(this.behaviours);
77 +
78 + // Merge the properties.
79 + ImmutableMap.Builder<String, String> properties = ImmutableMap.builder();
80 + properties.putAll(other.properties).putAll(this.properties);
81 +
82 + return new DefaultDriver(name, manufacturer, hwVersion, swVersion,
83 + behaviours.build(), properties.build());
84 + }
85 +
86 + @Override
87 + public String name() {
88 + return name;
89 + }
90 +
91 + @Override
92 + public String manufacturer() {
93 + return manufacturer;
94 + }
95 +
96 + @Override
97 + public String hwVersion() {
98 + return hwVersion;
99 + }
100 +
101 + @Override
102 + public String swVersion() {
103 + return swVersion;
104 + }
105 +
106 + @Override
107 + public Set<Class<? extends Behaviour>> behaviours() {
108 + return behaviours.keySet();
109 + }
110 +
111 + @Override
112 + public boolean hasBehaviour(Class<? extends Behaviour> behaviourClass) {
113 + return behaviours.containsKey(behaviourClass);
114 + }
115 +
116 + /**
117 + * Creates an instance of behaviour primed with the specified driver data.
118 + *
119 + * @param data driver data context
120 + * @param behaviourClass driver behaviour class
121 + * @param handler indicates behaviour is intended for handler context
122 + * @param <T> type of behaviour
123 + * @return behaviour instance
124 + */
125 + public <T extends Behaviour> T createBehaviour(DriverData data,
126 + Class<T> behaviourClass,
127 + boolean handler) {
128 + checkArgument(handler || !HandlerBehaviour.class.isAssignableFrom(behaviourClass),
129 + "{} is applicable only to handler context", behaviourClass.getName());
130 +
131 + // Locate the implementation of the requested behaviour.
132 + Class<? extends Behaviour> implementation = behaviours.get(behaviourClass);
133 + checkArgument(implementation != null, "{} not supported", behaviourClass.getName());
134 +
135 + // Create an instance of the behaviour and apply data as its context.
136 + T behaviour = createBehaviour(behaviourClass, implementation);
137 + behaviour.setData(data);
138 + return behaviour;
139 + }
140 +
141 + @SuppressWarnings("unchecked")
142 + private <T extends Behaviour> T createBehaviour(Class<T> behaviourClass,
143 + Class<? extends Behaviour> implementation) {
144 + try {
145 + return (T) implementation.newInstance();
146 + } catch (InstantiationException | IllegalAccessException e) {
147 + // TODO: add a specific unchecked exception
148 + throw new IllegalArgumentException("Unable to create behaviour", e);
149 + }
150 + }
151 +
152 + @Override
153 + public Set<String> keys() {
154 + return properties.keySet();
155 + }
156 +
157 + @Override
158 + public String value(String key) {
159 + return properties.get(key);
160 + }
161 +
162 + @Override
163 + public Map<String, String> properties() {
164 + return properties;
165 + }
166 +
167 + @Override
168 + public String toString() {
169 + return toStringHelper(this)
170 + .add("name", name)
171 + .add("manufacturer", manufacturer)
172 + .add("hwVersion", hwVersion)
173 + .add("swVersion", swVersion)
174 + .add("behaviours", behaviours)
175 + .add("properties", properties)
176 + .toString();
177 + }
178 +
179 +}
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.net.driver;
17 +
18 +import com.google.common.collect.ImmutableSet;
19 +import org.onosproject.net.MutableAnnotations;
20 +
21 +import java.util.HashMap;
22 +import java.util.Map;
23 +import java.util.Set;
24 +
25 +import static com.google.common.base.MoreObjects.toStringHelper;
26 +
27 +/**
28 + * Default implementation of driver data descriptor.
29 + */
30 +public class DefaultDriverData implements DriverData {
31 +
32 + private final Driver type;
33 + private final Map<String, String> properties;
34 +
35 + /**
36 + * Creates new driver data.
37 + *
38 + * @param type parent driver type
39 + */
40 + public DefaultDriverData(Driver type) {
41 + this.type = type;
42 + this.properties = new HashMap<>();
43 + }
44 +
45 + @Override
46 + public Driver type() {
47 + return type;
48 + }
49 +
50 + @Override
51 + public <T extends Behaviour> T behaviour(Class<T> behaviourClass) {
52 + return type.createBehaviour(this, behaviourClass, false);
53 + }
54 +
55 + @Override
56 + public MutableAnnotations set(String key, String value) {
57 + properties.put(key, value);
58 + return this;
59 + }
60 +
61 + @Override
62 + public MutableAnnotations clear(String... keys) {
63 + if (keys.length == 0) {
64 + properties.clear();
65 + } else {
66 + for (String key : keys) {
67 + properties.remove(key);
68 + }
69 + }
70 + return this;
71 + }
72 +
73 + @Override
74 + public Set<String> keys() {
75 + return ImmutableSet.copyOf(properties.keySet());
76 + }
77 +
78 + @Override
79 + public String value(String key) {
80 + return properties.get(key);
81 + }
82 +
83 + @Override
84 + public String toString() {
85 + return toStringHelper(this)
86 + .add("type", type)
87 + .add("properties", properties)
88 + .toString();
89 + }
90 +
91 +}
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.net.driver;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +
20 +/**
21 + * Default implementation of driver handler.
22 + */
23 +public class DefaultDriverHandler implements DriverHandler {
24 +
25 + private final DefaultDriverData data;
26 +
27 + /**
28 + * Creates new driver handler with the attached driver data.
29 + *
30 + * @param data driver data to attach
31 + */
32 + public DefaultDriverHandler(DefaultDriverData data) {
33 + this.data = data;
34 + }
35 +
36 + @Override
37 + public DriverData data() {
38 + return data;
39 + }
40 +
41 + @Override
42 + public <T extends Behaviour> T behaviour(Class<T> behaviourClass) {
43 + return data.type().createBehaviour(this.data, behaviourClass, true);
44 + }
45 +
46 + @Override
47 + public String toString() {
48 + return toStringHelper(this)
49 + .add("data", data)
50 + .toString();
51 + }
52 +
53 +}
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.net.driver;
17 +
18 +import com.google.common.collect.ImmutableSet;
19 +
20 +import java.util.HashMap;
21 +import java.util.Map;
22 +import java.util.Set;
23 +
24 +import static com.google.common.base.MoreObjects.toStringHelper;
25 +
26 +/**
27 + * Default driver provider implementation.
28 + */
29 +public class DefaultDriverProvider implements DriverProvider {
30 +
31 + private final Map<String, DefaultDriver> drivers = new HashMap<>();
32 +
33 + @Override
34 + public Set<Driver> getDrivers() {
35 + return ImmutableSet.copyOf(drivers.values());
36 + }
37 +
38 + /**
39 + * Adds the specified driver to be provided.
40 + *
41 + * @param driverClasses driver to be provided
42 + */
43 + public void addDrivers(Set<DefaultDriver> driverClasses) {
44 + for (DefaultDriver driverClass : driverClasses) {
45 + addDriver(driverClass);
46 + }
47 + }
48 +
49 + /**
50 + * Adds the specified driver to be provided.
51 + *
52 + * @param driverClass driver to be provided
53 + */
54 + public void addDriver(DefaultDriver driverClass) {
55 + DefaultDriver ddc = drivers.get(driverClass.name());
56 + if (ddc == null) {
57 + // If we don't have the driver yet, just use the new one.
58 + drivers.put(driverClass.name(), driverClass);
59 + } else {
60 + // Otherwise merge the existing driver with the new one and rebind.
61 + drivers.put(driverClass.name(), ddc.merge(driverClass));
62 + }
63 + }
64 +
65 + @Override
66 + public String toString() {
67 + return toStringHelper(this).add("drivers", drivers).toString();
68 + }
69 +}
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.net.driver;
17 +
18 +import org.onosproject.net.Annotations;
19 +
20 +import java.util.Map;
21 +import java.util.Set;
22 +
23 +/**
24 + * Representation of a specific family of device drivers. Behaviour configuration
25 + * data is stored using {@link org.onosproject.net.Annotations}.
26 + */
27 +public interface Driver extends Annotations {
28 +
29 + /**
30 + * Returns the driver name. This is expected to be a reverse-DNS,
31 + * Java package-like name.
32 + *
33 + * @return driver name
34 + */
35 + String name();
36 +
37 + /**
38 + * Returns the device manufacturer name.
39 + *
40 + * @return manufacturer name
41 + */
42 + String manufacturer();
43 +
44 + /**
45 + * Returns the device hardware version.
46 + *
47 + * @return hardware version
48 + */
49 + String hwVersion();
50 +
51 + /**
52 + * Returns the device software version.
53 + *
54 + * @return software version
55 + */
56 + String swVersion();
57 +
58 + /**
59 + * Returns the set of behaviours supported by this driver.
60 + *
61 + * @return set of device driver behaviours
62 + */
63 + Set<Class<? extends Behaviour>> behaviours();
64 +
65 + /**
66 + * Indicates whether or not the driver supports the specified class
67 + * of behaviour.
68 + *
69 + * @param behaviourClass behaviour class
70 + * @return true if behaviour is supported
71 + */
72 + boolean hasBehaviour(Class<? extends Behaviour> behaviourClass);
73 +
74 + /**
75 + * Creates an instance of behaviour primed with the specified driver data.
76 + *
77 + * @param data driver data context
78 + * @param behaviourClass driver behaviour class
79 + * @param handler indicates behaviour is intended for handler context
80 + * @param <T> type of behaviour
81 + * @return behaviour instance
82 + */
83 + <T extends Behaviour> T createBehaviour(DriverData data, Class<T> behaviourClass,
84 + boolean handler);
85 +
86 + /**
87 + * Returns the set of annotations as map of key/value properties.
88 + *
89 + * @return map of properties
90 + */
91 + Map<String, String> properties();
92 +
93 +}
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.net.driver;
17 +
18 +import java.util.Set;
19 +
20 +/**
21 + * Service for managing drivers and driver behaviour implementations.
22 + */
23 +public interface DriverAdminService extends DriverService {
24 +
25 + /**
26 + * Returns the set of driver providers currently registered.
27 + *
28 + * @return registered driver providers
29 + */
30 + Set<DriverProvider> getProviders();
31 +
32 + /**
33 + * Registers the specified driver provider.
34 + *
35 + * @param provider driver provider to register
36 + */
37 + void registerProvider(DriverProvider provider);
38 +
39 + /**
40 + * Unregisters the specified driver provider.
41 + *
42 + * @param provider driver provider to unregister
43 + */
44 + void unregisterProvider(DriverProvider provider);
45 +
46 +}
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.net.driver;
17 +
18 +/**
19 + * Abstraction of handler behaviour used to set-up and tear-down driver
20 + * connection with a device.
21 + */
22 +public interface DriverConnect extends HandlerBehaviour {
23 +
24 + /**
25 + * Connects to the device.
26 + *
27 + * @param credentials optional login credentials in string form
28 + */
29 + void connect(String... credentials);
30 +
31 + /**
32 + * Disconnects from the device.
33 + */
34 + void disconnect();
35 +
36 +}
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.net.driver;
17 +
18 +import org.onosproject.net.MutableAnnotations;
19 +
20 +/**
21 + * Container for data about a device. Data is stored using
22 + * {@link org.onosproject.net.MutableAnnotations}.
23 + */
24 +public interface DriverData extends MutableAnnotations {
25 +
26 + /**
27 + * Returns the parent device driver.
28 + *
29 + * @return device driver
30 + */
31 + Driver type();
32 +
33 + /**
34 + * Returns the specified facet of behaviour to access the device data.
35 + *
36 + * @param behaviourClass behaviour class
37 + * @param <T> type of behaviour
38 + * @return requested behaviour or null if not supported
39 + */
40 + <T extends Behaviour> T behaviour(Class<T> behaviourClass);
41 +
42 +}
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.net.driver;
17 +
18 +/**
19 + * Representation of context for interacting with a device.
20 + */
21 +public interface DriverHandler {
22 +
23 + /**
24 + * Returns the device driver data.
25 + *
26 + * @return device driver data
27 + */
28 + DriverData data();
29 +
30 + /**
31 + * Returns the specified facet of behaviour to interact with the device.
32 + *
33 + * @param behaviourClass behaviour class
34 + * @param <T> type of behaviour
35 + * @return behaviour
36 + */
37 + <T extends Behaviour> T behaviour(Class<T> behaviourClass);
38 +
39 +}
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.net.driver;
17 +
18 +import java.util.Set;
19 +
20 +/**
21 + * Represents entity capable of providing device drivers and their
22 + * behaviours.
23 + */
24 +public interface DriverProvider {
25 +
26 + /**
27 + * Returns the set of driver types and behaviour implementations to be
28 + * made available by this provider.
29 + *
30 + * @return set of driver types and their behaviours
31 + */
32 + Set<Driver> getDrivers();
33 +
34 +}
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.net.driver;
17 +
18 +import org.onosproject.net.DeviceId;
19 +
20 +import java.util.Set;
21 +
22 +/**
23 + * Service for obtaining drivers and driver behaviour implementations.
24 + */
25 +public interface DriverService {
26 +
27 + /**
28 + * Returns the overall set of drivers being provided, optionally
29 + * filtered to only those that support all specified behaviours.
30 + *
31 + * @param withBehaviours optional behaviour classes to query by
32 + * @return provided drivers
33 + */
34 + Set<Driver> getDrivers(Class<? extends Behaviour>... withBehaviours);
35 +
36 + /**
37 + * Returns the specified driver.
38 + *
39 + * @param driverName driver name
40 + * @return driver
41 + */
42 + Driver getDriver(String driverName);
43 +
44 + /**
45 + * Returns the driver that matches the specified primordial device
46 + * discovery information.
47 + *
48 + * @param mfr device manufacturer
49 + * @param hw device hardware name/version
50 + * @param sw device software version
51 + * @return driver or null of no matching one is found
52 + */
53 + Driver getDriver(String mfr, String hw, String sw);
54 +
55 + /**
56 + * Creates a new driver handler for the specified driver.
57 + *
58 + * @param driverName driver name
59 + * @param deviceId device identifier
60 + * @param credentials optional login credentials in string form
61 + * @return driver handler
62 + */
63 + DriverHandler createHandler(String driverName, DeviceId deviceId,
64 + String... credentials);
65 +
66 + /**
67 + * Creates a new driver handler for the specified driver data.
68 + *
69 + * @param data driver data
70 + * @param deviceId device identifier
71 + * @param credentials optional login credentials in string form
72 + * @return driver handler
73 + */
74 + DriverHandler createHandler(DriverData data, DeviceId deviceId,
75 + String... credentials);
76 +
77 +}
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.net.driver;
17 +
18 +/**
19 + * Representation of a facet of device behaviour that can be used to interact
20 + * with a device (in context of {@link org.onosproject.net.driver.DriverHandler}).
21 + */
22 +public interface HandlerBehaviour extends Behaviour {
23 +}
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.net.driver;
17 +
18 +import com.google.common.collect.ImmutableMap;
19 +import org.apache.commons.configuration.ConfigurationException;
20 +import org.apache.commons.configuration.HierarchicalConfiguration;
21 +import org.apache.commons.configuration.XMLConfiguration;
22 +
23 +import java.io.IOException;
24 +import java.io.InputStream;
25 +import java.util.Map;
26 +
27 +/**
28 + * Utility capable of reading driver configuration XML resources and producing
29 + * a device driver provider as a result.
30 + * <p>
31 + * The drivers stream structure is as follows:
32 + * </p>
33 + * <pre>
34 + * &lt;drivers&gt;
35 + * &lt;driver name=“...” [manufacturer="..." hwVersion="..." swVersion="..."]&gt;
36 + * &lt;behaviour api="..." impl="..."/&gt;
37 + * ...
38 + * [&lt;property name=“key”&gt;value&lt;/key&gt;]
39 + * ...
40 + * &lt;/driver&gt;
41 + * ...
42 + * &lt;/drivers&gt;
43 + * </pre>
44 + */
45 +public class XmlDriverLoader {
46 +
47 + private static final String DRIVERS = "drivers";
48 + private static final String DRIVER = "driver";
49 +
50 + private static final String BEHAVIOUR = "behaviour";
51 + private static final String PROPERTY = "property";
52 +
53 + private static final String NAME = "[@name]";
54 + private static final String MFG = "[@manufacturer]";
55 + private static final String HW = "[@hwVersion]";
56 + private static final String SW = "[@swVersion]";
57 + private static final String API = "[@api]";
58 + private static final String IMPL = "[@impl]";
59 +
60 + private final ClassLoader classLoader;
61 +
62 + /**
63 + * Creates a new driver loader capable of loading drivers from the supplied
64 + * class loader.
65 + *
66 + * @param classLoader class loader to use
67 + */
68 + public XmlDriverLoader(ClassLoader classLoader) {
69 + this.classLoader = classLoader;
70 + }
71 +
72 + /**
73 + * Loads the specified drivers resource as an XML stream and parses it to
74 + * produce a ready-to-register driver provider.
75 + *
76 + * @param driversStream stream containing the drivers definitions
77 + * @return driver provider
78 + * @throws java.io.IOException if issues are encountered reading the stream
79 + * or parsing the driver definitions within
80 + */
81 + public DefaultDriverProvider loadDrivers(InputStream driversStream) throws IOException {
82 + try {
83 + XMLConfiguration cfg = new XMLConfiguration();
84 + cfg.setRootElementName(DRIVERS);
85 + cfg.setAttributeSplittingDisabled(true);
86 +
87 + cfg.load(driversStream);
88 + return loadDrivers(cfg);
89 + } catch (ConfigurationException e) {
90 + throw new IOException("Unable to load drivers", e);
91 + }
92 + }
93 +
94 + /**
95 + * Loads a driver provider from the supplied hierarchical configuration.
96 + *
97 + * @param driversCfg hierarchical configuration containing the drivers definitions
98 + * @return driver provider
99 + */
100 + public DefaultDriverProvider loadDrivers(HierarchicalConfiguration driversCfg) {
101 + DefaultDriverProvider provider = new DefaultDriverProvider();
102 + for (HierarchicalConfiguration cfg : driversCfg.configurationsAt(DRIVER)) {
103 + provider.addDriver(loadDriver(cfg));
104 + }
105 + return provider;
106 + }
107 +
108 + /**
109 + * Loads a driver from the supplied hierarchical configuration.
110 + *
111 + * @param driverCfg hierarchical configuration containing the driver definition
112 + * @return driver
113 + */
114 + public DefaultDriver loadDriver(HierarchicalConfiguration driverCfg) {
115 + String name = driverCfg.getString(NAME);
116 + String manufacturer = driverCfg.getString(MFG, "");
117 + String hwVersion = driverCfg.getString(HW, "");
118 + String swVersion = driverCfg.getString(SW, "");
119 +
120 + return new DefaultDriver(name, manufacturer, hwVersion, swVersion,
121 + parseBehaviours(driverCfg),
122 + parseProperties(driverCfg));
123 + }
124 +
125 + // Parses the behaviours section.
126 + private Map<Class<? extends Behaviour>, Class<? extends Behaviour>>
127 + parseBehaviours(HierarchicalConfiguration driverCfg) {
128 + ImmutableMap.Builder<Class<? extends Behaviour>,
129 + Class<? extends Behaviour>> behaviours = ImmutableMap.builder();
130 + for (HierarchicalConfiguration b : driverCfg.configurationsAt(BEHAVIOUR)) {
131 + behaviours.put(getClass(b.getString(API)), getClass(b.getString(IMPL)));
132 + }
133 + return behaviours.build();
134 + }
135 +
136 + // Parses the properties section.
137 + private Map<String, String> parseProperties(HierarchicalConfiguration driverCfg) {
138 + ImmutableMap.Builder<String, String> properties = ImmutableMap.builder();
139 + for (HierarchicalConfiguration b : driverCfg.configurationsAt(PROPERTY)) {
140 + properties.put(b.getString(NAME), (String) b.getRootNode().getValue());
141 + }
142 + return properties.build();
143 + }
144 +
145 + @SuppressWarnings("unchecked")
146 + private Class<? extends Behaviour> getClass(String className) {
147 + try {
148 + return (Class<? extends Behaviour>) classLoader.loadClass(className);
149 + } catch (ClassNotFoundException e) {
150 + throw new IllegalArgumentException("Unable to load class " + className, e);
151 + }
152 + }
153 +
154 +}
1 +/*
2 + * Copyright 2015 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 +/**
18 + * Set of facilities to allow the platform to be extended with
19 + * device specific behaviours and to allow modeling device behaviours while
20 + * hiding details of specific device driver implementations.
21 + * <p>
22 + * {@link org.onosproject.net.driver.Driver} is a representation of a
23 + * specific family of devices supports set of
24 + * {@link org.onosproject.net.driver.Behaviour behaviour classes}. Default
25 + * implementation is provided by the platform and allows DriverProviders to
26 + * add different behaviour implementations via DriverService.
27 + * </p>
28 + * <p>
29 + * {@link org.onosproject.net.driver.DriverData} is a container for data
30 + * learned about a device. It is associated with a specific
31 + * {@link org.onosproject.net.driver.Driver}
32 + * and provides set of {@link org.onosproject.net.driver.Behaviour behaviours}
33 + * for talking about a device. A default
34 + * implementation provided by platform and has mutable key/value store for use by
35 + * implementations of {@link org.onosproject.net.driver.Behaviour behaviours}.
36 + * </p>
37 + * <p>
38 + * {@link org.onosproject.net.driver.DriverHandler} is an entity used as a
39 + * context to interact with a device. It has a peer
40 + * {@link org.onosproject.net.driver.DriverData} instance, which is used to
41 + * store information learned about a device. It also
42 + * provides set of {@link org.onosproject.net.driver.Behaviour behaviours}
43 + * for talking to a device.
44 + * </p>
45 + * <p>
46 + * {@link org.onosproject.net.driver.DriverService} can be used to query the
47 + * inventory of device drivers and their behaviours, while the
48 + * {@link org.onosproject.net.driver.DriverAdminService} allows adding/removing
49 + * drivers and managing behaviour implementations.
50 + * {@link org.onosproject.net.driver.DriverProvider} is an entity capable
51 + * of add/removing drivers and supplying and managing behaviour
52 + * implementations. A default implementation is provided by the framework along
53 + * with a {@link org.onosproject.net.driver.XmlDriverLoader loader utility} to
54 + * create a driver provider from an XML file structured as follows:
55 + * <pre>
56 + * &lt;drivers&gt;
57 + * &lt;driver name=“...” [manufacturer="..." hwVersion="..." swVersion="..."]&gt;
58 + * &lt;behaviour api="..." impl="..."/&gt;
59 + * ...
60 + * [&lt;property name=“key”&gt;value&lt;/key&gt;]
61 + * ...
62 + * &lt;/driver&gt;
63 + * ...
64 + * &lt;/drivers&gt;
65 + * </pre>
66 + *
67 + */
68 +package org.onosproject.net.driver;
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.net.driver;
17 +
18 +import com.google.common.collect.ImmutableMap;
19 +import org.junit.Before;
20 +import org.junit.Test;
21 +
22 +import static org.junit.Assert.*;
23 +
24 +public class DefaultDriverDataTest {
25 +
26 + DefaultDriver ddc;
27 + DefaultDriverData data;
28 +
29 + @Before
30 + public void setUp() {
31 + ddc = new DefaultDriver("foo.bar", "Circus", "lux", "1.2a",
32 + ImmutableMap.of(TestBehaviour.class,
33 + TestBehaviourImpl.class),
34 + ImmutableMap.of("foo", "bar"));
35 + data = new DefaultDriverData(ddc);
36 + }
37 +
38 + @Test
39 + public void basics() {
40 + assertSame("incorrect type", ddc, data.type());
41 + assertTrue("incorrect toString", data.toString().contains("foo.bar"));
42 + }
43 +
44 + @Test
45 + public void behaviour() {
46 + TestBehaviour behaviour = data.behaviour(TestBehaviour.class);
47 + assertTrue("incorrect behaviour", behaviour instanceof TestBehaviourImpl);
48 + }
49 +
50 + @Test
51 + public void setAndClearAnnotations() {
52 + data.set("croc", "aqua");
53 + data.set("roo", "mars");
54 + data.set("dingo", "bat");
55 + assertEquals("incorrect property", "bat", data.value("dingo"));
56 + data.clear("dingo", "roo");
57 + assertNull("incorrect property", data.value("dingo"));
58 + assertNull("incorrect property", data.value("root"));
59 + assertEquals("incorrect property", "aqua", data.value("croc"));
60 + assertEquals("incorrect properties", 1, data.keys().size());
61 + }
62 +
63 + @Test
64 + public void clearAllAnnotations() {
65 + data.set("croc", "aqua");
66 + data.set("roo", "mars");
67 + data.set("dingo", "bat");
68 + assertEquals("incorrect property", "bat", data.value("dingo"));
69 + data.clear();
70 + assertEquals("incorrect properties", 0, data.keys().size());
71 + }
72 +
73 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.net.driver;
17 +
18 +import com.google.common.collect.ImmutableMap;
19 +import org.junit.Before;
20 +import org.junit.Test;
21 +
22 +import static org.junit.Assert.assertSame;
23 +import static org.junit.Assert.assertTrue;
24 +
25 +public class DefaultDriverHandlerTest {
26 +
27 + DefaultDriver ddc;
28 + DefaultDriverData data;
29 + DefaultDriverHandler handler;
30 +
31 + @Before
32 + public void setUp() {
33 + ddc = new DefaultDriver("foo.bar", "Circus", "lux", "1.2a",
34 + ImmutableMap.of(TestBehaviour.class,
35 + TestBehaviourImpl.class,
36 + TestBehaviourTwo.class,
37 + TestBehaviourTwoImpl.class),
38 + ImmutableMap.of("foo", "bar"));
39 + data = new DefaultDriverData(ddc);
40 + handler = new DefaultDriverHandler(data);
41 + }
42 +
43 + @Test
44 + public void basics() {
45 + assertSame("incorrect data", data, handler.data());
46 + assertTrue("incorrect toString", handler.toString().contains("1.2a"));
47 + }
48 +
49 + @Test
50 + public void behaviour() {
51 + TestBehaviourTwo behaviour = handler.behaviour(TestBehaviourTwo.class);
52 + assertTrue("incorrect behaviour", behaviour instanceof TestBehaviourTwoImpl);
53 + }
54 +
55 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.net.driver;
17 +
18 +import com.google.common.collect.ImmutableMap;
19 +import org.junit.Test;
20 +
21 +import java.util.Set;
22 +
23 +import static com.google.common.collect.ImmutableSet.of;
24 +import static org.junit.Assert.assertEquals;
25 +
26 +public class DefaultDriverProviderTest {
27 +
28 + @Test
29 + public void basics() {
30 + DefaultDriverProvider ddp = new DefaultDriverProvider();
31 + DefaultDriver one = new DefaultDriver("foo.bar", "Circus", "lux", "1.2a",
32 + ImmutableMap.of(TestBehaviour.class,
33 + TestBehaviourImpl.class),
34 + ImmutableMap.of("foo", "bar"));
35 + DefaultDriver two = new DefaultDriver("foo.bar", "", "", "",
36 + ImmutableMap.of(TestBehaviourTwo.class,
37 + TestBehaviourTwoImpl.class),
38 + ImmutableMap.of("goo", "wee"));
39 + DefaultDriver three = new DefaultDriver("goo.foo", "BigTop", "better", "2.2",
40 + ImmutableMap.of(TestBehaviourTwo.class,
41 + TestBehaviourTwoImpl.class),
42 + ImmutableMap.of("goo", "gee"));
43 +
44 + ddp.addDrivers(of(one, two, three));
45 +
46 + Set<Driver> drivers = ddp.getDrivers();
47 + assertEquals("incorrect types", 2, drivers.size());
48 + }
49 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.net.driver;
17 +
18 +import com.google.common.collect.ImmutableMap;
19 +import org.junit.Test;
20 +
21 +import static org.junit.Assert.assertEquals;
22 +import static org.junit.Assert.assertTrue;
23 +
24 +public class DefaultDriverTest {
25 +
26 + @Test
27 + public void basics() {
28 + DefaultDriver ddc = new DefaultDriver("foo.bar", "Circus", "lux", "1.2a",
29 + ImmutableMap.of(TestBehaviour.class,
30 + TestBehaviourImpl.class),
31 + ImmutableMap.of("foo", "bar"));
32 + assertEquals("incorrect name", "foo.bar", ddc.name());
33 + assertEquals("incorrect mfr", "Circus", ddc.manufacturer());
34 + assertEquals("incorrect hw", "lux", ddc.hwVersion());
35 + assertEquals("incorrect sw", "1.2a", ddc.swVersion());
36 +
37 + assertEquals("incorrect behaviour count", 1, ddc.behaviours().size());
38 + assertTrue("incorrect behaviour", ddc.hasBehaviour(TestBehaviour.class));
39 +
40 + assertEquals("incorrect property count", 1, ddc.properties().size());
41 + assertEquals("incorrect key count", 1, ddc.keys().size());
42 + assertEquals("incorrect property", "bar", ddc.value("foo"));
43 +
44 + assertTrue("incorrect toString", ddc.toString().contains("lux"));
45 + }
46 +
47 + @Test
48 + public void merge() {
49 + DefaultDriver one = new DefaultDriver("foo.bar", "Circus", "lux", "1.2a",
50 + ImmutableMap.of(TestBehaviour.class,
51 + TestBehaviourImpl.class),
52 + ImmutableMap.of("foo", "bar"));
53 + DefaultDriver ddc =
54 + one.merge(new DefaultDriver("foo.bar", "", "", "",
55 + ImmutableMap.of(TestBehaviourTwo.class,
56 + TestBehaviourTwoImpl.class),
57 + ImmutableMap.of("goo", "wee")));
58 +
59 + assertEquals("incorrect name", "foo.bar", ddc.name());
60 + assertEquals("incorrect mfr", "Circus", ddc.manufacturer());
61 + assertEquals("incorrect hw", "lux", ddc.hwVersion());
62 + assertEquals("incorrect sw", "1.2a", ddc.swVersion());
63 +
64 + assertEquals("incorrect behaviour count", 2, ddc.behaviours().size());
65 + assertTrue("incorrect behaviour", ddc.hasBehaviour(TestBehaviourTwo.class));
66 +
67 + assertEquals("incorrect property count", 2, ddc.properties().size());
68 + assertEquals("incorrect key count", 2, ddc.keys().size());
69 + assertEquals("incorrect property", "wee", ddc.value("goo"));
70 +
71 + assertTrue("incorrect toString", ddc.toString().contains("Circus"));
72 + }
73 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.net.driver;
17 +
18 +/**
19 + * Test behaviour.
20 + */
21 +public interface TestBehaviour extends Behaviour {
22 +}
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.net.driver;
17 +
18 +/**
19 + * Test behaviour.
20 + */
21 +public class TestBehaviourImpl extends AbstractBehaviour implements TestBehaviour {
22 +}
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.net.driver;
17 +
18 +/**
19 + * Bad test behaviour.
20 + */
21 +public final class TestBehaviourNoConstructorImpl
22 + extends AbstractBehaviour implements TestBehaviour {
23 + private TestBehaviourNoConstructorImpl() {
24 +
25 + }
26 +}
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.net.driver;
17 +
18 +/**
19 + * Test behaviour.
20 + */
21 +public interface TestBehaviourTwo extends HandlerBehaviour {
22 +}
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.net.driver;
17 +
18 +/**
19 + * Test behaviour.
20 + */
21 +public class TestBehaviourTwoImpl extends AbstractBehaviour implements TestBehaviourTwo {
22 +}
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.net.driver;
17 +
18 +import org.junit.Test;
19 +
20 +import java.io.IOException;
21 +import java.io.InputStream;
22 +
23 +import static org.junit.Assert.assertEquals;
24 +import static org.junit.Assert.assertTrue;
25 +
26 +/**
27 + * Tests of the XML driver loader implementation.
28 + */
29 +public class XmlDriverLoaderTest {
30 +
31 + @Test
32 + public void basics() throws IOException {
33 + XmlDriverLoader loader = new XmlDriverLoader(getClass().getClassLoader());
34 + InputStream stream = getClass().getResourceAsStream("drivers.1.xml");
35 + DriverProvider provider = loader.loadDrivers(stream);
36 + System.out.println(provider);
37 + assertEquals("incorrect driver count", 1, provider.getDrivers().size());
38 +
39 + Driver driver = provider.getDrivers().iterator().next();
40 + assertEquals("incorrect driver name", "foo.1", driver.name());
41 + assertEquals("incorrect driver mfg", "Circus", driver.manufacturer());
42 + assertEquals("incorrect driver hw", "1.2a", driver.hwVersion());
43 + assertEquals("incorrect driver sw", "2.2", driver.swVersion());
44 +
45 + assertEquals("incorrect driver behaviours", 2, driver.behaviours().size());
46 + assertTrue("incorrect driver behaviour", driver.hasBehaviour(TestBehaviour.class));
47 +
48 + assertEquals("incorrect driver properties", 2, driver.properties().size());
49 + assertTrue("incorrect driver property", driver.properties().containsKey("p1"));
50 + }
51 +
52 + @Test(expected = IOException.class)
53 + public void badXML() throws IOException {
54 + XmlDriverLoader loader = new XmlDriverLoader(getClass().getClassLoader());
55 + loader.loadDrivers(getClass().getResourceAsStream("drivers.bad.xml"));
56 + }
57 +
58 + @Test(expected = IllegalArgumentException.class)
59 + public void noClass() throws IOException {
60 + XmlDriverLoader loader = new XmlDriverLoader(getClass().getClassLoader());
61 + loader.loadDrivers(getClass().getResourceAsStream("drivers.noclass.xml"));
62 + }
63 +
64 + @Test(expected = IllegalArgumentException.class)
65 + public void noConstructor() throws IOException {
66 + XmlDriverLoader loader = new XmlDriverLoader(getClass().getClassLoader());
67 + InputStream stream = getClass().getResourceAsStream("drivers.noconstructor.xml");
68 + DriverProvider provider = loader.loadDrivers(stream);
69 + Driver driver = provider.getDrivers().iterator().next();
70 + driver.createBehaviour(new DefaultDriverData(driver), TestBehaviour.class, false);
71 + }
72 +
73 +}
...\ No newline at end of file ...\ No newline at end of file
1 +<?xml version="1.0" encoding="UTF-8"?>
2 +<!--
3 + ~ Copyright 2015 Open Networking Laboratory
4 + ~
5 + ~ Licensed under the Apache License, Version 2.0 (the "License");
6 + ~ you may not use this file except in compliance with the License.
7 + ~ You may obtain a copy of the License at
8 + ~
9 + ~ http://www.apache.org/licenses/LICENSE-2.0
10 + ~
11 + ~ Unless required by applicable law or agreed to in writing, software
12 + ~ distributed under the License is distributed on an "AS IS" BASIS,
13 + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 + ~ See the License for the specific language governing permissions and
15 + ~ limitations under the License.
16 + -->
17 +<drivers>
18 + <driver name="foo.1" manufacturer="Circus" hwVersion="1.2a" swVersion="2.2">
19 + <fingerprint>ding</fingerprint>
20 + <fingerprint>bat</fingerprint>
21 +
22 + <behaviour api="org.onosproject.net.driver.TestBehaviour"
23 + impl="org.onosproject.net.driver.TestBehaviourImpl"/>
24 + <behaviour api="org.onosproject.net.driver.TestBehaviourTwo"
25 + impl="org.onosproject.net.driver.TestBehaviourTwoImpl"/>
26 +
27 + <property name="p1">v1</property>
28 + <property name="p2">v2</property>
29 + </driver>
30 +</drivers>
...\ No newline at end of file ...\ No newline at end of file
1 +<?xml version="1.0" encoding="UTF-8"?>
2 +<!--
3 + ~ Copyright 2015 Open Networking Laboratory
4 + ~
5 + ~ Licensed under the Apache License, Version 2.0 (the "License");
6 + ~ you may not use this file except in compliance with the License.
7 + ~ You may obtain a copy of the License at
8 + ~
9 + ~ http://www.apache.org/licenses/LICENSE-2.0
10 + ~
11 + ~ Unless required by applicable law or agreed to in writing, software
12 + ~ distributed under the License is distributed on an "AS IS" BASIS,
13 + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 + ~ See the License for the specific language governing permissions and
15 + ~ limitations under the License.
16 + -->
17 +<drivers>
18 + <driver name="foo.1" manufacturer="Circus" hwVersion="1.2a" swVersion="2.2">
19 + <behaviour api="org.onosproject.net.driver.TestBehaviour"
20 + impl="org.onosproject.net.driver.TestBehaviourImpl"/>
21 + </driverXXXXX> <!-- intentional to test handling malformed XML -->
22 +</drivers>
...\ No newline at end of file ...\ No newline at end of file
1 +<?xml version="1.0" encoding="UTF-8"?>
2 +<!--
3 + ~ Copyright 2015 Open Networking Laboratory
4 + ~
5 + ~ Licensed under the Apache License, Version 2.0 (the "License");
6 + ~ you may not use this file except in compliance with the License.
7 + ~ You may obtain a copy of the License at
8 + ~
9 + ~ http://www.apache.org/licenses/LICENSE-2.0
10 + ~
11 + ~ Unless required by applicable law or agreed to in writing, software
12 + ~ distributed under the License is distributed on an "AS IS" BASIS,
13 + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 + ~ See the License for the specific language governing permissions and
15 + ~ limitations under the License.
16 + -->
17 +<drivers>
18 + <driver name="foo.1" manufacturer="Circus" hwVersion="1.2a" swVersion="2.2">
19 + <behaviour api="org.onosproject.net.driver.TestBehaviourXX"
20 + impl="org.onosproject.net.driver.TestBehaviourImpl"/>
21 + </driver>
22 +</drivers>
...\ No newline at end of file ...\ No newline at end of file
1 +<?xml version="1.0" encoding="UTF-8"?>
2 +<!--
3 + ~ Copyright 2015 Open Networking Laboratory
4 + ~
5 + ~ Licensed under the Apache License, Version 2.0 (the "License");
6 + ~ you may not use this file except in compliance with the License.
7 + ~ You may obtain a copy of the License at
8 + ~
9 + ~ http://www.apache.org/licenses/LICENSE-2.0
10 + ~
11 + ~ Unless required by applicable law or agreed to in writing, software
12 + ~ distributed under the License is distributed on an "AS IS" BASIS,
13 + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 + ~ See the License for the specific language governing permissions and
15 + ~ limitations under the License.
16 + -->
17 +<drivers>
18 + <driver name="foo.1" manufacturer="Circus" hwVersion="1.2a" swVersion="2.2">
19 + <behaviour api="org.onosproject.net.driver.TestBehaviour"
20 + impl="org.onosproject.net.driver.TestBehaviourNoConstructorImpl"/>
21 + </driver>
22 +</drivers>
...\ No newline at end of file ...\ No newline at end of file
...@@ -51,6 +51,9 @@ ...@@ -51,6 +51,9 @@
51 <bundle>mvn:com.fasterxml.jackson.core/jackson-annotations/2.4.2</bundle> 51 <bundle>mvn:com.fasterxml.jackson.core/jackson-annotations/2.4.2</bundle>
52 <bundle>mvn:com.fasterxml.jackson.core/jackson-databind/2.4.2</bundle> 52 <bundle>mvn:com.fasterxml.jackson.core/jackson-databind/2.4.2</bundle>
53 53
54 + <bundle>mvn:commons-configuration/commons-configuration/1.10</bundle>
55 + <bundle>mvn:commons-collections/commons-collections/3.2.1</bundle>
56 +
54 <!-- FIXME: we should switch to use fasterxml jackson --> 57 <!-- FIXME: we should switch to use fasterxml jackson -->
55 <bundle>mvn:org.codehaus.jackson/jackson-core-asl/1.9.13</bundle> 58 <bundle>mvn:org.codehaus.jackson/jackson-core-asl/1.9.13</bundle>
56 <bundle>mvn:org.codehaus.jackson/jackson-mapper-asl/1.9.13</bundle> 59 <bundle>mvn:org.codehaus.jackson/jackson-mapper-asl/1.9.13</bundle>
......
...@@ -54,7 +54,8 @@ ...@@ -54,7 +54,8 @@
54 54
55 <scm> 55 <scm>
56 <connection>scm:git:https://gerrit.onosproject.org/onos</connection> 56 <connection>scm:git:https://gerrit.onosproject.org/onos</connection>
57 - <developerConnection>scm:git:https://gerrit.onosproject.org/onos</developerConnection> 57 + <developerConnection>scm:git:https://gerrit.onosproject.org/onos
58 + </developerConnection>
58 <url>http://gerrit.onosproject.org/</url> 59 <url>http://gerrit.onosproject.org/</url>
59 </scm> 60 </scm>
60 61
...@@ -166,6 +167,12 @@ ...@@ -166,6 +167,12 @@
166 </dependency> 167 </dependency>
167 168
168 <dependency> 169 <dependency>
170 + <groupId>commons-configuration</groupId>
171 + <artifactId>commons-configuration</artifactId>
172 + <version>1.10</version>
173 + </dependency>
174 +
175 + <dependency>
169 <groupId>org.codehaus.jackson</groupId> 176 <groupId>org.codehaus.jackson</groupId>
170 <artifactId>jackson-core-asl</artifactId> 177 <artifactId>jackson-core-asl</artifactId>
171 <version>1.9.13</version> 178 <version>1.9.13</version>
...@@ -417,7 +424,8 @@ ...@@ -417,7 +424,8 @@
417 <redirectTestOutputToFile>true 424 <redirectTestOutputToFile>true
418 </redirectTestOutputToFile> 425 </redirectTestOutputToFile>
419 <printSummary>true</printSummary> 426 <printSummary>true</printSummary>
420 - <excludedGroups>org.onlab.junit.IntegrationTest</excludedGroups> 427 + <excludedGroups>org.onlab.junit.IntegrationTest
428 + </excludedGroups>
421 </configuration> 429 </configuration>
422 </plugin> 430 </plugin>
423 431
...@@ -474,20 +482,21 @@ ...@@ -474,20 +482,21 @@
474 </configuration> 482 </configuration>
475 </plugin> 483 </plugin>
476 <plugin> 484 <plugin>
477 - <groupId>org.codehaus.mojo</groupId> 485 + <groupId>org.codehaus.mojo</groupId>
478 - <artifactId>findbugs-maven-plugin</artifactId> 486 + <artifactId>findbugs-maven-plugin</artifactId>
479 - <version>3.0.0</version> 487 + <version>3.0.0</version>
480 - <dependencies> 488 + <dependencies>
481 - <dependency> 489 + <dependency>
482 - <groupId>org.onosproject</groupId> 490 + <groupId>org.onosproject</groupId>
483 - <artifactId>onos-build-conf</artifactId> 491 + <artifactId>onos-build-conf</artifactId>
484 - <version>1.0</version> 492 + <version>1.0</version>
485 - </dependency> 493 + </dependency>
486 - </dependencies> 494 + </dependencies>
487 - <configuration> 495 + <configuration>
488 - <effort>Max</effort> 496 + <effort>Max</effort>
489 - <excludeFilterFile>onos/findbugs-suppressions.xml</excludeFilterFile> 497 + <excludeFilterFile>onos/findbugs-suppressions.xml
490 - </configuration> 498 + </excludeFilterFile>
499 + </configuration>
491 </plugin> 500 </plugin>
492 501
493 <!-- TODO: add findbugs plugin for static code analysis; for explicit invocation only --> 502 <!-- TODO: add findbugs plugin for static code analysis; for explicit invocation only -->
...@@ -550,8 +559,10 @@ ...@@ -550,8 +559,10 @@
550 </dependencies> 559 </dependencies>
551 <configuration> 560 <configuration>
552 <!-- begin: workaround for unexpected NullPointerException on Eclipse --> 561 <!-- begin: workaround for unexpected NullPointerException on Eclipse -->
553 - <sourceDirectory>${project.build.sourceDirectory}</sourceDirectory> 562 + <sourceDirectory>${project.build.sourceDirectory}
554 - <testSourceDirectory>${project.build.testSourceDirectory}</testSourceDirectory> 563 + </sourceDirectory>
564 + <testSourceDirectory>${project.build.testSourceDirectory}
565 + </testSourceDirectory>
555 <!-- end: workaround for unexpected NullPointerException on Eclipse --> 566 <!-- end: workaround for unexpected NullPointerException on Eclipse -->
556 <configLocation>onos/checkstyle.xml</configLocation> 567 <configLocation>onos/checkstyle.xml</configLocation>
557 <suppressionsLocation>onos/suppressions.xml 568 <suppressionsLocation>onos/suppressions.xml
......