Exemple #1
0
	def getPolicy(self, idx):
		if idx == 1:
			return policy.Policy('config/p1_ModelWin.txt', self.getNumActions(), self.getNumStates())
		if idx == 2:
			return policy.Policy('config/p2_ModelWin.txt', self.getNumActions(), self.getNumStates())
		else:
			print("Unknown policy given to ModelWin::getPolicy")
			exit()
 def __init__(self, gamma, tau, hiddenSize, numInputs, actionSpace, device = torch.device('cpu')):
     self.device = device
     self.actionSpace = actionSpace
     self.numInputs = numInputs
     self.model = p.Policy(hiddenSize, numInputs, actionSpace, device).to(device=device)
     self.target = p.Policy(hiddenSize, numInputs, actionSpace, device).to(device=device)
     hardUpdate(self.target, self.model)
     self.gamma = gamma
     self.tau = tau
     self.optimizer = Adam(self.model.parameters(), lr=1e-4, weight_decay=1e-5)
     self.loss = torch.nn.MSELoss(reduction='sum')
Exemple #3
0
 def __init__(self):
     self.server_config = server.server_config
     self.servers = self.get_servers()
     self.total_servers = len(self.servers)
     self.policy_obj = policy.Policy(self.total_servers)
     self.current_connection_count_list = []
     for each in self.servers:
         self.current_connection_count_list.append(0)
    
     self.last_server = 0
     self.curr_conn_list = []
     self.policy = policy.Policy(self.total_servers)
     self.persi_hit = 0
Exemple #4
0
def test_check_policy():
    """
    Test that packet match against policy works correctly
    """
    #*** Instantiate tc, flows and identities classes, specifying
    #*** a particular main_policy file to use:
    policy = policy_module.Policy(
        config,
        pol_dir_default="config/tests/regression",
        pol_dir_user="******",
        pol_filename="main_policy_regression_static.yaml")
    flow = flows_module.Flow(config)
    ident = identities.Identities(config, policy)

    #*** Note: cannot query a classification until a packet has been
    #*** ingested - will throw error

    #*** Ingest a packet:
    #*** Test Flow 1 Packet 1 (Client TCP SYN):
    # 10.1.0.1 10.1.0.2 TCP 74 43297 > http [SYN]
    flow.ingest_packet(DPID1, INPORT1, pkts.RAW[0], datetime.datetime.now())
    #*** Check policy:
    policy.check_policy(flow, ident)
    #*** Should not match any rules in that policy:
    logger.debug("flow.classification.classified=%s",
                 flow.classification.classified)
    assert flow.classification.classified == 1
    assert flow.classification.classification_tag == ""
    assert flow.classification.actions == {}

    #*** Re-instantiate policy with different policy that should classify:
    policy = policy_module.Policy(
        config,
        pol_dir_default="config/tests/regression",
        pol_dir_user="******",
        pol_filename="main_policy_regression_static_3.yaml")

    #*** Re-ingest packet:
    #*** Test Flow 1 Packet 1 (Client TCP SYN):
    # 10.1.0.1 10.1.0.2 TCP 74 43297 > http [SYN]
    flow.ingest_packet(DPID1, INPORT1, pkts.RAW[0], datetime.datetime.now())
    #*** Check policy:
    policy.check_policy(flow, ident)
    #*** Should match policy:
    assert flow.classification.classified == 1
    assert flow.classification.classification_tag == "Constrained Bandwidth Traffic"
    logger.debug("flow.classification.actions=%s", flow.classification.actions)
    assert flow.classification.actions == {
        'set_desc': 'Constrained Bandwidth Traffic',
        'qos_treatment': 'constrained_bw'
    }
Exemple #5
0
	def getPolicy(self, idx):
		if idx == 1:
			return policy.Policy('config/p1.txt', self.getNumActions(), self.getNumStates())
		if idx == 2:
			return policy.Policy('config/p2.txt', self.getNumActions(), self.getNumStates())
		if idx == 3:
			return policy.Policy('config/p3.txt', self.getNumActions(), self.getNumStates())
		if idx == 4:
			return policy.Policy('config/p4.txt', self.getNumActions(), self.getNumStates())
		if idx == 5:
			return policy.Policy('config/p5.txt', self.getNumActions(), self.getNumStates())
		else:
			print("Unknown policy given to Gridworld::getPolicy")
			exit()
Exemple #6
0
def load_policy_network(name):
    in_dim = preprocessing.state_to_tensor.features
    out_dim = preprocessing.action_to_tensor.features
    model = L.Classifier(policy.Policy(in_dim, out_dim))
    serializers.load_npz('%s.model.npz' % name, model)

    def policy_network(gs):
        state = preprocessing.state_to_tensor(gs)
        Y = model.predictor([state]).data[0]

        actions = []
        for idx, pos in gs.legal_moves():
            action = preprocessing.action_to_tensor(gs, idx, pos, gs.size)
            actions.append(action)

        # 确保即使actions为空列表,也要构造一个int型的空np数组
        actions = np.array(actions, dtype=np.int32)
        Y = Y[actions]
        Y = Y.reshape((1, Y.size))
        Y = Variable(Y)

        P = F.softmax(Y).data[0]

        for idx, pos in enumerate(gs.legal_moves()):
            yield pos, P[idx]

    return policy_network
Exemple #7
0
def delete_prereq():
    with open('configure.json') as file:
        json_text = json.load(file)

    # Delete Thing
    thing_name = json_text['thing_name']
    thing_obj = thing.Thing(thing_name)
    thing_obj.delete()

    # Delete certificate
    cert_id_filename = thing_name + '_cert_id.txt'
    cert_id_file = open(cert_id_filename, 'r')
    cert_id = cert_id_file.read()
    cert_obj = certs.Certificate(cert_id)
    cert_obj.delete()
    cert_id_file.close()
    cert_id_file_path = os.path.abspath(cert_id_filename)
    os.chmod(cert_id_file_path, 0o666)
    os.remove(cert_id_filename)

    # Delete cert_pem file and private_key_pem file
    cert_pem_filename = thing_name + '_cert.pem'
    private_key_pem_filename = thing_name + '_private_key.pem'
    cert_pem_file_path = os.path.abspath(cert_pem_filename)
    private_key_pem_file_path = os.path.abspath(private_key_pem_filename)
    os.chmod(cert_pem_file_path, 0o666)
    os.chmod(private_key_pem_file_path, 0o666)
    os.remove(cert_pem_filename)
    os.remove(private_key_pem_filename)

    # Delete policy
    policy_obj = policy.Policy(thing_name)
    policy_obj.delete()
def cleanup():
    certIdlength = 64
    with open('configure.json') as file:
        json_text = json.load(file)

    thing_name = json_text['thing_name']
    thing_obj = thing.Thing(thing_name)

    principals = thing_obj.list_principals()

    # Delete certificates and policies attached to thing
    for eachPrincipal in principals:
        certId = eachPrincipal[-certIdlength:]
        certarn = eachPrincipal
        cert_obj = certs.Certificate(certId)

        # Get policies attached to certificate
        policies_attached = cert_obj.list_policies()

        # Delete certificate
        cert_obj.delete()

        # Delete policies attached to the certificate
        for each_policy in policies_attached:
            policy_obj = policy.Policy(each_policy['policyName'])
            policy_obj.delete()

    # Delete Thing
    thing_obj.delete()
Exemple #9
0
    def __init__(self, controll_config, samples, en_data, en_train,
                 en_auto_sla, accuracy, sleep_interval, sample_len, llcM):
        #self.logging.basicConfig('logger.log',logging.INFO)
        #self.logger = logging.getLogger('example1')
        self.enable_data_driven = en_data
        self.enable_training = en_train
        self.enable_auto_sla = en_auto_sla
        self.sleep_interval = sleep_interval
        self.allGroups = samples  # ["app1","app2"]
        self.sample_len = sample_len
        self.controlConfig = controll_config
        self.slas = {}

        self.policies = {}
        for g in samples:
            self.policies[g] = po.Policy(g, self.allGroups, controll_config,
                                         accuracy)
            self.slas[g] = controll_config[g]["SLA"]["ipc"]

        self.currentInfo = {}
        self.llcM = llcM

        self.throttled_group = set()

        self.relax_count = 0
Exemple #10
0
    def one_step_lookahead(self, value, error_prob=0):

        new_policy_matrix = [[[None for y in xrange(self.length)]
                              for x in xrange(self.width)]
                             for h in xrange(self.num_headings)]
        for state in self.state_space.states:
            possible_states = self.state_space.get_adjacent_states(state)
            max_action_value = float("-inf")
            best_action = None
            for action_tuple in ac.action_space:
                action = ac.Action(action_tuple[0], action_tuple[1])
                action_value = 0
                for next_state in possible_states:
                    x, y, h = next_state.get_state()
                    action_value += self.transition_prob(
                        error_prob, state, action,
                        next_state) * value[h][x][y]  # add all Psa(s')V(s')
                if action_value > max_action_value:
                    max_action_value = action_value
                    best_action = action

            x, y, h = state.get_state()
            new_policy_matrix[h][x][y] = best_action

        new_policy = pol.Policy(new_policy_matrix)
        return new_policy
Exemple #11
0
def delete_prereq():
    with open('configure.json') as file:
        json_text = json.load(file)

    # Delete Thing
    thing_name = json_text['thing_name']
    thing_obj = thing.Thing(thing_name)
    thing_obj.delete()

    # Delete certificate
    cert_id_filename = thing_name + '_cert_id_file'
    cert_id_file = open(cert_id_filename, 'r')
    cert_id = cert_id_file.read()
    cert_obj = certs.Certificate(cert_id)
    cert_obj.delete()
    os.remove(cert_id_filename)

    # Delete cert_pem file and private_key_pem file
    cert_pem_filename = thing_name + '_cert_pem_file'
    private_key_pem_filename = thing_name + '_private_key_pem_file'
    os.remove(cert_pem_filename)
    os.remove(private_key_pem_filename)

    # Delete policy
    policy_name = thing_name + '_amazon_freertos_policy'
    policy_obj = policy.Policy(policy_name)
    policy_obj.delete()
Exemple #12
0
def get_target(s_real, discount, max_reward=[999999, 0], depth=0):
    """returns the optimal target given a state"""
    if depth == depth_to_check:
        return 1, s_real.reward()

    p = policy.Policy()

    for i in range(8):
        s = state.State(
            p,
            ev_profile=s_real.EV_PROFILE,
            time=s_real.time,
            house_demand=s_real.house_demand,
            ev_at_home=s_real.ev_at_home,
            ev_charge=s_real.ev_charge,
            bat_charge=s_real.bat_charge,
            flexi_charge=s_real.flexi_charge,
            ev_capacity=s_real.EV_CAPACITY,
            battery_capacity=s_real.BATTERY_CAPACITY,
            variable_load_power_req=s_real.VARIABLE_LOAD_POWER_REQ,
            solar_generation_capacity=s_real.SOLAR_GENERATION_CAPACITY,
            solar_generated=s_real.solar_generated)
        p.manual_update(i)
        s.update(p)
        reward = s.reward() + get_target(s, discount, max_reward,
                                         depth + 1)[1] * discount
        if reward < max_reward[0]:
            max_reward = [reward, i]

    result = [0 for i in range(8)]
    result[max_reward[1]] = 1
    return result, max_reward[0]
def prereq():
    with open('configure.json') as file:
        json_text = json.load(file)

    # Validate that the entries in the JSON are valid.
    validate_json_text(json_text)

    # Create a Thing
    thing_name = json_text['thing_name']
    thing_obj = thing.Thing(thing_name)
    if not thing_obj.create():

        # Create a Certificate
        cert_obj = certs.Certificate()
        result = cert_obj.create()

        # Store certId
        cert_id = result['certificateId']
        cert_id_filename = thing_name + '_cert_id_file'
        print('Writing certificate ID to: {}'.format(cert_id_filename))
        cert_id_file = open(cert_id_filename, 'w')
        cert_id_file.write(cert_id)
        cert_id_file_path = os.path.abspath(cert_id_filename)
        os.chmod(cert_id_file_path, 0o444)
        cert_id_file.close()

        # Store cert_pem as file
        cert_pem = result['certificatePem']
        cert_pem_filename = thing_name + '_cert_pem_file'
        print('Writing certificate PEM to: {}'.format(cert_pem_filename))
        cert_pem_file = open(cert_pem_filename, 'w')
        cert_pem_file.write(cert_pem)
        cert_pem_file_path = os.path.abspath(cert_pem_filename)
        os.chmod(cert_pem_file_path, 0o444)
        cert_pem_file.close()

        # Store private key PEM as file
        private_key_pem = result['keyPair']['PrivateKey']
        private_key_pem_filename = thing_name + '_private_key_pem_file'
        print(
            'Writing private key PEM to: {}'.format(private_key_pem_filename))
        private_key_pem_file = open(private_key_pem_filename, 'w')
        private_key_pem_file.write(private_key_pem)
        private_key_pem_file_path = os.path.abspath(private_key_pem_filename)
        os.chmod(private_key_pem_file_path, 0o444)
        private_key_pem_file.close()

        # Create a Policy
        policy_document = misc.create_policy_document()
        policy_name = thing_name + '_amazon_freertos_policy'
        policy_obj = policy.Policy(policy_name, policy_document)
        policy_obj.create()

        # Attach certificate to Thing
        cert_obj.attach_thing(thing_name)

        # Attach policy to certificate
        cert_obj.attach_policy(policy_name)
        print("Completed prereq operation!")
Exemple #14
0
def test_start_crawler():
    plc = policy.Policy()
    plc.CRAWLER_TYPE = 2
    plc.CRAWLER_NUMBER = 1
    plc.APPLY_TIME_INTERVAL = True
    plc.START_FILTER[URL] = "www.gdgpo.gov.cn"

    start_crawler.start_crawlers(plc)
Exemple #15
0
def test_check_tc_rule():
    #*** Instantiate classes:
    policy = policy_module.Policy(
        config,
        pol_dir_default="config/tests/regression",
        pol_dir_user="******",
        pol_filename="main_policy_regression_static.yaml")
    flow = flows_module.Flow(config)
    ident = identities.Identities(config, policy)

    #*** Test Flow 1 Packet 1 (Client TCP SYN):
    # 10.1.0.1 10.1.0.2 TCP 74 43297 > http [SYN]
    flow.ingest_packet(DPID1, INPORT1, pkts.RAW[0], datetime.datetime.now())
    #*** Set policy.pkt as work around for not calling parent method that sets it:
    policy.pkt = flow.packet

    #*** main_policy_regression_static.yaml shouldn't match HTTP (rule 0):
    tc_rules = policy_module.TCRules(policy)
    tc_rule = policy_module.TCRule(tc_rules, policy, 0)
    tc_rule_result = tc_rule.check_tc_rule(flow, ident)
    assert tc_rule_result.match == False
    assert tc_rule_result.continue_to_inspect == False
    assert tc_rule_result.classification_tag == ""
    assert tc_rule_result.actions == {}

    #*** main_policy_regression_static_3.yaml should match HTTP (rule 0):
    policy = policy_module.Policy(
        config,
        pol_dir_default="config/tests/regression",
        pol_dir_user="******",
        pol_filename="main_policy_regression_static_3.yaml")
    ident = identities.Identities(config, policy)
    tc_rules = policy_module.TCRules(policy)
    tc_rule = policy_module.TCRule(tc_rules, policy, 0)
    tc_rule_result = tc_rule.check_tc_rule(flow, ident)
    assert tc_rule_result.match == True
    assert tc_rule_result.continue_to_inspect == False
    assert tc_rule_result.classification_tag == "Constrained Bandwidth Traffic"
    assert tc_rule_result.actions == {
        'qos_treatment': 'constrained_bw',
        'set_desc': 'Constrained Bandwidth Traffic'
    }
Exemple #16
0
def test_harvest_DHCP():
    """
    Test harvesting identity metadata from an IPv4 DHCP request
    Note: this test is very basic and does not cover much...
    TBD: cover more scenarios and DHCP message types
    """
    #*** Instantiate flow, policy and identities objects:
    flow = flows_module.Flow(config)
    policy = policy_module.Policy(config)
    identities = identities_module.Identities(config, policy)

    #*** Client to Server DHCP Request:
    flow.ingest_packet(DPID1, INPORT1, pkts_dhcp.RAW[2],
                       datetime.datetime.now())
    identities.harvest(pkts_dhcp.RAW[2], flow.packet)
    flow_pkt = flow.packet

    assert identities.dhcp_msg.dpid == DPID1
    assert identities.dhcp_msg.in_port == INPORT1
    assert identities.dhcp_msg.eth_src == flow_pkt.eth_src
    assert identities.dhcp_msg.eth_dst == flow_pkt.eth_dst
    assert identities.dhcp_msg.ip_src == flow_pkt.ip_src
    assert identities.dhcp_msg.ip_dst == flow_pkt.ip_dst
    assert identities.dhcp_msg.tp_src == flow_pkt.tp_src
    assert identities.dhcp_msg.tp_dst == flow_pkt.tp_dst
    assert identities.dhcp_msg.transaction_id == '0xabc5667f'
    assert identities.dhcp_msg.host_name == 'pc1'
    assert identities.dhcp_msg.message_type == 'DHCPREQUEST'

    #*** Server to Client DHCP ACK:
    #*** Set ingest time so we can check validity based on lease
    ingest_time = datetime.datetime.now()
    flow.ingest_packet(DPID1, INPORT2, pkts_dhcp.RAW[3], ingest_time)
    identities.harvest(pkts_dhcp.RAW[3], flow.packet)
    flow_pkt = flow.packet

    assert identities.dhcp_msg.dpid == DPID1
    assert identities.dhcp_msg.in_port == INPORT2
    assert identities.dhcp_msg.eth_src == flow_pkt.eth_src
    assert identities.dhcp_msg.eth_dst == flow_pkt.eth_dst
    assert identities.dhcp_msg.ip_src == flow_pkt.ip_src
    assert identities.dhcp_msg.ip_dst == flow_pkt.ip_dst
    assert identities.dhcp_msg.tp_src == flow_pkt.tp_src
    assert identities.dhcp_msg.tp_dst == flow_pkt.tp_dst
    assert identities.dhcp_msg.transaction_id == '0xabc5667f'
    assert identities.dhcp_msg.host_name == ''
    assert identities.dhcp_msg.ip_assigned == '10.1.0.1'
    assert identities.dhcp_msg.message_type == 'DHCPACK'
    assert identities.dhcp_msg.lease_time == 300

    result_identity = identities.findbynode('pc1')
    logger.debug("result_identity=%s", result_identity)
    assert result_identity['mac_address'] == pkts_dhcp.ETH_SRC[2]
    assert result_identity['ip_address'] == '10.1.0.1'
Exemple #17
0
    def __init__(self, text=None, part2=False):

        # 1. Set the initial values
        self.part2 = part2
        self.text = text
        self.policies = []

        # 2. Process text (if any)
        if text is not None and len(text) > 0:
            for line in text:
                self.policies.append(policy.Policy(text=line))
Exemple #18
0
def prereq():
    with open('configure.json') as file:
        json_text = json.load(file)

    # If using existing policy, make sure policy exists before
    # creating the thing
    thing_name = json_text.get('thing_name', "")
    mac_addr = json_text.get('mac_addr', "")

    # Create a Thing
    thing_obj = thing.Thing(thing_name)
    if not thing_obj.create():
        # Create a Certificate
        cert_obj = certs.Certificate()
        result = cert_obj.create()

        # Store certId
        cert_id = result['certificateId']
        cert_id_filename = thing_name + '_cert_id.txt'
        cert_id_file = open(cert_id_filename, 'w')
        cert_id_file.write(cert_id)
        cert_id_file_path = os.path.abspath(cert_id_filename)
        os.chmod(cert_id_file_path, 0o664)
        cert_id_file.close()

        # Store cert_pem as file
        cert_pem = result['certificatePem']
        cert_pem_filename = thing_name + '_cert.pem'
        cert_pem_file = open(cert_pem_filename, 'w')
        cert_pem_file.write(cert_pem)
        cert_pem_file_path = os.path.abspath(cert_pem_filename)
        os.chmod(cert_pem_file_path, 0o664)
        cert_pem_file.close()

        # Store private key PEM as file
        private_key_pem = result['keyPair']['PrivateKey']
        private_key_pem_filename = thing_name + '_private_key.pem'
        private_key_pem_file = open(private_key_pem_filename, 'w')
        private_key_pem_file.write(private_key_pem)
        private_key_pem_file_path = os.path.abspath(private_key_pem_filename)
        os.chmod(private_key_pem_file_path, 0o664)
        private_key_pem_file.close()

        # Create thing policy
        policy_document_text = misc.create_policy_document_text(thing_name)
        if policy_document_text:
            policy_obj = policy.Policy(thing_name, policy_document_text)
            cert_policy_name = policy_obj.create()
            print('Creating Certificate Policy {}'.format(cert_policy_name))

        # Attach the Policy to the Cert, Cert to thing
        cert_obj.attach_thing(thing_name)
        cert_obj.attach_policy(cert_policy_name)
Exemple #19
0
    def add_new_policy(self, wiera, policy_spec):
        #		if policy_id not in self.policies:
        wiera_id = policy_spec['id']

        if wiera_id in self.policies:
            return self.policies[wiera_id]

        new_policy = policy.Policy(wiera, policy_spec)
        new_policy_id = new_policy.get_policy_id()
        self.policies[new_policy_id] = new_policy

        return new_policy
Exemple #20
0
    def __init__(self):
        #Topics & Subs, Pubs
        lidarscan_topic = '/scan'
        drive_topic = '/drive'

        self.pol = pl.Policy()

        self.lidar_sub = rospy.Subscriber(lidarscan_topic, LaserScan,
                                          self.lidar_callback)
        self.drive_pub = rospy.Publisher(drive_topic,
                                         AckermannDriveStamped,
                                         queue_size=20)
Exemple #21
0
def prereq():
    with open('configure.json') as configure_file:
        json_text = json.load(configure_file)

    # Create a Thing
    thing_name = json_text['thing_name']
    thing_obj = thing.Thing(thing_name)
    if not thing_obj.create():

        # Create a Certificate
        cert_obj = certs.Certificate()
        result = cert_obj.create()

        # Store certId
        cert_id = result['certificateId']
        cert_id_filename = thing_name + '_cert_id_file.txt'
        cert_id_file = open(cert_id_filename, 'w')
        cert_id_file.write(cert_id)
        cert_id_file_path = os.path.abspath(cert_id_filename)
        os.chmod(cert_id_file_path, 0o444)
        cert_id_file.close()

        # Store cert_pem as file
        cert_pem = result['certificatePem']
        cert_pem_filename = thing_name + '_cert_pem_file.pem'
        cert_pem_file = open(cert_pem_filename, 'w')
        cert_pem_file.write(cert_pem)
        cert_pem_file_path = os.path.abspath(cert_pem_filename)
        os.chmod(cert_pem_file_path, 0o444)
        cert_pem_file.close()

        # Store private key PEM as file
        private_key_pem = result['keyPair']['PrivateKey']
        private_key_pem_filename = thing_name + '_private_key_pem_file.pem'
        private_key_pem_file = open(private_key_pem_filename, 'w')
        private_key_pem_file.write(private_key_pem)
        private_key_pem_file_path = os.path.abspath(private_key_pem_filename)
        os.chmod(private_key_pem_file_path, 0o444)
        private_key_pem_file.close()

        # Create a Policy
        policy_document = misc.create_policy_document()
        policy_name = thing_name + '_amazon_freertos_policy'
        policy_obj = policy.Policy(policy_name, policy_document)
        policy_obj.create()

        # Attach certificate to Thing
        cert_obj.attach_thing(thing_name)

        # Attach policy to certificate
        cert_obj.attach_policy(policy_name)
Exemple #22
0
def test_get_classification():
    """
    Test get_classification which takes a flow_hash
    and return a dictionary of a classification object
    for the flow_hash (if found), otherwise
    a dictionary of an empty classification object.
    """
    #*** Instantiate flow, policy and identities objects:
    flow = flows_module.Flow(config)
    policy = policy_module.Policy(config)
    ident = identities_module.Identities(config, policy)

    #*** Initial main_policy that matches tcp-80:
    policy = policy_module.Policy(
        config,
        pol_dir_default="config/tests/regression",
        pol_dir_user="******",
        pol_filename="main_policy_regression_static_3.yaml")

    #*** Ingest Flow 1 Packet 0 (Client TCP SYN):
    flow.ingest_packet(DPID1, INPORT1, pkts.RAW[0], datetime.datetime.now())
    #*** Classify the packet:
    policy.check_policy(flow, ident)

    logger.debug("pkt0 flow classification is %s",
                 flow.classification.dbdict())

    #*** Write classification result to classifications collection:
    flow.classification.commit()

    #*** Retrieve classification via get_classification and check results:
    clasfn_result = api.get_classification(flow.classification.flow_hash)
    assert clasfn_result['classified'] == 1
    assert clasfn_result[
        'classification_tag'] == "Constrained Bandwidth Traffic"
    assert clasfn_result['actions'][
        'set_desc'] == "Constrained Bandwidth Traffic"
    assert clasfn_result['actions']['qos_treatment'] == "constrained_bw"
Exemple #23
0
    def __init__(self, name, x, y, n, edge_proximity, gamma, hpolicy, mpolicy,
                 frequencies_dict):
        '''Defines an agent, which represents a node in the city-level infection network.

        :param str name: name of the city
        :param int x: width
        :param int y: height
        :param int n: num agents in city
        :param float edge_proximity: edge proximity (proxy for infectivity)
        :param float gamma: experimental gamma denominator
        :param str hpolicy: health policy name
        :param list[str, dict] mpolicy: movement policy name
        :param dict frequencies_dict: dictionary of special point frequencies
        '''
        self.POLICIES = None

        self.gamma = gamma  # gamma naught for covid 19
        self.num_infected = 0
        self.num_removed = 0
        self.num_susceptible = 0
        self.num_quarantined = 0
        self.N = n

        self.name = name

        self.width = x
        self.height = y
        self.datafile = '{}x{}x{}parameter_sweep_data.txt'.format(
            self.width, self.height, self.N)

        self.area = self.width * self.height
        self.agents_per_market = frequencies_dict['market']
        self.agents_per_transit = frequencies_dict['transit']
        self.agents_per_work = frequencies_dict['work']
        self.agents_per_home = frequencies_dict['home']

        self.past_networks = []
        self.network = None
        self.edge_proximity = edge_proximity  # proxy for infectivity
        self.policy = policy.Policy(hpolicy, mpolicy)

        self.agents = [Agent(i, self) for i in range(0, self.N)]

        self.quarantine_center_location = None
        self.quarantine_threshold = 4
        self.quarantine_rate = 0.05

        self.setup_agent_central_locations()
        self.agent_dict = {v.number: v for v in self.agents}
def prereq():
    with open('configure.json') as file:
        json_text = json.load(file)
        aws_config = json_text["aws_config"]
        optiga_config = json_text["optiga_trust_config"]

    # Create a Certificate
    cert_obj = certs.Certificate()
    result = cert_obj.create(optiga_config['executable_path'],
                             optiga_config['i2c_device'],
                             optiga_config['privatekey_objectid'],
                             optiga_config['certificate_objectid'])

    # Create a Thing if doesn't exist
    thing_name = aws_config['thing_name']
    thing_obj = thing.Thing(thing_name)
    if not thing_obj.exists():
        thing_obj.create()

        # Store certId
        cert_id = result['certificateId']
        cert_id_filename = thing_name + '_cert_id_file'
        cert_id_file = open(cert_id_filename, 'w')
        cert_id_file.write(cert_id)
        cert_id_file_path = os.path.abspath(cert_id_filename)
        os.chmod(cert_id_file_path, 0o444)
        cert_id_file.close()

        # Store cert_pem as file
        cert_pem = result['certificatePem']
        cert_pem_filename = thing_name + '_cert_pem_file'
        cert_pem_file = open(cert_pem_filename, 'w')
        cert_pem_file.write(cert_pem)
        cert_pem_file_path = os.path.abspath(cert_pem_filename)
        os.chmod(cert_pem_file_path, 0o444)
        cert_pem_file.close()

    # Create a Policy if doesn't exist
    policy_obj = policy.Policy(aws_config['policy_name'])
    if not policy_obj.exists():
        policy_document = misc.create_policy_document()
        policy_obj.attach_rules(policy_document)
        policy_obj.create()

    # Attach certificate to Thing
    cert_obj.attach_thing(aws_config['thing_name'])

    # Attach policy to certificate
    cert_obj.attach_policy(aws_config['policy_name'])
Exemple #25
0
def test_custom_classifiers():
    """
    Check deduplicated list of custom classifiers works
    """
    #*** Instantiate policy, specifying
    #*** a particular main_policy file to use that has no custom classifiers:
    policy = policy_module.Policy(
        config,
        pol_dir_default="config/tests/regression",
        pol_dir_user="******",
        pol_filename="main_policy_regression_static.yaml")
    assert policy.tc_rules.custom_classifiers == []

    #*** Instantiate policy, specifying
    #*** a custom statistical main_policy file to use that has a
    #*** custom classifier:
    policy = policy_module.Policy(
        config,
        pol_dir_default="config/tests/regression",
        pol_dir_user="******",
        pol_filename="main_policy_regression_statistical.yaml")
    assert policy.tc_rules.custom_classifiers == [
        'statistical_qos_bandwidth_1'
    ]
def setup():
    with open('configure.json') as file:
        json_text = json.load(file)

    # Create a Thing
    thing_name = json_text['thing_name']
    thing_obj = thing.Thing(thing_name)
    if not thing_obj.create():

        # Create a Certificate
        cert_obj = certs.Certificate()
        result = cert_obj.create()

        # Store cert_pem
        cert_pem = result['certificatePem']

        # Store Private key PEM
        private_key_pem = result['keyPair']['PrivateKey']

        wifi_ssid = json_text['wifi_ssid']
        wifi_passwd = json_text['wifi_password']
        wifi_security = json_text['wifi_security']

        # Modify 'aws_clientcredential.h' file
        misc.client_credential(
            wifi_ssid = wifi_ssid,
            wifi_passwd = wifi_passwd,
            wifi_security = wifi_security,
            thing_name = thing_name,
            credentials_or_keys = "client_credential")

        # Modify 'aws_clientcredential_keys.h' file
        misc.client_credential(
            client_certificate_pem = cert_pem,
            clientprivate_key_pem = private_key_pem,
            credentials_or_keys = "client_keys")

        # Create a Policy
        policy_document = misc.create_policy_document()
        policy_name = thing_name + '_amazon_freertos_policy'
        policy_obj = policy.Policy(policy_name, policy_document)
        policy_obj.create()

        # Attach certificate to Thing
        cert_obj.attach_thing(thing_name)

        # Attach policy to certificate
        cert_obj.attach_policy(policy_name)
Exemple #27
0
def test_harvest_LLDP():
    """
    Test harvesting identity metadata from LLDP packets
    """
    #*** Instantiate flow, policy and identities objects:
    flow = flows_module.Flow(config)
    policy = policy_module.Policy(config)
    identities = identities_module.Identities(config, policy)

    #*** Test no result found by checking before LLDP ingestion:
    result_identity = identities.findbynode(pkts_lldp.LLDP_SYSTEM_NAME[0])
    assert result_identity == 0

    #*** LLDP packet 0:
    flow.ingest_packet(DPID1, INPORT1, pkts_lldp.RAW[0],
                       datetime.datetime.now())
    identities.harvest(pkts_lldp.RAW[0], flow.packet)
    result_identity = identities.findbynode(pkts_lldp.LLDP_SYSTEM_NAME[0])
    assert result_identity['host_name'] == pkts_lldp.LLDP_SYSTEM_NAME[0]
    assert result_identity['host_desc'] == pkts_lldp.LLDP_SYSTEM_DESC[0]
    assert result_identity['dpid'] == DPID1
    assert result_identity['in_port'] == INPORT1
    assert result_identity['mac_address'] == pkts_lldp.ETH_SRC[0]
    assert result_identity['harvest_type'] == 'LLDP'

    #*** LLDP packet 1:
    flow.ingest_packet(DPID1, INPORT1, pkts_lldp.RAW[1],
                       datetime.datetime.now())
    identities.harvest(pkts_lldp.RAW[1], flow.packet)
    result_identity = identities.findbynode(pkts_lldp.LLDP_SYSTEM_NAME[1])
    assert result_identity['host_name'] == pkts_lldp.LLDP_SYSTEM_NAME[1]
    assert result_identity['host_desc'] == pkts_lldp.LLDP_SYSTEM_DESC[1]
    assert result_identity['dpid'] == DPID1
    assert result_identity['in_port'] == INPORT1
    assert result_identity['mac_address'] == pkts_lldp.ETH_SRC[1]
    assert result_identity['harvest_type'] == 'LLDP'

    #*** LLDP packet 2:
    flow.ingest_packet(DPID1, INPORT1, pkts_lldp.RAW[2],
                       datetime.datetime.now())
    identities.harvest(pkts_lldp.RAW[2], flow.packet)
    result_identity = identities.findbynode(pkts_lldp.LLDP_SYSTEM_NAME[2])
    assert result_identity['host_name'] == pkts_lldp.LLDP_SYSTEM_NAME[2]
    assert result_identity['host_desc'] == pkts_lldp.LLDP_SYSTEM_DESC[2]
    assert result_identity['dpid'] == DPID1
    assert result_identity['in_port'] == INPORT1
    assert result_identity['mac_address'] == pkts_lldp.ETH_SRC[2]
    assert result_identity['harvest_type'] == 'LLDP'
Exemple #28
0
def test_qos():
    """
    Test the assignment of QoS queues based on a qos_treatment action
    """
    #*** Instantiate policy, specifying
    #*** a particular main_policy file to use that has no custom classifiers:
    policy = policy_module.Policy(
        config,
        pol_dir_default="config/tests/regression",
        pol_dir_user="******",
        pol_filename="main_policy_regression_static.yaml")
    assert policy.qos('default_priority') == 0
    assert policy.qos('constrained_bw') == 1
    assert policy.qos('high_priority') == 2
    assert policy.qos('low_priority') == 3
    assert policy.qos('foo') == 0
def optimal_control_es(mdp, start, gamma, episode_length, iterations):
    p = policy.Policy()
    for i in range(iterations):
        # Need to completly Randomize the Initial State action of the trajectory
        exploring_act0 = int(random.uniform(0, mdp.A - 1))
        next_state0 = mdp.next_state(start, exploring_act0)
        exploring_act1 = int(random.uniform(0, mdp.A - 1))
        next_state1 = mdp.next_state(start, exploring_act1)

        episode, rewards, actions = get_episode(mdp, next_state1,
                                                episode_length, p)
        episode = [start, next_state0] + episode
        rewards = [
            mdp.next_reward(start, exploring_act0),
            mdp.next_reward(next_state0, exploring_act1)
        ] + rewards
        actions = [exploring_act0, exploring_act1] + actions

        Q = dict()
        G = 0
        #print(str(episode) + " " + str(actions))
        print(str(i) + "...")

        def contains(l, a):
            for i in l:
                if a == i:
                    return True
            return False

        state_actions = list(zip(episode, actions))
        for i in reversed(range(1, len(state_actions))):
            G = gamma * G + rewards[i]
            if not contains(state_actions[:i - 1], (episode[i], actions[i])):
                Q[(episode[i],
                   actions[i])] = (Q[(episode[i], actions[i])][0] + G,
                                   Q[(episode[i], actions[i])][1] +
                                   1) if (episode[i], actions[i]) in Q else (G,
                                                                             1)
                rewards_action = []
                for a in range(mdp.A):
                    if (episode[i], a) in Q:
                        rewards_action.append(Q[(episode[i], a)][0] /
                                              Q[(episode[i], a)][1])
                    else:
                        rewards_action.append(0)
                p.update(episode[i], rewards_action.index(max(rewards_action)))
    return p
Exemple #30
0
def test_get_dns_ip():
    """
    Test looking up a DNS CNAME to get an IP address
    """
    #*** Instantiate flow, policy and identities objects:
    flow = flows_module.Flow(config)
    policy = policy_module.Policy(config)
    identities = identities_module.Identities(config, policy)

    #*** DNS packet 1 (NAME to CNAME, then second answer with IP for CNAME):
    flow.ingest_packet(DPID1, INPORT1, pkts_dns.RAW[1],
                       datetime.datetime.now())
    identities.harvest(pkts_dns.RAW[1], flow.packet)

    logger.debug("Testing lookup of CNAME=%s", pkts_dns.DNS_CNAME[1])
    result_ip = api.get_dns_ip(pkts_dns.DNS_CNAME[1])
    assert result_ip == pkts_dns.DNS_IP[1]