Example #1
0
    def put(self, switch_id, switch):
        """
        This function implements update 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 PUT request body and adds the record to UCM if all the
        UCM_ATTRIBUTES are present.

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

        switch.id = switch_id
        switches = list(self.conn.get_switchs(switch_id=switch.id))

        if len(switches) < 1:
            raise EntityNotFound(_('Switch'), switch_id)

        old_switch = Switch.from_db_model(switches[0]).as_dict(api_models.Switch)
        updated_switch = switch.as_dict(api_models.Switch)
        old_switch.update(updated_switch)
        try:
            sw_in = api_models.Switch(**old_switch)
        except Exception:
            LOG.exception("Error while putting switch: %s" % old_switch)
            error = _("Switch incorrect")
            response.translatable_error = error
            raise wsme.exc.ClientSideError(unicode(error))

        switch_out = self.conn.update_virtualmachine(sw_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:
                    req = {'name': {'type': constants.DATA_TYPES['string'], 'value': str(switch.name)},
                           'dmpath': constants.PATH_PREFIX + '.' + self.dmpath}
                    try:
                        rec = _ucm.get_exact_record(req)
                        if not rec:
                            error = _("Unable to find Switch record in 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 find Switch record in UCM")
                        response.translatable_error = error
                        raise wsme.exc.ClientSideError(unicode(error))

                    ret_val = _ucm.update_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 Update Switch record to UCM")
                    response.translatable_error = error
                    raise wsme.exc.ClientSideError(unicode(error))
Example #2
0
    def put(self, nw_id, virtualnetwork):
        """
        This function implements update 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 PUT request body and adds the record to UCM if all the
        UCM_ATTRIBUTES are present.

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

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

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

        old_vn = VirtualNetwork.from_db_model(virtualnetworks[0]).as_dict(api_models.VirtualNetwork)
        updated_vn = virtualnetwork.as_dict(api_models.VirtualNetwork)
        old_vn.update(updated_vn)
        try:
            vn_in = api_models.VirtualNetwork(**old_vn)
        except Exception:
            LOG.exception("Error while putting virtual network: %s" % old_vn)
            error = _("Virtual Network incorrect")
            response.translatable_error = error
            raise wsme.exc.ClientSideError(unicode(error))

        vn_out = self.conn.update_virtualnetwork(vn_in)
        
        #UCM Configuration Start
        if cfg.CONF.api.ucm_support:
            body = vn_out.as_dict()
            ucm_record = utils.generate_ucm_data(self, body, [])
            if UCM_LOADED:
                try:
                    req = {'name': {'type': constants.DATA_TYPES['string'], 'value': str(vn_out.name)},
                           'dmpath': constants.PATH_PREFIX + '.' + self.dmpath}
                    try:
                        rec = _ucm.get_exact_record(req)
                        if not rec:
                            error = _("Unable to find Virtual Network record in 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 find Virtual Network record in UCM")
                        response.translatable_error = error
                        raise wsme.exc.ClientSideError(unicode(error))

                    ret_val = _ucm.update_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))
                except UCMException, msg:
                    LOG.info(_("UCM Exception raised. %s\n"), msg)
                    error = _("Unable to Update Virtual Network record to UCM")
                    response.translatable_error = error
                    raise wsme.exc.ClientSideError(unicode(error))
Example #3
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))
Example #4
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))
Example #5
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))
Example #6
0
    def put(self, nw_id, sub_id, subnet):
        """
        This function implements update 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 PUT request body and adds the record to UCM if all the
        UCM_ATTRIBUTES are present.

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

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

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

        sn = subnet.as_dict(api_models.Subnet)
        if 'dns_servers' in sn:
            for ip in sn['dns_servers']:
                try:
                    validate_ip(ip)
                except Exception:
                    LOG.exception("Error while putting subnet: %s" % str(sn))
                    error = _("Subnet incorrect. Invalid DNS Server IP")
                    response.translatable_error = error
                    raise wsme.exc.ClientSideError(unicode(error))

        subnets = list(self.conn.get_subnets(sub_id=sub_id, nw_id=nw_id))

        if len(subnets) < 1:
            raise EntityNotFound(_('Subnet'), sub_id)

        subnets[0].pools = self.pools_from_db_model(subnets[0].pools)
        old_subnet = Subnet.from_db_model(subnets[0]).as_dict(api_models.Subnet)

        old_subnet.update(sn)

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

        sub = self.conn.update_subnet(sub_in)
        sub.pools = self.pools_from_db_model(sub.pools, final=True)
        #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, [])

            del ucm_record['pool_start_addr']
            del ucm_record['pool_end_addr']
            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]}
            else:
                for i in range(0, 4):
                    ucm_record['dns_server'+str(i + 1)] = {'type': constants.DATA_TYPES['ipaddr'],
                                                           'value': '0.0.0.0'}

            if UCM_LOADED:
                try:
                    req = {'name': {'type': constants.DATA_TYPES['string'], 'value': str(sub.name)},
                           'dmpath': constants.PATH_PREFIX + '.' + self.dmpath}
                    try:
                        rec = _ucm.get_exact_record(req)
                        if not rec:
                            error = _("Unable to find Subnet record in 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 find Subnet record in UCM")
                        response.translatable_error = error
                        raise wsme.exc.ClientSideError(unicode(error))

                    ret_val = _ucm.update_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 Update Subnet record to UCM")
                    response.translatable_error = error
                    raise wsme.exc.ClientSideError(unicode(error))
Example #7
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))