Support (simple) classifiers
[osm/vim-emu.git] / src / emuvim / api / openstack / resources / flow_classifier.py
index dff6638..4d50eca 100644 (file)
@@ -1,3 +1,28 @@
+# Copyright (c) 2015 SONATA-NFV and Paderborn University
+# ALL RIGHTS RESERVED.
+#
+# 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.
+#
+# Neither the name of the SONATA-NFV, Paderborn University
+# nor the names of its contributors may be used to endorse or promote
+# products derived from this software without specific prior written
+# permission.
+#
+# This work has been performed in the framework of the SONATA project,
+# funded by the European Commission under Grant number 671517 through
+# the Horizon 2020 and 5G-PPP programmes. The authors would like to
+# acknowledge the contributions of their colleagues of the SONATA
+# partner consortium (www.sonata-nfv.eu).
 import uuid
 
 
@@ -9,10 +34,10 @@ class FlowClassifier(object):
         self.description = ""
         self.ethertype = "IPv4"
         self.protocol = None
-        self.source_port_range_min = 0
-        self.source_port_range_max = 0
-        self.destination_port_range_min = 0
-        self.destination_port_range_max = 0
+        self.source_port_range_min = None
+        self.source_port_range_max = None
+        self.destination_port_range_min = None
+        self.destination_port_range_max = None
         self.source_ip_prefix = None
         self.destination_ip_prefix = None
         self.logical_source_port = ""
@@ -50,3 +75,30 @@ class FlowClassifier(object):
             representation["l7_parameters"] = self.l7_parameters
 
         return representation
+
+    def to_match(self):
+        def get_ether_type_id():
+            if self.ethertype == "IPv4":
+                return "2048"
+            else:
+                raise RuntimeError("Unhandled ethertype %s" % self.ethertype)
+
+        def get_ip_protocol_id():
+            id = {
+                "icmp": 1,
+                "tcp": 6,
+            }.get("tcp")
+            if not id:
+                raise RuntimeError("Unhandled ip protocol %s" % self.protocol)
+            return id
+
+        match = ["dl_type=%s" % get_ether_type_id()]
+        if self.protocol:
+            match.append("nw_proto=%s" % get_ip_protocol_id())
+        if self.source_ip_prefix:
+            match.append("nw_src=%s" % self.source_ip_prefix)
+        if self.destination_ip_prefix:
+            match.append("nw_dst=%s" % self.destination_ip_prefix)
+        if self.destination_port_range_min:
+            match.append("tp_dst=%s" % self.destination_port_range_min)
+        return ",".join(match)