Inserted set and get controllers methods in ovsdb controller config
Change-Id: I791ff2ae159d0ac50beff22abda2b187913428f6
diff --git a/drivers/pom.xml b/drivers/pom.xml
index 749a68c..56a39a8 100644
--- a/drivers/pom.xml
+++ b/drivers/pom.xml
@@ -55,8 +55,8 @@
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onos-core-serializers</artifactId>
- <version>1.4.0-SNAPSHOT</version>
- </dependency>
+ <version>${project.version}</version>
+ </dependency>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onos-ovsdb-api</artifactId>
@@ -72,6 +72,25 @@
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr.annotations</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onos-api</artifactId>
+ <version>${project.version}</version>
+ <classifier>tests</classifier>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onlab-junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onos-ovsdb-api</artifactId>
+ <version>${project.version}</version>
+ <classifier>tests</classifier>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
diff --git a/drivers/src/main/java/org/onosproject/driver/ovsdb/OvsdbControllerConfig.java b/drivers/src/main/java/org/onosproject/driver/ovsdb/OvsdbControllerConfig.java
new file mode 100644
index 0000000..a00d3db
--- /dev/null
+++ b/drivers/src/main/java/org/onosproject/driver/ovsdb/OvsdbControllerConfig.java
@@ -0,0 +1,102 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.driver.ovsdb;
+
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.TpPort;
+import org.onosproject.net.AnnotationKeys;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.behaviour.ControllerConfig;
+import org.onosproject.net.behaviour.ControllerInfo;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.net.driver.DriverHandler;
+import org.onosproject.ovsdb.controller.OvsdbBridge;
+import org.onosproject.ovsdb.controller.OvsdbClientService;
+import org.onosproject.ovsdb.controller.OvsdbController;
+import org.onosproject.ovsdb.controller.OvsdbNodeId;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import static com.google.common.base.Preconditions.checkState;
+import static org.onlab.util.Tools.delay;
+
+/**
+ * Implementation of controller config which allows to get and set controllers.
+ */
+public class OvsdbControllerConfig extends AbstractHandlerBehaviour implements ControllerConfig {
+ @Override
+ public List<ControllerInfo> getControllers() {
+ DriverHandler handler = handler();
+ OvsdbClientService clientService = getOvsdbClientService(handler);
+ Set<ControllerInfo> controllers = clientService.getControllers(
+ handler().data().deviceId());
+ return new ArrayList<>(controllers);
+ }
+
+ @Override
+ public void setControllers(List<ControllerInfo> controllers) {
+ DriverHandler handler = handler();
+ OvsdbClientService clientService = getOvsdbClientService(handler);
+ if (!clientService.getControllers(handler().data().deviceId())
+ .equals(controllers)) {
+ clientService.setControllersWithDeviceId(handler().
+ data().deviceId(), controllers);
+ }
+ }
+
+ // Used for getting OvsdbClientService.
+ private OvsdbClientService getOvsdbClientService(DriverHandler handler) {
+ OvsdbController ovsController = handler.get(OvsdbController.class);
+ DeviceService deviceService = handler.get(DeviceService.class);
+ DeviceId ofDeviceId = handler.data().deviceId();
+ String[] mgmtAddress = deviceService.getDevice(ofDeviceId)
+ .annotations().value(AnnotationKeys.MANAGEMENT_ADDRESS).split(":");
+ String targetIp = mgmtAddress[0];
+ TpPort targetPort = null;
+ if (mgmtAddress.length > 1) {
+ targetPort = TpPort.tpPort(Integer.parseInt(mgmtAddress[1]));
+ }
+
+ List<OvsdbNodeId> nodeIds = ovsController.getNodeIds().stream()
+ .filter(nodeId -> nodeId.getIpAddress().equals(targetIp))
+ .collect(Collectors.toList());
+ if (nodeIds.size() == 0) {
+ //TODO decide what port?
+ ovsController.connect(IpAddress.valueOf(targetIp),
+ targetPort == null ? TpPort.tpPort(6640) : targetPort);
+ delay(1000); //FIXME... connect is async
+ }
+ List<OvsdbClientService> clientServices = ovsController.getNodeIds().stream()
+ .filter(nodeId -> nodeId.getIpAddress().equals(targetIp))
+ .map(ovsController::getOvsdbClient)
+ .filter(cs -> cs.getBridges().stream().anyMatch(b -> dpidMatches(b, ofDeviceId)))
+ .collect(Collectors.toList());
+ checkState(clientServices.size() > 0, "No clientServices found");
+ //FIXME add connection to management address if null --> done ?
+ return clientServices.size() > 0 ? clientServices.get(0) : null;
+ }
+
+ private static boolean dpidMatches(OvsdbBridge bridge, DeviceId deviceId) {
+ String bridgeDpid = "of:" + bridge.datapathId().value();
+ String ofDpid = deviceId.toString();
+ return bridgeDpid.equals(ofDpid);
+ }
+}
\ No newline at end of file
diff --git a/drivers/src/main/resources/onos-drivers.xml b/drivers/src/main/resources/onos-drivers.xml
index aea743f..5059d4b 100644
--- a/drivers/src/main/resources/onos-drivers.xml
+++ b/drivers/src/main/resources/onos-drivers.xml
@@ -30,6 +30,8 @@
manufacturer="Nicira, Inc\." hwVersion="Open vSwitch" swVersion="2\..*">
<behaviour api="org.onosproject.openflow.controller.driver.OpenFlowSwitchDriver"
impl="org.onosproject.driver.handshaker.NiciraSwitchHandshaker"/>
+ <behaviour api="org.onosproject.net.behaviour.ControllerConfig"
+ impl="org.onosproject.driver.ovsdb.OvsdbControllerConfig"/>
</driver>
<driver name="ovs-corsa" extends="ovs"
manufacturer="Corsa" hwVersion="emulation" swVersion="0.0.0">
diff --git a/drivers/src/test/java/org/onosproject/driver/ovsdb/OvsdbControllerConfigTest.java b/drivers/src/test/java/org/onosproject/driver/ovsdb/OvsdbControllerConfigTest.java
new file mode 100644
index 0000000..4a91efc
--- /dev/null
+++ b/drivers/src/test/java/org/onosproject/driver/ovsdb/OvsdbControllerConfigTest.java
@@ -0,0 +1,95 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.driver.ovsdb;
+
+import com.google.common.collect.ImmutableMap;
+import org.junit.Before;
+import org.junit.Test;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.behaviour.ControllerConfig;
+import org.onosproject.net.device.DeviceServiceAdapter;
+import org.onosproject.net.driver.DefaultDriver;
+import org.onosproject.net.driver.DefaultDriverData;
+import org.onosproject.net.driver.DefaultDriverHandler;
+import org.onosproject.ovsdb.controller.driver.OvsdbClientServiceAdapter;
+import org.onosproject.ovsdb.controller.driver.OvsdbControllerAdapter;
+
+/**
+ * Created by Andrea on 10/7/15.
+ */
+public class OvsdbControllerConfigTest {
+
+
+ private static final DeviceId DEVICE_ID = DeviceId.deviceId("foo");
+
+ private DefaultDriver ddc;
+ private DefaultDriverData data;
+ private DefaultDriverHandler handler;
+
+ private TestDeviceService deviceService = new TestDeviceService();
+ private TestOvsdbController controller = new TestOvsdbController();
+ private TestOvsdbClient client = new TestOvsdbClient();
+
+ private OvsdbControllerConfig controllerConfig;
+
+
+ @Before
+ public void setUp() {
+ controllerConfig = new OvsdbControllerConfig();
+
+ ddc = new DefaultDriver("foo.bar", null, "Circus", "lux", "1.2a",
+ ImmutableMap.of(ControllerConfig.class,
+ OvsdbControllerConfig.class),
+ ImmutableMap.of("foo", "bar"));
+ data = new DefaultDriverData(ddc, DEVICE_ID);
+ handler = new DefaultDriverHandler(data);
+ //handler.controllerConfig.setHandler(handler);
+ //TODO setTestService directory on handler
+ //TODO setup ovsdb fake controller with fake ovsdbclient
+ //TODO setup fake device service
+ }
+
+ @Test
+ public void testGetControllers() throws Exception {
+// DriverService driverService = new Driv
+// AbstractBehaviour ab = new AbstractBehaviour();
+// DriverHandler handler = handler();
+// List<ControllerInfo> controllersList =
+// controllerConfig.getControllers(DeviceId.deviceId("0000000000000018"));
+// log.info("controllers " + controllersList);
+
+ }
+
+ @Test
+ public void testSetControllers() throws Exception {
+
+ }
+
+
+ private class TestDeviceService extends DeviceServiceAdapter {
+
+ }
+
+ private class TestOvsdbController extends OvsdbControllerAdapter {
+
+
+ }
+
+ private class TestOvsdbClient extends OvsdbClientServiceAdapter {
+
+ }
+}
\ No newline at end of file