예제 #1
0
def ues():
    if request.method == 'GET':
        res = requests.get(ems_conf['amf-service_url'] + "/ues")
        if res.status_code == requests.codes.ok:
            ret = json.loads(res.text)
            # add disconnected ues
            ue_set = set()
            for ue in ret["uelist"]:
                ue_set.add(ue["imsi"].replace('.', ''))
            for ue in config["UE_INFO"]:
                if ue["imsi"].replace('.', '') not in ue_set:
                    ue_tmp = {}
                    ue_tmp["imsi"] = ue["imsi"]
                    ue_tmp["uekey"] = ue["key"]["k"]
                    ue_tmp["status"] = "DISCONNECTED"
                    ret["uelist"].append(ue_tmp)
            return ret
        else:
            return '{ "message" : "cannot get data from amf-service" }'
    elif request.method == 'POST':  # create ue
        # create ue in config
        req_data_str = request.data.decode('ascii')
        # convert str to dict
        req_dict = json.loads(req_data_str)
        # convert dict to attrdict type string
        req_gen_conf = gen_conf(req_dict)
        # convert string to attrdict
        new_ue = libconf.loads(req_gen_conf[1:-1])
        # check ue format
        if not type_check(new_ue, "ue"):
            return '{ "message" : "wrong ue format" }'
        # check if imsi already exist
        ue_list_tmp = list(config["UE_INFO"])
        for ue_tmp in ue_list_tmp:
            if ue_tmp["imsi"].replace('.',
                                      '') == new_ue["imsi"].replace('.', ''):
                return '{ "message" : "ue already exist" }'
        imsi = new_ue["imsi"].replace('.', '')
        new_ue["imsi"] = imsi[:3] + "." + imsi[3:5] + "." + imsi[5:]
        ue_list_tmp.append(new_ue)
        config["UE_INFO"] = tuple(ue_list_tmp)
        configReadWrite.writeConfig(config, 'conf/all.conf')
        configReadWrite.genNfConf(config, containers)
        # create ue in 5gc
        post_data = {}
        post_data["imsi"] = new_ue["imsi"]
        post_data["key"] = {}
        post_data["key"]["k"] = new_ue["key"]["k"]
        if "op" in new_ue["key"]:
            post_data["key"]["op"] = new_ue["key"]["op"]
        if "opc" in new_ue["key"]:
            post_data["key"]["opc"] = new_ue["key"]["opc"]
        res = requests.post(ems_conf['amf-service_url'] + "/ues",
                            data=json.dumps(post_data))
        if res.status_code == requests.codes.ok:
            return '{ "message" : "ue created" }'
        else:
            return '{ "message" : "cannot create ue in amf" }'
예제 #2
0
def confpage():
    if 'POST' == request.method:
        configReadWrite.storeConfig(layout=layout,
                                    config=config,
                                    data=request.form)
        configReadWrite.writeConfig(config, 'conf/all.conf')
        configReadWrite.genNfConf(config, containers)

        return redirect(url_for('homepage'))
    return render_template("editConfig.html", layout=layout, config=config)
예제 #3
0
def configapi():
    global config
    if request.method == 'GET':
        # convert attrdict to json string
        return json.dumps(config)
    elif request.method == 'POST':
        req_data_str = request.data.decode('ascii')
        # convert str to dict
        req_dict = json.loads(req_data_str)
        # convert dict to attrdict type string
        req_gen_conf = gen_conf(req_dict)
        # convert string to attrdict
        config = libconf.loads(req_gen_conf[1:-1])
        # write config to config files
        configReadWrite.writeConfig(config, 'conf/all.conf')
        configReadWrite.genNfConf(config, containers)
        return '{ "message" : "success" }'
예제 #4
0
def uesimsi(imsi):
    if not type_check(imsi, "imsi"):
        return '{ "message" : "wrong imsi format" }'
    if request.method == 'GET':  # read ue
        res = requests.get(ems_conf['amf-service_url'] + "/ues")
        if res.status_code == requests.codes.ok:
            data = json.loads(res.text)
            if "uelist" in data:
                for ue in data["uelist"]:
                    if ue["imsi"].replace('.', '') == imsi.replace('.', ''):
                        return ue
            # search in conf
            for ue in config["UE_INFO"]:
                if ue["imsi"].replace('.', '') == imsi.replace('.', ''):
                    ue_tmp = {}
                    ue_tmp["imsi"] = imsi
                    ue_tmp["uekey"] = ue["key"]["k"]
                    ue_tmp["status"] = "DISCONNECTED"
                    return ue_tmp
            return '{ "message" : "ue not exist" }'
        else:
            return '{ "message" : "cannot get data from amf-service" }'
    elif request.method == 'PUT':  # update ue
        # update ue in config
        for ue in config["UE_INFO"]:
            if ue["imsi"].replace('.', '') == imsi.replace('.', ''):
                ue_list_tmp = list(config["UE_INFO"])
                for i, ue_tmp in enumerate(ue_list_tmp):
                    if ue_tmp["imsi"].replace('.',
                                              '') == imsi.replace('.', ''):
                        req_data_str = request.data.decode('ascii')
                        # convert str to dict
                        req_dict = json.loads(req_data_str)
                        # convert dict to attrdict type string
                        req_gen_conf = gen_conf(req_dict)
                        # convert string to attrdict
                        new_ue = libconf.loads(req_gen_conf[1:-1])
                        if not type_check(new_ue, "key"):
                            return False
                        ue_list_tmp[i]["key"] = new_ue
                        break
                config["UE_INFO"] = tuple(ue_list_tmp)
                configReadWrite.writeConfig(config, 'conf/all.conf')
                configReadWrite.genNfConf(config, containers)
                # update ue in 5gc
                put_data = {}
                put_data["k"] = new_ue["k"]
                if "op" in new_ue:
                    put_data["op"] = new_ue["op"]
                if "opc" in new_ue:
                    put_data["opc"] = new_ue["opc"]
                imsi = imsi.replace('.', '')
                res = requests.put(ems_conf['amf-service_url'] + "/ues/" +
                                   imsi[:3] + "." + imsi[3:5] + "." + imsi[5:],
                                   data=json.dumps(put_data))
                if res.status_code == requests.codes.ok:
                    return '{ "message" : "ue updated" }'
                else:
                    return '{ "message" : "cannot update ue in amf" }'
        return '{ "message" : "ue not exist" }'
    elif request.method == 'DELETE':  # delete ue
        # delete ue in config
        for ue in config["UE_INFO"]:
            if ue["imsi"].replace('.', '') == imsi.replace('.', ''):
                ue_list_tmp = list(config["UE_INFO"])
                for ue_tmp in ue_list_tmp:
                    if ue_tmp["imsi"].replace('.',
                                              '') == imsi.replace('.', ''):
                        ue_list_tmp.remove(ue_tmp)
                        break
                config["UE_INFO"] = tuple(ue_list_tmp)
                configReadWrite.writeConfig(config, 'conf/all.conf')
                configReadWrite.genNfConf(config, containers)
                # delete ue in 5gc
                imsi = imsi.replace('.', '')
                res = requests.delete(ems_conf['amf-service_url'] + "/ues/" +
                                      imsi[:3] + "." + imsi[3:5] + "." +
                                      imsi[5:])
                if res.status_code == requests.codes.ok:
                    return '{ "message" : "ue deleted" }'
                else:
                    return '{ "message" : "cannot delete ue in amf" }'
        return '{ "message" : "ue not exist" }'