blob: 0e9b18a0584fcd69a0f629c4724dee52bb4ebc22 [file] [log] [blame]
Frank Wange11a98d2016-10-26 17:04:03 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Frank Wange11a98d2016-10-26 17:04:03 +08003 *
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
17package org.onosproject.drivers.ovsdb;
18
19import org.onlab.packet.IpAddress;
20import org.onlab.util.Bandwidth;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.behaviour.DefaultQosDescription;
23import org.onosproject.net.behaviour.QosConfigBehaviour;
24import org.onosproject.net.behaviour.QosDescription;
25import org.onosproject.net.behaviour.QosId;
tanbangchengc944c282017-11-12 20:42:59 +080026import org.onosproject.net.behaviour.QueueDescription;
Frank Wange11a98d2016-10-26 17:04:03 +080027import org.onosproject.net.driver.AbstractHandlerBehaviour;
28import org.onosproject.net.driver.DriverHandler;
29import org.onosproject.ovsdb.controller.OvsdbClientService;
30import org.onosproject.ovsdb.controller.OvsdbController;
31import org.onosproject.ovsdb.controller.OvsdbNodeId;
32import org.onosproject.ovsdb.controller.OvsdbQos;
33import org.slf4j.Logger;
34
35import java.util.Collection;
tanbangchengc944c282017-11-12 20:42:59 +080036import java.util.List;
37import java.util.Map;
Frank Wange11a98d2016-10-26 17:04:03 +080038import java.util.Set;
39import java.util.stream.Collectors;
40
41import static org.onosproject.ovsdb.controller.OvsdbConstant.CBS;
42import static org.onosproject.ovsdb.controller.OvsdbConstant.CIR;
43import static org.onosproject.ovsdb.controller.OvsdbConstant.MAX_RATE;
44import static org.onosproject.ovsdb.controller.OvsdbConstant.QOS_EGRESS_POLICER;
45import static org.onosproject.ovsdb.controller.OvsdbConstant.QOS_EXTERNAL_ID_KEY;
46import static org.onosproject.ovsdb.controller.OvsdbConstant.QOS_TYPE_PREFIX;
47import static org.slf4j.LoggerFactory.getLogger;
48
49/**
50 * OVSDB-based implementation of qos config behaviour.
51 */
52public 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
tanbangchengc944c282017-11-12 20:42:59 +0800123 @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 Wange11a98d2016-10-26 17:04:03 +0800135 // 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(":");
Niraj Dubey2d20f332019-09-26 13:48:39 +0530139 if (splits.length < 2) {
Frank Wange11a98d2016-10-26 17:04:03 +0800140 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