Generate onos_app rule

There are three genrules:
 1. Build features.xml
 2. Build app.xml
 3. Generate app.oar

Change-Id: I6adfd47fadf40ad2440998071a01894458629ac6
diff --git a/apps/fwd/BUCK b/apps/fwd/BUCK
index 6a2fac0..1e49af9 100644
--- a/apps/fwd/BUCK
+++ b/apps/fwd/BUCK
@@ -14,7 +14,7 @@
     '//lib:TEST',
 ]
 
-java_library(
+osgi_jar(
     name = CURRENT_NAME,
     srcs = glob([SRC + '/*.java']),
     deps = COMPILE_DEPS,
@@ -29,3 +29,16 @@
            [CURRENT_TARGET],
     source_under_test = [CURRENT_TARGET],
 )
+
+BUNDLES = [
+    (CURRENT_TARGET, ONOS_GROUP_ID + ':' + CURRENT_NAME + ':' + ONOS_VERSION),
+]
+
+onos_app(
+    app_name = 'org.onosproject.fwd',
+    title = 'Reactive Forwarding App',
+    category = 'Traffic Steering',
+    url = 'http://onosproject.org',
+    description = 'Reactive forwarding application using flow subsystem.',
+    included_bundles = BUNDLES,
+)
\ No newline at end of file
diff --git a/buck-tools/BUCK b/buck-tools/BUCK
index 96c787a..1b8e549 100644
--- a/buck-tools/BUCK
+++ b/buck-tools/BUCK
@@ -6,6 +6,21 @@
 )
 
 python_binary(
+  name = 'onos-app-writer',
+  main = 'onos_app.py',
+  deps = [],
+  visibility = ['PUBLIC'],
+)
+
+python_binary(
+  name = 'onos-app-oar',
+  main = 'onos_oar.py',
+  deps = [],
+  visibility = ['PUBLIC'],
+)
+
+
+python_binary(
   name = 'pack_war',
   main = 'pack_war.py',
   deps = [':util'],
diff --git a/buck-tools/default.defs b/buck-tools/default.defs
index 5ed0fea..24caa38 100644
--- a/buck-tools/default.defs
+++ b/buck-tools/default.defs
@@ -1,9 +1,10 @@
 include_defs('//bucklets/maven_jar.bucklet')
 include_defs('//bucklets/onos.bucklet')
+include_defs('//bucklets/onos_app.bucklet')
 
 BASE_DEPS = [
     '//lib:junit',
     '//lib:hamcrest-all',
     '//lib:slf4j-api',
     '//lib:guava-testlib',
-]
+]
\ No newline at end of file
diff --git a/buck-tools/onos_app.py b/buck-tools/onos_app.py
new file mode 100755
index 0000000..ab2dfc6
--- /dev/null
+++ b/buck-tools/onos_app.py
@@ -0,0 +1,161 @@
+#!/usr/bin/env python
+#FIXME Add license
+
+##### Templates for features.xml
+FEATURES_HEADER = '''\
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<features xmlns="http://karaf.apache.org/xmlns/features/v1.2.0"
+          name="%(feature_repo_name)s">
+'''
+FEATURE_HEADER= '''\
+    <feature name="%(feature_name)s" version="%(version)s"
+             description="%(title)s">
+'''
+EXISTING_FEATURE = '        <feature>%s</feature>\n'
+BUNDLE = '        <bundle>%s</bundle>\n'
+FEATURE_FOOTER = '    </feature>\n'
+FEATURES_FOOTER = '</features>'
+
+##### Templates for app.xml
+APP_HEADER = '''\
+<?xml version="1.0" encoding="UTF-8"?>
+<app name="%(app_name)s" origin="%(origin)s" version="%(version)s"
+        title="%(title)s" category="%(category)s" url="%(url)s"
+        featuresRepo="%(feature_repo_name)s"
+        features="%(feature_name)s" apps="%(apps)s">
+    <description>%(description)s</description>
+'''
+ARTIFACT = '    <artifact>%s</artifact>\n'
+APP_FOOTER = '</app>'
+
+
+def mvnUrl(bundle):
+    return 'mvn:' + bundle.replace(':', '/')
+
+def generateFeatureFile(feature_name,
+                        version,
+                        title,
+                        feature_repo_name,
+                        features = [],
+                        bundles = [],
+                        **kwargs):
+    values = {
+        'feature_name' : feature_name,
+        'version' : version,
+        'title' : title,
+        'feature_repo_name' : '-'.join(feature_repo_name.split(':')[1:3]),
+    }
+
+    output = FEATURES_HEADER % values + FEATURE_HEADER % values
+
+    for feature in features:
+        output += EXISTING_FEATURE % feature
+
+    for bundle in bundles:
+        output += BUNDLE % mvnUrl(bundle)
+
+    output += FEATURE_FOOTER + FEATURES_FOOTER
+    return output
+
+def generateAppFile(app_name,
+                    origin,
+                    version,
+                    title,
+                    category,
+                    url,
+                    feature_repo_name,
+                    feature_name,
+                    description = None,
+                    apps = [],
+                    artifacts = [],
+                    **kwargs):
+    values = {
+        'app_name' : app_name,
+        'origin' : origin,
+        'version' : version,
+        'title' : title,
+        'category' : category,
+        'url' : url,
+        'feature_repo_name' : mvnUrl(feature_repo_name) + '/xml/features',
+        'feature_name' : feature_name,
+    }
+
+    values['description'] = description if description else title
+    values['apps'] = ','.join(apps) if apps else ''
+
+    output = APP_HEADER % values
+
+    for artifact in artifacts:
+        output += ARTIFACT % mvnUrl(artifact)
+
+    output += APP_FOOTER
+    return output
+
+
+if __name__ == '__main__':
+    import sys, optparse
+
+    parser = optparse.OptionParser()
+    parser.add_option("-n", "--name",     dest="feature_name", help="Feature Name")
+    parser.add_option("-a", "--app",      dest="app_name",     help="App Name")
+    parser.add_option("-o", "--origin",   dest="origin",       help="Origin")
+    parser.add_option("-c", "--category", dest="category",     help="Category")
+    parser.add_option("-u", "--url",      dest="url",          help="URL")
+    parser.add_option("-v", "--version",  dest="version",      help="Version")
+    parser.add_option("-t", "--title",    dest="title",        help="Title")
+    parser.add_option("-r", "--repo",     dest="repo_name",    help="Repo Name")
+
+    parser.add_option('-b', '--bundle',
+                      action="append", dest='included_bundles',
+                      metavar="BUNDLE", help='Included Bundle (multiple allowed)')
+    parser.add_option('-e', '--excluded-bundle',
+                      action="append", dest='excluded_bundles',
+                      metavar="BUNDLE", help='Excluded Bundle (multiple allowed)')
+    parser.add_option('-f', '--feature',
+                      action="append", dest='features',
+                      metavar="FEATURE", help='Existing Feature (multiple allowed)')
+    parser.add_option('-d', '--apps',
+                      action="append", dest='apps',
+                      metavar="FEATURE", help='Required App (multiple allowed)')
+
+    parser.add_option("-A", "--write-app", dest="write_app", action="store_true")
+    parser.add_option("-F", "--write-features", dest="write_features", action="store_true")
+
+    (options, args) = parser.parse_args()
+
+    values = {}
+    if options.feature_name and options.version and options.title:
+        values['feature_name'] = options.feature_name.split(':')[1]
+        values['version'] = options.version
+        values['title'] = options.title
+    else:
+        sys.stderr.write('ERROR: Feature Name, Version, and Title are required\n')
+        sys.stderr.flush()
+        sys.exit(1)
+
+    if options.app_name and options.origin and options.category and options.url:
+        values['app_name'] = options.app_name
+        values['origin'] = options.origin
+        values['category'] = options.category
+        values['url'] = options.url
+    elif options.write_app:
+        sys.stderr.write('ERROR: Feature Name, Version, and Title are required\n')
+        sys.stderr.flush()
+        sys.exit(1)
+
+    values['feature_repo_name'] = options.repo_name if options.repo_name \
+                                    else options.feature_name
+
+    if options.write_features:
+        bundles = []
+        if options.included_bundles:
+            bundles += options.included_bundles
+        if options.excluded_bundles:
+            bundles += options.excluded_bundles
+        print generateFeatureFile(bundles=bundles,
+                                  features=options.features,
+                                  **values)
+    if options.write_app:
+        print generateAppFile(artifacts=options.included_bundles,
+                              apps=options.apps,
+                              **values)
\ No newline at end of file
diff --git a/buck-tools/onos_oar.py b/buck-tools/onos_oar.py
new file mode 100755
index 0000000..99fd498
--- /dev/null
+++ b/buck-tools/onos_oar.py
@@ -0,0 +1,38 @@
+#!/usr/bin/env python
+#FIXME Add license
+
+from zipfile import ZipFile
+
+def generateOar(output, files=[]):
+    # Note this is not a compressed zip
+    with ZipFile(output, 'w') as zip:
+        for file, mvnCoords in files:
+            filename = file.split('/')[-1]
+            if mvnCoords == 'NONE':
+                dest = filename
+            else:
+                groupId, artifactId, version = mvnCoords.split(':')
+                groupId = groupId.replace('.', '/')
+                extension = filename.split('.')[-1]
+                if extension == 'jar':
+                    filename = '%s-%s.jar' % ( artifactId, version )
+                dest = 'm2/%s/%s/%s/%s' % ( groupId, artifactId, version, filename )
+            print file, '->', dest
+            zip.write(file, dest)
+
+if __name__ == '__main__':
+    import sys
+
+    if len(sys.argv) < 2:
+        print 'USAGE'
+        sys.exit(1)
+
+    output = sys.argv[1]
+    args = sys.argv[2:]
+
+    if len(args) % 2 != 0:
+        print 'There must be an even number of args: file mvn_coords'
+        sys.exit(2)
+
+    files = zip(*[iter(args)]*2)
+    generateOar(output, files)
diff --git a/bucklets/onos.bucklet b/bucklets/onos.bucklet
index ba2ed17..ad6ccf6 100644
--- a/bucklets/onos.bucklet
+++ b/bucklets/onos.bucklet
@@ -4,11 +4,14 @@
 FORCE_INSTALL=True
 NONE='NONE'
 
+ONOS_GROUP_ID = 'org.onosproject'
+ONOS_VERSION = '1.6.0-SNAPSHOT'
+
 def osgi_jar(
     name,
     srcs,
-    group_id = 'org.onosproject',
-    version = '1.6.0-SNAPSHOT',
+    group_id = ONOS_GROUP_ID,
+    version = ONOS_VERSION,
     deps = [],
     visibility = ['PUBLIC'],
     license = 'NONE',
@@ -62,7 +65,7 @@
   genrule(
     name = osgi_jar_name,
     bash = bash,
-    out = name + '.jar',
+    out = '%s-%s.jar' % (name, version), #FIXME add version to jar file
     srcs =  glob(['src/main/webapp/**']),
     visibility = [], #intentially, not visible
   )
@@ -110,12 +113,3 @@
     visibility = visibility,
   )
 
-def onos_app(
-    name,
-    **kwargs):
-
-    osgi_jar(
-      name = name,
-      **kwargs
-    )
-
diff --git a/bucklets/onos_app.bucklet b/bucklets/onos_app.bucklet
new file mode 100644
index 0000000..027d4fe
--- /dev/null
+++ b/bucklets/onos_app.bucklet
@@ -0,0 +1,61 @@
+ONOS_ORIGIN = 'ON.Lab'
+ONOS_VERSION = '1.6.0-SNAPSHOT'
+DEFAULT_APP_CATEGORY = 'Utility'
+
+def onos_app(
+        app_name,
+        title,
+        version = ONOS_VERSION,
+        origin = ONOS_ORIGIN,
+        category = DEFAULT_APP_CATEGORY,
+        url = None,
+        description = None, #TODO make this a file
+        #TODO icon,
+        feature_name = None,
+        required_features = [ 'onos-api' ],
+        required_apps = [],
+        included_bundles = [],
+        excluded_bundles = [],
+        **kwargs):
+
+    if not feature_name and len(included_bundles) == 1:
+        feature_name = included_bundles[0][1]
+
+    args = [ '-n %s' % feature_name,
+             '-v %s' % version,
+             '-t "%s"' % title,
+             '-o "%s"' % origin,
+             '-c "%s"' % category,
+             '-a "%s"' % app_name,
+             '-u %s' % url,
+             ]
+    args += [ '-f %s' % f for f in required_features ]
+    args += [ '-b %s' % b for (t, b) in included_bundles ]
+    args += [ '-e %s' % b for (t, b) in excluded_bundles ]
+    args += [ '-d %s' % a for a in required_apps ]
+
+    cmd = '$(exe //buck-tools:onos-app-writer) -F ' + ' '.join(args) + ' > $OUT'
+    genrule(
+        name = 'app-features',
+        bash = cmd,
+        out = '%s-%s-features.xml' % (feature_name.split(':')[1], version),
+        visibility = [],
+    )
+    cmd = '$(exe //buck-tools:onos-app-writer) -A ' + ' '.join(args) + ' > $OUT'
+    genrule(
+        name = 'app-xml',
+        bash = cmd,
+        out = 'app.xml',
+        visibility = [],
+    )
+
+    sources = [
+        '$(location :app-features) %s' % feature_name,
+        '$(location :app-xml) NONE',
+    ]
+    sources += ['$(location %s) %s' % i for i in included_bundles]
+    genrule(
+        name = 'app-oar',
+        out = 'app.oar',
+        bash = '$(exe //buck-tools:onos-app-oar) $OUT ' + ' '.join(sources)
+    )
diff --git a/providers/openflow/base/app.xml b/providers/openflow/base/app.xml
index fd6dc64..9baab60 100644
--- a/providers/openflow/base/app.xml
+++ b/providers/openflow/base/app.xml
@@ -21,7 +21,6 @@
     <description>${project.description}</description>
     <artifact>mvn:${project.groupId}/onos-of-api/${project.version}</artifact>
     <artifact>mvn:${project.groupId}/onos-of-ctl/${project.version}</artifact>
-    <artifact>mvn:${project.groupId}/onos-drivers/${project.version}</artifact>
 
     <artifact>mvn:${project.groupId}/onos-of-provider-device/${project.version}</artifact>
     <artifact>mvn:${project.groupId}/onos-of-provider-packet/${project.version}</artifact>