[VOL-3804] Test for BBSim

Change-Id: I5dbecebb8f876379b34e60cb56388ea681866a03
diff --git a/libraries/bbsim.robot b/libraries/bbsim.robot
new file mode 100644
index 0000000..12ce3a6
--- /dev/null
+++ b/libraries/bbsim.robot
@@ -0,0 +1,59 @@
+# 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.
+# onos common functions
+
+*** Settings ***
+Documentation     Library for BBSimCtl interactions
+Resource          ./k8s.robot
+
+*** Keywords ***
+List ONUs
+    [Documentation]  Lists ONUs via BBSimctl
+    [Arguments]    ${namespace}    ${bbsim_pod_name}
+    ${onus}    ${rc}=    Exec Pod And Return Output And RC    ${namespace}    ${bbsim_pod_name}
+    ...    bbsimctl onu list
+    Log     ${onus}
+    Should Be Equal as Integers    ${rc}    0
+
+Restart Auth
+    [Documentation]  Restart Authentication on a BBSim ONU
+    [Arguments]    ${namespace}    ${bbsim_pod_name}    ${onu}
+    ${res}     ${rc}=    Exec Pod And Return Output And RC    ${namespace}    ${bbsim_pod_name}
+    ...    bbsimctl onu auth_restart ${onu}
+    Log     ${res}
+    Should Be Equal as Integers    ${rc}    0
+
+Restart DHCP
+    [Documentation]  Restart Dhcp on a BBSim ONU
+    [Arguments]    ${namespace}    ${bbsim_pod_name}    ${onu}
+    ${res}     ${rc}=    Exec Pod And Return Output And RC    ${namespace}    ${bbsim_pod_name}
+    ...    bbsimctl onu dhcp_restart ${onu}
+    Log     ${res}
+    Should Be Equal as Integers    ${rc}    0
+
+List Service
+    [Documentation]  Lists Service via BBSimctl
+    [Arguments]    ${namespace}    ${bbsim_pod_name}
+    ${service}    ${rc}=    Exec Pod And Return Output And RC    ${namespace}    ${bbsim_pod_name}
+    ...    bbsimctl service list
+    Log     ${service}
+    Should Be Equal as Integers    ${rc}    0
+
+JoinOrLeave Igmp
+    [Documentation]  Joins or Leaves Igmp on a BBSim ONU
+    [Arguments]    ${namespace}    ${bbsim_pod_name}    ${onu}    ${task}    ${group_address}=224.0.0.22
+    ${res}    ${rc}=    Exec Pod And Return Output And RC    ${namespace}    ${bbsim_pod_name}
+    ...    bbsimctl onu igmp ${onu} ${task} ${group_address}
+    Log     ${res}
+    Should Be Equal as Integers    ${rc}    0
diff --git a/libraries/k8s.robot b/libraries/k8s.robot
index 213246e..c8cfd69 100644
--- a/libraries/k8s.robot
+++ b/libraries/k8s.robot
@@ -68,13 +68,19 @@
     [Documentation]    Uses kubectl to execute a command in the pod and return the output
     ${rc}    ${exec_pod_name}=    Run and Return Rc and Output
     ...    kubectl -n ${namespace} get pods -l app.kubernetes.io/name=${name} -o name
+
+Exec Pod And Return Output And RC
+    [Arguments]    ${namespace}    ${name}    ${command}
+    [Documentation]    Uses kubectl to execute a command in the pod and return the output
+    ${rc}    ${exec_pod_name}=    Run and Return Rc and Output
+    ...    kubectl get pods -n ${namespace} | grep ${name} | awk 'NR==1{print $1}'
+
     Log    ${exec_pod_name}
     Should Not Be Empty    ${exec_pod_name}    Unable to parse pod name
     ${rc}    ${output}=    Run and Return Rc and Output
     ...    kubectl exec -i ${exec_pod_name} -n ${namespace} -- ${command}
     Log    ${output}
-    [return]    ${output}
-
+    [return]    ${output}   ${rc}
 
 Exec Pod Separate Stderr
     [Arguments]    ${namespace}    ${name}    ${command}
@@ -137,6 +143,14 @@
     Log    ${currentStatusofPod}
     Should Contain    ${currentStatusofPod}    ${expectedStatus}
 
+Get Pod Name By Label
+    [Arguments]    ${namespace}    ${label_key}   ${label_value}
+    [Documentation]  Return a pod name from a given label
+    ${rc}    ${pod_name}=    Run and Return Rc and Output
+        ...    kubectl get pods -n ${namespace} -l ${label_key}=${label_value} --no-headers | awk '{print $1}'
+    Should Not Be Empty    ${pod_name}    Pod not found
+    [return]  ${pod_name}
+
 Validate Pods Status By Label
     [Arguments]    ${namespace}    ${label_key}   ${label_value}    ${expectedStatus}
     [Documentation]    To run the kubectl command and check the status of all pods filter
diff --git a/libraries/onos.robot b/libraries/onos.robot
index 66904b9..1f785f5 100755
--- a/libraries/onos.robot
+++ b/libraries/onos.robot
@@ -419,6 +419,38 @@
     ${aaa_users}=    Execute ONOS CLI Command    ${ip}    ${port}    aaa-users | grep AUTHORIZED | grep ${onu_port}
     Should Not Be Empty    ${aaa_users}    ONU port ${onu_port} not found in aaa-users
 
+Verify ONU in Groups
+    [Arguments]    ${ip_onos}    ${port_onos}    ${deviceId}    ${onu_port}    ${group_exist}=True
+    [Documentation]    Verifies that the specified onu_port exists in groups output
+    ${result}=    Execute ONOS CLI Command    ${ip_onos}    ${port_onos}    groups -j
+    Log    Groups: ${result}
+    ${groups}=    To Json    ${result}
+    ${length}=    Get Length    ${groups}
+    ${buckets}=    Create List
+    ${matched}=    Set Variable    False
+    FOR    ${INDEX}    IN RANGE    0    ${length}
+        ${value}=    Get From List    ${groups}    ${INDEX}
+        ${devId}=    Get From Dictionary    ${value}    deviceId
+        ${bucket}=    Get From Dictionary    ${value}    buckets
+        Run Keyword If    '${devId}'=='${deviceId}'
+        ...    Append To List    ${buckets}    ${bucket}
+    END
+    ${bucket_len}=    Get Length    ${buckets}
+    FOR    ${INDEX1}    IN RANGE    0    ${bucket_len}
+        ${value}=    Get From List    ${buckets}    ${INDEX}
+        ${value_bucket}=    Get From List    ${value}    0
+        ${treatment}=    Get From Dictionary    ${value_bucket}    treatment
+        ${instructions}=    Get From Dictionary    ${treatment}    instructions
+        ${instructions_val}=    Get From List    ${instructions}    0
+        ${port}=    Get From Dictionary    ${instructions_val}    port
+        ${matched}=    Set Variable If    '${port}'=='${onu_port}'    True    False
+        Exit For Loop If    ${matched}
+    END
+    Run Keyword If    ${group_exist}
+    ...    Should Be True    ${matched}    No match for ${deviceId} and ${onu_port} found in ONOS groups
+    ...    ELSE
+    ...    Should Be True    '${matched}'=='False'    Match for ${deviceId} and ${onu_port} found in ONOS groups
+
 Assert Number of AAA-Users
     [Arguments]    ${onos_ssh_connection}    ${expected_onus}   ${deviceId}
     [Documentation]    Matches for number of aaa-users authorized based on number of onus