Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -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 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
Hyunsun Moon | e0f3e28 | 2016-05-13 18:58:35 -0700 | [diff] [blame] | 20 | import com.google.common.base.Strings; |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Sets; |
Hyunsun Moon | e0f3e28 | 2016-05-13 18:58:35 -0700 | [diff] [blame] | 22 | import org.onosproject.xosclient.api.OpenStackAccess; |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 23 | import org.onosproject.xosclient.api.VtnServiceApi; |
| 24 | import org.onosproject.xosclient.api.XosAccess; |
Hyunsun Moon | 4c39663 | 2016-05-13 04:17:53 -0700 | [diff] [blame] | 25 | import org.onosproject.xosclient.api.VtnService; |
| 26 | import org.onosproject.xosclient.api.VtnServiceId; |
| 27 | |
Hyunsun Moon | e0f3e28 | 2016-05-13 18:58:35 -0700 | [diff] [blame] | 28 | import org.openstack4j.api.OSClient; |
Hyunsun Moon | d0bfe67 | 2016-06-30 10:55:10 -0700 | [diff] [blame] | 29 | import org.openstack4j.api.exceptions.AuthenticationException; |
Hyunsun Moon | e0f3e28 | 2016-05-13 18:58:35 -0700 | [diff] [blame] | 30 | import org.openstack4j.model.network.Network; |
| 31 | import org.openstack4j.model.network.Subnet; |
| 32 | import org.openstack4j.openstack.OSFactory; |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
| 35 | |
| 36 | import java.io.IOException; |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 37 | import java.util.Set; |
| 38 | |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 39 | import static com.google.common.base.Preconditions.checkNotNull; |
Hyunsun Moon | e0f3e28 | 2016-05-13 18:58:35 -0700 | [diff] [blame] | 40 | import static com.google.common.base.Preconditions.checkArgument; |
Hyunsun Moon | f1c0346 | 2016-06-21 18:06:01 -0700 | [diff] [blame] | 41 | import static org.onosproject.xosclient.api.VtnServiceApi.NetworkType.*; |
| 42 | import static org.onosproject.xosclient.api.VtnServiceApi.ServiceType.*; |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Provides CORD VTN service and service dependency APIs. |
| 46 | */ |
| 47 | public final class DefaultVtnServiceApi extends XosApi implements VtnServiceApi { |
| 48 | |
| 49 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 50 | |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 51 | /** |
| 52 | * Default constructor. |
Hyunsun Moon | 4c39663 | 2016-05-13 04:17:53 -0700 | [diff] [blame] | 53 | * |
| 54 | * @param baseUrl base url |
| 55 | * @param access xos access |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 56 | */ |
Hyunsun Moon | 4c39663 | 2016-05-13 04:17:53 -0700 | [diff] [blame] | 57 | public DefaultVtnServiceApi(String baseUrl, XosAccess access) { |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 58 | super(baseUrl, access); |
| 59 | } |
| 60 | |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 61 | @Override |
Hyunsun Moon | 4c39663 | 2016-05-13 04:17:53 -0700 | [diff] [blame] | 62 | public Set<VtnServiceId> services() { |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 63 | String response = restGet(EMPTY_STRING); |
| 64 | log.trace("Get services {}", response); |
| 65 | |
| 66 | ObjectMapper mapper = new ObjectMapper(); |
Hyunsun Moon | 4c39663 | 2016-05-13 04:17:53 -0700 | [diff] [blame] | 67 | Set<VtnServiceId> services = Sets.newHashSet(); |
| 68 | |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 69 | try { |
| 70 | JsonNode nodes = mapper.readTree(response); |
Hyunsun Moon | 4c39663 | 2016-05-13 04:17:53 -0700 | [diff] [blame] | 71 | nodes.fieldNames().forEachRemaining(id -> services.add(VtnServiceId.of(id))); |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 72 | } catch (IOException e) { |
| 73 | log.warn("Failed to get service list"); |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 74 | } |
Hyunsun Moon | 4c39663 | 2016-05-13 04:17:53 -0700 | [diff] [blame] | 75 | return services; |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Override |
Hyunsun Moon | 4c39663 | 2016-05-13 04:17:53 -0700 | [diff] [blame] | 79 | public VtnService service(VtnServiceId serviceId) { |
| 80 | // TODO implement this when XOS provides this API |
| 81 | return null; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public Set<VtnServiceId> providerServices(VtnServiceId tServiceId) { |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 86 | checkNotNull(tServiceId); |
| 87 | |
Hyunsun Moon | 4c39663 | 2016-05-13 04:17:53 -0700 | [diff] [blame] | 88 | String response = restGet(tServiceId.id()); |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 89 | log.trace("Get provider services {}", response); |
| 90 | |
| 91 | ObjectMapper mapper = new ObjectMapper(); |
Hyunsun Moon | 4c39663 | 2016-05-13 04:17:53 -0700 | [diff] [blame] | 92 | Set<VtnServiceId> pServices = Sets.newHashSet(); |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 93 | |
| 94 | try { |
| 95 | JsonNode nodes = mapper.readTree(response); |
Hyunsun Moon | 4c39663 | 2016-05-13 04:17:53 -0700 | [diff] [blame] | 96 | nodes.forEach(node -> pServices.add(VtnServiceId.of(node.asText()))); |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 97 | } catch (IOException e) { |
| 98 | log.warn("Failed to get service dependency"); |
| 99 | } |
| 100 | return pServices; |
| 101 | } |
| 102 | |
| 103 | @Override |
Hyunsun Moon | 4c39663 | 2016-05-13 04:17:53 -0700 | [diff] [blame] | 104 | public Set<VtnServiceId> tenantServices(VtnServiceId tServiceId) { |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 105 | checkNotNull(tServiceId); |
| 106 | |
| 107 | String response = restGet(EMPTY_STRING); |
| 108 | log.trace("Get tenant services {}", response); |
| 109 | |
| 110 | ObjectMapper mapper = new ObjectMapper(); |
Hyunsun Moon | 4c39663 | 2016-05-13 04:17:53 -0700 | [diff] [blame] | 111 | Set<VtnServiceId> tServices = Sets.newHashSet(); |
| 112 | |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 113 | try { |
| 114 | JsonNode nodes = mapper.readTree(response); |
Hyunsun Moon | 4c39663 | 2016-05-13 04:17:53 -0700 | [diff] [blame] | 115 | nodes.fields().forEachRemaining(entry -> entry.getValue().forEach( |
| 116 | pService -> { |
| 117 | if (pService.asText().equals(tServiceId.id())) { |
| 118 | tServices.add(VtnServiceId.of(entry.getKey())); |
| 119 | } |
| 120 | })); |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 121 | } catch (IOException e) { |
| 122 | log.warn("Failed to get service list"); |
| 123 | } |
| 124 | return tServices; |
| 125 | } |
Hyunsun Moon | e0f3e28 | 2016-05-13 18:58:35 -0700 | [diff] [blame] | 126 | |
| 127 | @Override |
| 128 | // TODO remove this when XOS provides this information |
| 129 | public VtnService service(VtnServiceId serviceId, OpenStackAccess osAccess) { |
| 130 | checkNotNull(osAccess); |
| 131 | |
| 132 | OSClient osClient = getOpenStackClient(osAccess); |
| 133 | Network osNet = osClient.networking().network().get(serviceId.id()); |
| 134 | if (osNet == null) { |
| 135 | log.warn("Failed to get OpenStack network {}", serviceId); |
| 136 | return null; |
| 137 | } |
| 138 | |
| 139 | // assumes all cord service networks has single subnet |
| 140 | Subnet osSubnet = osNet.getNeutronSubnets().stream() |
| 141 | .findFirst().orElse(null); |
| 142 | if (osSubnet == null) { |
| 143 | log.warn("Failed to get OpenStack subnet of network {}", serviceId); |
| 144 | return null; |
| 145 | } |
| 146 | |
Hyunsun Moon | 6504099 | 2016-07-27 18:41:34 -0700 | [diff] [blame] | 147 | return VtnService.build() |
| 148 | .id(serviceId) |
| 149 | .name(osNet.getName()) |
| 150 | .serviceType(serviceType(osNet.getName())) |
| 151 | .networkType(networkType(osNet.getName())) |
| 152 | .vni(osNet.getProviderSegID()) |
| 153 | .subnet(osSubnet.getCidr()) |
| 154 | .serviceIp(osSubnet.getGateway()) |
| 155 | .providerServices(providerServices(serviceId)) |
| 156 | .tenantServices(tenantServices(serviceId)) |
| 157 | .build(); |
Hyunsun Moon | e0f3e28 | 2016-05-13 18:58:35 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // TODO remove this when XOS provides this information |
| 161 | private OSClient getOpenStackClient(OpenStackAccess osAccess) { |
| 162 | checkNotNull(osAccess); |
| 163 | |
| 164 | // creating a client every time must be inefficient, but this method |
| 165 | // will be removed once XOS provides equivalent APIs |
Hyunsun Moon | d0bfe67 | 2016-06-30 10:55:10 -0700 | [diff] [blame] | 166 | try { |
| 167 | return OSFactory.builder() |
| 168 | .endpoint(osAccess.endpoint()) |
| 169 | .credentials(osAccess.user(), osAccess.password()) |
| 170 | .tenantName(osAccess.tenant()) |
| 171 | .authenticate(); |
| 172 | } catch (AuthenticationException e) { |
| 173 | log.warn("Failed to authenticate OpenStack API with {}", osAccess); |
| 174 | return null; |
| 175 | } |
Hyunsun Moon | e0f3e28 | 2016-05-13 18:58:35 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | // TODO remove this when XOS provides this information |
| 179 | private NetworkType networkType(String netName) { |
Hyunsun Moon | 6504099 | 2016-07-27 18:41:34 -0700 | [diff] [blame] | 180 | checkArgument(!Strings.isNullOrEmpty(netName), "VTN network name cannot be null"); |
Hyunsun Moon | e0f3e28 | 2016-05-13 18:58:35 -0700 | [diff] [blame] | 181 | |
| 182 | String name = netName.toUpperCase(); |
Hyunsun Moon | 91ba41a | 2016-06-10 16:52:39 -0700 | [diff] [blame] | 183 | if (name.contains(PUBLIC.name())) { |
Hyunsun Moon | e0f3e28 | 2016-05-13 18:58:35 -0700 | [diff] [blame] | 184 | return PUBLIC; |
Hyunsun Moon | f1c0346 | 2016-06-21 18:06:01 -0700 | [diff] [blame] | 185 | } else if (name.contains(MANAGEMENT_HOSTS.name())) { |
| 186 | return MANAGEMENT_HOSTS; |
| 187 | } else if (name.contains("MANAGEMENT")) { |
| 188 | return MANAGEMENT_LOCAL; |
Hyunsun Moon | e0f3e28 | 2016-05-13 18:58:35 -0700 | [diff] [blame] | 189 | } else { |
| 190 | return PRIVATE; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | // TODO remove this when XOS provides this information |
| 195 | private ServiceType serviceType(String netName) { |
Hyunsun Moon | 6504099 | 2016-07-27 18:41:34 -0700 | [diff] [blame] | 196 | checkArgument(!Strings.isNullOrEmpty(netName), "VTN network name cannot be null"); |
Hyunsun Moon | e0f3e28 | 2016-05-13 18:58:35 -0700 | [diff] [blame] | 197 | |
| 198 | String name = netName.toUpperCase(); |
Hyunsun Moon | 91ba41a | 2016-06-10 16:52:39 -0700 | [diff] [blame] | 199 | if (name.contains(VSG.name())) { |
Hyunsun Moon | e0f3e28 | 2016-05-13 18:58:35 -0700 | [diff] [blame] | 200 | return VSG; |
Hyunsun Moon | f1c0346 | 2016-06-21 18:06:01 -0700 | [diff] [blame] | 201 | } else if (name.contains(ACCESS_AGENT.name())) { |
| 202 | return ACCESS_AGENT; |
Hyunsun Moon | 91ba41a | 2016-06-10 16:52:39 -0700 | [diff] [blame] | 203 | } else if (name.contains(ServiceType.MANAGEMENT.name())) { |
| 204 | return ServiceType.MANAGEMENT; |
Hyunsun Moon | e0f3e28 | 2016-05-13 18:58:35 -0700 | [diff] [blame] | 205 | } else { |
Hyunsun Moon | 91ba41a | 2016-06-10 16:52:39 -0700 | [diff] [blame] | 206 | return DEFAULT; |
Hyunsun Moon | e0f3e28 | 2016-05-13 18:58:35 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 209 | } |