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