Daniel Park
Committed by Gerrit Code Review

Fix to update floating IP association/disassociation in OpenstackInterfaceServce

Change-Id: I04b96109296125849bc91d5bc92e476338749935
...@@ -15,9 +15,11 @@ ...@@ -15,9 +15,11 @@
15 */ 15 */
16 package org.onosproject.openstackinterface; 16 package org.onosproject.openstackinterface;
17 17
18 +import org.onlab.packet.Ip4Address;
18 import org.onosproject.net.Port; 19 import org.onosproject.net.Port;
19 20
20 import java.util.Collection; 21 import java.util.Collection;
22 +import java.util.Optional;
21 23
22 /** 24 /**
23 * Handles port management REST API from Openstack for VMs. 25 * Handles port management REST API from Openstack for VMs.
...@@ -49,7 +51,7 @@ public interface OpenstackInterfaceService { ...@@ -49,7 +51,7 @@ public interface OpenstackInterfaceService {
49 /** 51 /**
50 * Returns port information for the port ID given. 52 * Returns port information for the port ID given.
51 * 53 *
52 - * @param portId Port ID 54 + * @param portId port id
53 * @return port information 55 * @return port information
54 */ 56 */
55 OpenstackPort port(String portId); 57 OpenstackPort port(String portId);
...@@ -57,7 +59,7 @@ public interface OpenstackInterfaceService { ...@@ -57,7 +59,7 @@ public interface OpenstackInterfaceService {
57 /** 59 /**
58 * Returns network information list for the network ID given. 60 * Returns network information list for the network ID given.
59 * 61 *
60 - * @param networkId Network ID 62 + * @param networkId network id
61 * @return network information, or null if not present 63 * @return network information, or null if not present
62 */ 64 */
63 OpenstackNetwork network(String networkId); 65 OpenstackNetwork network(String networkId);
...@@ -72,7 +74,7 @@ public interface OpenstackInterfaceService { ...@@ -72,7 +74,7 @@ public interface OpenstackInterfaceService {
72 /** 74 /**
73 * Returns subnet information for the subnet ID give. 75 * Returns subnet information for the subnet ID give.
74 * 76 *
75 - * @param subnetId Subnet ID 77 + * @param subnetId subnet id
76 * @return subnet information, or null if not present 78 * @return subnet information, or null if not present
77 */ 79 */
78 OpenstackSubnet subnet(String subnetId); 80 OpenstackSubnet subnet(String subnetId);
...@@ -94,7 +96,7 @@ public interface OpenstackInterfaceService { ...@@ -94,7 +96,7 @@ public interface OpenstackInterfaceService {
94 /** 96 /**
95 * Returns the router information for the router ID given. 97 * Returns the router information for the router ID given.
96 * 98 *
97 - * @param routerId router ID 99 + * @param routerId router id
98 * @return router information 100 * @return router information
99 */ 101 */
100 OpenstackRouter router(String routerId); 102 OpenstackRouter router(String routerId);
...@@ -114,6 +116,16 @@ public interface OpenstackInterfaceService { ...@@ -114,6 +116,16 @@ public interface OpenstackInterfaceService {
114 */ 116 */
115 Collection<OpenstackFloatingIP> floatingIps(); 117 Collection<OpenstackFloatingIP> floatingIps();
116 118
119 + /**
120 + * Updates a floating IP and its association with an internal port.
121 + *
122 + * @param id floating ip id
123 + * @param portId port id
124 + * @param fixedIpAddress fixed ip address of the port
125 + * @return true if the update succeed
126 + */
127 + boolean updateFloatingIp(String id, String portId, Optional<Ip4Address> fixedIpAddress);
128 +
117 129
118 130
119 } 131 }
......
...@@ -83,5 +83,10 @@ ...@@ -83,5 +83,10 @@
83 <groupId>org.onosproject</groupId> 83 <groupId>org.onosproject</groupId>
84 <artifactId>onlab-misc</artifactId> 84 <artifactId>onlab-misc</artifactId>
85 </dependency> 85 </dependency>
86 + <dependency>
87 + <groupId>commons-io</groupId>
88 + <artifactId>commons-io</artifactId>
89 + <version>2.4</version>
90 + </dependency>
86 </dependencies> 91 </dependencies>
87 </project> 92 </project>
......
...@@ -21,6 +21,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode; ...@@ -21,6 +21,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
21 import com.google.common.base.Strings; 21 import com.google.common.base.Strings;
22 import com.google.common.collect.ImmutableSet; 22 import com.google.common.collect.ImmutableSet;
23 import com.google.common.collect.Lists; 23 import com.google.common.collect.Lists;
24 +import org.apache.commons.io.IOUtils;
24 import org.apache.felix.scr.annotations.Activate; 25 import org.apache.felix.scr.annotations.Activate;
25 import org.apache.felix.scr.annotations.Component; 26 import org.apache.felix.scr.annotations.Component;
26 import org.apache.felix.scr.annotations.Deactivate; 27 import org.apache.felix.scr.annotations.Deactivate;
...@@ -28,6 +29,7 @@ import org.apache.felix.scr.annotations.Reference; ...@@ -28,6 +29,7 @@ import org.apache.felix.scr.annotations.Reference;
28 import org.apache.felix.scr.annotations.ReferenceCardinality; 29 import org.apache.felix.scr.annotations.ReferenceCardinality;
29 import org.apache.felix.scr.annotations.Service; 30 import org.apache.felix.scr.annotations.Service;
30 import org.glassfish.jersey.client.ClientProperties; 31 import org.glassfish.jersey.client.ClientProperties;
32 +import org.onlab.packet.Ip4Address;
31 import org.onosproject.core.ApplicationId; 33 import org.onosproject.core.ApplicationId;
32 import org.onosproject.core.CoreService; 34 import org.onosproject.core.CoreService;
33 import org.onosproject.net.Port; 35 import org.onosproject.net.Port;
...@@ -57,7 +59,11 @@ import javax.ws.rs.client.Entity; ...@@ -57,7 +59,11 @@ import javax.ws.rs.client.Entity;
57 import javax.ws.rs.client.Invocation; 59 import javax.ws.rs.client.Invocation;
58 import javax.ws.rs.client.WebTarget; 60 import javax.ws.rs.client.WebTarget;
59 import javax.ws.rs.core.MediaType; 61 import javax.ws.rs.core.MediaType;
62 +import javax.ws.rs.core.Response;
63 +import java.io.ByteArrayInputStream;
60 import java.io.IOException; 64 import java.io.IOException;
65 +import java.io.InputStream;
66 +import java.nio.charset.StandardCharsets;
61 import java.text.ParseException; 67 import java.text.ParseException;
62 import java.text.SimpleDateFormat; 68 import java.text.SimpleDateFormat;
63 import java.util.Calendar; 69 import java.util.Calendar;
...@@ -65,6 +71,7 @@ import java.util.Collection; ...@@ -65,6 +71,7 @@ import java.util.Collection;
65 import java.util.Collections; 71 import java.util.Collections;
66 import java.util.Date; 72 import java.util.Date;
67 import java.util.List; 73 import java.util.List;
74 +import java.util.Optional;
68 import java.util.Set; 75 import java.util.Set;
69 import java.util.concurrent.ExecutorService; 76 import java.util.concurrent.ExecutorService;
70 import java.util.concurrent.Executors; 77 import java.util.concurrent.Executors;
...@@ -91,6 +98,9 @@ public class OpenstackInterfaceManager implements OpenstackInterfaceService { ...@@ -91,6 +98,9 @@ public class OpenstackInterfaceManager implements OpenstackInterfaceService {
91 private static final String URI_SECURITY_GROUPS = "security-groups"; 98 private static final String URI_SECURITY_GROUPS = "security-groups";
92 private static final String URI_FLOATINGIPS = "floatingips"; 99 private static final String URI_FLOATINGIPS = "floatingips";
93 private static final String URI_TOKENS = "tokens"; 100 private static final String URI_TOKENS = "tokens";
101 + private static final String FLOATINGIP = "floatingip";
102 + private static final String PORT_ID = "port_id";
103 + private static final String FIXED_IP_ADDRESS = "fixed_ip_address";
94 104
95 private static final String PATH_ROUTERS = "routers"; 105 private static final String PATH_ROUTERS = "routers";
96 private static final String PATH_NETWORKS = "networks"; 106 private static final String PATH_NETWORKS = "networks";
...@@ -485,6 +495,63 @@ public class OpenstackInterfaceManager implements OpenstackInterfaceService { ...@@ -485,6 +495,63 @@ public class OpenstackInterfaceManager implements OpenstackInterfaceService {
485 return openstackFloatingIPs; 495 return openstackFloatingIPs;
486 } 496 }
487 497
498 + @Override
499 + public boolean updateFloatingIp(String id, String portId, Optional<Ip4Address> fixedIpAddress) {
500 + Invocation.Builder builder = getClientBuilder(neutronUrl, URI_FLOATINGIPS + "/" + id);
501 +
502 + if (builder == null || (portId != null && !fixedIpAddress.isPresent())) {
503 + log.warn("Failed to update floating IP");
504 + return false;
505 + }
506 +
507 + ObjectNode objectNode = createFloatingIpObject(portId, fixedIpAddress);
508 +
509 + InputStream inputStream = new ByteArrayInputStream(objectNode.toString().getBytes());
510 +
511 + try {
512 + Response response = builder.header(HEADER_AUTH_TOKEN, getToken())
513 + .put(Entity.entity(IOUtils.toString(inputStream, StandardCharsets.UTF_8),
514 + MediaType.APPLICATION_JSON));
515 + log.debug("updateFloatingIp called: {}, status: {}", response.readEntity(String.class),
516 + String.valueOf(response.getStatus()));
517 +
518 + return checkReply(response);
519 + } catch (IOException e) {
520 + log.error("Cannot do PUT {} request");
521 + return false;
522 + }
523 + }
524 +
525 + private ObjectNode createFloatingIpObject(String portId, Optional<Ip4Address> fixedIpAddress) {
526 + ObjectMapper mapper = new ObjectMapper();
527 + ObjectNode objectNode = mapper.createObjectNode();
528 +
529 + objectNode.putObject(FLOATINGIP)
530 + .put(PORT_ID, portId);
531 +
532 + if (portId != null) {
533 + objectNode.put(FIXED_IP_ADDRESS, fixedIpAddress.get().toString());
534 + }
535 +
536 + return objectNode;
537 + }
538 +
539 + private boolean checkReply(Response response) {
540 + if (response != null) {
541 + return checkStatusCode(response.getStatus());
542 + }
543 +
544 + log.warn("Null floating IP response from openstack");
545 + return false;
546 + }
547 +
548 + private boolean checkStatusCode(int statusCode) {
549 + if (statusCode == Response.Status.OK.getStatusCode()) {
550 + return true;
551 + }
552 +
553 + return false;
554 + }
488 private void configureNetwork() { 555 private void configureNetwork() {
489 OpenstackInterfaceConfig cfg = 556 OpenstackInterfaceConfig cfg =
490 cfgService.getConfig(appId, OpenstackInterfaceConfig.class); 557 cfgService.getConfig(appId, OpenstackInterfaceConfig.class);
......