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