#!/usr/bin/env python3
""""
 Copyright 2021-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.

 Reads the standard input for CLI command output from 'onos mlb list ocns'
 and checks that the output is correct.
"""

import check_cli
import sys

expected_headers = ['sCell', 'node', 'ID', 'sCell', 'PLMN', 'ID', 'sCell', 'cell', 'ID', 'sCell', 'object', 'ID',
                    'nCell', 'PLMN', 'ID', 'nCell', 'cell', 'ID', 'Ocn', '[dB]']


SCELL_NODE_ID_REGEX = r'^e2:([a-zA-Z0-9-]+|1(\/[a-z0-9]+)+)$'
SCELL_PLNMID_REGEX = r'^[0-9a-f]{5,6}$'
SCELL_CELL_ID_REGEX = r'^[0-9a-f]{6,10}$'
SCELL_OBJECT_ID_REGEX = r'^[0-9a-f]{12,15}$'
NCELL_PLNM_ID_REGEX = r'^[0-9]{5,6}$'
NCELL_CELL_ID_REGEX = r'^[0-9a-f]{6,10}$'

if __name__ == '__main__':
    success = int(0)
    failure = int(1)

    option = sys.argv[1]

    OCN_REGEX = ""

    if option == "overload":
        OCN_REGEX = r'^[-0-9]+$'

    elif option == "underload":
        OCN_REGEX = r'^[0-9]+$'

    else:
        print("overload or underload option must be spcified")
        exit(failure)

    expected_regexes = [
        SCELL_NODE_ID_REGEX,
        SCELL_PLNMID_REGEX,
        SCELL_CELL_ID_REGEX,
        SCELL_OBJECT_ID_REGEX,
        NCELL_PLNM_ID_REGEX,
        NCELL_CELL_ID_REGEX,
        OCN_REGEX
    ]
    check = 'MLB OCNs'
    ok,output = check_cli.check_cli_output(check, expected_headers, expected_regexes)

    if not ok:
        print ("Check " + check + " failed")
        exit(failure)

    print ("Check " + check + " passed")

    exit (success)

