Ejemplo n.º 1
0
    def post(self, virtualnetwork):
        """
        This function implements create record functionality of the RESTful request.
        It converts the requested body in JSON format to dictionary in string representation and verifies whether
        required ATTRIBUTES are present in the POST request body and adds the record to DB and UCM if all the
        UCM_ATTRIBUTES are present.

        :return: Dictionary of the added record.
        """

        change = virtualnetwork.as_dict(api_models.VirtualNetwork)

        virtualnetworks = list(self.conn.get_virtualnetworks(nw_id=virtualnetwork.id))

        if len(virtualnetworks) > 0:
            error = _("Virtual Network with the given id exists")
            response.translatable_error = error
            raise wsme.exc.ClientSideError(unicode(error))

        try:
            vn_in = api_models.VirtualNetwork(**change)
        except Exception:
            LOG.exception("Error while posting Virtual Network: %s" % change)
            error = _("Virtual Network incorrect")
            response.translatable_error = error
            raise wsme.exc.ClientSideError(unicode(error))

        vn_out = self.conn.create_virtualnetwork(vn_in)
        
        # UCM Configuration Start
        if cfg.CONF.api.ucm_support:
            body = vn_out.as_dict()
            if body['type'] == 'vxlan':
                body['type'] = 'VXLAN_TYPE'
            ucm_record = utils.generate_ucm_data(self, body, [])
            if UCM_LOADED:
                try:
                    ret_val = _ucm.add_record(ucm_record)
                    if ret_val != 0:
                        error = _("Unable to add Virtual Network record to UCM")
                        response.translatable_error = error
                        raise wsme.exc.ClientSideError(unicode(error))
                    else:
                        LOG.debug(_("Virtual Network Added to UCM"))
                except UCMException, msg:
                    LOG.info(_("UCM Exception raised. %s\n"), msg)
                    error = _("Unable to add Virtual Network record to UCM")
                    response.translatable_error = error
                    raise wsme.exc.ClientSideError(unicode(error))
Ejemplo n.º 2
0
    def post(self, switch):
        """
        This function implements create record functionality of the RESTful request.
        It converts the requested body in JSON format to dictionary in string representation and verifies whether
        required ATTRIBUTES are present in the POST request body and adds the record to DB and UCM if all the
        UCM_ATTRIBUTES are present.

        :return: Dictionary of the added record.
        """
        change = switch.as_dict(api_models.Switch)

        switches = list(self.conn.get_switches(name=switch.name))

        if len(switches) > 0:
            error = _("Switch with the given name exists")
            response.translatable_error = error
            raise wsme.exc.ClientSideError(unicode(error))

        try:
            switch_in = api_models.Switch(**change)
        except Exception:
            LOG.exception("Error while posting Switch: %s" % change)
            error = _("Switch incorrect")
            response.translatable_error = error
            raise wsme.exc.ClientSideError(unicode(error))

        switch_out = self.conn.create_switch(switch_in)

        #UCM Support Start
        if cfg.CONF.api.ucm_support:
            body = switch_out.as_dict()
            ucm_record = utils.generate_ucm_data(self, body, [])
            if UCM_LOADED:
                try:
                    ret_val = _ucm.add_record(ucm_record)
                    if ret_val != 0:
                        error = _("Unable to add Switch record to UCM")
                        response.translatable_error = error
                        raise wsme.exc.ClientSideError(unicode(error))
                except UCMException, msg:
                    LOG.info(_("UCM Exception raised. %s\n"), msg)
                    error = _("Unable to add Switch record to UCM")
                    response.translatable_error = error
                    raise wsme.exc.ClientSideError(unicode(error))
Ejemplo n.º 3
0
    def post(self, service):
        """
        This function implements create record functionality of the RESTful request.
        It converts the requested body in JSON format to dictionary in string representation and verifies whether
        required ATTRIBUTES are present in the POST request body and adds the record to DB and UCM if all the
        UCM_ATTRIBUTES are present.

        :return: Dictionary of the added record.
        """

        change = service.as_dict(api_models.Service)

        services = list(self.conn.get_services(service_id=service.id))

        if len(services) > 0:
            error = _("Network Service with the given id exists")
            response.translatable_error = error
            raise wsme.exc.ClientSideError(unicode(error))

        if change['form_factor_type'].lower() == 'physical':
            change['form_factor_type'] = 'Physical'
        elif change['form_factor_type'].lower() == 'virtual':
            change['form_factor_type'] = 'Virtual'

        change['type'] = change['type'].capitalize()
        if change['load_share_algorithm'].lower() == 'round_robin':
            change['load_share_algorithm'] = 'Round_Robin'
        elif change['load_share_algorithm'].lower() == 'hash_based':
            change['load_share_algorithm'] = 'Hash_Based'
        elif change['load_share_algorithm'].lower() == 'least_connections':
            change['load_share_algorithm'] = 'Least_Connections'

        if change['load_indication_type'].lower() == 'connection_based':
            change['load_indication_type'] = 'Connection_Based'
        elif change['load_indication_type'].lower() == 'traffic_based':
            change['load_indication_type'] = 'Traffic_Based'

        try:
            service_in = api_models.Service(**change)
        except Exception:
            LOG.exception("Error while posting Network Service: %s" % change)
            error = _("Network service incorrect")
            response.translatable_error = error
            raise wsme.exc.ClientSideError(unicode(error))

        service_out = self.conn.create_service(service_in)

        # UCM Configuration Start
        if cfg.CONF.api.ucm_support:
            body = service_out.as_dict()
            ucm_record = utils.generate_ucm_data(self, body, [])
            if UCM_LOADED:
                try:
                    ret_val = ucm.add_record(ucm_record)
                    if ret_val != 0:
                        error = _("Unable to add chain record to UCM")
                        response.translatable_error = error
                        raise wsme.exc.ClientSideError(unicode(error))
                except ucm.UCMException, msg:
                    LOG.info(_("UCM Exception raised. %s\n"), msg)
                    error = _("Unable to add chain record to UCM")
                    response.translatable_error = error
                    raise wsme.exc.ClientSideError(unicode(error))
Ejemplo n.º 4
0
    def post(self, nw_id, subnet):
        """
        This function implements create record functionality of the RESTful request.
        It converts the requested body in JSON format to dictionary in string representation and verifies whether
        required ATTRIBUTES are present in the POST request body and adds the record to UCM if all the
        UCM_ATTRIBUTES are present.

        :return: Dictionary of the added record.
        """
        sn = subnet.as_dict(api_models.Subnet)
        for ip in sn['dns_servers']:
            validate_ip(ip)
        subnets = list(self.conn.get_subnets(sub_id=subnet.id))

        if len(subnets) > 0:
            error = _("Subnet with the given id exists")
            response.translatable_error = error
            raise wsme.exc.ClientSideError(unicode(error))

        virtualnetworks = list(self.conn.get_virtualnetworks(nw_id=nw_id))

        if len(virtualnetworks) < 1:
            raise EntityNotFound(_('Virtual Network'), nw_id)

        sn['nw_id'] = nw_id

        # Expand Pools and update the details with IDs
        pools = []
        for pool in sn['pools']:
            pool.id = str(uuid.uuid4())
            pool.subnet_id = sn['id']
            pools.append(api_models.Pool(**pool.as_dict(api_models.Pool)))
        sn['pools'] = pools

        try:
            sub_in = api_models.Subnet(**sn)
        except Exception:
            LOG.exception("Error while posting Subnet: %s" % sn)
            error = _("Virtual Network incorrect")
            response.translatable_error = error
            raise wsme.exc.ClientSideError(unicode(error))

        sub = self.conn.create_subnet(sub_in)
        sub.pools = self.pools_from_db_model(sub.pools, final=True)
        sub.vnname = VirtualNetwork.from_db_model(virtualnetworks[0]).name

        #UCM Configuration Start
        if cfg.CONF.api.ucm_support:
            body = sub.as_dict()
            pools = []
            for pool in body['pools']:
                pools.append(pool.as_dict(api_models.Pool))
            body['pools'] = pools
            ucm_record = utils.generate_ucm_data(self, body, [])
            ucm_record['cidrip']['value'], ucm_record['cidrmask']['value'] = decode_cidr(body['cidr'])

            if len(body['pools']):
                ucm_record['pool_start_addr']['value'] = body['pools'][0]['start']
                ucm_record['pool_end_addr']['value'] = body['pools'][0]['end']
            if len(body['dns_servers']):
                for i in range(0, len(sub.dns_servers)):
                    ucm_record['dns_server'+str(i + 1)] = {'type': constants.DATA_TYPES['ipaddr'],
                                                           'value': sub.dns_servers[i]}

            if UCM_LOADED:
                try:
                    ret_val = _ucm.add_record(ucm_record)
                    if ret_val != 0:
                        error = _("Unable to add Subnet record to UCM")
                        response.translatable_error = error
                        raise wsme.exc.ClientSideError(unicode(error))
                except UCMException, msg:
                    LOG.info(_("UCM Exception raised. %s\n"), msg)
                    error = _("Unable to add Subnet record to UCM")
                    response.translatable_error = error
                    raise wsme.exc.ClientSideError(unicode(error))