Adding basic att workflow jenkinsfile

Change-Id: I8a259933525ad170101ec3867ab13f03b2505c01
diff --git a/Jenkinsfile-att-workflow b/Jenkinsfile-att-workflow
new file mode 100644
index 0000000..6247ca5
--- /dev/null
+++ b/Jenkinsfile-att-workflow
@@ -0,0 +1,278 @@
+// 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.
+
+node ("${TestNodeName}") {
+    timeout (240) {
+        try {
+            stage ("Parse deployment configuration file") {
+                sh returnStdout: true, script: 'rm -rf ${configRepoBaseDir}'
+                sh returnStdout: true, script: 'git clone -b ${branch} ${configRepoUrl}'
+                deployment_config = readYaml file: "${configRepoBaseDir}${configRepoFile}"
+            }
+            stage('Clone POD configurations') {
+                timeout(5) {
+                    sh returnStdout: true, script: """
+                    rm -rf helm-charts
+                    rm -rf config-files
+                    git clone -b ${branch} https://gerrit.opencord.org/helm-charts
+                    git clone https://bitbucket.org/onfcord/podconfigs.git config-files
+                    """
+                }
+            }
+            stage('Clean up') {
+                timeout(10) {
+                    sh returnStdout: true, script: """
+                    export KUBECONFIG=${deployment_config.kube_config}
+                    helm ls | grep att-workflow && helm delete --purge att-workflow || true
+                    helm ls | grep base-kubernetes && helm delete --purge base-kubernetes || true
+                    helm ls | grep xos-core && helm delete --purge xos-core || true
+                    helm ls | grep onos-fabric && helm delete --purge onos-fabric || true
+                    helm ls | grep onos-voltha && helm delete --purge onos-voltha || true
+                    helm ls | grep voltha && helm delete --purge voltha || true
+                    """
+                    // Wait for helm delete
+                    timeout(5) {
+                        waitUntil {
+                            helm_deleted = sh returnStdout: true, script: """
+                            export KUBECONFIG=${deployment_config.kube_config} &&
+                            helm ls | grep -E 'onos-voltha|onos-fabric|voltha\$|base-kubernetes|xos-core|att-workflow' | wc -l
+                            """
+                            return helm_deleted.toInteger() == 0
+                        }
+                    }
+                    timeout(5) {
+                        waitUntil {
+                            kubectl_deleted = sh returnStdout: true, script: """
+                            export KUBECONFIG=${deployment_config.kube_config} &&
+                            kubectl get pods --all-namespaces | grep -E 'onos-voltha|onos-fabric|att-workflow|base-kubernetes-|xos-' | wc -l
+                            """
+                            voltha_namespace_deleted = sh returnStdout: true, script: """
+                            export KUBECONFIG=${deployment_config.kube_config} &&
+                            kubectl get namespace | grep voltha | wc -l
+                            """
+                            return kubectl_deleted.toInteger() + voltha_namespace_deleted.toInteger() == 0
+                        }
+                    }
+                }
+            }
+            dir ("helm-charts") {
+                // stage('Install Voltha Kafka') {
+                //     timeout(10) {
+                //         sh returnStdout: true, script: """
+                //         export KUBECONFIG=${deployment_config.kube_config}
+                //         helm repo add incubator http://storage.googleapis.com/kubernetes-charts-incubator
+                //         helm install -f ../config-files/seba-values.yaml --name voltha-kafka --set replicas=1 --set persistence.enabled=false --set zookeeper.replicaCount=1 --set zookeeper.persistence.enabled=false incubator/kafka
+                //         """
+                //     }
+                //     timeout(10) {
+                //         waitUntil {
+                //             kafka_instances_running = sh returnStdout: true, script: """
+                //             export KUBECONFIG=${deployment_config.kube_config} &&
+                //             kubectl get pods | grep voltha-kafka | grep -i running | grep 1/1 | wc -l
+                //             """
+                //             return kafka_instances_running.toInteger() == 2
+                //         }
+                //     }
+                // }
+                stage('Install voltha') {
+                    timeout(10) {
+                        sh returnStdout: true, script: """
+                        export KUBECONFIG=${deployment_config.kube_config}
+                        helm repo add incubator https://kubernetes-charts-incubator.storage.googleapis.com/
+                        helm dep build voltha
+                        helm install -f ../config-files/seba-values.yaml -n voltha voltha
+                        """
+                    }
+                    timeout(10) {
+                        waitUntil {
+                            voltha_completed = sh returnStdout: true, script: """
+                            export KUBECONFIG=${deployment_config.kube_config} &&
+                            kubectl get pods -n voltha | grep -i running | grep 1/1 | wc -l
+                            """
+                            return voltha_completed.toInteger() == 8
+                        }
+                    }
+                }
+                stage('Install ONOS-Voltha') {
+                    timeout(10) {
+                        sh returnStdout: true, script: """
+                        export KUBECONFIG=${deployment_config.kube_config}
+                        helm install -f ../config-files/seba-values.yaml -n onos-voltha -f configs/onos-voltha.yaml onos
+                        """
+                    }
+                    timeout(10) {
+                        waitUntil {
+                            onos_voltha_completed = sh returnStdout: true, script: """
+                            export KUBECONFIG=${deployment_config.kube_config} &&
+                            kubectl get pods | grep -i onos-voltha | grep -i running | grep 1/1 | wc -l
+                            """
+                            return onos_voltha_completed.toInteger() == 1
+                        }
+                    }
+                }
+                stage('Install ONOS-Fabric') {
+                    timeout(10) {
+                        sh returnStdout: true, script: """
+                        export KUBECONFIG=${deployment_config.kube_config}
+                        helm install -f ../config-files/seba-values.yaml -n onos-fabric -f configs/onos-fabric.yaml onos
+                        """
+                    }
+                    timeout(10) {
+                        waitUntil {
+                            onos_fabric_completed = sh returnStdout: true, script: """
+                            export KUBECONFIG=${deployment_config.kube_config} &&
+                            kubectl get pods | grep -i onos-fabric | grep -i running | grep 1/1 | wc -l
+                            """
+                            return onos_fabric_completed.toInteger() == 1
+                        }
+                    }
+                }
+                stage('Install xos-core') {
+                    timeout(10) {
+                        sh returnStdout: true, script: """
+                        export KUBECONFIG=${deployment_config.kube_config}
+                        helm dep update xos-core
+                        helm install -f ../config-files/seba-values.yaml -n xos-core xos-core                                
+                        """
+                    }
+                    timeout(10) {
+                        waitUntil {
+                            xos_core_completed = sh returnStdout: true, script: """
+                            export KUBECONFIG=${deployment_config.kube_config} &&
+                            kubectl get pods | grep -i xos | grep -i running | grep 1/1 | wc -l
+                            """
+                            return xos_core_completed.toInteger() == 7
+                        }
+                    }
+                }
+                stage('Install att-workflow') {
+                    timeout(10) {
+                        sh returnStdout: true, script: """
+                        export KUBECONFIG=${deployment_config.kube_config}
+                        helm dep update xos-profiles/att-workflow
+                        helm install -f ../config-files/seba-values.yaml -n att-workflow xos-profiles/att-workflow
+                        """
+                    }
+                    timeout(10) {
+                        waitUntil {
+                            att_workflow_tosca_completed = sh returnStdout: true, script: """
+                            export KUBECONFIG=${deployment_config.kube_config} &&
+                            kubectl get pods | grep -i att-workflow-tosca-loader | grep -i completed | wc -l
+                            """
+                            return att_workflow_tosca_completed.toInteger() == 1
+                        }
+                    }
+                }
+                // stage('Install CORD Kafka') {
+                //     timeout(10) {
+                //         sh returnStdout: true, script: """
+                //         export KUBECONFIG=${deployment_config.kube_config}
+                //         helm repo add incubator http://storage.googleapis.com/kubernetes-charts-incubator
+                //         helm install -f ../config-files/seba-values.yaml --name cord-kafka --set replicas=1 --set persistence.enabled=false --set zookeeper.replicaCount=1 --set zookeeper.persistence.enabled=false incubator/kafka
+                //         """
+                //     }
+                //     timeout(10) {
+                //         waitUntil {
+                //             kafka_instances_running = sh returnStdout: true, script: """
+                //             export KUBECONFIG=${deployment_config.kube_config} &&
+                //             kubectl get pods | grep cord-kafka | grep -i Running | grep 1/1 | wc -l
+                //             """
+                //             return kafka_instances_running.toInteger() == 2
+                //         }
+                //     }
+                // }
+                stage('Install base-kubernetes') {
+                    timeout(10) {
+                        sh returnStdout: true, script: """
+                        export KUBECONFIG=${deployment_config.kube_config}
+                        helm dep update xos-profiles/base-kubernetes
+                        helm install -f ../config-files/seba-values.yaml -n base-kubernetes xos-profiles/base-kubernetes
+                        """
+                    }
+                    timeout(10) {
+                        waitUntil {
+                            base_kubernetes_tosca_running = sh returnStdout: true, script: """
+                            export KUBECONFIG=${deployment_config.kube_config} &&
+                            kubectl get pods | grep -i base-kubernetes-tosca-loader | grep -i completed | wc -l
+                            """
+                            return base_kubernetes_tosca_running.toInteger() == 1
+                        }
+                    }
+                }
+            }
+            stage('Restart OLT processes') {
+                for(int i=0; i < deployment_config.olts.size(); i++) {
+                    timeout(5) {
+                        sh returnStdout: true, script: """
+                        ssh-keyscan -H ${deployment_config.olts[i].ip} >> /home/ubuntu/.ssh/known_hosts
+                        sshpass -p ${deployment_config.olts[i].pass} ssh -l ${deployment_config.olts[i].user} ${deployment_config.olts[i].ip} 'service bal_core_dist stop' || true
+                        sshpass -p ${deployment_config.olts[i].pass} ssh -l ${deployment_config.olts[i].user} ${deployment_config.olts[i].ip} 'service openolt stop' || true
+                        sshpass -p ${deployment_config.olts[i].pass} ssh -l ${deployment_config.olts[i].user} ${deployment_config.olts[i].ip} '> /var/log/bal_core_dist.log'
+                        sshpass -p ${deployment_config.olts[i].pass} ssh -l ${deployment_config.olts[i].user} ${deployment_config.olts[i].ip} '> /var/log/openolt.log'
+                        sshpass -p ${deployment_config.olts[i].pass} ssh -l ${deployment_config.olts[i].user} ${deployment_config.olts[i].ip} 'service bal_core_dist start &'
+                        sleep 5
+                        sshpass -p ${deployment_config.olts[i].pass} ssh -l ${deployment_config.olts[i].user} ${deployment_config.olts[i].ip} 'service openolt start &'
+                        """
+                    }
+                    timeout(15) {
+                        waitUntil {
+                            onu_discovered = sh returnStdout: true, script: "sshpass -p ${deployment_config.olts[i].pass} ssh -l ${deployment_config.olts[i].user} ${deployment_config.olts[i].ip} 'cat /var/log/openolt.log | grep \"olt indication, oper_state:up\" | wc -l'"
+                            return onu_discovered.toInteger() > 0
+                        }
+                    }
+                }
+            }
+            dir ("config-files") {
+                stage('Configure R-CORD - Fabric and whitelist') {
+                    timeout(1) {
+                        waitUntil {
+                            out_fabric = sh returnStdout: true, script: """
+                            cd tosca/att-workflow &&
+                            curl -s -H "xos-username: admin@opencord.org" -H "xos-password: letmein" -X POST --data-binary @pod-configuration.yaml http://${deployment_config.nodes[0].ip}:30007/run | grep -i "created models" | wc -l
+                            """
+                            return out_fabric.toInteger() == 1
+                        }
+                    }
+                }
+                stage('Configure R-CORD - Subscriber') {
+                    timeout(1) {
+                        waitUntil {
+                            out_subscriber = sh returnStdout: true, script: """
+                            cd tosca/att-workflow &&
+                            curl -s -H 'xos-username: admin@opencord.org' -H 'xos-password: letmein' -X POST --data-binary @subscriber.yaml http://${deployment_config.nodes[0].ip}:30007/run | grep -i "created models" | wc -l
+                            """
+                            return out_subscriber.toInteger() == 1
+                        }
+                    }
+                }
+                stage('Configure R-CORD - OLT') {
+                    timeout(1) {
+                        waitUntil {
+                            out_olt = sh returnStdout: true, script: """
+                            cd tosca/att-workflow &&
+                            curl -H 'xos-username: admin@opencord.org' -H 'xos-password: letmein' -X POST --data-binary @pod-olt.yaml http://${deployment_config.nodes[0].ip}:30007/run | grep -i "created models" | wc -l
+                            """
+                            return out_olt.toInteger() == 1
+                        }
+                    }
+                }
+            }
+            currentBuild.result = 'SUCCESS'
+        } catch (err) {
+            currentBuild.result = 'FAILURE'
+            step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: "${notificationEmails}", sendToIndividuals: false])
+        }
+        echo "RESULT: ${currentBuild.result}"
+    }
+}