[ONOS-5565]Implementation of QosConfig and QueueConfig

Change-Id: I6a367b53cfca2e85e8aaa6cddb541d7b3ffccbc0
diff --git a/drivers/ovsdb/src/main/java/org/onosproject/drivers/ovsdb/OvsdbPortConfig.java b/drivers/ovsdb/src/main/java/org/onosproject/drivers/ovsdb/OvsdbPortConfig.java
new file mode 100644
index 0000000..0c08e6b
--- /dev/null
+++ b/drivers/ovsdb/src/main/java/org/onosproject/drivers/ovsdb/OvsdbPortConfig.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2017-present 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.drivers.ovsdb;
+
+import org.onlab.packet.IpAddress;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.behaviour.PortConfigBehaviour;
+import org.onosproject.net.behaviour.QosDescription;
+import org.onosproject.net.device.PortDescription;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.net.driver.DriverHandler;
+import org.onosproject.ovsdb.controller.OvsdbClientService;
+import org.onosproject.ovsdb.controller.OvsdbController;
+import org.onosproject.ovsdb.controller.OvsdbNodeId;
+import org.slf4j.Logger;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * OVSDB-based implementation of port config behaviour.
+ */
+public class OvsdbPortConfig extends AbstractHandlerBehaviour implements PortConfigBehaviour {
+
+    private final Logger log = getLogger(getClass());
+
+    @Override
+    public void applyQoS(PortDescription portDesc, QosDescription qosDesc) {
+        OvsdbClientService ovsdbClient = getOvsdbClient(handler());
+        if (ovsdbClient == null) {
+            return;
+        }
+        ovsdbClient.applyQos(portDesc.portNumber(), qosDesc.qosId().name());
+    }
+
+    @Override
+    public void removeQoS(PortNumber portNumber) {
+        OvsdbClientService ovsdbClient = getOvsdbClient(handler());
+        if (ovsdbClient == null) {
+            return;
+        }
+        ovsdbClient.removeQos(portNumber);
+    }
+
+    // OvsdbNodeId(IP) is used in the adaptor while DeviceId(ovsdb:IP)
+    // is used in the core. So DeviceId need be changed to OvsdbNodeId.
+    private OvsdbNodeId changeDeviceIdToNodeId(DeviceId deviceId) {
+        String[] splits = deviceId.toString().split(":");
+        if (splits.length < 1) {
+            return null;
+        }
+        IpAddress ipAddress = IpAddress.valueOf(splits[1]);
+        return new OvsdbNodeId(ipAddress, 0);
+    }
+
+    private OvsdbClientService getOvsdbClient(DriverHandler handler) {
+        OvsdbController ovsController = handler.get(OvsdbController.class);
+        OvsdbNodeId nodeId = changeDeviceIdToNodeId(handler.data().deviceId());
+
+        return ovsController.getOvsdbClient(nodeId);
+    }
+}
+
diff --git a/drivers/ovsdb/src/main/java/org/onosproject/drivers/ovsdb/OvsdbQosConfig.java b/drivers/ovsdb/src/main/java/org/onosproject/drivers/ovsdb/OvsdbQosConfig.java
new file mode 100644
index 0000000..00fcb80
--- /dev/null
+++ b/drivers/ovsdb/src/main/java/org/onosproject/drivers/ovsdb/OvsdbQosConfig.java
@@ -0,0 +1,138 @@
+/*
+ * Copyright 2017-present 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.drivers.ovsdb;
+
+import org.onlab.packet.IpAddress;
+import org.onlab.util.Bandwidth;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.behaviour.DefaultQosDescription;
+import org.onosproject.net.behaviour.QosConfigBehaviour;
+import org.onosproject.net.behaviour.QosDescription;
+import org.onosproject.net.behaviour.QosId;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.net.driver.DriverHandler;
+import org.onosproject.ovsdb.controller.OvsdbClientService;
+import org.onosproject.ovsdb.controller.OvsdbController;
+import org.onosproject.ovsdb.controller.OvsdbNodeId;
+import org.onosproject.ovsdb.controller.OvsdbQos;
+import org.slf4j.Logger;
+
+import java.util.Collection;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import static org.onosproject.ovsdb.controller.OvsdbConstant.CBS;
+import static org.onosproject.ovsdb.controller.OvsdbConstant.CIR;
+import static org.onosproject.ovsdb.controller.OvsdbConstant.MAX_RATE;
+import static org.onosproject.ovsdb.controller.OvsdbConstant.QOS_EGRESS_POLICER;
+import static org.onosproject.ovsdb.controller.OvsdbConstant.QOS_EXTERNAL_ID_KEY;
+import static org.onosproject.ovsdb.controller.OvsdbConstant.QOS_TYPE_PREFIX;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * OVSDB-based implementation of qos config behaviour.
+ */
+public class OvsdbQosConfig extends AbstractHandlerBehaviour implements QosConfigBehaviour {
+
+    private final Logger log = getLogger(getClass());
+
+    @Override
+    public Collection<QosDescription> getQoses() {
+        OvsdbClientService ovsdbClient = getOvsdbClient(handler());
+        if (ovsdbClient == null) {
+            return null;
+        }
+        Set<OvsdbQos> qoses = ovsdbClient.getQoses();
+
+        return qoses.stream()
+                .map(qos -> DefaultQosDescription.builder()
+                        .qosId(QosId.qosId(qos.externalIds().get(QOS_EXTERNAL_ID_KEY)))
+                        .type(QOS_EGRESS_POLICER.equals(qos.qosType()) ?
+                                QosDescription.Type.EGRESS_POLICER :
+                                QosDescription.Type.valueOf(qos.qosType()
+                                        .replace(QOS_TYPE_PREFIX, "")
+                                        .toUpperCase()))
+                        .maxRate(qos.otherConfigs().get(MAX_RATE) != null ?
+                                Bandwidth.bps(Long.parseLong(qos.otherConfigs().get(MAX_RATE))) :
+                                Bandwidth.bps(0L))
+                        .cbs(qos.otherConfigs().get(CBS) != null ?
+                                Long.valueOf(qos.otherConfigs().get(CBS)) : null)
+                        .cir(qos.otherConfigs().get(CIR) != null ?
+                                Long.valueOf(qos.otherConfigs().get(CIR)) : null)
+                        .build())
+                .collect(Collectors.toSet());
+    }
+
+    @Override
+    public QosDescription getQos(QosDescription qosDesc) {
+        OvsdbClientService ovsdbClient = getOvsdbClient(handler());
+        if (ovsdbClient == null) {
+            return null;
+        }
+        OvsdbQos qos = ovsdbClient.getQos(qosDesc.qosId());
+        if (qos == null) {
+            return null;
+        }
+        return DefaultQosDescription.builder()
+                        .qosId(QosId.qosId(qos.externalIds().get(QOS_EXTERNAL_ID_KEY)))
+                        .type(QOS_EGRESS_POLICER.equals(qos.qosType()) ?
+                                QosDescription.Type.EGRESS_POLICER :
+                                QosDescription.Type.valueOf(qos.qosType()
+                                        .replace(QOS_TYPE_PREFIX, "")
+                                        .toUpperCase()))
+                        .maxRate(qos.otherConfigs().get(MAX_RATE) != null ?
+                                Bandwidth.bps(Long.parseLong(qos.otherConfigs().get(MAX_RATE))) :
+                                Bandwidth.bps(0L))
+                        .cbs(qos.otherConfigs().get(CBS) != null ?
+                                Long.valueOf(qos.otherConfigs().get(CBS)) : null)
+                        .cir(qos.otherConfigs().get(CIR) != null ?
+                                Long.valueOf(qos.otherConfigs().get(CIR)) : null)
+                        .build();
+    }
+
+    @Override
+    public boolean addQoS(QosDescription qos) {
+        OvsdbClientService ovsdbClient = getOvsdbClient(handler());
+        OvsdbQos ovsdbQos = OvsdbQos.builder(qos).build();
+        return ovsdbClient.createQos(ovsdbQos);
+    }
+
+    @Override
+    public void deleteQoS(QosId qosId) {
+        OvsdbClientService ovsdbClient = getOvsdbClient(handler());
+        ovsdbClient.dropQos(qosId);
+    }
+
+    // OvsdbNodeId(IP) is used in the adaptor while DeviceId(ovsdb:IP)
+    // is used in the core. So DeviceId need be changed to OvsdbNodeId.
+    private OvsdbNodeId changeDeviceIdToNodeId(DeviceId deviceId) {
+        String[] splits = deviceId.toString().split(":");
+        if (splits.length < 1) {
+            return null;
+        }
+        IpAddress ipAddress = IpAddress.valueOf(splits[1]);
+        return new OvsdbNodeId(ipAddress, 0);
+    }
+
+    private OvsdbClientService getOvsdbClient(DriverHandler handler) {
+        OvsdbController ovsController = handler.get(OvsdbController.class);
+        OvsdbNodeId nodeId = changeDeviceIdToNodeId(handler.data().deviceId());
+
+        return ovsController.getOvsdbClient(nodeId);
+    }
+}
+
diff --git a/drivers/ovsdb/src/main/java/org/onosproject/drivers/ovsdb/OvsdbQueueConfig.java b/drivers/ovsdb/src/main/java/org/onosproject/drivers/ovsdb/OvsdbQueueConfig.java
new file mode 100644
index 0000000..79ed4fc
--- /dev/null
+++ b/drivers/ovsdb/src/main/java/org/onosproject/drivers/ovsdb/OvsdbQueueConfig.java
@@ -0,0 +1,161 @@
+/*
+ * Copyright 2017-present 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.drivers.ovsdb;
+
+import org.onlab.packet.IpAddress;
+import org.onlab.util.Bandwidth;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.behaviour.DefaultQueueDescription;
+import org.onosproject.net.behaviour.QueueConfigBehaviour;
+import org.onosproject.net.behaviour.QueueDescription;
+import org.onosproject.net.behaviour.QueueDescription.Type;
+import org.onosproject.net.behaviour.QueueId;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.net.driver.DriverHandler;
+import org.onosproject.ovsdb.controller.OvsdbClientService;
+import org.onosproject.ovsdb.controller.OvsdbController;
+import org.onosproject.ovsdb.controller.OvsdbNodeId;
+import org.onosproject.ovsdb.controller.OvsdbQueue;
+import org.slf4j.Logger;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.EnumSet;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import static org.onosproject.ovsdb.controller.OvsdbConstant.BURST;
+import static org.onosproject.ovsdb.controller.OvsdbConstant.MAX_RATE;
+import static org.onosproject.ovsdb.controller.OvsdbConstant.MIN_RATE;
+import static org.onosproject.ovsdb.controller.OvsdbConstant.PRIORITY;
+import static org.onosproject.ovsdb.controller.OvsdbConstant.QUEUE_EXTERNAL_ID_KEY;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * OVSDB-based implementation of queue config behaviour.
+ */
+public class OvsdbQueueConfig extends AbstractHandlerBehaviour implements QueueConfigBehaviour {
+
+    private final Logger log = getLogger(getClass());
+
+    @Override
+    public Collection<QueueDescription> getQueues() {
+        OvsdbClientService ovsdbClient = getOvsdbClient(handler());
+        if (ovsdbClient == null) {
+            return Collections.emptyList();
+        }
+
+        Set<OvsdbQueue> queues = ovsdbClient.getQueues();
+        return queues.stream()
+                .map(q -> DefaultQueueDescription.builder()
+                        .queueId(QueueId.queueId(q.externalIds().get(QUEUE_EXTERNAL_ID_KEY)))
+                        .type(types(q))
+                        .dscp(q.dscp().isPresent() ? q.dscp().get().intValue() : null)
+                        .maxRate(q.otherConfigs().get(MAX_RATE) != null ?
+                                Bandwidth.bps(Long.parseLong(q.otherConfigs().get(MAX_RATE))) :
+                                Bandwidth.bps(0L))
+                        .minRate(q.otherConfigs().get(MIN_RATE) != null ?
+                                Bandwidth.bps(Long.parseLong(q.otherConfigs().get(MIN_RATE))) :
+                                Bandwidth.bps(0L))
+                        .burst(q.otherConfigs().get(BURST) != null ?
+                                Long.valueOf(q.otherConfigs().get(BURST)) : 0L)
+                        .priority(q.otherConfigs().get(PRIORITY) != null ?
+                                Long.valueOf(q.otherConfigs().get(PRIORITY)) : 0L)
+                        .build())
+                .collect(Collectors.toSet());
+    }
+
+    @Override
+    public QueueDescription getQueue(QueueDescription queueDesc) {
+        OvsdbClientService ovsdbClient = getOvsdbClient(handler());
+        if (ovsdbClient == null) {
+            return null;
+        }
+
+        OvsdbQueue queue = ovsdbClient.getQueue(queueDesc.queueId());
+        if (queue == null) {
+            return null;
+        }
+        return DefaultQueueDescription.builder()
+                .queueId(QueueId.queueId(queue.externalIds().get(QUEUE_EXTERNAL_ID_KEY)))
+                .type(types(queue))
+                .dscp(queue.dscp().isPresent() ? queue.dscp().get().intValue() : null)
+                .maxRate(queue.otherConfigs().get(MAX_RATE) != null ?
+                        Bandwidth.bps(Long.parseLong(queue.otherConfigs().get(MAX_RATE))) :
+                        Bandwidth.bps(0L))
+                .minRate(queue.otherConfigs().get(MIN_RATE) != null ?
+                        Bandwidth.bps(Long.parseLong(queue.otherConfigs().get(MIN_RATE))) :
+                        Bandwidth.bps(0L))
+                .burst(queue.otherConfigs().get(BURST) != null ?
+                        Long.valueOf(queue.otherConfigs().get(BURST)) : 0L)
+                .priority(queue.otherConfigs().get(PRIORITY) != null ?
+                        Long.valueOf(queue.otherConfigs().get(PRIORITY)) : 0L)
+                .build();
+    }
+
+    @Override
+    public boolean addQueue(QueueDescription queue) {
+        OvsdbClientService ovsdbClient = getOvsdbClient(handler());
+        OvsdbQueue ovsdbQueue = OvsdbQueue.builder(queue).build();
+        return ovsdbClient.createQueue(ovsdbQueue);
+    }
+
+    @Override
+    public void deleteQueue(QueueId queueId) {
+        OvsdbClientService ovsdbClient = getOvsdbClient(handler());
+        ovsdbClient.dropQueue(queueId);
+    }
+
+    private EnumSet<Type> types(OvsdbQueue queue) {
+        EnumSet<Type> enumSet = EnumSet.noneOf(Type.class);
+        if (queue == null) {
+            return enumSet;
+        }
+
+        if (queue.otherConfigs().get(MAX_RATE) != null) {
+            enumSet.add(Type.MAX);
+        }
+        if (queue.otherConfigs().get(MIN_RATE) != null) {
+            enumSet.add(Type.MIN);
+        }
+        if (queue.otherConfigs().get(BURST) != null) {
+            enumSet.add(Type.BURST);
+        }
+        if (queue.otherConfigs().get(PRIORITY) != null) {
+            enumSet.add(Type.PRIORITY);
+        }
+        return enumSet;
+    }
+
+    // OvsdbNodeId(IP) is used in the adaptor while DeviceId(ovsdb:IP)
+    // is used in the core. So DeviceId need be changed to OvsdbNodeId.
+    private OvsdbNodeId changeDeviceIdToNodeId(DeviceId deviceId) {
+        String[] splits = deviceId.toString().split(":");
+        if (splits.length < 1) {
+            return null;
+        }
+        IpAddress ipAddress = IpAddress.valueOf(splits[1]);
+        return new OvsdbNodeId(ipAddress, 0);
+    }
+
+    private OvsdbClientService getOvsdbClient(DriverHandler handler) {
+        OvsdbController ovsController = handler.get(OvsdbController.class);
+        OvsdbNodeId nodeId = changeDeviceIdToNodeId(handler.data().deviceId());
+
+        return ovsController.getOvsdbClient(nodeId);
+    }
+}
\ No newline at end of file
diff --git a/drivers/ovsdb/src/main/resources/ovsdb-drivers.xml b/drivers/ovsdb/src/main/resources/ovsdb-drivers.xml
index 15d7d5f..bb00f22 100644
--- a/drivers/ovsdb/src/main/resources/ovsdb-drivers.xml
+++ b/drivers/ovsdb/src/main/resources/ovsdb-drivers.xml
@@ -23,6 +23,8 @@
                    impl="org.onosproject.drivers.ovsdb.OvsdbBridgeConfig"/>
         <behaviour api="org.onosproject.net.behaviour.InterfaceConfig"
                    impl="org.onosproject.drivers.ovsdb.OvsdbInterfaceConfig"/>
+        <behaviour api="org.onosproject.net.behaviour.PortConfigBehaviour"
+                   impl="org.onosproject.drivers.ovsdb.OvsdbPortConfig"/>
     </driver>
     <driver name="ovs" extends="default"
             manufacturer="Nicira, Inc\." hwVersion="Open vSwitch" swVersion="2\..*">
@@ -30,6 +32,10 @@
                    impl="org.onosproject.drivers.ovsdb.OvsdbControllerConfig"/>
         <behaviour api="org.onosproject.net.behaviour.MirroringConfig"
                    impl="org.onosproject.drivers.ovsdb.OvsdbMirroringConfig"/>
+        <behaviour api="org.onosproject.net.behaviour.QosConfigBehaviour"
+                   impl="org.onosproject.drivers.ovsdb.OvsdbQosConfig"/>
+        <behaviour api="org.onosproject.net.behaviour.QueueConfigBehaviour"
+                   impl="org.onosproject.drivers.ovsdb.OvsdbQueueConfig"/>
     </driver>
 </drivers>