Frank Wang | e11a98d | 2016-10-26 17:04:03 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-present 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 | 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.DefaultQueueDescription; |
| 23 | import org.onosproject.net.behaviour.QueueConfigBehaviour; |
| 24 | import org.onosproject.net.behaviour.QueueDescription; |
| 25 | import org.onosproject.net.behaviour.QueueDescription.Type; |
| 26 | import org.onosproject.net.behaviour.QueueId; |
| 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.OvsdbQueue; |
| 33 | import org.slf4j.Logger; |
| 34 | |
| 35 | import java.util.Collection; |
| 36 | import java.util.Collections; |
| 37 | import java.util.EnumSet; |
| 38 | import java.util.Set; |
| 39 | import java.util.stream.Collectors; |
| 40 | |
| 41 | import static org.onosproject.ovsdb.controller.OvsdbConstant.BURST; |
| 42 | import static org.onosproject.ovsdb.controller.OvsdbConstant.MAX_RATE; |
| 43 | import static org.onosproject.ovsdb.controller.OvsdbConstant.MIN_RATE; |
| 44 | import static org.onosproject.ovsdb.controller.OvsdbConstant.PRIORITY; |
| 45 | import static org.onosproject.ovsdb.controller.OvsdbConstant.QUEUE_EXTERNAL_ID_KEY; |
| 46 | import static org.slf4j.LoggerFactory.getLogger; |
| 47 | |
| 48 | /** |
| 49 | * OVSDB-based implementation of queue config behaviour. |
| 50 | */ |
| 51 | public class OvsdbQueueConfig extends AbstractHandlerBehaviour implements QueueConfigBehaviour { |
| 52 | |
| 53 | private final Logger log = getLogger(getClass()); |
| 54 | |
| 55 | @Override |
| 56 | public Collection<QueueDescription> getQueues() { |
| 57 | OvsdbClientService ovsdbClient = getOvsdbClient(handler()); |
| 58 | if (ovsdbClient == null) { |
| 59 | return Collections.emptyList(); |
| 60 | } |
| 61 | |
| 62 | Set<OvsdbQueue> queues = ovsdbClient.getQueues(); |
| 63 | return queues.stream() |
| 64 | .map(q -> DefaultQueueDescription.builder() |
| 65 | .queueId(QueueId.queueId(q.externalIds().get(QUEUE_EXTERNAL_ID_KEY))) |
| 66 | .type(types(q)) |
| 67 | .dscp(q.dscp().isPresent() ? q.dscp().get().intValue() : null) |
| 68 | .maxRate(q.otherConfigs().get(MAX_RATE) != null ? |
| 69 | Bandwidth.bps(Long.parseLong(q.otherConfigs().get(MAX_RATE))) : |
| 70 | Bandwidth.bps(0L)) |
| 71 | .minRate(q.otherConfigs().get(MIN_RATE) != null ? |
| 72 | Bandwidth.bps(Long.parseLong(q.otherConfigs().get(MIN_RATE))) : |
| 73 | Bandwidth.bps(0L)) |
| 74 | .burst(q.otherConfigs().get(BURST) != null ? |
| 75 | Long.valueOf(q.otherConfigs().get(BURST)) : 0L) |
| 76 | .priority(q.otherConfigs().get(PRIORITY) != null ? |
| 77 | Long.valueOf(q.otherConfigs().get(PRIORITY)) : 0L) |
| 78 | .build()) |
| 79 | .collect(Collectors.toSet()); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public QueueDescription getQueue(QueueDescription queueDesc) { |
| 84 | OvsdbClientService ovsdbClient = getOvsdbClient(handler()); |
| 85 | if (ovsdbClient == null) { |
| 86 | return null; |
| 87 | } |
| 88 | |
| 89 | OvsdbQueue queue = ovsdbClient.getQueue(queueDesc.queueId()); |
| 90 | if (queue == null) { |
| 91 | return null; |
| 92 | } |
| 93 | return DefaultQueueDescription.builder() |
| 94 | .queueId(QueueId.queueId(queue.externalIds().get(QUEUE_EXTERNAL_ID_KEY))) |
| 95 | .type(types(queue)) |
| 96 | .dscp(queue.dscp().isPresent() ? queue.dscp().get().intValue() : null) |
| 97 | .maxRate(queue.otherConfigs().get(MAX_RATE) != null ? |
| 98 | Bandwidth.bps(Long.parseLong(queue.otherConfigs().get(MAX_RATE))) : |
| 99 | Bandwidth.bps(0L)) |
| 100 | .minRate(queue.otherConfigs().get(MIN_RATE) != null ? |
| 101 | Bandwidth.bps(Long.parseLong(queue.otherConfigs().get(MIN_RATE))) : |
| 102 | Bandwidth.bps(0L)) |
| 103 | .burst(queue.otherConfigs().get(BURST) != null ? |
| 104 | Long.valueOf(queue.otherConfigs().get(BURST)) : 0L) |
| 105 | .priority(queue.otherConfigs().get(PRIORITY) != null ? |
| 106 | Long.valueOf(queue.otherConfigs().get(PRIORITY)) : 0L) |
| 107 | .build(); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public boolean addQueue(QueueDescription queue) { |
| 112 | OvsdbClientService ovsdbClient = getOvsdbClient(handler()); |
| 113 | OvsdbQueue ovsdbQueue = OvsdbQueue.builder(queue).build(); |
| 114 | return ovsdbClient.createQueue(ovsdbQueue); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void deleteQueue(QueueId queueId) { |
| 119 | OvsdbClientService ovsdbClient = getOvsdbClient(handler()); |
| 120 | ovsdbClient.dropQueue(queueId); |
| 121 | } |
| 122 | |
| 123 | private EnumSet<Type> types(OvsdbQueue queue) { |
| 124 | EnumSet<Type> enumSet = EnumSet.noneOf(Type.class); |
| 125 | if (queue == null) { |
| 126 | return enumSet; |
| 127 | } |
| 128 | |
| 129 | if (queue.otherConfigs().get(MAX_RATE) != null) { |
| 130 | enumSet.add(Type.MAX); |
| 131 | } |
| 132 | if (queue.otherConfigs().get(MIN_RATE) != null) { |
| 133 | enumSet.add(Type.MIN); |
| 134 | } |
| 135 | if (queue.otherConfigs().get(BURST) != null) { |
| 136 | enumSet.add(Type.BURST); |
| 137 | } |
| 138 | if (queue.otherConfigs().get(PRIORITY) != null) { |
| 139 | enumSet.add(Type.PRIORITY); |
| 140 | } |
| 141 | return enumSet; |
| 142 | } |
| 143 | |
| 144 | // OvsdbNodeId(IP) is used in the adaptor while DeviceId(ovsdb:IP) |
| 145 | // is used in the core. So DeviceId need be changed to OvsdbNodeId. |
| 146 | private OvsdbNodeId changeDeviceIdToNodeId(DeviceId deviceId) { |
| 147 | String[] splits = deviceId.toString().split(":"); |
| 148 | if (splits.length < 1) { |
| 149 | return null; |
| 150 | } |
| 151 | IpAddress ipAddress = IpAddress.valueOf(splits[1]); |
| 152 | return new OvsdbNodeId(ipAddress, 0); |
| 153 | } |
| 154 | |
| 155 | private OvsdbClientService getOvsdbClient(DriverHandler handler) { |
| 156 | OvsdbController ovsController = handler.get(OvsdbController.class); |
| 157 | OvsdbNodeId nodeId = changeDeviceIdToNodeId(handler.data().deviceId()); |
| 158 | |
| 159 | return ovsController.getOvsdbClient(nodeId); |
| 160 | } |
| 161 | } |