Committed by
Gerrit Code Review
[ONOS-2262] - OVSDB -- Create the implementation of HostProvider using
OVSDB prototol. 1.Notify the Host System when the ovsdb vm port is added. 2.Notify the Host System when the ovsdb vm port is removed. Change-Id: I46355cec84db897360a6eb37190a5bd7a8f3047d
Showing
9 changed files
with
731 additions
and
0 deletions
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.ovsdb.controller; | ||
17 | + | ||
18 | +import static com.google.common.base.MoreObjects.toStringHelper; | ||
19 | + | ||
20 | +import java.util.Objects; | ||
21 | +import java.util.Set; | ||
22 | + | ||
23 | +import org.onlab.packet.IpAddress; | ||
24 | +import org.onlab.packet.MacAddress; | ||
25 | + | ||
26 | +/** | ||
27 | + * This class is default event subject that implements OvsdbEventSubject. | ||
28 | + */ | ||
29 | +public class DefaultEventSubject implements OvsdbEventSubject { | ||
30 | + private final MacAddress mac; | ||
31 | + private final Set<IpAddress> ips; | ||
32 | + private final OvsdbPortName portname; | ||
33 | + private final OvsdbPortNumber portnumber; | ||
34 | + private final OvsdbDatapathId dpid; | ||
35 | + private final OvsdbPortType portType; | ||
36 | + private final OvsdbIfaceId ifaceid; | ||
37 | + | ||
38 | + /** | ||
39 | + * Creates an end-station event subject using the supplied information. | ||
40 | + * | ||
41 | + * @param mac host MAC address | ||
42 | + * @param ips host MAC ips | ||
43 | + * @param portname port name | ||
44 | + * @param portnumber port number | ||
45 | + * @param dpid ovs dpid | ||
46 | + * @param portType port type | ||
47 | + * @param ifaceid vm ifaceid | ||
48 | + */ | ||
49 | + public DefaultEventSubject(MacAddress mac, Set<IpAddress> ips, | ||
50 | + OvsdbPortName portname, OvsdbPortNumber portnumber, OvsdbDatapathId dpid, | ||
51 | + OvsdbPortType portType, OvsdbIfaceId ifaceid) { | ||
52 | + super(); | ||
53 | + this.mac = mac; | ||
54 | + this.ips = ips; | ||
55 | + this.portname = portname; | ||
56 | + this.portnumber = portnumber; | ||
57 | + this.dpid = dpid; | ||
58 | + this.portType = portType; | ||
59 | + this.ifaceid = ifaceid; | ||
60 | + } | ||
61 | + | ||
62 | + @Override | ||
63 | + public MacAddress hwAddress() { | ||
64 | + return mac; | ||
65 | + } | ||
66 | + | ||
67 | + @Override | ||
68 | + public Set<IpAddress> ipAddress() { | ||
69 | + return ips; | ||
70 | + } | ||
71 | + | ||
72 | + @Override | ||
73 | + public OvsdbPortName portName() { | ||
74 | + return portname; | ||
75 | + } | ||
76 | + | ||
77 | + @Override | ||
78 | + public OvsdbPortNumber portNumber() { | ||
79 | + return portnumber; | ||
80 | + } | ||
81 | + | ||
82 | + @Override | ||
83 | + public OvsdbPortType portType() { | ||
84 | + return portType; | ||
85 | + } | ||
86 | + | ||
87 | + @Override | ||
88 | + public OvsdbDatapathId dpid() { | ||
89 | + return dpid; | ||
90 | + } | ||
91 | + | ||
92 | + @Override | ||
93 | + public OvsdbIfaceId ifaceid() { | ||
94 | + return ifaceid; | ||
95 | + } | ||
96 | + | ||
97 | + @Override | ||
98 | + public int hashCode() { | ||
99 | + return Objects.hash(mac, portname, portnumber, dpid, portType, ifaceid); | ||
100 | + } | ||
101 | + | ||
102 | + @Override | ||
103 | + public boolean equals(Object obj) { | ||
104 | + if (this == obj) { | ||
105 | + return true; | ||
106 | + } | ||
107 | + if (obj instanceof DefaultEventSubject) { | ||
108 | + final DefaultEventSubject other = (DefaultEventSubject) obj; | ||
109 | + return Objects.equals(this.mac, other.mac) | ||
110 | + && Objects.equals(this.portname, other.portname) | ||
111 | + && Objects.equals(this.portnumber, other.portnumber) | ||
112 | + && Objects.equals(this.dpid, other.dpid) | ||
113 | + && Objects.equals(this.portType, other.portType) | ||
114 | + && Objects.equals(this.ifaceid, other.ifaceid); | ||
115 | + } | ||
116 | + return false; | ||
117 | + } | ||
118 | + | ||
119 | + @Override | ||
120 | + public String toString() { | ||
121 | + return toStringHelper(this).add("mac", mac).add("portname", portname) | ||
122 | + .add("portnumber", portnumber).add("portType", portType) | ||
123 | + .add("ipAddresses", ips).add("dpid", dpid).add("ifaceid", ifaceid) | ||
124 | + .toString(); | ||
125 | + } | ||
126 | +} |
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.ovsdb.controller; | ||
17 | + | ||
18 | +import java.util.Set; | ||
19 | + | ||
20 | +import org.onlab.packet.IpAddress; | ||
21 | +import org.onlab.packet.MacAddress; | ||
22 | + | ||
23 | +/** | ||
24 | + * Represents for a entity that carry important information for listener. | ||
25 | + */ | ||
26 | +public interface OvsdbEventSubject extends EventSubject { | ||
27 | + /** | ||
28 | + * Returns the MAC address associated with this host (NIC). | ||
29 | + * | ||
30 | + * @return the MAC address of this host | ||
31 | + */ | ||
32 | + MacAddress hwAddress(); | ||
33 | + | ||
34 | + /** | ||
35 | + * Returns the IP address associated with this host's MAC. | ||
36 | + * | ||
37 | + * @return host IP address | ||
38 | + */ | ||
39 | + Set<IpAddress> ipAddress(); | ||
40 | + | ||
41 | + /** | ||
42 | + * Returns the Port name associated with the host. | ||
43 | + * | ||
44 | + * @return port name | ||
45 | + */ | ||
46 | + OvsdbPortName portName(); | ||
47 | + | ||
48 | + /** | ||
49 | + * Returns the Port number associated with the host. | ||
50 | + * | ||
51 | + * @return port number | ||
52 | + */ | ||
53 | + OvsdbPortNumber portNumber(); | ||
54 | + | ||
55 | + /** | ||
56 | + * Returns the Port type associated with the host. | ||
57 | + * | ||
58 | + * @return port type | ||
59 | + */ | ||
60 | + OvsdbPortType portType(); | ||
61 | + | ||
62 | + /** | ||
63 | + * Returns the Ovs dpid associated with the host. | ||
64 | + * | ||
65 | + * @return Ovs dpid | ||
66 | + */ | ||
67 | + OvsdbDatapathId dpid(); | ||
68 | + | ||
69 | + /** | ||
70 | + * Returns the vm ifaceid associated with the host. | ||
71 | + * | ||
72 | + * @return vm ifaceid | ||
73 | + */ | ||
74 | + OvsdbIfaceId ifaceid(); | ||
75 | +} |
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.ovsdb.controller; | ||
17 | + | ||
18 | +import static com.google.common.base.MoreObjects.toStringHelper; | ||
19 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
20 | +import java.util.Objects; | ||
21 | + | ||
22 | +/** | ||
23 | + * The class representing a ifaceid. This class is immutable. | ||
24 | + */ | ||
25 | +public class OvsdbIfaceId { | ||
26 | + private final String value; | ||
27 | + | ||
28 | + /** | ||
29 | + * Constructor from a String ifaceid. | ||
30 | + * | ||
31 | + * @param value the ifaceid to use | ||
32 | + */ | ||
33 | + public OvsdbIfaceId(String value) { | ||
34 | + checkNotNull(value, "value is not null"); | ||
35 | + this.value = value; | ||
36 | + } | ||
37 | + | ||
38 | + /** | ||
39 | + * Gets the value of the ifaceid. | ||
40 | + * | ||
41 | + * @return the value of the ifaceid | ||
42 | + */ | ||
43 | + public String value() { | ||
44 | + return value; | ||
45 | + } | ||
46 | + | ||
47 | + @Override | ||
48 | + public int hashCode() { | ||
49 | + return Objects.hash(value); | ||
50 | + } | ||
51 | + | ||
52 | + @Override | ||
53 | + public boolean equals(Object obj) { | ||
54 | + if (this == obj) { | ||
55 | + return true; | ||
56 | + } | ||
57 | + if (obj instanceof OvsdbIfaceId) { | ||
58 | + final OvsdbIfaceId otherIfaceId = (OvsdbIfaceId) obj; | ||
59 | + return Objects.equals(this.value, otherIfaceId.value); | ||
60 | + } | ||
61 | + return false; | ||
62 | + } | ||
63 | + | ||
64 | + @Override | ||
65 | + public String toString() { | ||
66 | + return toStringHelper(this).add("value", value).toString(); | ||
67 | + } | ||
68 | +} |
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.ovsdb.controller; | ||
17 | + | ||
18 | +import static com.google.common.base.MoreObjects.toStringHelper; | ||
19 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
20 | + | ||
21 | +import java.util.Objects; | ||
22 | + | ||
23 | +/** | ||
24 | + * The class representing a port type. This class is immutable. | ||
25 | + */ | ||
26 | +public class OvsdbPortType { | ||
27 | + | ||
28 | + private final String value; | ||
29 | + | ||
30 | + /** | ||
31 | + * Constructor from a String port type. | ||
32 | + * | ||
33 | + * @param value the port type to use | ||
34 | + */ | ||
35 | + public OvsdbPortType(String value) { | ||
36 | + checkNotNull(value, "value is not null"); | ||
37 | + this.value = value; | ||
38 | + } | ||
39 | + | ||
40 | + /** | ||
41 | + * Gets the value of the port type. | ||
42 | + * | ||
43 | + * @return the value of the port type | ||
44 | + */ | ||
45 | + public String value() { | ||
46 | + return value; | ||
47 | + } | ||
48 | + | ||
49 | + @Override | ||
50 | + public int hashCode() { | ||
51 | + return Objects.hash(value); | ||
52 | + } | ||
53 | + | ||
54 | + @Override | ||
55 | + public boolean equals(Object obj) { | ||
56 | + if (this == obj) { | ||
57 | + return true; | ||
58 | + } | ||
59 | + if (obj instanceof OvsdbPortType) { | ||
60 | + final OvsdbPortType otherOvsdbPortType = (OvsdbPortType) obj; | ||
61 | + return Objects.equals(this.value, otherOvsdbPortType.value); | ||
62 | + } | ||
63 | + return false; | ||
64 | + } | ||
65 | + | ||
66 | + @Override | ||
67 | + public String toString() { | ||
68 | + return toStringHelper(this).add("value", value).toString(); | ||
69 | + } | ||
70 | +} |
providers/ovsdb/host/pom.xml
0 → 100644
1 | +<?xml version="1.0"?> | ||
2 | +<project | ||
3 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" | ||
4 | + xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
5 | + <modelVersion>4.0.0</modelVersion> | ||
6 | + | ||
7 | + <parent> | ||
8 | + <groupId>org.onosproject</groupId> | ||
9 | + <artifactId>onos-ovsdb-providers</artifactId> | ||
10 | + <version>1.3.0-SNAPSHOT</version> | ||
11 | + </parent> | ||
12 | + | ||
13 | + <artifactId>onos-ovsdb-provider-host</artifactId> | ||
14 | + <packaging>bundle</packaging> | ||
15 | + | ||
16 | + <dependencies> | ||
17 | + <dependency> | ||
18 | + <groupId>org.onosproject</groupId> | ||
19 | + <artifactId>onos-ovsdb-api</artifactId> | ||
20 | + <version>${project.version}</version> | ||
21 | + </dependency> | ||
22 | + </dependencies> | ||
23 | + | ||
24 | +</project> |
providers/ovsdb/host/src/main/java/org/onosproject/ovsdb/provider/host/OvsdbHostProvider.java
0 → 100644
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.ovsdb.provider.host; | ||
17 | + | ||
18 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
19 | +import static org.onlab.util.Tools.toHex; | ||
20 | +import static org.slf4j.LoggerFactory.getLogger; | ||
21 | + | ||
22 | +import java.net.URI; | ||
23 | +import java.net.URISyntaxException; | ||
24 | + | ||
25 | +import org.apache.felix.scr.annotations.Activate; | ||
26 | +import org.apache.felix.scr.annotations.Component; | ||
27 | +import org.apache.felix.scr.annotations.Deactivate; | ||
28 | +import org.apache.felix.scr.annotations.Reference; | ||
29 | +import org.apache.felix.scr.annotations.ReferenceCardinality; | ||
30 | +import org.apache.felix.scr.annotations.Service; | ||
31 | +import org.onlab.packet.VlanId; | ||
32 | +import org.onosproject.core.CoreService; | ||
33 | +import org.onosproject.net.DefaultAnnotations; | ||
34 | +import org.onosproject.net.DeviceId; | ||
35 | +import org.onosproject.net.Host; | ||
36 | +import org.onosproject.net.HostId; | ||
37 | +import org.onosproject.net.HostLocation; | ||
38 | +import org.onosproject.net.PortNumber; | ||
39 | +import org.onosproject.net.host.DefaultHostDescription; | ||
40 | +import org.onosproject.net.host.HostDescription; | ||
41 | +import org.onosproject.net.host.HostProvider; | ||
42 | +import org.onosproject.net.host.HostProviderRegistry; | ||
43 | +import org.onosproject.net.host.HostProviderService; | ||
44 | +import org.onosproject.net.host.HostService; | ||
45 | +import org.onosproject.net.provider.AbstractProvider; | ||
46 | +import org.onosproject.net.provider.ProviderId; | ||
47 | +import org.onosproject.ovsdb.controller.EventSubject; | ||
48 | +import org.onosproject.ovsdb.controller.OvsdbController; | ||
49 | +import org.onosproject.ovsdb.controller.OvsdbEvent; | ||
50 | +import org.onosproject.ovsdb.controller.OvsdbEventListener; | ||
51 | +import org.onosproject.ovsdb.controller.OvsdbEventSubject; | ||
52 | +import org.slf4j.Logger; | ||
53 | + | ||
54 | +/** | ||
55 | + * Provider which uses an ovsdb controller to detect host. | ||
56 | + */ | ||
57 | +@Component(immediate = true) | ||
58 | +@Service | ||
59 | +public class OvsdbHostProvider extends AbstractProvider implements HostProvider { | ||
60 | + private final Logger log = getLogger(getClass()); | ||
61 | + | ||
62 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
63 | + protected HostProviderRegistry providerRegistry; | ||
64 | + | ||
65 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
66 | + protected HostService hostService; | ||
67 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
68 | + protected CoreService coreService; | ||
69 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
70 | + protected OvsdbController controller; | ||
71 | + | ||
72 | + private HostProviderService providerService; | ||
73 | + private OvsdbEventListener innerEventListener = new InnerOvsdbEventListener(); | ||
74 | + | ||
75 | + @Activate | ||
76 | + public void activate() { | ||
77 | + providerService = providerRegistry.register(this); | ||
78 | + controller.addOvsdbEventListener(innerEventListener); | ||
79 | + log.info("Started"); | ||
80 | + } | ||
81 | + | ||
82 | + @Deactivate | ||
83 | + public void deactivate() { | ||
84 | + providerRegistry.unregister(this); | ||
85 | + providerService = null; | ||
86 | + log.info("Stopped"); | ||
87 | + } | ||
88 | + | ||
89 | + public OvsdbHostProvider() { | ||
90 | + super(new ProviderId("ovsdb", "org.onosproject.ovsdb.provider.host")); | ||
91 | + } | ||
92 | + | ||
93 | + @Override | ||
94 | + public void triggerProbe(Host host) { | ||
95 | + log.info("Triggering probe on host {}", host); | ||
96 | + } | ||
97 | + | ||
98 | + private class InnerOvsdbEventListener implements OvsdbEventListener { | ||
99 | + | ||
100 | + @Override | ||
101 | + public void handle(OvsdbEvent<EventSubject> event) { | ||
102 | + OvsdbEventSubject subject = null; | ||
103 | + if (event.subject() instanceof OvsdbEventSubject) { | ||
104 | + subject = (OvsdbEventSubject) event.subject(); | ||
105 | + } | ||
106 | + checkNotNull(subject, "EventSubject is not null"); | ||
107 | + // If ifaceid is null,it indicates this is not a vm port. | ||
108 | + if (subject.ifaceid() == null) { | ||
109 | + return; | ||
110 | + } | ||
111 | + switch (event.type()) { | ||
112 | + case PORT_ADDED: | ||
113 | + HostId hostId = HostId.hostId(subject.hwAddress(), null); | ||
114 | + DeviceId deviceId = DeviceId.deviceId(uri(subject.dpid().value())); | ||
115 | + PortNumber portNumber = PortNumber.portNumber(subject | ||
116 | + .portNumber().value(), subject.portName().value()); | ||
117 | + HostLocation loaction = new HostLocation(deviceId, portNumber, | ||
118 | + 0L); | ||
119 | + DefaultAnnotations annotations = DefaultAnnotations.builder() | ||
120 | + .set("ifaceid", subject.ifaceid().value()).build(); | ||
121 | + HostDescription hostDescription = new DefaultHostDescription( | ||
122 | + subject.hwAddress(), | ||
123 | + VlanId.vlanId(), | ||
124 | + loaction, | ||
125 | + annotations); | ||
126 | + providerService.hostDetected(hostId, hostDescription); | ||
127 | + break; | ||
128 | + case PORT_REMOVED: | ||
129 | + HostId host = HostId.hostId(subject.hwAddress(), null); | ||
130 | + providerService.hostVanished(host); | ||
131 | + break; | ||
132 | + default: | ||
133 | + break; | ||
134 | + } | ||
135 | + | ||
136 | + } | ||
137 | + | ||
138 | + } | ||
139 | + | ||
140 | + public URI uri(String value) { | ||
141 | + try { | ||
142 | + return new URI("of", toHex(Long.valueOf(value)), null); | ||
143 | + } catch (URISyntaxException e) { | ||
144 | + return null; | ||
145 | + } | ||
146 | + } | ||
147 | +} |
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 | + *Provider that uses ovsdb controller as a means of infrastructure host discovery. | ||
19 | + * | ||
20 | + */ | ||
21 | +package org.onosproject.ovsdb.provider.host; | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
providers/ovsdb/host/src/test/java/org/onosproject/ovsdb/provider/host/OvsdbHostProviderTest.java
0 → 100644
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.ovsdb.provider.host; | ||
17 | + | ||
18 | +import static org.junit.Assert.assertEquals; | ||
19 | +import static org.junit.Assert.assertNotNull; | ||
20 | + | ||
21 | +import java.util.List; | ||
22 | +import java.util.Set; | ||
23 | + | ||
24 | +import org.junit.After; | ||
25 | +import org.junit.Before; | ||
26 | +import org.junit.Test; | ||
27 | +import org.onlab.packet.MacAddress; | ||
28 | +import org.onosproject.net.DeviceId; | ||
29 | +import org.onosproject.net.HostId; | ||
30 | +import org.onosproject.net.host.HostDescription; | ||
31 | +import org.onosproject.net.host.HostProvider; | ||
32 | +import org.onosproject.net.host.HostProviderRegistry; | ||
33 | +import org.onosproject.net.host.HostProviderService; | ||
34 | +import org.onosproject.net.provider.AbstractProviderService; | ||
35 | +import org.onosproject.net.provider.ProviderId; | ||
36 | +import org.onosproject.ovsdb.controller.DefaultEventSubject; | ||
37 | +import org.onosproject.ovsdb.controller.EventSubject; | ||
38 | +import org.onosproject.ovsdb.controller.OvsdbClientService; | ||
39 | +import org.onosproject.ovsdb.controller.OvsdbController; | ||
40 | +import org.onosproject.ovsdb.controller.OvsdbDatapathId; | ||
41 | +import org.onosproject.ovsdb.controller.OvsdbEvent; | ||
42 | +import org.onosproject.ovsdb.controller.OvsdbEventListener; | ||
43 | +import org.onosproject.ovsdb.controller.OvsdbIfaceId; | ||
44 | +import org.onosproject.ovsdb.controller.OvsdbNodeId; | ||
45 | +import org.onosproject.ovsdb.controller.OvsdbNodeListener; | ||
46 | +import org.onosproject.ovsdb.controller.OvsdbPortName; | ||
47 | +import org.onosproject.ovsdb.controller.OvsdbPortNumber; | ||
48 | +import org.onosproject.ovsdb.controller.OvsdbPortType; | ||
49 | + | ||
50 | +/** | ||
51 | + * Test for ovsdb host provider. | ||
52 | + */ | ||
53 | +public class OvsdbHostProviderTest { | ||
54 | + private static final MacAddress MAC = MacAddress | ||
55 | + .valueOf("00:00:11:00:00:01"); | ||
56 | + private final OvsdbHostProvider provider = new OvsdbHostProvider(); | ||
57 | + private final TestHostRegistry hostRegistry = new TestHostRegistry(); | ||
58 | + protected OvsdbControllerTest controller = new OvsdbControllerTest(); | ||
59 | + private TestHostProviderService providerService; | ||
60 | + | ||
61 | + @Before | ||
62 | + public void setUp() { | ||
63 | + provider.providerRegistry = hostRegistry; | ||
64 | + provider.controller = controller; | ||
65 | + provider.activate(); | ||
66 | + } | ||
67 | + | ||
68 | + @Test | ||
69 | + public void basics() { | ||
70 | + assertNotNull("registration expected", providerService); | ||
71 | + assertEquals("incorrect provider", provider, providerService.provider()); | ||
72 | + } | ||
73 | + | ||
74 | + @Test | ||
75 | + public void portAdded() { | ||
76 | + DefaultEventSubject eventSubject = new DefaultEventSubject(MAC, null, | ||
77 | + new OvsdbPortName("portName"), | ||
78 | + new OvsdbPortNumber(0L), | ||
79 | + new OvsdbDatapathId("10002"), | ||
80 | + new OvsdbPortType("vxlan"), | ||
81 | + new OvsdbIfaceId("102345")); | ||
82 | + controller.ovsdbEventListener | ||
83 | + .handle(new OvsdbEvent<EventSubject>( | ||
84 | + OvsdbEvent.Type.PORT_ADDED, | ||
85 | + eventSubject)); | ||
86 | + assertNotNull("never went throught the provider service", | ||
87 | + providerService.added); | ||
88 | + | ||
89 | + } | ||
90 | + | ||
91 | + @Test | ||
92 | + public void portRemoved() { | ||
93 | + DefaultEventSubject eventSubject = new DefaultEventSubject(MAC, null, | ||
94 | + new OvsdbPortName("portName"), | ||
95 | + new OvsdbPortNumber(0L), | ||
96 | + new OvsdbDatapathId("10002"), | ||
97 | + new OvsdbPortType("vxlan"), | ||
98 | + new OvsdbIfaceId("102345")); | ||
99 | + controller.ovsdbEventListener | ||
100 | + .handle(new OvsdbEvent<EventSubject>( | ||
101 | + OvsdbEvent.Type.PORT_REMOVED, | ||
102 | + eventSubject)); | ||
103 | + assertEquals("port status unhandled", 1, providerService.removeCount); | ||
104 | + } | ||
105 | + | ||
106 | + @After | ||
107 | + public void tearDown() { | ||
108 | + provider.deactivate(); | ||
109 | + provider.coreService = null; | ||
110 | + provider.providerRegistry = null; | ||
111 | + } | ||
112 | + | ||
113 | + private class TestHostRegistry implements HostProviderRegistry { | ||
114 | + | ||
115 | + @Override | ||
116 | + public HostProviderService register(HostProvider provider) { | ||
117 | + providerService = new TestHostProviderService(provider); | ||
118 | + return providerService; | ||
119 | + } | ||
120 | + | ||
121 | + @Override | ||
122 | + public void unregister(HostProvider provider) { | ||
123 | + } | ||
124 | + | ||
125 | + @Override | ||
126 | + public Set<ProviderId> getProviders() { | ||
127 | + return null; | ||
128 | + } | ||
129 | + | ||
130 | + } | ||
131 | + | ||
132 | + private class TestHostProviderService | ||
133 | + extends AbstractProviderService<HostProvider> | ||
134 | + implements HostProviderService { | ||
135 | + | ||
136 | + DeviceId added = null; | ||
137 | + DeviceId moved = null; | ||
138 | + DeviceId spine = null; | ||
139 | + public int removeCount; | ||
140 | + | ||
141 | + protected TestHostProviderService(HostProvider provider) { | ||
142 | + super(provider); | ||
143 | + } | ||
144 | + | ||
145 | + @Override | ||
146 | + public void hostDetected(HostId hostId, HostDescription hostDescription) { | ||
147 | + DeviceId descr = hostDescription.location().deviceId(); | ||
148 | + if (added == null) { | ||
149 | + added = descr; | ||
150 | + } else if ((moved == null) && !descr.equals(added)) { | ||
151 | + moved = descr; | ||
152 | + } else { | ||
153 | + spine = descr; | ||
154 | + } | ||
155 | + } | ||
156 | + | ||
157 | + @Override | ||
158 | + public void hostVanished(HostId hostId) { | ||
159 | + removeCount++; | ||
160 | + } | ||
161 | + | ||
162 | + } | ||
163 | + | ||
164 | + private class OvsdbControllerTest implements OvsdbController { | ||
165 | + private OvsdbEventListener ovsdbEventListener = null; | ||
166 | + | ||
167 | + @Override | ||
168 | + public void addNodeListener(OvsdbNodeListener listener) { | ||
169 | + | ||
170 | + } | ||
171 | + | ||
172 | + @Override | ||
173 | + public void removeNodeListener(OvsdbNodeListener listener) { | ||
174 | + | ||
175 | + } | ||
176 | + | ||
177 | + @Override | ||
178 | + public void addOvsdbEventListener(OvsdbEventListener listener) { | ||
179 | + ovsdbEventListener = listener; | ||
180 | + | ||
181 | + } | ||
182 | + | ||
183 | + @Override | ||
184 | + public void removeOvsdbEventListener(OvsdbEventListener listener) { | ||
185 | + ovsdbEventListener = null; | ||
186 | + | ||
187 | + } | ||
188 | + | ||
189 | + @Override | ||
190 | + public List<OvsdbNodeId> getNodeIds() { | ||
191 | + return null; | ||
192 | + } | ||
193 | + | ||
194 | + @Override | ||
195 | + public OvsdbClientService getOvsdbClient(OvsdbNodeId nodeId) { | ||
196 | + return null; | ||
197 | + } | ||
198 | + } | ||
199 | +} |
-
Please register or login to post a comment