blob: baff814dfa06beaaefdfa60186eef7eaf525c9d4 [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;
28import org.openstack4j.model.network.IP;
29import org.openstack4j.model.network.Port;
30import org.openstack4j.openstack.OSFactory;
31import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
Hyunsun Moon4c396632016-05-13 04:17:53 -070033
Hyunsun Moone0f3e282016-05-13 18:58:35 -070034import java.util.Map;
Hyunsun Moon4c396632016-05-13 04:17:53 -070035import java.util.Set;
36
Hyunsun Moone0f3e282016-05-13 18:58:35 -070037import static com.google.common.base.Preconditions.checkNotNull;
38
Hyunsun Moon4c396632016-05-13 04:17:53 -070039/**
40 * Provides CORD VTN port APIs.
41 */
Hyunsun Moone0f3e282016-05-13 18:58:35 -070042public final class DefaultVtnPortApi extends XosApi implements VtnPortApi {
43
44 private final Logger log = LoggerFactory.getLogger(getClass());
Hyunsun Moon4c396632016-05-13 04:17:53 -070045
46 /**
47 * Default constructor.
48 *
49 * @param baseUrl base url
50 * @param access xos access
51 */
52 public DefaultVtnPortApi(String baseUrl, XosAccess access) {
53 super(baseUrl, access);
54 }
55
56 @Override
57 public Set<VtnPort> vtnPorts() {
58 // TODO implement this when XOS provides this information
59 return null;
60 }
61
62 @Override
63 public Set<VtnPort> vtnPorts(VtnServiceId serviceId) {
64 // TODO implement this when XOS provides this information
65 return null;
66 }
67
68 @Override
69 public VtnPort vtnPort(VtnPortId portId) {
70 // TODO implement this when XOS provides this information
71 return null;
72 }
Hyunsun Moone0f3e282016-05-13 18:58:35 -070073
74 @Override
75 // TODO remove this when XOS provides this information
76 public VtnPort vtnPort(String portName, OpenStackAccess osAccess) {
77 checkNotNull(osAccess);
78
79 OSClient osClient = getOpenStackClient(osAccess);
80 Port osPort = osClient.networking().port().list()
81 .stream()
82 .filter(p -> p.getId().contains(portName.substring(3)))
83 .findFirst().orElse(null);
84 if (osPort == null) {
85 log.warn("Failed to get OpenStack port for {}", portName);
86 return null;
87 }
88 return getVtnPort(osPort);
89 }
90
91 @Override
92 // TODO remove this when XOS provides this information
93 public VtnPort vtnPort(VtnPortId portId, OpenStackAccess osAccess) {
94 checkNotNull(osAccess);
95
96 OSClient osClient = getOpenStackClient(osAccess);
97 Port osPort = osClient.networking().port().get(portId.id());
98 if (osPort == null) {
99 log.warn("Failed to get OpenStack port {}", portId);
100 return null;
101 }
102 return getVtnPort(osPort);
103 }
104
105 // TODO remove this when XOS provides this information
106 private VtnPort getVtnPort(Port osPort) {
107 checkNotNull(osPort);
108
109 // assumes all vtn port has single IP address
110 IP ipAddr = osPort.getFixedIps().stream().findFirst().orElse(null);
111 if (ipAddr == null) {
112 log.warn("Failed to get IP address for {}", osPort);
113 return null;
114 }
115
116 Map<IpAddress, MacAddress> addressPairs = Maps.newHashMap();
117 osPort.getAllowedAddressPairs().stream().forEach(
118 pair -> addressPairs.put(IpAddress.valueOf(pair.getIpAddress()),
119 MacAddress.valueOf(pair.getMacAddress())));
120
121 return new VtnPort(VtnPortId.of(osPort.getId()),
122 osPort.getName(),
123 VtnServiceId.of(osPort.getNetworkId()),
124 MacAddress.valueOf(osPort.getMacAddress()),
125 IpAddress.valueOf(ipAddr.getIpAddress()),
126 addressPairs);
127 }
128
129 // TODO remove this when XOS provides this information
130 private OSClient getOpenStackClient(OpenStackAccess osAccess) {
131 checkNotNull(osAccess);
132
133 // creating a client every time must be inefficient, but this method
134 // will be removed once XOS provides equivalent APIs
135 return OSFactory.builder()
136 .endpoint(osAccess.endpoint())
137 .credentials(osAccess.user(), osAccess.password())
138 .tenantName(osAccess.tenant())
139 .authenticate();
140 }
Hyunsun Moon4c396632016-05-13 04:17:53 -0700141}