diff options
author | Ashlee Young <ashlee@wildernessvoice.com> | 2015-11-03 14:08:10 -0800 |
---|---|---|
committer | Ashlee Young <ashlee@wildernessvoice.com> | 2015-11-03 14:08:10 -0800 |
commit | 643ee33289bd2cb9e6afbfb09b4ed72d467ba1c2 (patch) | |
tree | c2c376a44a359544fe3d4c45eb0cc0e2ec4a7080 /framework/src/onos/apps/openstackswitching | |
parent | 46eeb79b54345bdafb6055b8ee4bad4ce8b01274 (diff) |
This updates ONOS src tree to commit id
03fa5e571cabbd001ddb1598847e1150b11c7333
Change-Id: I13b554026d6f902933e35887d29bd5fdb669c0bd
Signed-off-by: Ashlee Young <ashlee@wildernessvoice.com>
Diffstat (limited to 'framework/src/onos/apps/openstackswitching')
15 files changed, 1813 insertions, 0 deletions
diff --git a/framework/src/onos/apps/openstackswitching/pom.xml b/framework/src/onos/apps/openstackswitching/pom.xml new file mode 100644 index 00000000..245b9e80 --- /dev/null +++ b/framework/src/onos/apps/openstackswitching/pom.xml @@ -0,0 +1,121 @@ +<?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-apps</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-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> + </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.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/src/main/java/org/onosproject/openstackswitching/OpenstackArpHandler.java b/framework/src/onos/apps/openstackswitching/src/main/java/org/onosproject/openstackswitching/OpenstackArpHandler.java new file mode 100644 index 00000000..afaf7a22 --- /dev/null +++ b/framework/src/onos/apps/openstackswitching/src/main/java/org/onosproject/openstackswitching/OpenstackArpHandler.java @@ -0,0 +1,51 @@ +/* +* 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.onosproject.net.packet.InboundPacket; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.HashMap; + +/** + * It handles ARP packet from VMs. + */ +public class OpenstackArpHandler { + + private static Logger log = LoggerFactory + .getLogger(OpenstackArpHandler.class); + + HashMap<String, OpenstackPort> openstackPortHashMap; + + /** + * Constructs an OpenstackArpHandler. + * + * @param openstackPortMap port map + */ + public OpenstackArpHandler(HashMap<String, OpenstackPort> openstackPortMap) { + this.openstackPortHashMap = openstackPortMap; + } + + /** + * Processes ARP packets. + * + * @param pkt ARP request packet + */ + public void processPacketIn(InboundPacket pkt) { + log.warn("Received an ARP packet"); + } +} diff --git a/framework/src/onos/apps/openstackswitching/src/main/java/org/onosproject/openstackswitching/OpenstackDhcpHandler.java b/framework/src/onos/apps/openstackswitching/src/main/java/org/onosproject/openstackswitching/OpenstackDhcpHandler.java new file mode 100644 index 00000000..9c3641c1 --- /dev/null +++ b/framework/src/onos/apps/openstackswitching/src/main/java/org/onosproject/openstackswitching/OpenstackDhcpHandler.java @@ -0,0 +1,45 @@ +/* +* 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.onosproject.net.packet.InboundPacket; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * It handles DHCP request packets. + */ +public class OpenstackDhcpHandler { + + private static Logger log = LoggerFactory + .getLogger(OpenstackDhcpHandler.class); + + /** + * Returns OpenstackDhcpHandler reference. + */ + public OpenstackDhcpHandler() { + + } + + /** + * Processes DHCP request packets. + * + * @param pkt DHCP request packet + */ + public void processPacketIn(InboundPacket pkt) { + log.warn("Received a DHCP packet"); + } +} diff --git a/framework/src/onos/apps/openstackswitching/src/main/java/org/onosproject/openstackswitching/OpenstackNetwork.java b/framework/src/onos/apps/openstackswitching/src/main/java/org/onosproject/openstackswitching/OpenstackNetwork.java new file mode 100644 index 00000000..dc7c0263 --- /dev/null +++ b/framework/src/onos/apps/openstackswitching/src/main/java/org/onosproject/openstackswitching/OpenstackNetwork.java @@ -0,0 +1,112 @@ +/* + * 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 static com.google.common.base.Preconditions.checkNotNull; + + +/** + * Represents the network information given by Neutron. + */ +public final class OpenstackNetwork { + + private String name; + private String tenantId; + private String segmentId; + private String networkType; + private String id; + + /** + * Returns the builder object of the OpenstackNetwork class. + * + * @return OpenstackNetwork builder object + */ + public static OpenstackNetwork.Builder builder() { + return new Builder(); + } + + private OpenstackNetwork(String name, String tenantId, String id, String sid, + String type) { + this.name = checkNotNull(name); + this.tenantId = checkNotNull(tenantId); + this.segmentId = checkNotNull(sid); + this.id = checkNotNull(id); + this.networkType = checkNotNull(type); + } + + public String name() { + return this.name; + } + + public String tenantId() { + return this.tenantId; + } + + public String id() { + return this.id; + } + + public String segmentId() { + return this.segmentId; + } + + public String networkType() { + return this.networkType; + } + + public static final class Builder { + private String name; + private String tenantId; + private String id; + private String sid; + private String networkType; + + public Builder name(String name) { + this.name = name; + + return this; + } + + public Builder tenantId(String tenantId) { + this.tenantId = tenantId; + + return this; + } + + public Builder id(String id) { + this.id = id; + + return this; + } + + public Builder segmentId(String sid) { + this.sid = sid; + + return this; + } + + public Builder networkType(String type) { + this.networkType = type; + + return this; + } + + public OpenstackNetwork build() { + return new OpenstackNetwork(name, tenantId, id, sid, networkType); + } + + } +} diff --git a/framework/src/onos/apps/openstackswitching/src/main/java/org/onosproject/openstackswitching/OpenstackPort.java b/framework/src/onos/apps/openstackswitching/src/main/java/org/onosproject/openstackswitching/OpenstackPort.java new file mode 100644 index 00000000..4326b4fc --- /dev/null +++ b/framework/src/onos/apps/openstackswitching/src/main/java/org/onosproject/openstackswitching/OpenstackPort.java @@ -0,0 +1,350 @@ +/* + * 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.Lists; +import org.onlab.packet.Ip4Address; +import org.onlab.packet.MacAddress; + +import java.util.HashMap; +import java.util.List; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * It represents the Openstack Port information. + */ +public final class OpenstackPort { + + public enum PortStatus { + UP, + DOWN + } + + private PortStatus status; + private String name; + // FIX_ME + private String allowedAddressPairs; + private boolean adminStateUp; + private String networkId; + private String tenantId; + private String deviceOwner; + private MacAddress macAddress; + // <subnet id, ip address> + private HashMap<String, Ip4Address> fixedIps; + private String id; + private List<String> securityGroups; + private String deviceId; + + private OpenstackPort(PortStatus status, String name, boolean adminStateUp, + String networkId, String tenantId, String deviceOwner, + MacAddress macAddress, HashMap fixedIps, String id, + List<String> securityGroups, String deviceId) { + + this.status = status; + this.name = name; + this.adminStateUp = adminStateUp; + this.networkId = checkNotNull(networkId); + this.tenantId = checkNotNull(tenantId); + this.deviceOwner = deviceOwner; + this.macAddress = checkNotNull(macAddress); + this.fixedIps = checkNotNull(fixedIps); + this.id = checkNotNull(id); + this.securityGroups = securityGroups; + this.deviceId = deviceId; + } + + + + /** + * Returns OpenstackPort builder object. + * + * @return OpenstackPort builder + */ + public static OpenstackPort.Builder builder() { + return new Builder(); + } + + /** + * Returns port status. + * + * @return port status + */ + public PortStatus status() { + return status; + } + + /** + * Returns port name. + * + * @return port name + */ + public String name() { + return name; + } + + /** + * Returns whether admin state up or not. + * + * @return true if admin state up, false otherwise + */ + public boolean isAdminStateUp() { + return adminStateUp; + } + + /** + * Returns network ID. + * + * @return network ID + */ + public String networkId() { + return networkId; + } + + /** + * Returns device owner. + * + * @return device owner + */ + public String deviceOwner() { + return deviceOwner; + } + + /** + * Returns mac address. + * + * @return mac address + */ + public MacAddress macAddress() { + return macAddress; + } + + /** + * Returns the fixed IP information. + * + * @return fixed IP info + */ + public HashMap fixedIps() { + return fixedIps; + } + + /** + * Returns port ID. + * + * @return port ID + */ + public String id() { + return id; + } + + /** + * Returns security group information. + * + * @return security group info + */ + public List<String> securityGroups() { + return securityGroups; + } + + /** + * Returns device ID. + * + * @return device ID + */ + public String deviceId() { + return deviceId; + } + + // TODO : Implement the following functions when necessary + //@Override + //public void equals(Object that) { + // + //} + // + //@Override + //public int hashCode() { + // + //} + + /** + * OpenstackPort Builder class. + */ + public static final class Builder { + + private PortStatus status; + private String name; + // FIX_ME + private String allowedAddressPairs; + private boolean adminStateUp; + private String networkId; + private String tenantId; + private String deviceOwner; + private MacAddress macAddress; + // list of hash map <subnet id, ip address> + private HashMap<String, Ip4Address> fixedIps; + private String id; + private List<String> securityGroups; + private String deviceId; + + Builder() { + fixedIps = new HashMap<>(); + securityGroups = Lists.newArrayList(); + } + + /** + * Sets port status. + * + * @param status port status + * @return Builder object + */ + public Builder portStatus(PortStatus status) { + this.status = status; + + return this; + } + + /** + * Sets port name. + * + * @param name port name + * @return Builder object + */ + public Builder name(String name) { + this.name = name; + + return this; + } + + /** + * Sets whether admin state up or not. + * + * @param isAdminStateUp true if admin state is up, false otherwise + * @return Builder object + */ + public Builder adminState(boolean isAdminStateUp) { + this.adminStateUp = isAdminStateUp; + + return this; + } + + /** + * Sets network ID. + * + * @param networkId network ID + * @return Builder object + */ + public Builder netwrokId(String networkId) { + this.networkId = networkId; + + return this; + } + + /** + * Sets tenant ID. + * + * @param tenantId tenant ID + * @return Builder object + */ + public Builder tenantId(String tenantId) { + this.tenantId = tenantId; + + return this; + } + + /** + * Sets device owner. + * + * @param owner device owner + * @return Builder object + */ + public Builder deviceOwner(String owner) { + this.deviceOwner = owner; + + return this; + } + + /** + * Sets MAC address of the port. + * + * @param mac MAC address + * @return Builder object + */ + public Builder macAddress(MacAddress mac) { + this.macAddress = mac; + + return this; + } + + /** + * Sets Fixed IP address information. + * + * @param fixedIpList Fixed IP info + * @return Builder object + */ + public Builder fixedIps(HashMap<String, Ip4Address> fixedIpList) { + fixedIps.putAll(fixedIpList); + + return this; + } + + /** + * Sets ID of the port. + * + * @param id ID of the port + * @return Builder object + */ + public Builder id(String id) { + this.id = id; + + return this; + } + + /** + * Sets security group of the port. + * + * @param securityGroup security group of the port + * @return Builder object + */ + public Builder securityGroup(String securityGroup) { + securityGroups.add(securityGroup); + + return this; + } + + /** + * Sets device ID of the port. + * + * @param deviceId device ID + * @return Builder object + */ + public Builder deviceId(String deviceId) { + this.deviceId = deviceId; + + return this; + } + + /** + * Builds an OpenstackPort object. + * + * @return OpenstackPort objecet + */ + public OpenstackPort build() { + return new OpenstackPort(status, name, adminStateUp, networkId, networkId, + deviceOwner, macAddress, fixedIps, id, securityGroups, deviceId); + } + } +} + diff --git a/framework/src/onos/apps/openstackswitching/src/main/java/org/onosproject/openstackswitching/OpenstackSwitchingManager.java b/framework/src/onos/apps/openstackswitching/src/main/java/org/onosproject/openstackswitching/OpenstackSwitchingManager.java new file mode 100644 index 00000000..baae7f80 --- /dev/null +++ b/framework/src/onos/apps/openstackswitching/src/main/java/org/onosproject/openstackswitching/OpenstackSwitchingManager.java @@ -0,0 +1,437 @@ +/* + * 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.Lists; +import com.google.common.collect.Maps; +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.IPv4; +import org.onlab.packet.Ip4Address; +import org.onlab.packet.Ip4Prefix; +import org.onlab.packet.MacAddress; +import org.onlab.packet.UDP; +import org.onosproject.core.ApplicationId; +import org.onosproject.core.CoreService; +import org.onosproject.net.Device; +import org.onosproject.net.DeviceId; +import org.onosproject.net.Port; +import org.onosproject.net.device.DeviceEvent; +import org.onosproject.net.device.DeviceListener; +import org.onosproject.net.device.DeviceService; +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.HashMap; +import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +@SuppressWarnings("ALL") +@Service +@Component(immediate = true) +/** + * It 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; + + + public static final int DHCP_PORT = 67; + + private ApplicationId appId; + private OpenstackArpHandler arpHandler; + private OpenstackDhcpHandler dhcpHandler = new OpenstackDhcpHandler(); + private OpenstackSwitchingRulePopulator rulePopulator; + private ExecutorService deviceEventExcutorService = Executors.newFixedThreadPool(10); + + private InternalPacketProcessor internalPacketProcessor = new InternalPacketProcessor(); + private InternalDeviceListener internalDeviceListener = new InternalDeviceListener(); + + // Map <port_id, OpenstackPort> + private HashMap<String, OpenstackPort> openstackPortMap; + // Map <network_id, OpenstackNetwork> + private HashMap<String, OpenstackNetwork> openstackNetworkMap; + // Map <vni, List <Entry <portName, host ip>> + private HashMap<String, List<PortInfo>> vniPortMap; + private HashMap<Ip4Address, Port> tunnelPortMap; + + + @Activate + protected void activate() { + appId = coreService + .registerApplication("org.onosproject.openstackswitching"); + rulePopulator = new OpenstackSwitchingRulePopulator(appId, flowObjectiveService); + packetService.addProcessor(internalPacketProcessor, PacketProcessor.director(1)); + deviceService.addListener(internalDeviceListener); + + openstackPortMap = Maps.newHashMap(); + openstackNetworkMap = Maps.newHashMap(); + vniPortMap = Maps.newHashMap(); + tunnelPortMap = Maps.newHashMap(); + + arpHandler = new OpenstackArpHandler(openstackPortMap); + + log.info("Started"); + } + + @Deactivate + protected void deactivate() { + packetService.removeProcessor(internalPacketProcessor); + deviceService.removeListener(internalDeviceListener); + + deviceEventExcutorService.shutdown(); + + log.info("Stopped"); + } + + @Override + public void createPorts(OpenstackPort openstackPort) { + openstackPortMap.put(openstackPort.id(), openstackPort); + } + + @Override + public void deletePorts() { + + } + + @Override + public void updatePorts() { + + } + + @Override + public void createNetwork(OpenstackNetwork openstackNetwork) { + openstackNetworkMap.put(openstackNetwork.id(), openstackNetwork); + } + + private void processDeviceAdded(Device device) { + log.warn("device {} is added", device.id()); + rulePopulator.populateDefaultRules(device.id()); + } + + private void processPortAdded(Device device, Port port) { + // TODO: Simplify the data structure to store the network info + // TODO: Make it stateless + // TODO: All the logics need to be processed inside of the rulePopulator class + synchronized (vniPortMap) { + log.warn("port {} is updated", port.toString()); + + updatePortMaps(device, port); + if (!port.annotations().value("portName").equals("vxlan")) { + populateFlowRulesForTrafficToSameCnode(device, port); + populateFlowRulesForTrafficToDifferentCnode(device, port); + } + } + } + + private void processPortRemoved(Device device, Port port) { + log.warn("port {} is removed", port.toString()); + // TODO: need to update the vniPortMap + } + + /** + * 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); + // TODO: Avoid duplicate flow rule set up for VMs in other Cnode + // (possibly avoided by flowrule subsystem?) + if (tunnelPortMap.get(hostIpAddress) == null) { + log.warn("There is no tunnel port information"); + return; + } + String vni = getVniForPort(portName); + MacAddress vmMac = getVmMacAddressForPort(portName); + if (!vniPortMap.isEmpty() && vniPortMap.get(vni) != null) { + for (PortInfo portInfo : vniPortMap.get(vni)) { + if (!portInfo.portName.equals(portName) && + !portInfo.hostIp.equals(hostIpAddress)) { + MacAddress vmMacx = getVmMacAddressForPort(portInfo.portName); + rulePopulator.populateForwardingRuleForOtherCnode(vni, + device.id(), portInfo.hostIp, portInfo.fixedIp, vmMacx, + tunnelPortMap.get(hostIpAddress).number(), + portInfo.deviceId, hostIpAddress, fixedIp, vmMac, + tunnelPortMap.get(portInfo.hostIp).number()); + } + } + } + } + + /** + * 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) { + Ip4Prefix cidr = getCidrForPort(port.annotations().value("portName")); + Ip4Address vmIp = getFixedIpAddressForPort(port.annotations().value("portName")); + if (vmIp != null) { + rulePopulator.populateForwardingRule(vmIp, device.id(), port, cidr); + } + } + + /** + * Updates the port maps using the port information. + * + * @param device device info + * @param port port of the VM + */ + private void updatePortMaps(Device device, Port port) { + String portName = port.annotations().value("portName"); + String channelId = device.annotations().value("channelId"); + Ip4Address hostIpAddress = Ip4Address.valueOf(channelId.split(":")[0]); + if (portName.startsWith("vxlan")) { + tunnelPortMap.put(hostIpAddress, port); + } else { + String vni = getVniForPort(portName); + Ip4Address fixedIp = getFixedIpAddressForPort(portName); + if (vniPortMap.get(vni) == null) { + vniPortMap.put(vni, Lists.newArrayList()); + } + vniPortMap.get(vni).add(new PortInfo(device.id(), portName, fixedIp, hostIpAddress)); + } + } + + /** + * Returns CIDR information from the subnet map for the port. + * + * @param portName port name of the port of the VM + * @return CIDR of the VNI of the VM + */ + private Ip4Prefix getCidrForPort(String portName) { + String networkId = null; + String uuid = portName.substring(3); + OpenstackPort port = openstackPortMap.values().stream() + .filter(p -> p.id().startsWith(uuid)) + .findFirst().get(); + if (port == null) { + log.warn("No port information for port {}", portName); + return null; + } + + //OpenstackSubnet subnet = openstackSubnetMap.values().stream() + // .filter(s -> s.networkId().equals(port.networkId())) + // .findFirst().get(); + //if (subnet == null) { + // log.warn("No subnet information for network {}", subnet.id()); + // return null; + //} + + //return Ip4Prefix.valueOf(subnet.cidr()); + return null; + } + + /** + * Returns the VNI of the VM of the port. + * + * @param portName VM port + * @return VNI + */ + private String getVniForPort(String portName) { + String networkId = null; + String uuid = portName.substring(3); + OpenstackPort port = openstackPortMap.values().stream() + .filter(p -> p.id().startsWith(uuid)) + .findFirst().get(); + if (port == null) { + log.warn("No port information for port {}", portName); + return null; + } + OpenstackNetwork network = openstackNetworkMap.values().stream() + .filter(n -> n.id().equals(port.networkId())) + .findFirst().get(); + if (network == null) { + log.warn("No VNI information for network {}", network.id()); + 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) { + + // FIXME - For now we use the information stored from neutron Rest API call. + // TODO - Later, the information needs to be extracted from Neutron on-demand. + String uuid = portName.substring(3); + OpenstackPort port = openstackPortMap.values().stream() + .filter(p -> p.id().startsWith(uuid)) + .findFirst().get(); + + 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 = openstackPortMap.values().stream() + .filter(p -> p.id().startsWith(uuid)) + .findFirst().get(); + + if (port == null) { + log.error("There is no mac information for port name {}", portName); + return null; + } + + return port.macAddress(); + } + + 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); + } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) { + IPv4 ipPacket = (IPv4) ethernet.getPayload(); + + if (ipPacket.getProtocol() == IPv4.PROTOCOL_UDP) { + UDP udpPacket = (UDP) ipPacket.getPayload(); + if (udpPacket.getDestinationPort() == DHCP_PORT) { + dhcpHandler.processPacketIn(pkt); + } + } + } + } + } + + private class InternalDeviceListener implements DeviceListener { + + @Override + public void event(DeviceEvent event) { + deviceEventExcutorService.execute(new InternalEventHandler(event)); + } + } + + private class InternalEventHandler implements Runnable { + + volatile DeviceEvent deviceEvent; + + InternalEventHandler(DeviceEvent deviceEvent) { + this.deviceEvent = deviceEvent; + } + + @Override + public void run() { + 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 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/src/main/java/org/onosproject/openstackswitching/OpenstackSwitchingRulePopulator.java b/framework/src/onos/apps/openstackswitching/src/main/java/org/onosproject/openstackswitching/OpenstackSwitchingRulePopulator.java new file mode 100644 index 00000000..9ead05f0 --- /dev/null +++ b/framework/src/onos/apps/openstackswitching/src/main/java/org/onosproject/openstackswitching/OpenstackSwitchingRulePopulator.java @@ -0,0 +1,228 @@ +/* +* 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.IPv4; +import org.onlab.packet.Ip4Address; +import org.onlab.packet.Ip4Prefix; +import org.onlab.packet.MacAddress; +import org.onlab.packet.TpPort; +import org.onosproject.core.ApplicationId; +import org.onosproject.net.DeviceId; +import org.onosproject.net.Port; +import org.onosproject.net.PortNumber; +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.flowobjective.DefaultForwardingObjective; +import org.onosproject.net.flowobjective.FlowObjectiveService; +import org.onosproject.net.flowobjective.ForwardingObjective; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * It populates switching flow rules. + * + */ +public class OpenstackSwitchingRulePopulator { + + private static Logger log = LoggerFactory + .getLogger(OpenstackSwitchingRulePopulator.class); + + private FlowObjectiveService flowObjectiveService; + private ApplicationId appId; + + /** + * Creates OpenstackSwitchingRulPopulator. + * + * @param appId application id + * @param flowObjectiveService FlowObjectiveService reference + */ + public OpenstackSwitchingRulePopulator(ApplicationId appId, + FlowObjectiveService flowObjectiveService) { + this.flowObjectiveService = flowObjectiveService; + this.appId = appId; + } + + /** + * Populates flows rules for forwarding packets to and from VMs. + * + * @param ip v4 IP Address + * @param id device ID + * @param port port + * @param cidr v4 IP prefix + * @return true if it succeeds to populate rules, false otherwise. + */ + public boolean populateForwardingRule(Ip4Address ip, DeviceId id, Port port, Ip4Prefix cidr) { + + + setFlowRuleForVMsInSameCnode(ip, id, port, cidr); + + return true; + } + + /** + * Populates the common flows rules for all VMs. + * + * - Send ARP packets to the controller + * - Send DHCP packets to the controller + * + * @param id Device ID to populates rules to + */ + public void populateDefaultRules(DeviceId id) { + + //setFlowRuleForDHCP(id); + setFlowRuleForArp(id); + + log.warn("Default rule has been set"); + } + + /** + * Populates the forwarding rules for VMs with the same VNI but in other Code. + * + * @param vni VNI for the networks + * @param id device ID to populates the flow rules + * @param hostIp host IP address of the VM + * @param vmIp fixed IP address for the VM + * @param vmMac MAC address for the VM + * @param tunnelPort tunnel port number for the VM + * @param idx device ID for OVS of the other VM + * @param hostIpx host IP address of the other VM + * @param vmIpx fixed IP address of the other VM + * @param vmMacx MAC address for the other VM + * @param tunnelPortx x tunnel port number for other VM + */ + public void populateForwardingRuleForOtherCnode(String vni, DeviceId id, Ip4Address hostIp, + Ip4Address vmIp, MacAddress vmMac, PortNumber tunnelPort, + DeviceId idx, Ip4Address hostIpx, + Ip4Address vmIpx, MacAddress vmMacx, PortNumber tunnelPortx) { + setVxLanFlowRule(vni, id, hostIp, vmIp, vmMac, tunnelPort); + setVxLanFlowRule(vni, idx, hostIpx, vmIpx, vmMacx, tunnelPortx); + } + + /** + * Populates the flow rules for DHCP packets from VMs. + * + * @param id device ID to set the rules + */ + private void setFlowRuleForDHCP(DeviceId id) { + TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); + TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); + + sBuilder.matchEthType(Ethernet.TYPE_IPV4) + .matchIPProtocol(IPv4.PROTOCOL_UDP) + .matchUdpDst(TpPort.tpPort(OpenstackSwitchingManager.DHCP_PORT)); + tBuilder.setOutput(PortNumber.CONTROLLER); + + ForwardingObjective fo = DefaultForwardingObjective.builder() + .withSelector(sBuilder.build()) + .withTreatment(tBuilder.build()) + .withPriority(5000) + .withFlag(ForwardingObjective.Flag.VERSATILE) + .fromApp(appId) + .add(); + + flowObjectiveService.forward(id, fo); + } + + /** + * Populates the flow rules for ARP packets from VMs. + * + * @param id device ID to put rules. + */ + private void setFlowRuleForArp(DeviceId id) { + TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); + TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); + + sBuilder.matchEthType(Ethernet.TYPE_ARP); + tBuilder.setOutput(PortNumber.CONTROLLER); + + ForwardingObjective fo = DefaultForwardingObjective.builder() + .withSelector(sBuilder.build()) + .withTreatment(tBuilder.build()) + .withPriority(5000) + .withFlag(ForwardingObjective.Flag.VERSATILE) + .fromApp(appId) + .add(); + + flowObjectiveService.forward(id, fo); + } + + /** + * 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 + * @param cidr subnet info of the VMs + */ + private void setFlowRuleForVMsInSameCnode(Ip4Address ip4Address, DeviceId id, + Port port, Ip4Prefix cidr) { + TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); + TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); + + sBuilder.matchEthType(Ethernet.TYPE_IPV4) + .matchIPDst(ip4Address.toIpPrefix()) + .matchIPSrc(cidr); + tBuilder.setOutput(port.number()); + + ForwardingObjective fo = DefaultForwardingObjective.builder() + .withSelector(sBuilder.build()) + .withTreatment(tBuilder.build()) + .withPriority(5000) + .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 + * @param tunnelPort tunnel port to forward traffic to + */ + private void setVxLanFlowRule(String vni, DeviceId id, Ip4Address hostIp, + Ip4Address vmIp, MacAddress vmMac, PortNumber tunnelPort) { + TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); + TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); + + sBuilder.matchEthType(Ethernet.TYPE_IPV4) + .matchIPDst(vmIp.toIpPrefix()); + tBuilder.setTunnelId(Long.parseLong(vni)) + //.setTunnelDst() <- for Nicira ext + //.setEthDst(vmMac) + .setOutput(tunnelPort); + + ForwardingObjective fo = DefaultForwardingObjective.builder() + .withSelector(sBuilder.build()) + .withTreatment(tBuilder.build()) + .withPriority(5000) + .withFlag(ForwardingObjective.Flag.VERSATILE) + .fromApp(appId) + .add(); + + flowObjectiveService.forward(id, fo); + } +} diff --git a/framework/src/onos/apps/openstackswitching/src/main/java/org/onosproject/openstackswitching/OpenstackSwitchingService.java b/framework/src/onos/apps/openstackswitching/src/main/java/org/onosproject/openstackswitching/OpenstackSwitchingService.java new file mode 100644 index 00000000..d97b39c8 --- /dev/null +++ b/framework/src/onos/apps/openstackswitching/src/main/java/org/onosproject/openstackswitching/OpenstackSwitchingService.java @@ -0,0 +1,49 @@ +/* + * 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; + +/** + * It handles port management REST API from Openstack for VMs. + */ +public interface OpenstackSwitchingService { + + /** + * Store the port information created by Openstack. + * + * @param openstackPort port information + */ + void createPorts(OpenstackPort openstackPort); + + /** + * Removes flow rules corresponding to the port removed by Openstack. + * + */ + void deletePorts(); + + /** + * Updates flow rules corresponding to the port information updated by Openstack. + * + */ + void updatePorts(); + + /** + * Store the network information created by openstack. + * + * @param openstackNetwork network information + */ + void createNetwork(OpenstackNetwork openstackNetwork); + +} diff --git a/framework/src/onos/apps/openstackswitching/src/main/java/org/onosproject/openstackswitching/package-info.java b/framework/src/onos/apps/openstackswitching/src/main/java/org/onosproject/openstackswitching/package-info.java new file mode 100644 index 00000000..cd50f912 --- /dev/null +++ b/framework/src/onos/apps/openstackswitching/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/src/main/java/org/onosproject/openstackswitching/web/OpenstackNetworkCodec.java b/framework/src/onos/apps/openstackswitching/src/main/java/org/onosproject/openstackswitching/web/OpenstackNetworkCodec.java new file mode 100644 index 00000000..43bd1583 --- /dev/null +++ b/framework/src/onos/apps/openstackswitching/src/main/java/org/onosproject/openstackswitching/web/OpenstackNetworkCodec.java @@ -0,0 +1,64 @@ +/* + * 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); + + 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.name(networkInfo.path(NETWORK_TYPE).asText()); + onb.segmentId(networkInfo.path(SEGMENTATION_ID).asText()); + } + + return onb.build(); + } + +} diff --git a/framework/src/onos/apps/openstackswitching/src/main/java/org/onosproject/openstackswitching/web/OpenstackNetworkWebResource.java b/framework/src/onos/apps/openstackswitching/src/main/java/org/onosproject/openstackswitching/web/OpenstackNetworkWebResource.java new file mode 100644 index 00000000..f4c401fb --- /dev/null +++ b/framework/src/onos/apps/openstackswitching/src/main/java/org/onosproject/openstackswitching/web/OpenstackNetworkWebResource.java @@ -0,0 +1,62 @@ +/* + * 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.OpenstackNetwork; +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.POST; +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; + +@Path("networks") +public class OpenstackNetworkWebResource extends AbstractWebResource { + + protected static final Logger log = LoggerFactory + .getLogger(OpenstackNetworkWebResource.class); + + private static final OpenstackNetworkCodec NETWORK_CODEC = new OpenstackNetworkCodec(); + + @POST + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public Response createNetwork(InputStream input) { + try { + ObjectMapper mapper = new ObjectMapper(); + ObjectNode networkNode = (ObjectNode) mapper.readTree(input); + + OpenstackNetwork openstackNetwork = NETWORK_CODEC.decode(networkNode, this); + + OpenstackSwitchingService switchingService = get(OpenstackSwitchingService.class); + switchingService.createNetwork(openstackNetwork); + return Response.status(Response.Status.OK).build(); + } catch (Exception e) { + log.error("Creates VirtualPort failed because of exception {}", + e.toString()); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString()) + .build(); + } + } +} diff --git a/framework/src/onos/apps/openstackswitching/src/main/java/org/onosproject/openstackswitching/web/OpenstackPortCodec.java b/framework/src/onos/apps/openstackswitching/src/main/java/org/onosproject/openstackswitching/web/OpenstackPortCodec.java new file mode 100644 index 00000000..765b6901 --- /dev/null +++ b/framework/src/onos/apps/openstackswitching/src/main/java/org/onosproject/openstackswitching/web/OpenstackPortCodec.java @@ -0,0 +1,104 @@ +/* + * 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; + +/** + * It 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); + + 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/src/main/java/org/onosproject/openstackswitching/web/OpenstackPortWebResource.java b/framework/src/onos/apps/openstackswitching/src/main/java/org/onosproject/openstackswitching/web/OpenstackPortWebResource.java new file mode 100644 index 00000000..67a9cebb --- /dev/null +++ b/framework/src/onos/apps/openstackswitching/src/main/java/org/onosproject/openstackswitching/web/OpenstackPortWebResource.java @@ -0,0 +1,106 @@ +/* + * 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; + +@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 = get(OpenstackSwitchingService.class); + switchingService.createPorts(openstackPort); + log.info("REST API ports is called with {}", portNode.toString()); + return Response.status(Response.Status.OK).build(); + } catch (Exception e) { + log.error("Creates VirtualPort failed because of exception {}", + e.toString()); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString()) + .build(); + } + } + + @DELETE + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public Response deletesPorts(InputStream input) { + try { + ObjectMapper mapper = new ObjectMapper(); + ObjectNode portNode = (ObjectNode) mapper.readTree(input); + + OpenstackSwitchingService switchingService = get(OpenstackSwitchingService.class); + switchingService.deletePorts(); + log.info("REST API ports is called with {}", portNode.toString()); + return Response.status(Response.Status.OK).build(); + } catch (Exception e) { + log.error("Delete VirtualPort failed because of exception {}", + e.toString()); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString()) + .build(); + } + } + + @PUT + @Path("{id}") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public Response updatePorts(InputStream input) { + try { + ObjectMapper mapper = new ObjectMapper(); + ObjectNode portNode = (ObjectNode) mapper.readTree(input); + + OpenstackSwitchingService switchingService = get(OpenstackSwitchingService.class); + switchingService.updatePorts(); + log.info("REST API ports is called with {}", portNode.toString()); + return Response.status(Response.Status.OK).build(); + } catch (Exception e) { + log.error("Update VirtualPort failed because of exception {}", + e.toString()); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString()) + .build(); + } + } +} diff --git a/framework/src/onos/apps/openstackswitching/src/main/java/org/onosproject/openstackswitching/web/package-info.java b/framework/src/onos/apps/openstackswitching/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/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/src/main/webapp/WEB-INF/web.xml b/framework/src/onos/apps/openstackswitching/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 00000000..53b0e2e9 --- /dev/null +++ b/framework/src/onos/apps/openstackswitching/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,44 @@ +<?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 + </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> |