[CORD-1796] Generate swagger spec from xproto

Change-Id: Ia5697b386b216d677bef73f08c36c022e28038ca
diff --git a/lib/xos-genx/tests/swagger_test.py b/lib/xos-genx/tests/swagger_test.py
new file mode 100644
index 0000000..4dde349
--- /dev/null
+++ b/lib/xos-genx/tests/swagger_test.py
@@ -0,0 +1,69 @@
+
+# Copyright 2017-present Open Networking Foundation
+#
+# 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.
+
+
+import unittest
+
+import yaml
+from xosgenx.generator import XOSGenerator
+from helpers import FakeArgs
+
+class Args:
+    pass
+
+class XOSGeneratorTest(unittest.TestCase):
+
+    def test_swagger_target(self):
+        """
+        [XOS-GenX] The swagger xtarget should generate the appropriate json
+        """
+        # xosgenx --output . --target xosgenx/targets/swagger.xtarget --write-to-file single  --dest-file swagger.yaml ../../xos/core/models/core.xproto
+        # http-server --cors Users/teone/Sites/opencord/orchestration/xos/lib/xos-genx/
+        xproto = \
+            """
+            option app_label = "core";
+    
+            message Instance::instance_policy (XOSBase) {
+                 option validators = "instance_creator:Instance has no creator, instance_isolation: Container instance {obj.name} must use container image, instance_isolation_container_vm_parent:Container-vm instance {obj.name} must have a parent, instance_parent_isolation_container_vm:Parent field can only be set on Container-vm instances ({obj.name}), instance_isolation_vm: VM Instance {obj.name} must use VM image, instance_creator_privilege: instance creator has no privileges on slice";
+                 optional string instance_id = 1 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance id", null = True, db_index = False];
+                 optional string instance_uuid = 2 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance uuid", null = True, db_index = False];
+                 required string name = 3 [max_length = 200, content_type = "stripped", blank = False, help_text = "Instance name", null = False, db_index = False];
+                 optional string instance_name = 4 [max_length = 200, content_type = "stripped", blank = True, help_text = "OpenStack generated name", null = True, db_index = False];
+                 optional string ip = 5 [max_length = 39, content_type = "ip", blank = True, help_text = "Instance ip address", null = True, db_index = False, gui_hidden = True];
+                 required manytoone image->Image:instances = 6 [db_index = True, null = False, blank = False];
+                 optional manytoone creator->User:instances = 7 [db_index = True, null = True, blank = True];
+                 required manytoone slice->Slice:instances = 8 [db_index = True, null = False, blank = False];
+                 required manytoone deployment->Deployment:instance_deployment = 9 [db_index = True, null = False, blank = False];
+                 required manytoone node->Node:instances = 10 [db_index = True, null = False, blank = False];
+                 required int32 numberCores = 11 [help_text = "Number of cores for instance", default = 0, null = False, db_index = False, blank = False];
+                 required manytoone flavor->Flavor:instance = 12 [help_text = "Flavor of this instance", null = False, db_index = True, blank = False];
+                 optional string userData = 13 [help_text = "user_data passed to instance during creation", null = True, db_index = False, blank = True, varchar = True];
+                 required string isolation = 14 [default = "vm", choices = "(('vm', 'Virtual Machine'), ('container', 'Container'), ('container_vm', 'Container In VM'))", max_length = 30, blank = False, null = False, db_index = False];
+                 optional string volumes = 15 [help_text = "Comma-separated list of directories to expose to parent context", null = True, db_index = False, blank = True];
+                 optional manytoone parent->Instance:instance = 16 [help_text = "Parent Instance for containers nested inside of VMs", null = True, db_index = True, blank = True];
+            }
+            """
+        args = FakeArgs()
+        args.inputs = xproto
+        args.target = 'swagger.xtarget'
+        args.output = "/Users/teone/Sites/opencord/orchestration/xos/lib/xos-genx"
+        args.write_to_file = "single"
+        args.dest_file = "swagger.yaml"
+        args.quiet = False
+        output = XOSGenerator.generate(args)
+        self.assertEqual(True, False);
+
+if __name__ == '__main__':
+    unittest.main()
diff --git a/lib/xos-genx/xosgenx/jinja2_extensions/base.py b/lib/xos-genx/xosgenx/jinja2_extensions/base.py
index 84f0a97..431de3f 100644
--- a/lib/xos-genx/xosgenx/jinja2_extensions/base.py
+++ b/lib/xos-genx/xosgenx/jinja2_extensions/base.py
@@ -247,3 +247,35 @@
         return "boolean"
     else:
         return type
+
+def xproto_type_to_swagger_type(f):
+    try:
+        content_type = f['options']['content_type']
+        content_type = eval(content_type)
+    except:
+        content_type = None
+        pass
+
+    if 'choices' in f['options']:
+        return 'string'
+    elif content_type == 'date':
+        return 'string'
+    elif f['type'] == 'bool':
+        return 'boolean'
+    elif f['type'] == 'string':
+        return 'string'
+    elif f['type'] in ['int','uint32','int32'] or 'link' in f:
+        return 'integer'
+    elif f['type'] in ['double','float']:
+        return 'string'
+
+def xproto_field_to_swagger_enum(f):
+    if 'choices' in f['options']:
+        list = []
+
+        for c in eval(xproto_unquote(f['options']['choices'])):
+            list.append(c[0])
+
+        return list
+    else:
+        return False
\ No newline at end of file
diff --git a/lib/xos-genx/xosgenx/targets/swagger.xtarget b/lib/xos-genx/xosgenx/targets/swagger.xtarget
new file mode 100644
index 0000000..3d089b9
--- /dev/null
+++ b/lib/xos-genx/xosgenx/targets/swagger.xtarget
@@ -0,0 +1,135 @@
+swagger: "2.0"
+info:
+  title: "XOS REST APIs"
+  description: "XOS - REST API Docs"
+  version: "1.0.0"
+  contact:
+    email: "cord-dev@opencord.org"
+
+{#tags:#}
+{#{%- for object in proto.messages %}#}
+{#- name: "{{ xproto_unquote(xproto_first_non_empty([object.options.app_label, object.options.name])) }}"#}
+{#  description: "{{ xproto_unquote(xproto_first_non_empty([object.options.app_label, object.options.name])) }} Models"#}
+{#  externalDocs:#}
+{#    description: "Find out more"#}
+{#    url: "https://guide.opencord.org/xos/core_models.html"#}
+{#{% endfor %}#}
+
+paths:
+{%- for object in proto.messages %}
+  /xosapi/v1/{{ xproto_unquote(xproto_first_non_empty([object.options.name, object.options.app_label, options.name, context.app_label])) }}/{{ xproto_pluralize(object) | lower }}/:
+    get:
+      tags:
+        - "{{ xproto_unquote(xproto_first_non_empty([object.options.app_label, object.options.name])) }}"
+      summary: "List {{xproto_pluralize(object)}}"
+      responses:
+        200:
+          description: OK
+        401:
+          description: "Unauthorized"
+        403:
+          description: "Forbidden"
+        500:
+          description: "Internal Server Error"
+    post:
+      tags:
+        - "{{ xproto_unquote(xproto_first_non_empty([object.options.app_label, object.options.name])) }}"
+      summary: "Create {{object.name}}"
+      parameters:
+      - in: "body"
+        name: "body"
+        description: "{{object.name}} model properties"
+        required: true
+        schema:
+          $ref: "#/definitions/{{object.name}}"
+      responses:
+        200:
+          description: OK
+        401:
+          description: "Unauthorized"
+        403:
+          description: "Forbidden"
+        500:
+          description: "Internal Server Error"
+  /xosapi/v1/{{ xproto_unquote(xproto_first_non_empty([object.options.name, object.options.app_label, options.name, context.app_label])) }}/{{ xproto_pluralize(object) | lower }}/{id}:
+    get:
+      tags:
+        - "{{ xproto_unquote(xproto_first_non_empty([object.options.app_label, object.options.name])) }}"
+      summary: "Get {{object.name}}"
+      parameters:
+        - in: "path"
+          name: "id"
+          description: "ID of {{ object.name }} to return"
+          required: true
+          type: "integer"
+          format: "int64"
+      responses:
+        200:
+          description: OK
+        401:
+          description: "Unauthorized"
+        403:
+          description: "Forbidden"
+        500:
+          description: "Internal Server Error"
+    put:
+      tags:
+        - "{{ xproto_unquote(xproto_first_non_empty([object.options.app_label, object.options.name])) }}"
+      summary: "Get {{object.name}}"
+      parameters:
+        - in: "path"
+          name: "id"
+          description: "ID of {{ object.name }} to return"
+          required: true
+          type: "integer"
+          format: "int64"
+      responses:
+        200:
+          description: OK
+        401:
+          description: "Unauthorized"
+        403:
+          description: "Forbidden"
+        500:
+          description: "Internal Server Error"
+    delete:
+      tags:
+        - "{{ xproto_unquote(xproto_first_non_empty([object.options.app_label, object.options.name])) }}"
+      summary: "Delete {{object.name}}"
+      parameters:
+        - in: "path"
+          name: "id"
+          description: "ID of {{ object.name }} to return"
+          required: true
+          type: "integer"
+          format: "int64"
+      responses:
+        200:
+          description: OK
+        401:
+          description: "Unauthorized"
+        403:
+          description: "Forbidden"
+        500:
+          description: "Internal Server Error"
+{% endfor %}
+
+definitions:
+{%- for object in proto.messages %}
+    {{ object.name }}:
+      type: "object"
+      properties:
+{%- for f in object.fields %}
+        {{ f.name }}:
+          type: {{ xproto_type_to_swagger_type(f) }}
+{%- if xproto_field_to_swagger_enum(f) %}
+          enum:
+{%- for e in xproto_field_to_swagger_enum(f) %}
+            - {{ e }}
+{%- endfor %}
+{%- endif %}
+{%- if f.options.help_text %}
+          description: "{{ xproto_unquote(f.options.help_text) }}"
+{% endif %}
+{%- endfor %}
+{% endfor %}