Frank Wang | e11a98d | 2016-10-26 17:04:03 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Frank Wang | e11a98d | 2016-10-26 17:04:03 +0800 | [diff] [blame] | 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.onosproject.net.DeviceId; |
| 21 | import org.onosproject.net.PortNumber; |
| 22 | import org.onosproject.net.behaviour.PortConfigBehaviour; |
| 23 | import org.onosproject.net.behaviour.QosDescription; |
| 24 | import org.onosproject.net.device.PortDescription; |
| 25 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 26 | import org.onosproject.net.driver.DriverHandler; |
| 27 | import org.onosproject.ovsdb.controller.OvsdbClientService; |
| 28 | import org.onosproject.ovsdb.controller.OvsdbController; |
| 29 | import org.onosproject.ovsdb.controller.OvsdbNodeId; |
| 30 | import org.slf4j.Logger; |
| 31 | |
| 32 | import static org.slf4j.LoggerFactory.getLogger; |
| 33 | |
| 34 | /** |
| 35 | * OVSDB-based implementation of port config behaviour. |
| 36 | */ |
| 37 | public 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 | |