sanghoshin
Committed by Gerrit Code Review

SONA: openstackSwitching

 - Added event handlers  (DEVICE_XX, PORT_XXX) and tested with mininet
 - Added default flow rule setup for ARP and DHCP
 - Added neutron network API handler
 - Added the feature to populate flow rules for the same subnet
 - Added the feature to populate flow rules for VMs in other Cnode using Nicira ext.
 - Modified the directory structure
 - Fixed nicira ext handling logic
 - Fixed neutron network API handler
 - Added the tenant isolation feature by checking the source IP address

Change-Id: I076d21f3c90f458727e33cb36b47d9b14ccfd68f
<?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>
/*
* 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;
/**
* Returns OpenstackArpHandler reference.
*
* @param openstackPortMap
*/
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");
}
}
/*
* 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");
}
}
/*
* 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);
}
}
}
/*
* 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);
}
}
}
/*
* 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;
/**
* Returns OpenstackSwitchingRule reference.
* @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.
*
* @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 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
*/
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);
}
}
/*
* 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);
}
/*
* 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();
}
}
/*
* 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();
}
}
}
/*
* 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;
}
}
/*
* 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();
}
}
}
<?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>
......@@ -13,10 +13,7 @@
~ 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">
--><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>
......@@ -60,6 +57,7 @@
<module>igmp</module>
<module>pim</module>
<module>mlb</module>
<module>openstackswitching</module>
</modules>
<properties>
......