blob: 00fcb80c530338aa31b464e426e26181bc9d05cd [file] [log] [blame]
Frank Wange11a98d2016-10-26 17:04:03 +08001/*
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
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;
26import org.onosproject.net.driver.AbstractHandlerBehaviour;
27import org.onosproject.net.driver.DriverHandler;
28import org.onosproject.ovsdb.controller.OvsdbClientService;
29import org.onosproject.ovsdb.controller.OvsdbController;
30import org.onosproject.ovsdb.controller.OvsdbNodeId;
31import org.onosproject.ovsdb.controller.OvsdbQos;
32import org.slf4j.Logger;
33
34import java.util.Collection;
35import java.util.Set;
36import java.util.stream.Collectors;
37
38import static org.onosproject.ovsdb.controller.OvsdbConstant.CBS;
39import static org.onosproject.ovsdb.controller.OvsdbConstant.CIR;
40import static org.onosproject.ovsdb.controller.OvsdbConstant.MAX_RATE;
41import static org.onosproject.ovsdb.controller.OvsdbConstant.QOS_EGRESS_POLICER;
42import static org.onosproject.ovsdb.controller.OvsdbConstant.QOS_EXTERNAL_ID_KEY;
43import static org.onosproject.ovsdb.controller.OvsdbConstant.QOS_TYPE_PREFIX;
44import static org.slf4j.LoggerFactory.getLogger;
45
46/**
47 * OVSDB-based implementation of qos config behaviour.
48 */
49public class OvsdbQosConfig extends AbstractHandlerBehaviour implements QosConfigBehaviour {
50
51 private final Logger log = getLogger(getClass());
52
53 @Override
54 public Collection<QosDescription> getQoses() {
55 OvsdbClientService ovsdbClient = getOvsdbClient(handler());
56 if (ovsdbClient == null) {
57 return null;
58 }
59 Set<OvsdbQos> qoses = ovsdbClient.getQoses();
60
61 return qoses.stream()
62 .map(qos -> DefaultQosDescription.builder()
63 .qosId(QosId.qosId(qos.externalIds().get(QOS_EXTERNAL_ID_KEY)))
64 .type(QOS_EGRESS_POLICER.equals(qos.qosType()) ?
65 QosDescription.Type.EGRESS_POLICER :
66 QosDescription.Type.valueOf(qos.qosType()
67 .replace(QOS_TYPE_PREFIX, "")
68 .toUpperCase()))
69 .maxRate(qos.otherConfigs().get(MAX_RATE) != null ?
70 Bandwidth.bps(Long.parseLong(qos.otherConfigs().get(MAX_RATE))) :
71 Bandwidth.bps(0L))
72 .cbs(qos.otherConfigs().get(CBS) != null ?
73 Long.valueOf(qos.otherConfigs().get(CBS)) : null)
74 .cir(qos.otherConfigs().get(CIR) != null ?
75 Long.valueOf(qos.otherConfigs().get(CIR)) : null)
76 .build())
77 .collect(Collectors.toSet());
78 }
79
80 @Override
81 public QosDescription getQos(QosDescription qosDesc) {
82 OvsdbClientService ovsdbClient = getOvsdbClient(handler());
83 if (ovsdbClient == null) {
84 return null;
85 }
86 OvsdbQos qos = ovsdbClient.getQos(qosDesc.qosId());
87 if (qos == null) {
88 return null;
89 }
90 return DefaultQosDescription.builder()
91 .qosId(QosId.qosId(qos.externalIds().get(QOS_EXTERNAL_ID_KEY)))
92 .type(QOS_EGRESS_POLICER.equals(qos.qosType()) ?
93 QosDescription.Type.EGRESS_POLICER :
94 QosDescription.Type.valueOf(qos.qosType()
95 .replace(QOS_TYPE_PREFIX, "")
96 .toUpperCase()))
97 .maxRate(qos.otherConfigs().get(MAX_RATE) != null ?
98 Bandwidth.bps(Long.parseLong(qos.otherConfigs().get(MAX_RATE))) :
99 Bandwidth.bps(0L))
100 .cbs(qos.otherConfigs().get(CBS) != null ?
101 Long.valueOf(qos.otherConfigs().get(CBS)) : null)
102 .cir(qos.otherConfigs().get(CIR) != null ?
103 Long.valueOf(qos.otherConfigs().get(CIR)) : null)
104 .build();
105 }
106
107 @Override
108 public boolean addQoS(QosDescription qos) {
109 OvsdbClientService ovsdbClient = getOvsdbClient(handler());
110 OvsdbQos ovsdbQos = OvsdbQos.builder(qos).build();
111 return ovsdbClient.createQos(ovsdbQos);
112 }
113
114 @Override
115 public void deleteQoS(QosId qosId) {
116 OvsdbClientService ovsdbClient = getOvsdbClient(handler());
117 ovsdbClient.dropQos(qosId);
118 }
119
120 // OvsdbNodeId(IP) is used in the adaptor while DeviceId(ovsdb:IP)
121 // is used in the core. So DeviceId need be changed to OvsdbNodeId.
122 private OvsdbNodeId changeDeviceIdToNodeId(DeviceId deviceId) {
123 String[] splits = deviceId.toString().split(":");
124 if (splits.length < 1) {
125 return null;
126 }
127 IpAddress ipAddress = IpAddress.valueOf(splits[1]);
128 return new OvsdbNodeId(ipAddress, 0);
129 }
130
131 private OvsdbClientService getOvsdbClient(DriverHandler handler) {
132 OvsdbController ovsController = handler.get(OvsdbController.class);
133 OvsdbNodeId nodeId = changeDeviceIdToNodeId(handler.data().deviceId());
134
135 return ovsController.getOvsdbClient(nodeId);
136 }
137}
138