initial commit

Change-Id: I650d5bf4f7c7896c91662e70f283e58fcd136363
diff --git a/Jenkinsfile b/Jenkinsfile
new file mode 100644
index 0000000..f4eee1f
--- /dev/null
+++ b/Jenkinsfile
@@ -0,0 +1,115 @@
+import groovy.json.JsonSlurperClassic
+
+env.IGNORE_LIST = ["All-Users"]
+
+env.approvers = 'andy@opennetworking.org,llp@opennetworking.org'
+env.recipients = 'cord-discuss@opencord.org'
+
+@NonCPS
+def jsonParseList(def json) {
+    j = json.minus(")]}'")
+    resp = new groovy.json.JsonSlurperClassic().parseText(j)
+    list = []
+    for (i in resp.keySet()) {
+        list << i
+    }
+    return list
+}
+
+@NonCPS
+def jsonParseMap(def json) {
+    def j = json.minus(")]}'")
+    return new groovy.json.JsonSlurperClassic().parseText(j)
+}
+
+def createBranch(def proj, def branch, def parent) {
+    cmd = 'ssh -p 29418 gerrit.opencord.org gerrit create-branch ' + proj + " " + branch + " " + parent
+    sh returnStdout: true, script: cmd
+}
+
+int checkBranchExists(def proj) {
+    if (env.IGNORE_LIST.contains(proj)) {
+        return 0
+    }
+    url = 'https://gerrit.opencord.org/projects/' + proj + '/branches/' + env.BRANCH_NAME
+    response = httpRequest url: url, validResponseCodes: '200,404'
+    if (response.status == 404) {
+        createBranch(proj, env.BRANCH_NAME, 'master')
+        return 1
+    }
+    return 0
+}
+
+node ('master') {
+
+    stage 'Check and create support branches'
+    def url = 'https://gerrit.opencord.org/projects/?type=CODE'
+    def response = httpRequest url: url, validResponseCodes: '200'
+    def info = jsonParseList(response.content)
+    int created = 0
+    for (index = 0; index < info.size(); index++) {
+        created += checkBranchExists(info[index])
+    }
+
+    if (created == 0) {
+
+        def now = new Date()
+        def metadata = 'None'
+        branch = 'cord-' + now.format("yyyyMMddHHmm", TimeZone.getTimeZone('UTC'))
+
+        stage 'Release?'
+        timeout(time: 12, unit: 'HOURS') {
+            mail to: env.approvers,
+                subject: "Job '${JOB_NAME}' is waiting up for promotion",
+                body: "Please go to ${BUILD_URL}input and promote or abort the release. It will timeout after 12 hours."
+            metadata = input id: 'release-build', message: 'Should I perform a release?',
+                parameters: [booleanParam(defaultValue: true,
+                description: 'Release onos applications (assumes versions have been updated)', name: 'build_onos_apps'),
+                string(defaultValue: branch, description: 'Release version', name: 'release_version')], submitter: 'llp,acb'
+        }
+
+        if (metadata['release_version'] == 'None') {
+            error 'Release version cannot be None'
+        }
+
+        stage 'Create new manifest branch'
+        createBranch('manifest', metadata['release_version'], env.BRANCH_NAME)
+        checkout changelog: false, poll: false, scm: [$class: 'GitSCM', branches: [[name: metadata['release_version'] ]], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'WipeWorkspace'], [$class: 'CleanBeforeCheckout']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'dd9d4677-2415-4f82-8e79-99dcd530f023', url: 'ssh://jenkins@gerrit.opencord.org:29418/manifest']]]
+        sh returnStdout: true, script: 'git checkout ' + metadata['release_version']
+        sh returnStdout: true, script: 'git pull origin ' + metadata['release_version']
+        sh returnStdout: true, script: 'cp ' + env.JENKINS_HOME + '/tmp/manifest-' + env.BRANCH_NAME + '.xml default.xml'
+
+        sh returnStdout: true, script: 'git commit -a -m "JENKINS: Updating manifest"'
+        sh returnStdout: true, script: 'git push origin ' + metadata['release_version']
+
+        stage 'Build and Release ONOS applications'
+
+        if (metadata['build_onos_apps']) {
+            checkout changelog: false, poll: false, scm: [$class: 'RepoScm', currentBranch: true,
+                manifestBranch: env.BRANCH_NAME, manifestGroup: 'onos',
+                manifestRepositoryUrl: 'https://gerrit.opencord.org/manifest', quiet: true]
+            if (env.BRANCH_NAME == 'master') {
+                sh returnStdout: true, script: 'cd onos-apps/apps && mvn clean deploy'
+            } else {
+                sh returnStdout: true, script: 'cd onos-apps/apps && mvn -Prelease clean deploy'
+            }
+        }
+
+        mail to: env.recipients,
+            subject: 'Nightly bleeding edge ' + metadata['release_version'] + ' released',
+            replyTo: 'cord-dev@opencord.org',
+            body: '''Hi CORD Community,
+
+                        |A new bleeding edge version of cord is available, feel free to test it.
+                        |You can obtain it using the following commands:
+
+                        |repo init -u https://gerrit.opencord.org/manifest -b '''.stripMargin() + metadata['release_version'] + '''
+                        |repo sync
+
+                        |Have fun!
+
+                        |--
+                        |CORD Automated Release
+                    '''.stripMargin()
+    }
+}