Devstack support for nicira plugin

Bug 1116847

Includes slight refactoring of L3 logic in main lib/quantum as previous
logic treated l3-agent as only way to do L3, whereas NVP has its own L3
support.

Change-Id: Ifd8c6864add5939432b544285cf027e52329dea2
diff --git a/lib/quantum b/lib/quantum
index daf945a..0fef1ae 100644
--- a/lib/quantum
+++ b/lib/quantum
@@ -284,14 +284,13 @@
         SUBNET_ID=$(quantum subnet-create --tenant_id $TENANT_ID --ip_version 4 --gateway $NETWORK_GATEWAY $NET_ID $FIXED_RANGE | grep ' id ' | get_field 2)
     fi
 
-    if is_service_enabled q-l3; then
+    if [[ "$Q_L3_ENABLED" == "True" ]]; then
         # Create a router, and add the private subnet as one of its interfaces
-        if [[ "$Q_USE_NAMESPACE" == "True" ]]; then
-            # If namespaces are enabled, create a tenant-owned router.
+        if [[ "$Q_L3_ROUTER_PER_TENANT" == "True" ]]; then
+            # create a tenant-owned router.
             ROUTER_ID=$(quantum router-create --tenant_id $TENANT_ID $Q_ROUTER_NAME | grep ' id ' | get_field 2)
         else
-            # If namespaces are disabled, the L3 agent can only target
-            # a single router, which should not be tenant-owned.
+            # Plugin only supports creating a single router, which should be admin owned.
             ROUTER_ID=$(quantum router-create $Q_ROUTER_NAME | grep ' id ' | get_field 2)
         fi
         quantum router-interface-add $ROUTER_ID $SUBNET_ID
@@ -300,16 +299,19 @@
         EXT_GW_IP=$(quantum subnet-create --ip_version 4 ${Q_FLOATING_ALLOCATION_POOL:+--allocation-pool $Q_FLOATING_ALLOCATION_POOL} $EXT_NET_ID $FLOATING_RANGE -- --enable_dhcp=False | grep 'gateway_ip' | get_field 2)
         quantum router-gateway-set $ROUTER_ID $EXT_NET_ID
 
-        if is_quantum_ovs_base_plugin && [[ "$Q_USE_NAMESPACE" = "True" ]]; then
-            CIDR_LEN=${FLOATING_RANGE#*/}
-            sudo ip addr add $EXT_GW_IP/$CIDR_LEN dev $PUBLIC_BRIDGE
-            sudo ip link set $PUBLIC_BRIDGE up
-            ROUTER_GW_IP=`quantum port-list -c fixed_ips -c device_owner | grep router_gateway | awk -F '"' '{ print $8; }'`
-            sudo route add -net $FIXED_RANGE gw $ROUTER_GW_IP
-        fi
-        if [[ "$Q_USE_NAMESPACE" == "False" ]]; then
-            # Explicitly set router id in l3 agent configuration
-            iniset $Q_L3_CONF_FILE DEFAULT router_id $ROUTER_ID
+        if is_service_enabled q-l3; then
+            # logic is specific to using the l3-agent for l3
+            if is_quantum_ovs_base_plugin && [[ "$Q_USE_NAMESPACE" = "True" ]]; then
+                CIDR_LEN=${FLOATING_RANGE#*/}
+                sudo ip addr add $EXT_GW_IP/$CIDR_LEN dev $PUBLIC_BRIDGE
+                sudo ip link set $PUBLIC_BRIDGE up
+                ROUTER_GW_IP=`quantum port-list -c fixed_ips -c device_owner | grep router_gateway | awk -F '"' '{ print $8; }'`
+                sudo route add -net $FIXED_RANGE gw $ROUTER_GW_IP
+            fi
+            if [[ "$Q_USE_NAMESPACE" == "False" ]]; then
+                # Explicitly set router id in l3 agent configuration
+                iniset $Q_L3_CONF_FILE DEFAULT router_id $ROUTER_ID
+            fi
         fi
    fi
 }
@@ -450,6 +452,9 @@
 }
 
 function _configure_quantum_l3_agent() {
+    Q_L3_ENABLED=True
+    # for l3-agent, only use per tenant router if we have namespaces
+    Q_L3_ROUTER_PER_TENANT=$Q_USE_NAMESPACE
     AGENT_L3_BINARY="$QUANTUM_DIR/bin/quantum-l3-agent"
     PUBLIC_BRIDGE=${PUBLIC_BRIDGE:-br-ex}
     Q_L3_CONF_FILE=$QUANTUM_CONF_DIR/l3_agent.ini
diff --git a/lib/quantum_plugins/nicira b/lib/quantum_plugins/nicira
new file mode 100644
index 0000000..bc9a36f
--- /dev/null
+++ b/lib/quantum_plugins/nicira
@@ -0,0 +1,150 @@
+# Quantum Nicira NVP plugin
+# ---------------------------
+
+# Save trace setting
+MY_XTRACE=$(set +o | grep xtrace)
+set +o xtrace
+
+source $TOP_DIR/lib/quantum_plugins/ovs_base
+
+function setup_integration_bridge() {
+    OVS_BRIDGE=${OVS_BRIDGE:-br-int}
+    _quantum_ovs_base_setup_bridge $OVS_BRIDGE
+    # Set manager to NVP controller (1st of list)
+    if [[ "$NVP_CONTROLLERS" != "" ]]; then
+        # Get the first controller
+        controllers=(${NVP_CONTROLLERS//,/ })
+        OVS_MGR_IP=${controllers[0]}
+    elif [[ "$NVP_CONTROLLER_CONNECTION" != "" ]]; then
+        conn=(${NVP_CONTROLLER_CONNECTION//\:/ })
+        OVS_MGR_IP=${conn[0]}
+    else
+        echo "Error - No controller specified. Unable to set a manager for OVS"
+        exit 1
+    fi
+    sudo ovs-vsctl set-manager ssl:$OVS_MGR_IP
+}
+
+function is_quantum_ovs_base_plugin() {
+    # NVP uses OVS, but not the l3-agent
+    return 0
+}
+
+function quantum_plugin_create_nova_conf() {
+    NOVA_VIF_DRIVER=${NOVA_VIF_DRIVER:-"nova.virt.libvirt.vif.LibvirtOpenVswitchDriver"}
+    # if n-cpu is enabled, then setup integration bridge
+    if is_service_enabled n-cpu; then
+        setup_integration_bridge
+    fi
+}
+
+function quantum_plugin_install_agent_packages() {
+    # Nicira Plugin does not run q-agt
+    :
+}
+
+function quantum_plugin_configure_common() {
+    Q_PLUGIN_CONF_PATH=etc/quantum/plugins/nicira
+    Q_PLUGIN_CONF_FILENAME=nvp.ini
+    Q_DB_NAME="quantum_nvp"
+    Q_PLUGIN_CLASS="quantum.plugins.nicira.nicira_nvp_plugin.QuantumPlugin.NvpPluginV2"
+}
+
+function quantum_plugin_configure_debug_command() {
+    :
+}
+
+function quantum_plugin_configure_dhcp_agent() {
+    setup_integration_bridge
+    iniset $Q_DHCP_CONF_FILE DEFAULT enable_isolated_metadata True
+    iniset $Q_DHCP_CONF_FILE DEFAULT enable_metadata_network True
+    iniset $Q_DHCP_CONF_FILE DEFAULT ovs_use_veth True
+}
+
+function quantum_plugin_configure_l3_agent() {
+   # Nicira plugin does not run L3 agent
+   echo "ERROR - q-l3 should must not be executed with Nicira plugin!"
+   exit 1
+}
+
+function quantum_plugin_configure_plugin_agent() {
+   # Nicira plugin does not run L2 agent
+   echo "ERROR - q-agt must not be executed with Nicira plugin!"
+   exit 1
+}
+
+function quantum_plugin_configure_service() {
+    if [[ "$MAX_LP_PER_BRIDGED_LS" != "" ]]; then
+        iniset /$Q_PLUGIN_CONF_FILE NVP max_lp_per_bridged_ls $MAX_LP_PER_BRIDGED_LS
+    fi
+    if [[ "$MAX_LP_PER_OVERLAY_LS" != "" ]]; then
+        iniset /$Q_PLUGIN_CONF_FILE NVP max_lp_per_overlay_ls $MAX_LP_PER_OVERLAY_LS
+    fi
+    if [[ "$FAILOVER_TIME" != "" ]]; then
+        iniset /$Q_PLUGIN_CONF_FILE NVP failover_time $FAILOVER_TIME
+    fi
+    if [[ "$CONCURRENT_CONNECTIONS" != "" ]]; then
+        iniset /$Q_PLUGIN_CONF_FILE NVP concurrent_connections $CONCURRENT_CONNECTIONS
+    fi
+
+    if [[ "$DEFAULT_CLUSTER" != "" ]]; then
+        # Make name shorter for sake of readability
+        DC=$DEFAULT_CLUSTER
+        if [[ "$DEFAULT_TZ_UUID" != "" ]]; then
+            iniset /$Q_PLUGIN_CONF_FILE "CLUSTER:$DC" default_tz_uuid $DEFAULT_TZ_UUID
+        else
+            echo "ERROR - The nicira plugin won't work without a default transport zone."
+            exit 1
+        fi
+        if [[ "$DEFAULT_L3_GW_SVC_UUID" != "" ]]; then
+            iniset /$Q_PLUGIN_CONF_FILE "CLUSTER:$DC" default_l3_gw_service_uuid $DEFAULT_L3_GW_SVC_UUID
+            Q_L3_ENABLED=True
+            Q_L3_ROUTER_PER_TENANT=True
+            iniset /$Q_PLUGIN_CONF_FILE NVP enable_metadata_access_network True
+        else
+            echo "WARNING - No l3 gw service enabled.  You will not be able to use the L3 API extension"
+        fi
+        if [[ "$DEFAULT_L2_GW_SVC_UUID" != "" ]]; then
+            iniset /$Q_PLUGIN_CONF_FILE "CLUSTER:$DC" default_l2_gw_service_uuid $DEFAULT_L2_GW_SVC_UUID
+        fi
+        # NVP_CONTROLLERS must be a comma separated string
+        if [[ "$NVP_CONTROLLERS" != "" ]]; then
+            iniset /$Q_PLUGIN_CONF_FILE "CLUSTER:$DC" nvp_controllers $NVP_CONTROLLERS
+        elif [[ "$NVP_CONTROLLER_CONNECTION" != "" ]]; then
+            # Only 1 controller can be specified in this case
+            iniset /$Q_PLUGIN_CONF_FILE "CLUSTER:$DC" nvp_controller_connection $NVP_CONTROLLER_CONNECTION
+        else
+            echo "ERROR - The nicira plugin needs at least an NVP controller."
+            exit 1
+        fi
+        if [[ "$NVP_USER" != "" ]]; then
+            iniset /$Q_PLUGIN_CONF_FILE "CLUSTER:$DC" nvp_user $NVP_USER
+        fi
+        if [[ "$NVP_PASSWORD" != "" ]]; then
+            iniset /$Q_PLUGIN_CONF_FILE "CLUSTER:$DC" nvp_password $NVP_PASSWORD
+        fi
+        if [[ "$NVP_REQ_TIMEOUT" != "" ]]; then
+            iniset /$Q_PLUGIN_CONF_FILE "CLUSTER:$DC" req_timeout $NVP_REQ_TIMEOUT
+        fi
+        if [[ "$NVP_HTTP_TIMEOUT" != "" ]]; then
+            iniset /$Q_PLUGIN_CONF_FILE "CLUSTER:$DC" http_timeout $NVP_HTTP_TIMEOUT
+        fi
+        if [[ "$NVP_RETRIES" != "" ]]; then
+            iniset /$Q_PLUGIN_CONF_FILE "CLUSTER:$DC" retries $NVP_RETRIES
+        fi
+        if [[ "$NVP_REDIRECTS" != "" ]]; then
+            iniset /$Q_PLUGIN_CONF_FILE "CLUSTER:$DC" redirects $NVP_REDIRECTS
+        fi
+    else
+        echo "ERROR - Default cluster not configured. Quantum will not start"
+        exit 1
+    fi
+}
+
+function quantum_plugin_setup_interface_driver() {
+    local conf_file=$1
+    iniset $conf_file DEFAULT interface_driver quantum.agent.linux.interface.OVSInterfaceDriver
+}
+
+# Restore xtrace
+$MY_XTRACE