blob: a7329d4cbcb6003da1cd589ed3eb810c374a7c11 [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();
118 osPort.getAllowedAddressPairs().stream().forEach(
119 pair -> addressPairs.put(IpAddress.valueOf(pair.getIpAddress()),
120 MacAddress.valueOf(pair.getMacAddress())));
121
122 return new VtnPort(VtnPortId.of(osPort.getId()),
123 osPort.getName(),
124 VtnServiceId.of(osPort.getNetworkId()),
125 MacAddress.valueOf(osPort.getMacAddress()),
126 IpAddress.valueOf(ipAddr.getIpAddress()),
127 addressPairs);
128 }
129
130 // TODO remove this when XOS provides this information
131 private OSClient getOpenStackClient(OpenStackAccess osAccess) {
132 checkNotNull(osAccess);
133
134 // creating a client every time must be inefficient, but this method
135 // will be removed once XOS provides equivalent APIs
Hyunsun Moond0bfe672016-06-30 10:55:10 -0700136 try {
137 return OSFactory.builder()
138 .endpoint(osAccess.endpoint())
139 .credentials(osAccess.user(), osAccess.password())
140 .tenantName(osAccess.tenant())
141 .authenticate();
142 } catch (AuthenticationException e) {
143 log.warn("Failed to authenticate OpenStack API with {}", osAccess);
144 return null;
145 }
Hyunsun Moone0f3e282016-05-13 18:58:35 -0700146 }
Hyunsun Moon4c396632016-05-13 04:17:53 -0700147}