Frank Wang | e11a98d | 2016-10-26 17:04:03 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Frank Wang | e11a98d | 2016-10-26 17:04:03 +0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.drivers.ovsdb; |
| 18 | |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onlab.util.Bandwidth; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.behaviour.DefaultQosDescription; |
| 23 | import org.onosproject.net.behaviour.QosConfigBehaviour; |
| 24 | import org.onosproject.net.behaviour.QosDescription; |
| 25 | import org.onosproject.net.behaviour.QosId; |
tanbangcheng | c944c28 | 2017-11-12 20:42:59 +0800 | [diff] [blame] | 26 | import org.onosproject.net.behaviour.QueueDescription; |
Frank Wang | e11a98d | 2016-10-26 17:04:03 +0800 | [diff] [blame] | 27 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 28 | import org.onosproject.net.driver.DriverHandler; |
| 29 | import org.onosproject.ovsdb.controller.OvsdbClientService; |
| 30 | import org.onosproject.ovsdb.controller.OvsdbController; |
| 31 | import org.onosproject.ovsdb.controller.OvsdbNodeId; |
| 32 | import org.onosproject.ovsdb.controller.OvsdbQos; |
| 33 | import org.slf4j.Logger; |
| 34 | |
| 35 | import java.util.Collection; |
tanbangcheng | c944c28 | 2017-11-12 20:42:59 +0800 | [diff] [blame] | 36 | import java.util.List; |
| 37 | import java.util.Map; |
Frank Wang | e11a98d | 2016-10-26 17:04:03 +0800 | [diff] [blame] | 38 | import java.util.Set; |
| 39 | import java.util.stream.Collectors; |
| 40 | |
| 41 | import static org.onosproject.ovsdb.controller.OvsdbConstant.CBS; |
| 42 | import static org.onosproject.ovsdb.controller.OvsdbConstant.CIR; |
| 43 | import static org.onosproject.ovsdb.controller.OvsdbConstant.MAX_RATE; |
| 44 | import static org.onosproject.ovsdb.controller.OvsdbConstant.QOS_EGRESS_POLICER; |
| 45 | import static org.onosproject.ovsdb.controller.OvsdbConstant.QOS_EXTERNAL_ID_KEY; |
| 46 | import static org.onosproject.ovsdb.controller.OvsdbConstant.QOS_TYPE_PREFIX; |
| 47 | import static org.slf4j.LoggerFactory.getLogger; |
| 48 | |
| 49 | /** |
| 50 | * OVSDB-based implementation of qos config behaviour. |
| 51 | */ |
| 52 | public class OvsdbQosConfig extends AbstractHandlerBehaviour implements QosConfigBehaviour { |
| 53 | |
| 54 | private final Logger log = getLogger(getClass()); |
| 55 | |
| 56 | @Override |
| 57 | public Collection<QosDescription> getQoses() { |
| 58 | OvsdbClientService ovsdbClient = getOvsdbClient(handler()); |
| 59 | if (ovsdbClient == null) { |
| 60 | return null; |
| 61 | } |
| 62 | Set<OvsdbQos> qoses = ovsdbClient.getQoses(); |
| 63 | |
| 64 | return qoses.stream() |
| 65 | .map(qos -> DefaultQosDescription.builder() |
| 66 | .qosId(QosId.qosId(qos.externalIds().get(QOS_EXTERNAL_ID_KEY))) |
| 67 | .type(QOS_EGRESS_POLICER.equals(qos.qosType()) ? |
| 68 | QosDescription.Type.EGRESS_POLICER : |
| 69 | QosDescription.Type.valueOf(qos.qosType() |
| 70 | .replace(QOS_TYPE_PREFIX, "") |
| 71 | .toUpperCase())) |
| 72 | .maxRate(qos.otherConfigs().get(MAX_RATE) != null ? |
| 73 | Bandwidth.bps(Long.parseLong(qos.otherConfigs().get(MAX_RATE))) : |
| 74 | Bandwidth.bps(0L)) |
| 75 | .cbs(qos.otherConfigs().get(CBS) != null ? |
| 76 | Long.valueOf(qos.otherConfigs().get(CBS)) : null) |
| 77 | .cir(qos.otherConfigs().get(CIR) != null ? |
| 78 | Long.valueOf(qos.otherConfigs().get(CIR)) : null) |
| 79 | .build()) |
| 80 | .collect(Collectors.toSet()); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public QosDescription getQos(QosDescription qosDesc) { |
| 85 | OvsdbClientService ovsdbClient = getOvsdbClient(handler()); |
| 86 | if (ovsdbClient == null) { |
| 87 | return null; |
| 88 | } |
| 89 | OvsdbQos qos = ovsdbClient.getQos(qosDesc.qosId()); |
| 90 | if (qos == null) { |
| 91 | return null; |
| 92 | } |
| 93 | return DefaultQosDescription.builder() |
| 94 | .qosId(QosId.qosId(qos.externalIds().get(QOS_EXTERNAL_ID_KEY))) |
| 95 | .type(QOS_EGRESS_POLICER.equals(qos.qosType()) ? |
| 96 | QosDescription.Type.EGRESS_POLICER : |
| 97 | QosDescription.Type.valueOf(qos.qosType() |
| 98 | .replace(QOS_TYPE_PREFIX, "") |
| 99 | .toUpperCase())) |
| 100 | .maxRate(qos.otherConfigs().get(MAX_RATE) != null ? |
| 101 | Bandwidth.bps(Long.parseLong(qos.otherConfigs().get(MAX_RATE))) : |
| 102 | Bandwidth.bps(0L)) |
| 103 | .cbs(qos.otherConfigs().get(CBS) != null ? |
| 104 | Long.valueOf(qos.otherConfigs().get(CBS)) : null) |
| 105 | .cir(qos.otherConfigs().get(CIR) != null ? |
| 106 | Long.valueOf(qos.otherConfigs().get(CIR)) : null) |
| 107 | .build(); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public boolean addQoS(QosDescription qos) { |
| 112 | OvsdbClientService ovsdbClient = getOvsdbClient(handler()); |
| 113 | OvsdbQos ovsdbQos = OvsdbQos.builder(qos).build(); |
| 114 | return ovsdbClient.createQos(ovsdbQos); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void deleteQoS(QosId qosId) { |
| 119 | OvsdbClientService ovsdbClient = getOvsdbClient(handler()); |
| 120 | ovsdbClient.dropQos(qosId); |
| 121 | } |
| 122 | |
tanbangcheng | c944c28 | 2017-11-12 20:42:59 +0800 | [diff] [blame] | 123 | @Override |
| 124 | public void insertQueues(QosId qosId, Map<Long, QueueDescription> queues) { |
| 125 | OvsdbClientService ovsdbClient = getOvsdbClient(handler()); |
| 126 | ovsdbClient.bindQueues(qosId, queues); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void deleteQueues(QosId qosId, List<Long> queueKeys) { |
| 131 | OvsdbClientService ovsdbClient = getOvsdbClient(handler()); |
| 132 | ovsdbClient.unbindQueues(qosId, queueKeys); |
| 133 | } |
| 134 | |
Frank Wang | e11a98d | 2016-10-26 17:04:03 +0800 | [diff] [blame] | 135 | // OvsdbNodeId(IP) is used in the adaptor while DeviceId(ovsdb:IP) |
| 136 | // is used in the core. So DeviceId need be changed to OvsdbNodeId. |
| 137 | private OvsdbNodeId changeDeviceIdToNodeId(DeviceId deviceId) { |
| 138 | String[] splits = deviceId.toString().split(":"); |
| 139 | if (splits.length < 1) { |
| 140 | return null; |
| 141 | } |
| 142 | IpAddress ipAddress = IpAddress.valueOf(splits[1]); |
| 143 | return new OvsdbNodeId(ipAddress, 0); |
| 144 | } |
| 145 | |
| 146 | private OvsdbClientService getOvsdbClient(DriverHandler handler) { |
| 147 | OvsdbController ovsController = handler.get(OvsdbController.class); |
| 148 | OvsdbNodeId nodeId = changeDeviceIdToNodeId(handler.data().deviceId()); |
| 149 | |
| 150 | return ovsController.getOvsdbClient(nodeId); |
| 151 | } |
| 152 | } |
| 153 | |