diff options
Diffstat (limited to 'framework/src/onos/apps/openstackswitching/app')
17 files changed, 1851 insertions, 0 deletions
diff --git a/framework/src/onos/apps/openstackswitching/app/app.xml b/framework/src/onos/apps/openstackswitching/app/app.xml new file mode 100644 index 00000000..e982b90d --- /dev/null +++ b/framework/src/onos/apps/openstackswitching/app/app.xml @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ Copyright 2015 Open Networking Laboratory + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<app name="org.onosproject.openstackswitching" origin="ON.Lab" version="${project.version}" + featuresRepo="mvn:${project.groupId}/${project.artifactId}/${project.version}/xml/features" + features="${project.artifactId}"> + <description>${project.description}</description> + <artifact>mvn:${project.groupId}/onos-app-openstackswitching/${project.version}</artifact> + <artifact>mvn:${project.groupId}/onos-app-dhcp-api/${project.version}</artifact> + <artifact>mvn:${project.groupId}/onos-app-dhcp/${project.version}</artifact> +</app> diff --git a/framework/src/onos/apps/openstackswitching/app/features.xml b/framework/src/onos/apps/openstackswitching/app/features.xml new file mode 100644 index 00000000..acb07b62 --- /dev/null +++ b/framework/src/onos/apps/openstackswitching/app/features.xml @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="UTF-8" standalone="yes"?> +<!-- + ~ Copyright 2015 Open Networking Laboratory + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<features xmlns="http://karaf.apache.org/xmlns/features/v1.2.0" name="${project.artifactId}-${project.version}"> + <repository>mvn:${project.groupId}/${project.artifactId}/${project.version}/xml/features</repository> + <feature name="${project.artifactId}" version="${project.version}" + description="${project.description}"> + <feature>onos-api</feature> + <bundle>mvn:${project.groupId}/onos-app-openstackswitching/${project.version}</bundle> + <bundle>mvn:${project.groupId}/onos-app-dhcp-api/${project.version}</bundle> + <bundle>mvn:${project.groupId}/onos-app-dhcp/${project.version}</bundle> + <bundle>mvn:com.sun.jersey/jersey-client/1.19</bundle> + <bundle>mvn:${project.groupId}/${project.artifactId}/${project.version}</bundle> + </feature> +</features> diff --git a/framework/src/onos/apps/openstackswitching/app/pom.xml b/framework/src/onos/apps/openstackswitching/app/pom.xml new file mode 100644 index 00000000..5460faef --- /dev/null +++ b/framework/src/onos/apps/openstackswitching/app/pom.xml @@ -0,0 +1,137 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ Copyright 2015 Open Networking Laboratory + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.onosproject</groupId> + <artifactId>onos-openstackswitching</artifactId> + <version>1.4.0-SNAPSHOT</version> + <relativePath>../pom.xml</relativePath> + </parent> + + <artifactId>onos-app-openstackswitching</artifactId> + <packaging>bundle</packaging> + + <description>SONA Openstack Switching applications</description> + <properties> + <onos.version>1.4.0-SNAPSHOT</onos.version> + <onos.app.name>org.onosproject.openstackswitching</onos.app.name> + <web.context>/onos/openstackswitching</web.context> + <api.version>1.0.0</api.version> + <api.title>ONOS OpenStack Switching REST API</api.title> + <api.description> + APIs for receiving Neutron information. + </api.description> + <api.package>org.onosproject.openstackswitching.web</api.package> + <onos.app.origin>SKT, Inc.</onos.app.origin> + </properties> + + + <dependencies> + <dependency> + <groupId>org.onosproject</groupId> + <artifactId>onos-app-openstackswitching-api</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.onosproject</groupId> + <artifactId>onos-rest</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.onosproject</groupId> + <artifactId>onlab-rest</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>javax.ws.rs</groupId> + <artifactId>jsr311-api</artifactId> + <version>1.1.1</version> + </dependency> + <dependency> + <groupId>com.sun.jersey</groupId> + <artifactId>jersey-servlet</artifactId> + </dependency> + <dependency> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-databind</artifactId> + </dependency> + <dependency> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-annotations</artifactId> + </dependency> + <dependency> + <groupId>org.osgi</groupId> + <artifactId>org.osgi.compendium</artifactId> + </dependency> + <dependency> + <groupId>org.osgi</groupId> + <artifactId>org.osgi.core</artifactId> + </dependency> + <dependency> + <groupId>org.onosproject</groupId> + <artifactId>onos-app-dhcp-api</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>com.sun.jersey</groupId> + <artifactId>jersey-client</artifactId> + <version>1.19</version> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + <extensions>true</extensions> + <configuration> + <instructions> + <_wab>src/main/webapp/</_wab> + <Bundle-SymbolicName> + ${project.groupId}.${project.artifactId} + </Bundle-SymbolicName> + <Import-Package> + org.slf4j, + org.osgi.framework, + javax.ws.rs, + javax.ws.rs.core, + com.sun.jersey.api.core, + com.sun.jersey.api.client, + com.sun.jersey.spi.container.servlet, + com.sun.jersey.server.impl.container.servlet, + com.fasterxml.jackson.databind, + com.fasterxml.jackson.databind.node, + com.fasterxml.jackson.core, + org.apache.karaf.shell.commands, + com.google.common.*, + org.onlab.packet.*, + org.onosproject.* + </Import-Package> + <Web-ContextPath>${web.context}</Web-ContextPath> + </instructions> + </configuration> + </plugin> + </plugins> + </build> + + +</project> diff --git a/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/OpenstackArpHandler.java b/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/OpenstackArpHandler.java new file mode 100644 index 00000000..944d12a1 --- /dev/null +++ b/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/OpenstackArpHandler.java @@ -0,0 +1,108 @@ +/* +* Copyright 2015 Open Networking Laboratory +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +package org.onosproject.openstackswitching; + +import org.onlab.packet.ARP; +import org.onlab.packet.Ethernet; +import org.onlab.packet.Ip4Address; +import org.onlab.packet.MacAddress; +import org.onosproject.net.flow.DefaultTrafficTreatment; +import org.onosproject.net.flow.TrafficTreatment; +import org.onosproject.net.packet.DefaultOutboundPacket; +import org.onosproject.net.packet.InboundPacket; +import org.onosproject.net.packet.OutboundPacket; +import org.onosproject.net.packet.PacketService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.nio.ByteBuffer; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * Handles ARP packet from VMs. + */ +public class OpenstackArpHandler { + + private static Logger log = LoggerFactory + .getLogger(OpenstackArpHandler.class); + private PacketService packetService; + private OpenstackRestHandler restHandler; + + /** + * Returns OpenstackArpHandler reference. + * + * @param restHandler rest API handler reference + * @param packetService PacketService reference + */ + public OpenstackArpHandler(OpenstackRestHandler restHandler, PacketService packetService) { + this.restHandler = checkNotNull(restHandler); + this.packetService = packetService; + } + + /** + * Processes ARP packets. + * + * @param pkt ARP request packet + */ + public void processPacketIn(InboundPacket pkt) { + Ethernet ethernet = pkt.parsed(); + ARP arp = (ARP) ethernet.getPayload(); + + if (arp.getOpCode() == ARP.OP_REQUEST) { + byte[] srcMacAddress = arp.getSenderHardwareAddress(); + byte[] srcIPAddress = arp.getSenderProtocolAddress(); + byte[] dstIPAddress = arp.getTargetProtocolAddress(); + + //Searches the Dst MAC Address based on openstackPortMap + MacAddress macAddress = null; + + OpenstackPort openstackPort = restHandler.getPorts().stream(). + filter(e -> e.fixedIps().containsValue(Ip4Address.valueOf( + dstIPAddress))).findAny().orElse(null); + + if (openstackPort != null) { + macAddress = openstackPort.macAddress(); + log.debug("Found MACAddress: {}", macAddress.toString()); + } else { + return; + } + + //Creates a response packet + ARP arpReply = new ARP(); + arpReply.setOpCode(ARP.OP_REPLY) + .setHardwareAddressLength(arp.getHardwareAddressLength()) + .setHardwareType(arp.getHardwareType()) + .setProtocolAddressLength(arp.getProtocolAddressLength()) + .setProtocolType(arp.getProtocolType()) + .setSenderHardwareAddress(macAddress.toBytes()) + .setSenderProtocolAddress(dstIPAddress) + .setTargetHardwareAddress(srcMacAddress) + .setTargetProtocolAddress(srcIPAddress); + + //Sends a response packet + ethernet.setDestinationMACAddress(srcMacAddress) + .setSourceMACAddress(macAddress) + .setEtherType(Ethernet.TYPE_ARP) + .setPayload(arpReply); + + TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder(); + builder.setOutput(pkt.receivedFrom().port()); + OutboundPacket packet = new DefaultOutboundPacket(pkt.receivedFrom().deviceId(), + builder.build(), ByteBuffer.wrap(ethernet.serialize())); + packetService.emit(packet); + } + } +} diff --git a/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/OpenstackRestHandler.java b/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/OpenstackRestHandler.java new file mode 100644 index 00000000..9065bc52 --- /dev/null +++ b/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/OpenstackRestHandler.java @@ -0,0 +1,179 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.openstackswitching; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.google.common.collect.Lists; +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.WebResource; +import org.onosproject.openstackswitching.web.OpenstackNetworkCodec; +import org.onosproject.openstackswitching.web.OpenstackPortCodec; +import org.onosproject.openstackswitching.web.OpenstackSubnetCodec; +import org.slf4j.Logger; +import javax.ws.rs.core.MediaType; +import java.io.IOException; +import java.util.Collection; +import java.util.List; + +import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.net.MediaType.JSON_UTF_8; +import static org.slf4j.LoggerFactory.getLogger; + +/** + * Handles REST Calls to Openstack Neutron. + * + */ +public class OpenstackRestHandler { + + private final Logger log = getLogger(getClass()); + private String neutronUrl; + private String keystoneUrl; + private String tokenId; + private String userName; + private String pass; + + /** + * Creates OpenstackRestHandler instance. + * + * @param cfg OpenstackSwitchingConfig reference + */ + public OpenstackRestHandler(OpenstackSwitchingConfig cfg) { + this.neutronUrl = checkNotNull(cfg.neutronServer()); + this.keystoneUrl = checkNotNull(cfg.keystoneServer()); + this.userName = checkNotNull(cfg.userName()); + this.pass = checkNotNull(cfg.password()); + } + + /** + * Returns network information stored in Neutron. + * + * @return List of OpenstackNetwork + */ + public Collection<OpenstackNetwork> getNetworks() { + + WebResource.Builder builder = getClientBuilder(neutronUrl + "networks"); + String response = builder.accept(MediaType.APPLICATION_JSON_TYPE). + header("X-Auth-Token", getToken()).get(String.class); + + ObjectMapper mapper = new ObjectMapper(); + List<OpenstackNetwork> openstackNetworks = Lists.newArrayList(); + try { + ObjectNode node = (ObjectNode) mapper.readTree(response); + ArrayNode networkList = (ArrayNode) node.path("networks"); + OpenstackNetworkCodec networkCodec = new OpenstackNetworkCodec(); + networkList.forEach(n -> openstackNetworks.add(networkCodec.decode((ObjectNode) n, null))); + } catch (IOException e) { + e.printStackTrace(); + } + + log.debug("networks response:" + response); + openstackNetworks.forEach(n -> log.debug("network ID: {}", n.id())); + + return openstackNetworks; + } + + /** + * Returns port information stored in Neutron. + * + * @return List of OpenstackPort + */ + public Collection<OpenstackPort> getPorts() { + + WebResource.Builder builder = getClientBuilder(neutronUrl + "ports"); + String response = builder.accept(MediaType.APPLICATION_JSON_TYPE). + header("X-Auth-Token", getToken()).get(String.class); + + ObjectMapper mapper = new ObjectMapper(); + List<OpenstackPort> openstackPorts = Lists.newArrayList(); + try { + ObjectNode node = (ObjectNode) mapper.readTree(response); + ArrayNode portList = (ArrayNode) node.path("ports"); + OpenstackPortCodec portCodec = new OpenstackPortCodec(); + portList.forEach(p -> openstackPorts.add(portCodec.decode((ObjectNode) p, null))); + } catch (IOException e) { + e.printStackTrace(); + } + + log.debug("port response:" + response); + openstackPorts.forEach(n -> log.debug("port ID: {}", n.id())); + + return openstackPorts; + } + + /** + * Returns Subnet information in Neutron. + * + * @return List of OpenstackSubnet + */ + public Collection<OpenstackSubnet> getSubnets() { + + WebResource.Builder builder = getClientBuilder(neutronUrl + "subnets"); + String response = builder.accept(MediaType.APPLICATION_JSON_TYPE). + header("X-Auth-Token", getToken()).get(String.class); + + ObjectMapper mapper = new ObjectMapper(); + List<OpenstackSubnet> subnets = Lists.newArrayList(); + try { + ObjectNode node = (ObjectNode) mapper.readTree(response); + ArrayNode subnetList = (ArrayNode) node.path("subnets"); + OpenstackSubnetCodec subnetCodec = new OpenstackSubnetCodec(); + subnetList.forEach(s -> subnets.add(subnetCodec.decode((ObjectNode) s, null))); + } catch (IOException e) { + e.printStackTrace(); + } + + log.debug("subnets response:" + response); + subnets.forEach(s -> log.debug("subnet ID: {}", s.id())); + + return subnets; + } + + private WebResource.Builder getClientBuilder(String uri) { + Client client = Client.create(); + WebResource resource = client.resource(uri); + return resource.accept(JSON_UTF_8.toString()) + .type(JSON_UTF_8.toString()); + } + + private String getToken() { + if (isTokenInvalid()) { + String request = "{\"auth\": {\"tenantName\": \"admin\", " + + "\"passwordCredentials\": {\"username\": \"" + + userName + "\",\"password\": \"" + pass + "\"}}}"; + WebResource.Builder builder = getClientBuilder(keystoneUrl + "tokens"); + String response = builder.accept(MediaType.APPLICATION_JSON).post(String.class, request); + + ObjectMapper mapper = new ObjectMapper(); + try { + ObjectNode node = (ObjectNode) mapper.readTree(response); + tokenId = node.path("access").path("token").path("id").asText(); + } catch (IOException e) { + e.printStackTrace(); + } + log.debug("token response:" + response); + } + + return tokenId; + } + + private boolean isTokenInvalid() { + //TODO: validation check for the existing token + return true; + } + +} diff --git a/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/OpenstackSwitchingConfig.java b/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/OpenstackSwitchingConfig.java new file mode 100644 index 00000000..ba39ff66 --- /dev/null +++ b/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/OpenstackSwitchingConfig.java @@ -0,0 +1,127 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.openstackswitching; + +import org.onosproject.core.ApplicationId; +import org.onosproject.net.config.Config; +import org.onosproject.net.config.basics.BasicElementConfig; + +/** + * Handles configuration for OpenstackSwitching app. + */ +public class OpenstackSwitchingConfig extends Config<ApplicationId> { + public static final String DONOTPUSH = "do_not_push_flows"; + public static final String NEUTRON_SERVER = "neutron_server"; + public static final String KEYSTONE_SERVER = "keystone_server"; + public static final String USER_NAME = "user_name"; + public static final String PASSWORD = "password"; + + /** + * Returns the flag whether the app pushes flows or not. + * + * @return the flag or false if not set + */ + public boolean doNotPushFlows() { + String flag = get(DONOTPUSH, "false"); + return Boolean.valueOf(flag); + } + + /** + * Returns the Neutron server IP address. + * + * @return Neutron server IP + */ + public String neutronServer() { + return get(NEUTRON_SERVER, ""); + } + + /** + * Returns the Keystone server IP address. + * + * @return Keystone server IP + */ + public String keystoneServer() { + return get(KEYSTONE_SERVER, ""); + } + + /** + * Returns the username for openstack. + * + * @return username for openstack + */ + public String userName() { + return get(USER_NAME, ""); + } + + /** + * Returns the password for openstack. + * + * @return password for openstack + */ + public String password() { + return get(PASSWORD, ""); + } + + /** + * Sets the flag whether the app pushes flows or not. + * + * @param flag the flag whether the app pushes flows or not + * @return self + */ + public BasicElementConfig doNotPushFlows(boolean flag) { + return (BasicElementConfig) setOrClear(DONOTPUSH, flag); + } + + /** + * Sets the neutron server IP address. + * + * @param url neutron server IP address + * @return itself + */ + public BasicElementConfig neutronServer(String url) { + return (BasicElementConfig) setOrClear(NEUTRON_SERVER, url); + } + + /** + * Sets the keystone server IP address. + * + * @param url keystone server IP address + * @return itself + */ + public BasicElementConfig keystoneServer(String url) { + return (BasicElementConfig) setOrClear(KEYSTONE_SERVER, url); + } + + /** + * Sets the username for openstack. + * + * @param username user name for openstack + * @return itself + */ + public BasicElementConfig userName(String username) { + return (BasicElementConfig) setOrClear(USER_NAME, username); + } + + /** + * Sets the password for openstack. + * + * @param password password for openstack + * @return itself + */ + public BasicElementConfig password(String password) { + return (BasicElementConfig) setOrClear(PASSWORD, password); + } +} diff --git a/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/OpenstackSwitchingManager.java b/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/OpenstackSwitchingManager.java new file mode 100644 index 00000000..d881d81c --- /dev/null +++ b/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/OpenstackSwitchingManager.java @@ -0,0 +1,374 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.openstackswitching; + +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Lists; +import org.apache.felix.scr.annotations.Activate; +import org.apache.felix.scr.annotations.Component; +import org.apache.felix.scr.annotations.Deactivate; +import org.apache.felix.scr.annotations.Reference; +import org.apache.felix.scr.annotations.ReferenceCardinality; +import org.apache.felix.scr.annotations.Service; +import org.onlab.packet.Ethernet; +import org.onlab.packet.Ip4Address; +import org.onosproject.core.ApplicationId; +import org.onosproject.core.CoreService; +import org.onosproject.dhcp.DhcpService; +import org.onosproject.net.Device; +import org.onosproject.net.DeviceId; +import org.onosproject.net.Port; +import org.onosproject.net.config.ConfigFactory; +import org.onosproject.net.config.NetworkConfigEvent; +import org.onosproject.net.config.NetworkConfigListener; +import org.onosproject.net.config.NetworkConfigRegistry; +import org.onosproject.net.device.DeviceEvent; +import org.onosproject.net.device.DeviceListener; +import org.onosproject.net.device.DeviceService; +import org.onosproject.net.driver.DriverService; +import org.onosproject.net.flowobjective.FlowObjectiveService; +import org.onosproject.net.packet.InboundPacket; +import org.onosproject.net.packet.PacketContext; +import org.onosproject.net.packet.PacketProcessor; +import org.onosproject.net.packet.PacketService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.util.List; +import java.util.Collection; +import java.util.Set; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.stream.Collectors; + +import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; + +@SuppressWarnings("ALL") +@Service +@Component(immediate = true) +/** + * Populates forwarding rules for VMs created by Openstack. + */ +public class OpenstackSwitchingManager implements OpenstackSwitchingService { + + private static Logger log = LoggerFactory + .getLogger(OpenstackSwitchingManager.class); + + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) + protected CoreService coreService; + + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) + protected PacketService packetService; + + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) + protected DeviceService deviceService; + + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) + protected FlowObjectiveService flowObjectiveService; + + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) + protected DhcpService dhcpService; + + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) + protected NetworkConfigRegistry cfgService; + + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) + protected DriverService driverService; + + private ApplicationId appId; + private boolean doNotPushFlows; + private Ip4Address neutronServer; + private Ip4Address keystoneServer; + private String userName; + private String password; + private OpenstackArpHandler arpHandler; + private OpenstackRestHandler restHandler; + + private ExecutorService deviceEventExcutorService = Executors.newFixedThreadPool(10); + + private InternalPacketProcessor internalPacketProcessor = new InternalPacketProcessor(); + private InternalDeviceListener internalDeviceListener = new InternalDeviceListener(); + private InternalConfigListener internalConfigListener = new InternalConfigListener(); + private final Set<ConfigFactory> factories = ImmutableSet.of( + new ConfigFactory<ApplicationId, OpenstackSwitchingConfig>(APP_SUBJECT_FACTORY, + OpenstackSwitchingConfig.class, + "openstackswitching") { + @Override + public OpenstackSwitchingConfig createConfig() { + return new OpenstackSwitchingConfig(); + } + } + ); + + @Activate + protected void activate() { + appId = coreService + .registerApplication("org.onosproject.openstackswitching"); + + factories.forEach(cfgService::registerConfigFactory); + packetService.addProcessor(internalPacketProcessor, PacketProcessor.director(1)); + deviceService.addListener(internalDeviceListener); + cfgService.addListener(internalConfigListener); + + internalConfigListener.configureNetwork(); + + log.info("Started"); + } + + @Deactivate + protected void deactivate() { + packetService.removeProcessor(internalPacketProcessor); + deviceService.removeListener(internalDeviceListener); + cfgService.removeListener(internalConfigListener); + + deviceEventExcutorService.shutdown(); + + log.info("Stopped"); + } + + @Override + public void createPorts(OpenstackPort openstackPort) { + registerDhcpInfo(openstackPort); + } + + @Override + public void deletePorts() { + + } + + @Override + public void updatePorts() { + + } + + @Override + public void createNetwork(OpenstackNetwork openstackNetwork) { + } + + @Override + public void createSubnet(OpenstackSubnet openstackSubnet) { + } + + @Override + public Collection<OpenstackPort> ports(String networkId) { + Collection<OpenstackPort> ports = restHandler.getPorts(); + List<OpenstackPort> portList = ports.stream() + .filter(p -> p.networkId().equals(networkId)) + .collect(Collectors.toList()); + + return portList; + } + + @Override + public OpenstackPort port(Port port) { + Collection<OpenstackPort> ports = restHandler.getPorts(); + String uuid = port.annotations().value("portName").substring(3); + return ports.stream() + .filter(p -> p.id().startsWith(uuid)) + .findFirst().orElse(null); + } + + @Override + public OpenstackPort port(String portId) { + Collection<OpenstackPort> ports = restHandler.getPorts(); + return ports.stream() + .filter(p -> p.id().equals(portId)) + .findFirst().orElse(null); + } + + @Override + public OpenstackNetwork network(String networkId) { + Collection<OpenstackNetwork> networks = restHandler.getNetworks(); + return networks.stream() + .filter(n -> n.id().equals(networkId)) + .findFirst().orElse(null); + } + + private void processDeviceAdded(Device device) { + log.debug("device {} is added", device.id()); + } + + private void processPortAdded(Device device, Port port) { + if (!port.annotations().value("portName").equals("vxlan")) { + OpenstackSwitchingRulePopulator rulePopulator = + new OpenstackSwitchingRulePopulator(appId, flowObjectiveService, + deviceService, restHandler, driverService); + rulePopulator.populateSwitchingRules(device, port); + } + } + + private void processPortRemoved(Device device, Port port) { + // TODO: Remove flow rules for the VM removed + log.debug("port {} is removed", port.toString()); + } + + private void registerDhcpInfo(OpenstackPort openstackPort) { + Ip4Address ip4Address; + Ip4Address subnetMask; + Ip4Address dhcpServer; + Ip4Address gatewayIPAddress; + Ip4Address domainServer; + OpenstackSubnet openstackSubnet; + + ip4Address = (Ip4Address) openstackPort.fixedIps().values().toArray()[0]; + + openstackSubnet = restHandler.getSubnets().stream() + .filter(n -> n.networkId().equals(openstackPort.networkId())) + .findFirst().get(); + + subnetMask = Ip4Address.valueOf(buildSubnetMask(openstackSubnet.cidr())); + gatewayIPAddress = Ip4Address.valueOf(openstackSubnet.gatewayIp()); + dhcpServer = gatewayIPAddress; + // TODO: supports multiple DNS servers + if (openstackSubnet.dnsNameservers().isEmpty()) { + domainServer = Ip4Address.valueOf("8.8.8.8"); + } else { + domainServer = openstackSubnet.dnsNameservers().get(0); + } + List<Ip4Address> options = Lists.newArrayList(); + options.add(subnetMask); + options.add(dhcpServer); + options.add(gatewayIPAddress); + options.add(domainServer); + + dhcpService.setStaticMapping(openstackPort.macAddress(), ip4Address, true, options); + } + + private byte[] buildSubnetMask(String cidr) { + int prefix; + String[] parts = cidr.split("/"); + prefix = Integer.parseInt(parts[1]); + int mask = 0xffffffff << (32 - prefix); + byte[] bytes = new byte[]{(byte) (mask >>> 24), + (byte) (mask >> 16 & 0xff), (byte) (mask >> 8 & 0xff), (byte) (mask & 0xff)}; + + return bytes; + } + + + + private class InternalPacketProcessor implements PacketProcessor { + + @Override + public void process(PacketContext context) { + + if (context.isHandled()) { + return; + } + + InboundPacket pkt = context.inPacket(); + Ethernet ethernet = pkt.parsed(); + + if (ethernet.getEtherType() == Ethernet.TYPE_ARP) { + arpHandler.processPacketIn(pkt); + } + } + } + + private class InternalDeviceListener implements DeviceListener { + + @Override + public void event(DeviceEvent deviceEvent) { + deviceEventExcutorService.execute(new InternalEventHandler(deviceEvent)); + } + } + + private class InternalEventHandler implements Runnable { + + volatile DeviceEvent deviceEvent; + + InternalEventHandler(DeviceEvent deviceEvent) { + this.deviceEvent = deviceEvent; + } + + @Override + public void run() { + + if (doNotPushFlows) { + return; + } + + switch (deviceEvent.type()) { + case DEVICE_ADDED: + processDeviceAdded((Device) deviceEvent.subject()); + break; + case DEVICE_UPDATED: + Port port = (Port) deviceEvent.subject(); + if (port.isEnabled()) { + processPortAdded((Device) deviceEvent.subject(), deviceEvent.port()); + } + break; + case DEVICE_AVAILABILITY_CHANGED: + Device device = (Device) deviceEvent.subject(); + if (deviceService.isAvailable(device.id())) { + processDeviceAdded(device); + } + break; + case PORT_ADDED: + processPortAdded((Device) deviceEvent.subject(), deviceEvent.port()); + break; + case PORT_UPDATED: + processPortAdded((Device) deviceEvent.subject(), deviceEvent.port()); + break; + case PORT_REMOVED: + processPortRemoved((Device) deviceEvent.subject(), deviceEvent.port()); + break; + default: + break; + } + } + } + + private class InternalConfigListener implements NetworkConfigListener { + + public void configureNetwork() { + OpenstackSwitchingConfig cfg = + cfgService.getConfig(appId, OpenstackSwitchingConfig.class); + if (cfg == null) { + log.error("There is no openstack server information in config."); + return; + } + doNotPushFlows = cfg.doNotPushFlows(); + restHandler = new OpenstackRestHandler(cfg); + arpHandler = new OpenstackArpHandler(restHandler, packetService); + } + + @Override + public void event(NetworkConfigEvent event) { + if (((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || + event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED)) && + event.configClass().equals(OpenstackSwitchingConfig.class)) { + configureNetwork(); + } + } + + } + + private final class PortInfo { + DeviceId deviceId; + String portName; + Ip4Address fixedIp; + Ip4Address hostIp; + + private PortInfo(DeviceId deviceId, String portName, Ip4Address fixedIp, + Ip4Address hostIp) { + this.deviceId = deviceId; + this.portName = portName; + this.fixedIp = fixedIp; + this.hostIp = hostIp; + } + } + +}
\ No newline at end of file diff --git a/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/OpenstackSwitchingRulePopulator.java b/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/OpenstackSwitchingRulePopulator.java new file mode 100644 index 00000000..38c03638 --- /dev/null +++ b/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/OpenstackSwitchingRulePopulator.java @@ -0,0 +1,307 @@ +/* +* Copyright 2015 Open Networking Laboratory +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +package org.onosproject.openstackswitching; + +import org.onlab.packet.Ethernet; +import org.onlab.packet.Ip4Address; +import org.onlab.packet.MacAddress; +import org.onosproject.core.ApplicationId; +import org.onosproject.net.Device; +import org.onosproject.net.DeviceId; +import org.onosproject.net.Port; +import org.onosproject.net.PortNumber; +import org.onosproject.net.behaviour.ExtensionTreatmentResolver; +import org.onosproject.net.device.DeviceService; +import org.onosproject.net.driver.DefaultDriverData; +import org.onosproject.net.driver.DefaultDriverHandler; +import org.onosproject.net.driver.Driver; +import org.onosproject.net.driver.DriverHandler; +import org.onosproject.net.driver.DriverService; +import org.onosproject.net.flow.DefaultTrafficSelector; +import org.onosproject.net.flow.DefaultTrafficTreatment; +import org.onosproject.net.flow.TrafficSelector; +import org.onosproject.net.flow.TrafficTreatment; +import org.onosproject.net.flow.instructions.ExtensionTreatment; +import org.onosproject.net.flow.instructions.ExtensionPropertyException; +import org.onosproject.net.flow.instructions.ExtensionTreatmentType; +import org.onosproject.net.flowobjective.DefaultForwardingObjective; +import org.onosproject.net.flowobjective.FlowObjectiveService; +import org.onosproject.net.flowobjective.ForwardingObjective; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Collection; + +/** + * Populates switching flow rules. + */ +public class OpenstackSwitchingRulePopulator { + + private static Logger log = LoggerFactory + .getLogger(OpenstackSwitchingRulePopulator.class); + private static final int SWITCHING_RULE_PRIORITY = 50000; + + private FlowObjectiveService flowObjectiveService; + private DriverService driverService; + private DeviceService deviceService; + private OpenstackRestHandler restHandler; + private ApplicationId appId; + + private Collection<OpenstackNetwork> openstackNetworkList; + private Collection<OpenstackPort> openstackPortList; + + /** + * Creates OpenstackSwitchingRulPopulator. + * + * @param appId application id + * @param flowObjectiveService FlowObjectiveService reference + * @param deviceService DeviceService reference + * @param driverService DriverService reference + */ + public OpenstackSwitchingRulePopulator(ApplicationId appId, + FlowObjectiveService flowObjectiveService, + DeviceService deviceService, + OpenstackRestHandler restHandler, + DriverService driverService) { + this.flowObjectiveService = flowObjectiveService; + this.deviceService = deviceService; + this.driverService = driverService; + this.restHandler = restHandler; + this.appId = appId; + + openstackNetworkList = restHandler.getNetworks(); + openstackPortList = restHandler.getPorts(); + } + + /** + * Populates flow rules for the VM created. + * + * @param device device to populate rules to + * @param port port for the VM created + */ + public void populateSwitchingRules(Device device, Port port) { + populateFlowRulesForTrafficToSameCnode(device, port); + populateFlowRulesForTrafficToDifferentCnode(device, port); + } + + /** + * Populates the flow rules for traffic to VMs in the same Cnode as the sender. + * + * @param device device to put the rules + * @param port port info of the VM + */ + private void populateFlowRulesForTrafficToSameCnode(Device device, Port port) { + Ip4Address vmIp = getFixedIpAddressForPort(port.annotations().value("portName")); + if (vmIp != null) { + setFlowRuleForVMsInSameCnode(vmIp, device.id(), port); + } + } + + /** + * Populates the flow rules for traffic to VMs in different Cnode using + * Nicira extention. + * + * @param device device to put rules + * @param port port information of the VM + */ + private void populateFlowRulesForTrafficToDifferentCnode(Device device, Port port) { + String portName = port.annotations().value("portName"); + String channelId = device.annotations().value("channelId"); + Ip4Address hostIpAddress = Ip4Address.valueOf(channelId.split(":")[0]); + Ip4Address fixedIp = getFixedIpAddressForPort(portName); + MacAddress vmMac = getVmMacAddressForPort(portName); + String vni = getVniForPort(portName); + deviceService.getAvailableDevices().forEach(d -> { + if (!d.equals(device)) { + deviceService.getPorts(d.id()).forEach(p -> { + String pName = p.annotations().value("portName"); + if (!p.equals(port) && vni.equals(getVniForPort(pName))) { + String cidx = d.annotations().value("channelId"); + Ip4Address hostIpx = Ip4Address.valueOf(cidx.split(":")[0]); + MacAddress vmMacx = getVmMacAddressForPort(pName); + Ip4Address fixedIpx = getFixedIpAddressForPort(pName); + + setVxLanFlowRule(vni, device.id(), hostIpx, fixedIpx, vmMacx); + setVxLanFlowRule(vni, d.id(), hostIpAddress, fixedIp, vmMac); + } + }); + } + }); + } + + /** + * Returns the VNI of the VM of the port. + * + * @param portName VM port + * @return VNI + */ + private String getVniForPort(String portName) { + String uuid = portName.substring(3); + OpenstackPort port = openstackPortList.stream() + .filter(p -> p.id().startsWith(uuid)) + .findAny().orElse(null); + if (port == null) { + log.warn("No port information for port {}", portName); + return null; + } + + OpenstackNetwork network = openstackNetworkList.stream() + .filter(n -> n.id().equals(port.networkId())) + .findAny().orElse(null); + if (network == null) { + log.warn("No VNI information for network {}", port.networkId()); + return null; + } + + return network.segmentId(); + } + + /** + * Returns the Fixed IP address of the VM. + * + * @param portName VM port info + * @return IP address of the VM + */ + private Ip4Address getFixedIpAddressForPort(String portName) { + + String uuid = portName.substring(3); + OpenstackPort port = openstackPortList.stream() + .filter(p -> p.id().startsWith(uuid)) + .findFirst().orElse(null); + + if (port == null) { + log.error("There is no port information for port name {}", portName); + return null; + } + + if (port.fixedIps().isEmpty()) { + log.error("There is no fixed IP info in the port information"); + return null; + } + + return (Ip4Address) port.fixedIps().values().toArray()[0]; + } + + /** + * Returns the MAC address of the VM of the port. + * + * @param portName VM port + * @return MAC address of the VM + */ + private MacAddress getVmMacAddressForPort(String portName) { + + String uuid = portName.substring(3); + OpenstackPort port = openstackPortList.stream() + .filter(p -> p.id().startsWith(uuid)) + .findFirst().orElse(null); + + if (port == null) { + log.error("There is port information for port name {}", portName); + return null; + } + + return port.macAddress(); + } + + /** + * Sets the flow rules for traffic between VMs in the same Cnode. + * + * @param ip4Address VM IP address + * @param id device ID to put rules + * @param port VM port + */ + private void setFlowRuleForVMsInSameCnode(Ip4Address ip4Address, DeviceId id, + Port port) { + TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); + TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); + + sBuilder.matchEthType(Ethernet.TYPE_IPV4) + .matchIPDst(ip4Address.toIpPrefix()); + tBuilder.setOutput(port.number()); + + ForwardingObjective fo = DefaultForwardingObjective.builder() + .withSelector(sBuilder.build()) + .withTreatment(tBuilder.build()) + .withPriority(SWITCHING_RULE_PRIORITY) + .withFlag(ForwardingObjective.Flag.VERSATILE) + .fromApp(appId) + .add(); + + flowObjectiveService.forward(id, fo); + } + + /** + * Sets the flow rules between traffic from VMs in different Cnode. + * + * @param vni VNI + * @param id device ID + * @param hostIp host IP of the VM + * @param vmIp fixed IP of the VM + * @param vmMac MAC address of the VM + */ + private void setVxLanFlowRule(String vni, DeviceId id, Ip4Address hostIp, + Ip4Address vmIp, MacAddress vmMac) { + TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); + TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); + + sBuilder.matchEthType(Ethernet.TYPE_IPV4) + .matchIPDst(vmIp.toIpPrefix()); + tBuilder.setTunnelId(Long.parseLong(vni)) + .extension(buildNiciraExtenstion(id, hostIp), id) + .setOutput(getTunnelPort(id)); + + ForwardingObjective fo = DefaultForwardingObjective.builder() + .withSelector(sBuilder.build()) + .withTreatment(tBuilder.build()) + .withPriority(SWITCHING_RULE_PRIORITY) + .withFlag(ForwardingObjective.Flag.VERSATILE) + .fromApp(appId) + .add(); + + flowObjectiveService.forward(id, fo); + } + + private ExtensionTreatment buildNiciraExtenstion(DeviceId id, Ip4Address hostIp) { + Driver driver = driverService.getDriver(id); + DriverHandler driverHandler = new DefaultDriverHandler(new DefaultDriverData(driver, id)); + ExtensionTreatmentResolver resolver = driverHandler.behaviour(ExtensionTreatmentResolver.class); + + ExtensionTreatment extensionInstruction = + resolver.getExtensionInstruction( + ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST.type()); + + try { + extensionInstruction.setPropertyValue("tunnelDst", hostIp); + } catch (ExtensionPropertyException e) { + log.error("Error setting Nicira extension setting {}", e); + } + + return extensionInstruction; + } + + private PortNumber getTunnelPort(DeviceId id) { + Port port = deviceService.getPorts(id).stream() + .filter(p -> p.annotations().value("portName").equals("vxlan")) + .findAny().orElse(null); + + if (port == null) { + log.error("No TunnelPort was created."); + return null; + } + return port.number(); + } +} diff --git a/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/package-info.java b/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/package-info.java new file mode 100644 index 00000000..cd50f912 --- /dev/null +++ b/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * OpenStack switch interface. + */ +package org.onosproject.openstackswitching; diff --git a/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackNetworkCodec.java b/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackNetworkCodec.java new file mode 100644 index 00000000..0a0b5dce --- /dev/null +++ b/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackNetworkCodec.java @@ -0,0 +1,69 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.openstackswitching.web; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import org.onosproject.codec.CodecContext; +import org.onosproject.codec.JsonCodec; +import org.onosproject.openstackswitching.OpenstackNetwork; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Implementation of the OpenstackNetwork Codec. + * + */ +public class OpenstackNetworkCodec extends JsonCodec<OpenstackNetwork> { + + protected static final Logger log = LoggerFactory + .getLogger(OpenstackNetworkCodec.class); + + private static final String NETWORK = "network"; + private static final String NAME = "name"; + private static final String TENANT_ID = "tenant_id"; + private static final String SEGMENTATION_ID = "provider:segmentation_id"; + private static final String NETWORK_TYPE = "provider:network_type"; + private static final String ID = "id"; + + @Override + public OpenstackNetwork decode(ObjectNode json, CodecContext context) { + + JsonNode networkInfo = json.get(NETWORK); + if (networkInfo == null) { + networkInfo = json; + } + + String name = networkInfo.path(NAME).asText(); + String tenantId = networkInfo.path(TENANT_ID).asText(); + String id = networkInfo.path(ID).asText(); + + OpenstackNetwork.Builder onb = OpenstackNetwork.builder(); + onb.name(name) + .tenantId(tenantId) + .id(id); + + if (!networkInfo.path(NETWORK_TYPE).isMissingNode()) { + onb.networkType(OpenstackNetwork.NetworkType.valueOf(networkInfo.path(NETWORK_TYPE). + asText().toUpperCase())); + onb.name(networkInfo.path(NETWORK_TYPE).asText()); + onb.segmentId(networkInfo.path(SEGMENTATION_ID).asText()); + } + + return onb.build(); + } + +} diff --git a/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackNetworkWebResource.java b/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackNetworkWebResource.java new file mode 100644 index 00000000..bf04cc4d --- /dev/null +++ b/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackNetworkWebResource.java @@ -0,0 +1,66 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.openstackswitching.web; + +import org.onosproject.rest.AbstractWebResource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.io.InputStream; + +/** + * Handles REST API call of Neutron ML2 plugin. + */ +@Path("networks") +public class OpenstackNetworkWebResource extends AbstractWebResource { + + protected static final Logger log = LoggerFactory + .getLogger(OpenstackNetworkWebResource.class); + + @POST + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public Response createNetwork(InputStream input) { + log.debug("REST API networks is called {}", input.toString()); + return Response.status(Response.Status.OK).build(); + } + + @PUT + @Path("{id}") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public Response updateNetwork(InputStream input) { + log.debug("REST API networks is called {}", input.toString()); + return Response.status(Response.Status.OK).build(); + } + + @DELETE + @Path("{id}") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public Response deleteNetwork(InputStream input) { + log.debug("REST API networks is called {}", input.toString()); + return Response.status(Response.Status.OK).build(); + } +} diff --git a/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackPortCodec.java b/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackPortCodec.java new file mode 100644 index 00000000..63e6d2ea --- /dev/null +++ b/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackPortCodec.java @@ -0,0 +1,107 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.openstackswitching.web; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import org.onlab.packet.Ip4Address; +import org.onlab.packet.MacAddress; +import org.onosproject.codec.CodecContext; +import org.onosproject.codec.JsonCodec; +import org.onosproject.openstackswitching.OpenstackPort; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.HashMap; + +/** + * Encodes and decodes the OpenstackPort. + */ +public class OpenstackPortCodec extends JsonCodec<OpenstackPort> { + + private static Logger log = LoggerFactory + .getLogger(OpenstackPortCodec.class); + + // JSON field names + private static final String PORT = "port"; + private static final String STATUS = "status"; + private static final String NAME = "name"; + private static final String ADDRESS_PAIR = "allowed_address_pairs"; + private static final String ADMIN_STATUS = "admin_status"; + private static final String NETWORK_ID = "network_id"; + private static final String TENANT_ID = "tenant_id"; + private static final String DEVICE_OWNER = "device_owner"; + private static final String MAC_ADDRESS = "mac_address"; + private static final String FIXED_IPS = "fixed_ips"; + private static final String SUBNET_ID = "subnet_id"; + private static final String IP_ADDRESS = "ip_address"; + private static final String ID = "id"; + private static final String SECURITY_GROUPS = "security_groups"; + private static final String DEVICE_ID = "device_id"; + + @Override + public OpenstackPort decode(ObjectNode json, CodecContext context) { + + HashMap<String, Ip4Address> fixedIpMap = new HashMap<>(); + JsonNode portInfo = json.get(PORT); + if (portInfo == null) { + portInfo = json; + } + + String status = portInfo.path(STATUS).asText(); + String name = portInfo.path(NAME).asText(); + boolean adminStateUp = portInfo.path(ADMIN_STATUS).asBoolean(); + String networkId = portInfo.path(NETWORK_ID).asText(); + String tenantId = portInfo.path(TENANT_ID).asText(); + String deviceOwner = portInfo.path(DEVICE_OWNER).asText(); + String macStr = portInfo.path(MAC_ADDRESS).asText(); + ArrayNode fixedIpList = (ArrayNode) portInfo.path(FIXED_IPS); + for (JsonNode fixedIpInfo: fixedIpList) { + String subnetId = fixedIpInfo.path(SUBNET_ID).asText(); + String ipAddressStr = fixedIpInfo.path(IP_ADDRESS).asText(); + if (ipAddressStr != null) { + Ip4Address ipAddress = Ip4Address.valueOf(ipAddressStr); + fixedIpMap.put(subnetId, ipAddress); + } + } + String id = portInfo.path(ID).asText(); + String securityGroups = portInfo.path(SECURITY_GROUPS).asText(); + String deviceId = portInfo.path(DEVICE_ID).asText(); + + OpenstackPort.Builder openstackPortBuilder = OpenstackPort.builder(); + openstackPortBuilder.portStatus(OpenstackPort.PortStatus.valueOf(status)) + .name(name) + .adminState(adminStateUp) + .netwrokId(networkId) + .tenantId(tenantId) + .deviceOwner(deviceOwner) + .macAddress(MacAddress.valueOf(macStr)) + .fixedIps(fixedIpMap) + .id(id) + .deviceId(deviceId); + + // FIX ME + if (!securityGroups.isEmpty()) { + openstackPortBuilder.securityGroup(securityGroups); + } + + OpenstackPort openstackPort = openstackPortBuilder.build(); + + return openstackPort; + } + +} diff --git a/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackPortWebResource.java b/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackPortWebResource.java new file mode 100644 index 00000000..faffa732 --- /dev/null +++ b/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackPortWebResource.java @@ -0,0 +1,88 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.openstackswitching.web; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import org.onosproject.openstackswitching.OpenstackPort; +import org.onosproject.openstackswitching.OpenstackSwitchingService; +import org.onosproject.rest.AbstractWebResource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.io.InputStream; + +/** + * Handles Rest API call from Neutron ML2 plugin. + */ +@Path("ports") +public class OpenstackPortWebResource extends AbstractWebResource { + + protected static final Logger log = LoggerFactory + .getLogger(OpenstackPortWebResource.class); + + private static final OpenstackPortCodec PORT_CODEC = new OpenstackPortCodec(); + + @POST + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public Response createPorts(InputStream input) { + try { + ObjectMapper mapper = new ObjectMapper(); + ObjectNode portNode = (ObjectNode) mapper.readTree(input); + + OpenstackPort openstackPort = PORT_CODEC.decode(portNode, this); + OpenstackSwitchingService switchingService = + getService(OpenstackSwitchingService.class); + switchingService.createPorts(openstackPort); + + log.debug("REST API ports is called with {}", portNode.toString()); + return Response.status(Response.Status.OK).build(); + + } catch (Exception e) { + log.error("Creates Port failed because of exception {}", + e.toString()); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString()) + .build(); + } + } + + @DELETE + @Path("{id}") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public Response deletesPorts(InputStream input) { + log.debug("REST API ports is called with {}", input.toString()); + return Response.status(Response.Status.OK).build(); + } + + @PUT + @Path("{id}") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public Response updatePorts(InputStream input) { + log.info("REST API ports is called with {}", input.toString()); + return Response.status(Response.Status.OK).build(); + } +} diff --git a/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackSubnetCodec.java b/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackSubnetCodec.java new file mode 100644 index 00000000..2a7af82a --- /dev/null +++ b/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackSubnetCodec.java @@ -0,0 +1,83 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.openstackswitching.web; + +import com.fasterxml.jackson.databind.JsonNode; + + +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.google.common.collect.Lists; +import org.onlab.packet.Ip4Address; +import org.onosproject.codec.CodecContext; +import org.onosproject.codec.JsonCodec; +import org.onosproject.openstackswitching.OpenstackSubnet; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +/** + * Encodes and decodes the OpenstackSubnet. + */ +public class OpenstackSubnetCodec extends JsonCodec<OpenstackSubnet> { + private static Logger log = LoggerFactory + .getLogger(OpenstackSubnetCodec.class); + + // JSON Field names + private static final String SUBNET = "subnet"; + private static final String NAME = "name"; + private static final String ENABLE_DHCP = "enable_dhcp"; + private static final String NETWORK_ID = "network_id"; + private static final String TENANT_ID = "tenant_id"; + private static final String DNS_NAMESERVERS = "dns_nameservers"; + private static final String GATEWAY_IP = "gateway_ip"; + private static final String CIDR = "cidr"; + private static final String ID = "id"; + + @Override + public OpenstackSubnet decode(ObjectNode json, CodecContext context) { + JsonNode subnetInfo = json.get(SUBNET); + if (subnetInfo == null) { + subnetInfo = json; + } + + String name = subnetInfo.path(NAME).asText(); + boolean enableDhcp = subnetInfo.path(ENABLE_DHCP).asBoolean(); + String networkId = subnetInfo.path(NETWORK_ID).asText(); + String tenantId = subnetInfo.path(TENANT_ID).asText(); + ArrayNode dnsNameservsers = (ArrayNode) subnetInfo.path(DNS_NAMESERVERS); + List<Ip4Address> dnsList = Lists.newArrayList(); + if (dnsNameservsers != null && !dnsNameservsers.isMissingNode()) { + dnsNameservsers.forEach(dns -> dnsList.add(Ip4Address.valueOf(dns.asText()))); + } + String gatewayIp = subnetInfo.path(GATEWAY_IP).asText(); + String cidr = subnetInfo.path(CIDR).asText(); + String id = subnetInfo.path(ID).asText(); + + OpenstackSubnet openstackSubnet = OpenstackSubnet.builder() + .setName(name) + .setEnableDhcp(enableDhcp) + .setNetworkId(networkId) + .setTenantId(tenantId) + .setDnsNameservers(dnsList) + .setGatewayIp(gatewayIp) + .setCidr(cidr) + .setId(id) + .build(); + return openstackSubnet; + } +} diff --git a/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackSubnetWebResource.java b/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackSubnetWebResource.java new file mode 100644 index 00000000..43205eac --- /dev/null +++ b/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackSubnetWebResource.java @@ -0,0 +1,69 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.openstackswitching.web; + +/** + * Handles Rest API call from Neutron ML2 plugin. + */ +import org.onosproject.rest.AbstractWebResource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.io.InputStream; + +@Path("subnets") +public class OpenstackSubnetWebResource extends AbstractWebResource { + protected static final Logger log = LoggerFactory + .getLogger(OpenstackSubnetWebResource.class); + + @POST + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public Response createSubnet(InputStream input) { + return Response.status(Response.Status.OK).build(); + } + + + @PUT + @Path("{subnetUUID}") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + public Response updateSubnet(@PathParam("id") String id, + final InputStream input) { + return Response.status(Response.Status.OK).build(); + + } + + @DELETE + @Path("{subnetUUID}") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + public Response deleteSubnet(@PathParam("id") String id, + final InputStream input) { + return Response.status(Response.Status.OK).build(); + } + + +} diff --git a/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/package-info.java b/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/package-info.java new file mode 100644 index 00000000..91e19c62 --- /dev/null +++ b/framework/src/onos/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * OpenStack switching REST API. + */ +package org.onosproject.openstackswitching.web; diff --git a/framework/src/onos/apps/openstackswitching/app/src/main/webapp/WEB-INF/web.xml b/framework/src/onos/apps/openstackswitching/app/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 00000000..4f50ef72 --- /dev/null +++ b/framework/src/onos/apps/openstackswitching/app/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,45 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ Copyright 2015 Open Networking Laboratory + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" + xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" + id="ONOS" version="2.5"> + <display-name>Openstack Switching REST API v1.0</display-name> + + <servlet> + <servlet-name>JAX-RS Service</servlet-name> + <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> + <init-param> + <param-name>com.sun.jersey.config.property.resourceConfigClass</param-name> + <param-value>com.sun.jersey.api.core.ClassNamesResourceConfig</param-value> + </init-param> + <init-param> + <param-name>com.sun.jersey.config.property.classnames</param-name> + <param-value> + org.onosproject.openstackswitching.web.OpenstackPortWebResource, + org.onosproject.openstackswitching.web.OpenstackNetworkWebResource, + org.onosproject.openstackswitching.web.OpenstackSubnetWebResource + </param-value> + </init-param> + <load-on-startup>1</load-on-startup> + </servlet> + + <servlet-mapping> + <servlet-name>JAX-RS Service</servlet-name> + <url-pattern>/*</url-pattern> + </servlet-mapping> +</web-app> |