blob: 0c08e6b23ddeb8509459caf0f5df7bbca23fa3b7 [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.onosproject.net.DeviceId;
21import org.onosproject.net.PortNumber;
22import org.onosproject.net.behaviour.PortConfigBehaviour;
23import org.onosproject.net.behaviour.QosDescription;
24import org.onosproject.net.device.PortDescription;
25import org.onosproject.net.driver.AbstractHandlerBehaviour;
26import org.onosproject.net.driver.DriverHandler;
27import org.onosproject.ovsdb.controller.OvsdbClientService;
28import org.onosproject.ovsdb.controller.OvsdbController;
29import org.onosproject.ovsdb.controller.OvsdbNodeId;
30import org.slf4j.Logger;
31
32import static org.slf4j.LoggerFactory.getLogger;
33
34/**
35 * OVSDB-based implementation of port config behaviour.
36 */
37public class OvsdbPortConfig extends AbstractHandlerBehaviour implements PortConfigBehaviour {
38
39 private final Logger log = getLogger(getClass());
40
41 @Override
42 public void applyQoS(PortDescription portDesc, QosDescription qosDesc) {
43 OvsdbClientService ovsdbClient = getOvsdbClient(handler());
44 if (ovsdbClient == null) {
45 return;
46 }
47 ovsdbClient.applyQos(portDesc.portNumber(), qosDesc.qosId().name());
48 }
49
50 @Override
51 public void removeQoS(PortNumber portNumber) {
52 OvsdbClientService ovsdbClient = getOvsdbClient(handler());
53 if (ovsdbClient == null) {
54 return;
55 }
56 ovsdbClient.removeQos(portNumber);
57 }
58
59 // OvsdbNodeId(IP) is used in the adaptor while DeviceId(ovsdb:IP)
60 // is used in the core. So DeviceId need be changed to OvsdbNodeId.
61 private OvsdbNodeId changeDeviceIdToNodeId(DeviceId deviceId) {
62 String[] splits = deviceId.toString().split(":");
63 if (splits.length < 1) {
64 return null;
65 }
66 IpAddress ipAddress = IpAddress.valueOf(splits[1]);
67 return new OvsdbNodeId(ipAddress, 0);
68 }
69
70 private OvsdbClientService getOvsdbClient(DriverHandler handler) {
71 OvsdbController ovsController = handler.get(OvsdbController.class);
72 OvsdbNodeId nodeId = changeDeviceIdToNodeId(handler.data().deviceId());
73
74 return ovsController.getOvsdbClient(nodeId);
75 }
76}
77