blob: 1061b27860cb69c0364279ae9db2c79ddc56d9f6 [file] [log] [blame]
Hyunsun Moon4c396632016-05-13 04:17:53 -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
Hyunsun Moone0f3e282016-05-13 18:58:35 -070018import com.google.common.collect.Maps;
19import org.onlab.packet.IpAddress;
20import org.onlab.packet.MacAddress;
21import org.onosproject.xosclient.api.OpenStackAccess;
Hyunsun Moon4c396632016-05-13 04:17:53 -070022import org.onosproject.xosclient.api.VtnPort;
23import org.onosproject.xosclient.api.VtnPortApi;
24import org.onosproject.xosclient.api.VtnPortId;
25import org.onosproject.xosclient.api.VtnServiceId;
26import org.onosproject.xosclient.api.XosAccess;
Hyunsun Moone0f3e282016-05-13 18:58:35 -070027import org.openstack4j.api.OSClient;
Hyunsun Moond0bfe672016-06-30 10:55:10 -070028import org.openstack4j.api.exceptions.AuthenticationException;
Hyunsun Moone0f3e282016-05-13 18:58:35 -070029import org.openstack4j.model.network.IP;
30import org.openstack4j.model.network.Port;
31import org.openstack4j.openstack.OSFactory;
32import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
Hyunsun Moon4c396632016-05-13 04:17:53 -070034
Hyunsun Moone0f3e282016-05-13 18:58:35 -070035import java.util.Map;
Hyunsun Moon4c396632016-05-13 04:17:53 -070036import java.util.Set;
37
Hyunsun Moone0f3e282016-05-13 18:58:35 -070038import static com.google.common.base.Preconditions.checkNotNull;
39
Hyunsun Moon4c396632016-05-13 04:17:53 -070040/**
41 * Provides CORD VTN port APIs.
42 */
Hyunsun Moone0f3e282016-05-13 18:58:35 -070043public final class DefaultVtnPortApi extends XosApi implements VtnPortApi {
44
45 private final Logger log = LoggerFactory.getLogger(getClass());
Hyunsun Moon4c396632016-05-13 04:17:53 -070046
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 Moone0f3e282016-05-13 18:58:35 -070074
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 SHIMIZUa09e1bb2016-08-01 14:25:25 -0700118 osPort.getAllowedAddressPairs().forEach(
Hyunsun Moone0f3e282016-05-13 18:58:35 -0700119 pair -> addressPairs.put(IpAddress.valueOf(pair.getIpAddress()),
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700120 MacAddress.valueOf(pair.getMacAddress())));
Hyunsun Moone0f3e282016-05-13 18:58:35 -0700121
Hyunsun Moon65040992016-07-27 18:41:34 -0700122 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 Moone0f3e282016-05-13 18:58:35 -0700130 }
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 Moond0bfe672016-06-30 10:55:10 -0700138 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 Moone0f3e282016-05-13 18:58:35 -0700148 }
Hyunsun Moon4c396632016-05-13 04:17:53 -0700149}