Hyunsun Moon | 4c39663 | 2016-05-13 04:17:53 -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 | package org.onosproject.xosclient.impl; |
| 17 | |
Hyunsun Moon | e0f3e28 | 2016-05-13 18:58:35 -0700 | [diff] [blame^] | 18 | import com.google.common.collect.Maps; |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onlab.packet.MacAddress; |
| 21 | import org.onosproject.xosclient.api.OpenStackAccess; |
Hyunsun Moon | 4c39663 | 2016-05-13 04:17:53 -0700 | [diff] [blame] | 22 | import org.onosproject.xosclient.api.VtnPort; |
| 23 | import org.onosproject.xosclient.api.VtnPortApi; |
| 24 | import org.onosproject.xosclient.api.VtnPortId; |
| 25 | import org.onosproject.xosclient.api.VtnServiceId; |
| 26 | import org.onosproject.xosclient.api.XosAccess; |
Hyunsun Moon | e0f3e28 | 2016-05-13 18:58:35 -0700 | [diff] [blame^] | 27 | import org.openstack4j.api.OSClient; |
| 28 | import org.openstack4j.model.network.IP; |
| 29 | import org.openstack4j.model.network.Port; |
| 30 | import org.openstack4j.openstack.OSFactory; |
| 31 | import org.slf4j.Logger; |
| 32 | import org.slf4j.LoggerFactory; |
Hyunsun Moon | 4c39663 | 2016-05-13 04:17:53 -0700 | [diff] [blame] | 33 | |
Hyunsun Moon | e0f3e28 | 2016-05-13 18:58:35 -0700 | [diff] [blame^] | 34 | import java.util.Map; |
Hyunsun Moon | 4c39663 | 2016-05-13 04:17:53 -0700 | [diff] [blame] | 35 | import java.util.Set; |
| 36 | |
Hyunsun Moon | e0f3e28 | 2016-05-13 18:58:35 -0700 | [diff] [blame^] | 37 | import static com.google.common.base.Preconditions.checkNotNull; |
| 38 | |
Hyunsun Moon | 4c39663 | 2016-05-13 04:17:53 -0700 | [diff] [blame] | 39 | /** |
| 40 | * Provides CORD VTN port APIs. |
| 41 | */ |
Hyunsun Moon | e0f3e28 | 2016-05-13 18:58:35 -0700 | [diff] [blame^] | 42 | public final class DefaultVtnPortApi extends XosApi implements VtnPortApi { |
| 43 | |
| 44 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Hyunsun Moon | 4c39663 | 2016-05-13 04:17:53 -0700 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Default constructor. |
| 48 | * |
| 49 | * @param baseUrl base url |
| 50 | * @param access xos access |
| 51 | */ |
| 52 | public DefaultVtnPortApi(String baseUrl, XosAccess access) { |
| 53 | super(baseUrl, access); |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public Set<VtnPort> vtnPorts() { |
| 58 | // TODO implement this when XOS provides this information |
| 59 | return null; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public Set<VtnPort> vtnPorts(VtnServiceId serviceId) { |
| 64 | // TODO implement this when XOS provides this information |
| 65 | return null; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public VtnPort vtnPort(VtnPortId portId) { |
| 70 | // TODO implement this when XOS provides this information |
| 71 | return null; |
| 72 | } |
Hyunsun Moon | e0f3e28 | 2016-05-13 18:58:35 -0700 | [diff] [blame^] | 73 | |
| 74 | @Override |
| 75 | // TODO remove this when XOS provides this information |
| 76 | public VtnPort vtnPort(String portName, OpenStackAccess osAccess) { |
| 77 | checkNotNull(osAccess); |
| 78 | |
| 79 | OSClient osClient = getOpenStackClient(osAccess); |
| 80 | Port osPort = osClient.networking().port().list() |
| 81 | .stream() |
| 82 | .filter(p -> p.getId().contains(portName.substring(3))) |
| 83 | .findFirst().orElse(null); |
| 84 | if (osPort == null) { |
| 85 | log.warn("Failed to get OpenStack port for {}", portName); |
| 86 | return null; |
| 87 | } |
| 88 | return getVtnPort(osPort); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | // TODO remove this when XOS provides this information |
| 93 | public VtnPort vtnPort(VtnPortId portId, OpenStackAccess osAccess) { |
| 94 | checkNotNull(osAccess); |
| 95 | |
| 96 | OSClient osClient = getOpenStackClient(osAccess); |
| 97 | Port osPort = osClient.networking().port().get(portId.id()); |
| 98 | if (osPort == null) { |
| 99 | log.warn("Failed to get OpenStack port {}", portId); |
| 100 | return null; |
| 101 | } |
| 102 | return getVtnPort(osPort); |
| 103 | } |
| 104 | |
| 105 | // TODO remove this when XOS provides this information |
| 106 | private VtnPort getVtnPort(Port osPort) { |
| 107 | checkNotNull(osPort); |
| 108 | |
| 109 | // assumes all vtn port has single IP address |
| 110 | IP ipAddr = osPort.getFixedIps().stream().findFirst().orElse(null); |
| 111 | if (ipAddr == null) { |
| 112 | log.warn("Failed to get IP address for {}", osPort); |
| 113 | return null; |
| 114 | } |
| 115 | |
| 116 | Map<IpAddress, MacAddress> addressPairs = Maps.newHashMap(); |
| 117 | osPort.getAllowedAddressPairs().stream().forEach( |
| 118 | pair -> addressPairs.put(IpAddress.valueOf(pair.getIpAddress()), |
| 119 | MacAddress.valueOf(pair.getMacAddress()))); |
| 120 | |
| 121 | return new VtnPort(VtnPortId.of(osPort.getId()), |
| 122 | osPort.getName(), |
| 123 | VtnServiceId.of(osPort.getNetworkId()), |
| 124 | MacAddress.valueOf(osPort.getMacAddress()), |
| 125 | IpAddress.valueOf(ipAddr.getIpAddress()), |
| 126 | addressPairs); |
| 127 | } |
| 128 | |
| 129 | // TODO remove this when XOS provides this information |
| 130 | private OSClient getOpenStackClient(OpenStackAccess osAccess) { |
| 131 | checkNotNull(osAccess); |
| 132 | |
| 133 | // creating a client every time must be inefficient, but this method |
| 134 | // will be removed once XOS provides equivalent APIs |
| 135 | return OSFactory.builder() |
| 136 | .endpoint(osAccess.endpoint()) |
| 137 | .credentials(osAccess.user(), osAccess.password()) |
| 138 | .tenantName(osAccess.tenant()) |
| 139 | .authenticate(); |
| 140 | } |
Hyunsun Moon | 4c39663 | 2016-05-13 04:17:53 -0700 | [diff] [blame] | 141 | } |