blob: aec3a84e5f82839bf2aebda390bd8f1935bdfffd [file] [log] [blame]
Hyunsun Moon7ad92202016-04-20 10:36:02 -07001/*
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 */
16package org.onosproject.xosclient.impl;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
Hyunsun Moone0f3e282016-05-13 18:58:35 -070020import com.google.common.base.Strings;
Hyunsun Moon7ad92202016-04-20 10:36:02 -070021import com.google.common.collect.Sets;
Hyunsun Moone0f3e282016-05-13 18:58:35 -070022import org.onlab.packet.IpAddress;
23import org.onlab.packet.IpPrefix;
24import org.onosproject.xosclient.api.OpenStackAccess;
Hyunsun Moon7ad92202016-04-20 10:36:02 -070025import org.onosproject.xosclient.api.VtnServiceApi;
26import org.onosproject.xosclient.api.XosAccess;
Hyunsun Moon4c396632016-05-13 04:17:53 -070027import org.onosproject.xosclient.api.VtnService;
28import org.onosproject.xosclient.api.VtnServiceId;
29
Hyunsun Moone0f3e282016-05-13 18:58:35 -070030import org.openstack4j.api.OSClient;
31import org.openstack4j.model.network.Network;
32import org.openstack4j.model.network.Subnet;
33import org.openstack4j.openstack.OSFactory;
Hyunsun Moon7ad92202016-04-20 10:36:02 -070034import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
36
37import java.io.IOException;
Hyunsun Moon7ad92202016-04-20 10:36:02 -070038import java.util.Set;
39
Hyunsun Moon7ad92202016-04-20 10:36:02 -070040import static com.google.common.base.Preconditions.checkNotNull;
Hyunsun Moone0f3e282016-05-13 18:58:35 -070041import static com.google.common.base.Preconditions.checkArgument;
Hyunsun Moon91ba41a2016-06-10 16:52:39 -070042import static org.onosproject.xosclient.api.VtnServiceApi.NetworkType.PRIVATE;
43import static org.onosproject.xosclient.api.VtnServiceApi.NetworkType.PUBLIC;
44import static org.onosproject.xosclient.api.VtnServiceApi.NetworkType.MANAGEMENT;
45import static org.onosproject.xosclient.api.VtnServiceApi.ServiceType.DEFAULT;
46import static org.onosproject.xosclient.api.VtnServiceApi.ServiceType.OLT_AGENT;
47import static org.onosproject.xosclient.api.VtnServiceApi.ServiceType.VSG;
Hyunsun Moon7ad92202016-04-20 10:36:02 -070048
49/**
50 * Provides CORD VTN service and service dependency APIs.
51 */
52public final class DefaultVtnServiceApi extends XosApi implements VtnServiceApi {
53
54 private final Logger log = LoggerFactory.getLogger(getClass());
55
Hyunsun Moon7ad92202016-04-20 10:36:02 -070056 /**
57 * Default constructor.
Hyunsun Moon4c396632016-05-13 04:17:53 -070058 *
59 * @param baseUrl base url
60 * @param access xos access
Hyunsun Moon7ad92202016-04-20 10:36:02 -070061 */
Hyunsun Moon4c396632016-05-13 04:17:53 -070062 public DefaultVtnServiceApi(String baseUrl, XosAccess access) {
Hyunsun Moon7ad92202016-04-20 10:36:02 -070063 super(baseUrl, access);
64 }
65
Hyunsun Moon7ad92202016-04-20 10:36:02 -070066 @Override
Hyunsun Moon4c396632016-05-13 04:17:53 -070067 public Set<VtnServiceId> services() {
Hyunsun Moon7ad92202016-04-20 10:36:02 -070068 String response = restGet(EMPTY_STRING);
69 log.trace("Get services {}", response);
70
71 ObjectMapper mapper = new ObjectMapper();
Hyunsun Moon4c396632016-05-13 04:17:53 -070072 Set<VtnServiceId> services = Sets.newHashSet();
73
Hyunsun Moon7ad92202016-04-20 10:36:02 -070074 try {
75 JsonNode nodes = mapper.readTree(response);
Hyunsun Moon4c396632016-05-13 04:17:53 -070076 nodes.fieldNames().forEachRemaining(id -> services.add(VtnServiceId.of(id)));
Hyunsun Moon7ad92202016-04-20 10:36:02 -070077 } catch (IOException e) {
78 log.warn("Failed to get service list");
Hyunsun Moon7ad92202016-04-20 10:36:02 -070079 }
Hyunsun Moon4c396632016-05-13 04:17:53 -070080 return services;
Hyunsun Moon7ad92202016-04-20 10:36:02 -070081 }
82
83 @Override
Hyunsun Moon4c396632016-05-13 04:17:53 -070084 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 Moon7ad92202016-04-20 10:36:02 -070091 checkNotNull(tServiceId);
92
Hyunsun Moon4c396632016-05-13 04:17:53 -070093 String response = restGet(tServiceId.id());
Hyunsun Moon7ad92202016-04-20 10:36:02 -070094 log.trace("Get provider services {}", response);
95
96 ObjectMapper mapper = new ObjectMapper();
Hyunsun Moon4c396632016-05-13 04:17:53 -070097 Set<VtnServiceId> pServices = Sets.newHashSet();
Hyunsun Moon7ad92202016-04-20 10:36:02 -070098
99 try {
100 JsonNode nodes = mapper.readTree(response);
Hyunsun Moon4c396632016-05-13 04:17:53 -0700101 nodes.forEach(node -> pServices.add(VtnServiceId.of(node.asText())));
Hyunsun Moon7ad92202016-04-20 10:36:02 -0700102 } catch (IOException e) {
103 log.warn("Failed to get service dependency");
104 }
105 return pServices;
106 }
107
108 @Override
Hyunsun Moon4c396632016-05-13 04:17:53 -0700109 public Set<VtnServiceId> tenantServices(VtnServiceId tServiceId) {
Hyunsun Moon7ad92202016-04-20 10:36:02 -0700110 checkNotNull(tServiceId);
111
112 String response = restGet(EMPTY_STRING);
113 log.trace("Get tenant services {}", response);
114
115 ObjectMapper mapper = new ObjectMapper();
Hyunsun Moon4c396632016-05-13 04:17:53 -0700116 Set<VtnServiceId> tServices = Sets.newHashSet();
117
Hyunsun Moon7ad92202016-04-20 10:36:02 -0700118 try {
119 JsonNode nodes = mapper.readTree(response);
Hyunsun Moon4c396632016-05-13 04:17:53 -0700120 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 Moon7ad92202016-04-20 10:36:02 -0700126 } catch (IOException e) {
127 log.warn("Failed to get service list");
128 }
129 return tServices;
130 }
Hyunsun Moone0f3e282016-05-13 18:58:35 -0700131
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 Moon91ba41a2016-06-10 16:52:39 -0700181 if (name.contains(PUBLIC.name())) {
Hyunsun Moone0f3e282016-05-13 18:58:35 -0700182 return PUBLIC;
Hyunsun Moon91ba41a2016-06-10 16:52:39 -0700183 } else if (name.contains(MANAGEMENT.name())) {
Hyunsun Moone0f3e282016-05-13 18:58:35 -0700184 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 Moon91ba41a2016-06-10 16:52:39 -0700195 if (name.contains(VSG.name())) {
Hyunsun Moone0f3e282016-05-13 18:58:35 -0700196 return VSG;
Hyunsun Moon91ba41a2016-06-10 16:52:39 -0700197 } else if (name.contains(OLT_AGENT.name())) {
Hyunsun Moone0f3e282016-05-13 18:58:35 -0700198 return OLT_AGENT;
Hyunsun Moon91ba41a2016-06-10 16:52:39 -0700199 } else if (name.contains(ServiceType.MANAGEMENT.name())) {
200 return ServiceType.MANAGEMENT;
Hyunsun Moone0f3e282016-05-13 18:58:35 -0700201 } else {
Hyunsun Moon91ba41a2016-06-10 16:52:39 -0700202 return DEFAULT;
Hyunsun Moone0f3e282016-05-13 18:58:35 -0700203 }
204 }
Hyunsun Moon7ad92202016-04-20 10:36:02 -0700205}