Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-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 | |
| 17 | package org.onosproject.drivers.ovsdb; |
| 18 | |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onlab.packet.VlanId; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.behaviour.InterfaceConfig; |
Hyunsun Moon | 8947866 | 2016-06-09 17:52:34 -0700 | [diff] [blame^] | 23 | import org.onosproject.net.behaviour.PatchDescription; |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 24 | import org.onosproject.net.behaviour.TunnelDescription; |
| 25 | import org.onosproject.net.device.DeviceInterfaceDescription; |
| 26 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 27 | import org.onosproject.net.driver.DriverHandler; |
| 28 | import org.onosproject.ovsdb.controller.OvsdbClientService; |
| 29 | import org.onosproject.ovsdb.controller.OvsdbController; |
| 30 | import org.onosproject.ovsdb.controller.OvsdbInterface; |
| 31 | import org.onosproject.ovsdb.controller.OvsdbNodeId; |
| 32 | import org.slf4j.Logger; |
| 33 | |
| 34 | import java.util.List; |
| 35 | |
| 36 | import static org.slf4j.LoggerFactory.getLogger; |
| 37 | |
| 38 | /** |
| 39 | * OVSDB-based implementation of interface config behaviour. |
| 40 | */ |
| 41 | public class OvsdbInterfaceConfig extends AbstractHandlerBehaviour implements InterfaceConfig { |
| 42 | |
| 43 | private final Logger log = getLogger(getClass()); |
| 44 | |
| 45 | @Override |
| 46 | public boolean addTunnelMode(String ifaceName, TunnelDescription tunnelDesc) { |
| 47 | OvsdbInterface ovsdbIface = OvsdbInterface.builder(tunnelDesc).build(); |
| 48 | OvsdbClientService ovsdbClient = getOvsdbClient(handler()); |
| 49 | |
| 50 | if (!tunnelDesc.deviceId().isPresent()) { |
| 51 | log.warn("Device ID is required {}", tunnelDesc); |
| 52 | return false; |
| 53 | } |
| 54 | return ovsdbClient.createInterface(tunnelDesc.deviceId().get(), ovsdbIface); |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public boolean removeTunnelMode(String ifaceName) { |
| 59 | OvsdbClientService ovsdbClient = getOvsdbClient(handler()); |
| 60 | return ovsdbClient.dropInterface(ifaceName); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public boolean addAccessMode(String ifaceName, VlanId vlanId) { |
| 65 | // TODO implement |
| 66 | throw new UnsupportedOperationException("Not implemented yet"); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public boolean removeAccessMode(String ifaceName) { |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 71 | throw new UnsupportedOperationException("Not implemented yet"); |
| 72 | } |
| 73 | |
| 74 | @Override |
Hyunsun Moon | 8947866 | 2016-06-09 17:52:34 -0700 | [diff] [blame^] | 75 | public boolean addPatchMode(String ifaceName, PatchDescription patchDesc) { |
| 76 | OvsdbInterface ovsdbIface = OvsdbInterface.builder(patchDesc).build(); |
| 77 | OvsdbClientService ovsdbClient = getOvsdbClient(handler()); |
| 78 | |
| 79 | if (!patchDesc.deviceId().isPresent()) { |
| 80 | log.warn("Device ID is required {}", patchDesc); |
| 81 | return false; |
| 82 | } |
| 83 | return ovsdbClient.createInterface(patchDesc.deviceId().get(), ovsdbIface); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public boolean removePatchMode(String ifaceName) { |
| 88 | OvsdbClientService ovsdbClient = getOvsdbClient(handler()); |
| 89 | return ovsdbClient.dropInterface(ifaceName); |
| 90 | } |
| 91 | |
| 92 | @Override |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 93 | public boolean addTrunkMode(String ifaceName, List<VlanId> vlanIds) { |
| 94 | // TODO implement |
| 95 | throw new UnsupportedOperationException("Not implemented yet"); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public boolean removeTrunkMode(String ifaceName) { |
| 100 | // TODO implement |
| 101 | throw new UnsupportedOperationException("Not implemented yet"); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public boolean addRateLimit(String ifaceName, short limit) { |
| 106 | // TODO implement |
| 107 | throw new UnsupportedOperationException("Not implemented yet"); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public boolean removeRateLimit(String ifaceName) { |
| 112 | // TODO implement |
| 113 | throw new UnsupportedOperationException("Not implemented yet"); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public List<DeviceInterfaceDescription> getInterfaces() { |
| 118 | // TODO implement |
| 119 | throw new UnsupportedOperationException("Not implemented yet"); |
| 120 | } |
| 121 | |
| 122 | // deprecated interfaces |
| 123 | @Override |
| 124 | public boolean addAccessInterface(DeviceId deviceId, String ifaceName, VlanId vlanId) { |
| 125 | throw new UnsupportedOperationException("Not implemented yet"); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public boolean removeAccessInterface(DeviceId deviceId, String ifaceName) { |
| 130 | throw new UnsupportedOperationException("Not implemented yet"); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public boolean addTrunkInterface(DeviceId deviceId, String ifaceName, List<VlanId> vlanIds) { |
| 135 | throw new UnsupportedOperationException("Not implemented yet"); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public List<DeviceInterfaceDescription> getInterfaces(DeviceId deviceId) { |
| 140 | return null; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public boolean removeTrunkInterface(DeviceId deviceId, String ifaceName) { |
| 145 | throw new UnsupportedOperationException("Not implemented yet"); |
| 146 | } |
| 147 | |
| 148 | // OvsdbNodeId(IP) is used in the adaptor while DeviceId(ovsdb:IP) |
| 149 | // is used in the core. So DeviceId need be changed to OvsdbNodeId. |
| 150 | private OvsdbNodeId changeDeviceIdToNodeId(DeviceId deviceId) { |
| 151 | String[] splits = deviceId.toString().split(":"); |
| 152 | if (splits.length < 1) { |
| 153 | return null; |
| 154 | } |
| 155 | IpAddress ipAddress = IpAddress.valueOf(splits[1]); |
| 156 | return new OvsdbNodeId(ipAddress, 0); |
| 157 | } |
| 158 | |
| 159 | private OvsdbClientService getOvsdbClient(DriverHandler handler) { |
| 160 | OvsdbController ovsController = handler.get(OvsdbController.class); |
| 161 | OvsdbNodeId nodeId = changeDeviceIdToNodeId(handler.data().deviceId()); |
| 162 | |
| 163 | return ovsController.getOvsdbClient(nodeId); |
| 164 | } |
| 165 | } |