Georgios Katsikas | 13ccba6 | 2020-03-18 12:05:03 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2020-present Open Networking Foundation |
| 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.server; |
| 18 | |
| 19 | import org.onlab.util.Bandwidth; |
| 20 | import org.onosproject.net.DeviceId; |
| 21 | import org.onosproject.net.behaviour.DefaultQueueDescription; |
| 22 | import org.onosproject.net.behaviour.QueueConfigBehaviour; |
| 23 | import org.onosproject.net.behaviour.QueueDescription; |
| 24 | import org.onosproject.net.behaviour.QueueDescription.Type; |
| 25 | import org.onosproject.net.behaviour.QueueId; |
| 26 | import org.onosproject.protocol.rest.RestSBDevice; |
| 27 | |
| 28 | import org.slf4j.Logger; |
| 29 | |
| 30 | import com.fasterxml.jackson.databind.JsonNode; |
| 31 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 32 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 33 | import com.google.common.collect.Sets; |
| 34 | import com.google.common.collect.ImmutableList; |
| 35 | |
| 36 | import java.io.InputStream; |
| 37 | import java.io.IOException; |
| 38 | import java.util.EnumSet; |
| 39 | import java.util.Collection; |
| 40 | import java.util.Collections; |
| 41 | import java.util.Map; |
| 42 | import javax.ws.rs.ProcessingException; |
| 43 | |
| 44 | import static com.google.common.base.Preconditions.checkNotNull; |
| 45 | import static org.onosproject.drivers.server.Constants.JSON; |
| 46 | import static org.onosproject.drivers.server.Constants.PARAM_ID; |
| 47 | import static org.onosproject.drivers.server.Constants.PARAM_NICS; |
| 48 | import static org.onosproject.drivers.server.Constants.PARAM_NIC_MAX_RATE; |
| 49 | import static org.onosproject.drivers.server.Constants.PARAM_QUEUES; |
| 50 | import static org.onosproject.drivers.server.Constants.PARAM_TYPE; |
| 51 | import static org.onosproject.drivers.server.Constants.MSG_DEVICE_NULL; |
| 52 | import static org.onosproject.drivers.server.Constants.MSG_DEVICE_ID_NULL; |
| 53 | import static org.onosproject.drivers.server.Constants.URL_NIC_QUEUE_ADMIN; |
| 54 | import static org.slf4j.LoggerFactory.getLogger; |
| 55 | |
| 56 | /** |
| 57 | * Implementation of queue config behaviour for server devices. |
| 58 | */ |
| 59 | public class ServerQueueConfig |
| 60 | extends BasicServerDriver |
| 61 | implements QueueConfigBehaviour { |
| 62 | |
| 63 | private final Logger log = getLogger(getClass()); |
| 64 | |
| 65 | public ServerQueueConfig() { |
| 66 | super(); |
| 67 | log.debug("Started"); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public Collection<QueueDescription> getQueues() { |
| 72 | // Retrieve the device ID from the handler |
| 73 | DeviceId deviceId = super.getDeviceId(); |
| 74 | checkNotNull(deviceId, MSG_DEVICE_ID_NULL); |
| 75 | |
| 76 | // Get the device |
| 77 | RestSBDevice device = super.getDevice(deviceId); |
| 78 | checkNotNull(device, MSG_DEVICE_NULL); |
| 79 | |
| 80 | // Hit the path that provides queue administration |
| 81 | InputStream response = null; |
| 82 | try { |
| 83 | response = getController().get(deviceId, URL_NIC_QUEUE_ADMIN, JSON); |
| 84 | } catch (ProcessingException pEx) { |
| 85 | log.error("Failed to get NIC queues from device: {}", deviceId); |
| 86 | return Collections.EMPTY_LIST; |
| 87 | } |
| 88 | |
| 89 | // Load the JSON into object |
| 90 | ObjectMapper mapper = new ObjectMapper(); |
| 91 | Map<String, Object> jsonMap = null; |
| 92 | JsonNode jsonNode = null; |
| 93 | ObjectNode objNode = null; |
| 94 | try { |
| 95 | jsonMap = mapper.readValue(response, Map.class); |
| 96 | jsonNode = mapper.convertValue(jsonMap, JsonNode.class); |
| 97 | objNode = (ObjectNode) jsonNode; |
| 98 | } catch (IOException ioEx) { |
| 99 | log.error("Failed to get NIC queues from device: {}", deviceId); |
| 100 | return Collections.EMPTY_LIST; |
| 101 | } |
| 102 | |
| 103 | if (objNode == null) { |
| 104 | log.error("Failed to get NIC queues from device: {}", deviceId); |
| 105 | return Collections.EMPTY_LIST; |
| 106 | } |
| 107 | |
| 108 | Collection<QueueDescription> queueDescs = Sets.newHashSet(); |
| 109 | |
| 110 | // Fetch NICs' array |
| 111 | JsonNode nicsNode = objNode.path(PARAM_NICS); |
| 112 | |
| 113 | for (JsonNode nn : nicsNode) { |
| 114 | ObjectNode nicObjNode = (ObjectNode) nn; |
| 115 | int nicId = nicObjNode.path(PARAM_ID).asInt(); |
| 116 | JsonNode queuesNode = nicObjNode.path(PARAM_QUEUES); |
| 117 | |
| 118 | // Each NIC has a set of queues |
| 119 | for (JsonNode qn : queuesNode) { |
| 120 | ObjectNode queueObjNode = (ObjectNode) qn; |
| 121 | |
| 122 | // Get the attributes of a queue |
| 123 | int queueIdInt = queueObjNode.path(PARAM_ID).asInt(); |
| 124 | String queueTypeStr = get(qn, PARAM_TYPE); |
| 125 | long queueRateInt = queueObjNode.path(PARAM_NIC_MAX_RATE).asLong(); |
| 126 | |
| 127 | QueueId queueId = QueueId.queueId("nic" + nicId + ":" + queueIdInt); |
| 128 | EnumSet<Type> queueTypes = getQueueTypesFromString(queueTypeStr); |
| 129 | Bandwidth queueRate = Bandwidth.mbps(queueRateInt); |
| 130 | |
| 131 | queueDescs.add( |
| 132 | DefaultQueueDescription.builder() |
| 133 | .queueId(queueId) |
| 134 | .type(queueTypes) |
| 135 | .maxRate(queueRate) |
| 136 | .build()); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | log.info("[Device {}] NIC queues: {}", deviceId, queueDescs); |
| 141 | |
| 142 | return ImmutableList.copyOf(queueDescs); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public QueueDescription getQueue(QueueDescription queueDesc) { |
| 147 | for (QueueDescription qDesc : getQueues()) { |
| 148 | if (queueDesc.queueId().equals(qDesc.queueId())) { |
| 149 | return qDesc; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return null; |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public boolean addQueue(QueueDescription queue) { |
| 158 | throw new UnsupportedOperationException("Add queue operation not supported"); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public void deleteQueue(QueueId queueId) { |
| 163 | throw new UnsupportedOperationException("Delete queue operation not supported"); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Convert string-based queue type into enum set. |
| 168 | * |
| 169 | * @param queueTypeStr string-based queue type |
| 170 | * @return queue types enum set |
| 171 | */ |
| 172 | private EnumSet<Type> getQueueTypesFromString(String queueTypeStr) { |
| 173 | EnumSet<Type> enumSet = EnumSet.noneOf(Type.class); |
| 174 | if ((queueTypeStr == null) || (queueTypeStr.isEmpty())) { |
| 175 | return enumSet; |
| 176 | } |
| 177 | |
| 178 | if (queueTypeStr.toUpperCase().equals("MIN")) { |
| 179 | enumSet.add(Type.MAX); |
| 180 | } else if (queueTypeStr.toUpperCase().equals("MAX")) { |
| 181 | enumSet.add(Type.MIN); |
| 182 | } else if (queueTypeStr.toUpperCase().equals("PRIORITY")) { |
| 183 | enumSet.add(Type.PRIORITY); |
| 184 | } else { |
| 185 | enumSet.add(Type.BURST); |
| 186 | } |
| 187 | |
| 188 | return enumSet; |
| 189 | } |
| 190 | |
| 191 | } |