sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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.openstackswitching; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import com.google.common.collect.Lists; |
| 22 | import com.sun.jersey.api.client.Client; |
| 23 | import com.sun.jersey.api.client.WebResource; |
| 24 | import org.onosproject.openstackswitching.web.OpenstackNetworkCodec; |
| 25 | import org.onosproject.openstackswitching.web.OpenstackPortCodec; |
| 26 | import org.onosproject.openstackswitching.web.OpenstackSubnetCodec; |
| 27 | import org.slf4j.Logger; |
| 28 | import javax.ws.rs.core.MediaType; |
| 29 | import java.io.IOException; |
| 30 | import java.util.Collection; |
| 31 | import java.util.List; |
| 32 | |
| 33 | import static com.google.common.base.Preconditions.checkNotNull; |
| 34 | import static com.google.common.net.MediaType.JSON_UTF_8; |
| 35 | import static org.slf4j.LoggerFactory.getLogger; |
| 36 | |
| 37 | /** |
| 38 | * Handles REST Calls to Openstack Neutron. |
| 39 | * |
| 40 | */ |
| 41 | public class OpenstackRestHandler { |
| 42 | |
| 43 | private final Logger log = getLogger(getClass()); |
| 44 | private String neutronUrl; |
| 45 | private String keystoneUrl; |
| 46 | private String tokenId; |
| 47 | private String userName; |
| 48 | private String pass; |
| 49 | |
| 50 | /** |
| 51 | * Creates OpenstackRestHandler instance. |
| 52 | * |
| 53 | * @param cfg OpenstackSwitchingConfig reference |
| 54 | */ |
| 55 | public OpenstackRestHandler(OpenstackSwitchingConfig cfg) { |
| 56 | this.neutronUrl = checkNotNull(cfg.neutronServer()); |
| 57 | this.keystoneUrl = checkNotNull(cfg.keystoneServer()); |
| 58 | this.userName = checkNotNull(cfg.userName()); |
| 59 | this.pass = checkNotNull(cfg.password()); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Returns network information stored in Neutron. |
| 64 | * |
| 65 | * @return List of OpenstackNetwork |
| 66 | */ |
| 67 | public Collection<OpenstackNetwork> getNetworks() { |
| 68 | |
| 69 | WebResource.Builder builder = getClientBuilder(neutronUrl + "networks"); |
| 70 | String response = builder.accept(MediaType.APPLICATION_JSON_TYPE). |
| 71 | header("X-Auth-Token", getToken()).get(String.class); |
| 72 | |
| 73 | ObjectMapper mapper = new ObjectMapper(); |
| 74 | List<OpenstackNetwork> openstackNetworks = Lists.newArrayList(); |
| 75 | try { |
| 76 | ObjectNode node = (ObjectNode) mapper.readTree(response); |
| 77 | ArrayNode networkList = (ArrayNode) node.path("networks"); |
| 78 | OpenstackNetworkCodec networkCodec = new OpenstackNetworkCodec(); |
| 79 | networkList.forEach(n -> openstackNetworks.add(networkCodec.decode((ObjectNode) n, null))); |
| 80 | } catch (IOException e) { |
| 81 | e.printStackTrace(); |
| 82 | } |
| 83 | |
| 84 | log.debug("networks response:" + response); |
| 85 | openstackNetworks.forEach(n -> log.debug("network ID: {}", n.id())); |
| 86 | |
| 87 | return openstackNetworks; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Returns port information stored in Neutron. |
| 92 | * |
| 93 | * @return List of OpenstackPort |
| 94 | */ |
| 95 | public Collection<OpenstackPort> getPorts() { |
| 96 | |
| 97 | WebResource.Builder builder = getClientBuilder(neutronUrl + "ports"); |
| 98 | String response = builder.accept(MediaType.APPLICATION_JSON_TYPE). |
| 99 | header("X-Auth-Token", getToken()).get(String.class); |
| 100 | |
| 101 | ObjectMapper mapper = new ObjectMapper(); |
| 102 | List<OpenstackPort> openstackPorts = Lists.newArrayList(); |
| 103 | try { |
| 104 | ObjectNode node = (ObjectNode) mapper.readTree(response); |
| 105 | ArrayNode portList = (ArrayNode) node.path("ports"); |
| 106 | OpenstackPortCodec portCodec = new OpenstackPortCodec(); |
| 107 | portList.forEach(p -> openstackPorts.add(portCodec.decode((ObjectNode) p, null))); |
| 108 | } catch (IOException e) { |
| 109 | e.printStackTrace(); |
| 110 | } |
| 111 | |
| 112 | log.debug("port response:" + response); |
| 113 | openstackPorts.forEach(n -> log.debug("port ID: {}", n.id())); |
| 114 | |
| 115 | return openstackPorts; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Returns Subnet information in Neutron. |
| 120 | * |
| 121 | * @return List of OpenstackSubnet |
| 122 | */ |
| 123 | public Collection<OpenstackSubnet> getSubnets() { |
| 124 | |
| 125 | WebResource.Builder builder = getClientBuilder(neutronUrl + "subnets"); |
| 126 | String response = builder.accept(MediaType.APPLICATION_JSON_TYPE). |
| 127 | header("X-Auth-Token", getToken()).get(String.class); |
| 128 | |
| 129 | ObjectMapper mapper = new ObjectMapper(); |
| 130 | List<OpenstackSubnet> subnets = Lists.newArrayList(); |
| 131 | try { |
| 132 | ObjectNode node = (ObjectNode) mapper.readTree(response); |
| 133 | ArrayNode subnetList = (ArrayNode) node.path("subnets"); |
| 134 | OpenstackSubnetCodec subnetCodec = new OpenstackSubnetCodec(); |
| 135 | subnetList.forEach(s -> subnets.add(subnetCodec.decode((ObjectNode) s, null))); |
| 136 | } catch (IOException e) { |
| 137 | e.printStackTrace(); |
| 138 | } |
| 139 | |
| 140 | log.debug("subnets response:" + response); |
| 141 | subnets.forEach(s -> log.debug("subnet ID: {}", s.id())); |
| 142 | |
| 143 | return subnets; |
| 144 | } |
| 145 | |
| 146 | private WebResource.Builder getClientBuilder(String uri) { |
| 147 | Client client = Client.create(); |
| 148 | WebResource resource = client.resource(uri); |
| 149 | return resource.accept(JSON_UTF_8.toString()) |
| 150 | .type(JSON_UTF_8.toString()); |
| 151 | } |
| 152 | |
| 153 | private String getToken() { |
| 154 | if (isTokenInvalid()) { |
| 155 | String request = "{\"auth\": {\"tenantName\": \"admin\", " + |
| 156 | "\"passwordCredentials\": {\"username\": \"" + |
| 157 | userName + "\",\"password\": \"" + pass + "\"}}}"; |
| 158 | WebResource.Builder builder = getClientBuilder(keystoneUrl + "tokens"); |
| 159 | String response = builder.accept(MediaType.APPLICATION_JSON).post(String.class, request); |
| 160 | |
| 161 | ObjectMapper mapper = new ObjectMapper(); |
| 162 | try { |
| 163 | ObjectNode node = (ObjectNode) mapper.readTree(response); |
| 164 | tokenId = node.path("access").path("token").path("id").asText(); |
| 165 | } catch (IOException e) { |
| 166 | e.printStackTrace(); |
| 167 | } |
| 168 | log.debug("token response:" + response); |
| 169 | } |
| 170 | |
| 171 | return tokenId; |
| 172 | } |
| 173 | |
| 174 | private boolean isTokenInvalid() { |
| 175 | //TODO: validation check for the existing token |
| 176 | return true; |
| 177 | } |
| 178 | |
| 179 | } |