Beispiel #1
0
    def resource_create(self, context, rt_q):
        project_id = self._project_id_neutron_to_vnc(rt_q['tenant_id'])
        project_obj = self._project_read(proj_id=project_id)
        rt_obj = vnc_api.RouteTable(name=rt_q['name'],
                                    parent_obj=project_obj)

        if rt_q['routes']:
            for route in rt_q['routes']['route']:
                try:
                    vm_obj = self._vnc_lib.virtual_machine_read(
                        id=route['next_hop'])
                    si_list = vm_obj.get_service_instance_refs()
                    if si_list:
                        fq_name = si_list[0]['to']
                        si_obj = self._vnc_lib.service_instance_read(
                            fq_name=fq_name)
                        route['next_hop'] = si_obj.get_fq_name_str()
                    rt_obj.set_routes(
                        vnc_api.RouteTableType.factory(**rt_q['routes']))
                except Exception:
                    pass
        try:
            self._resource_create(rt_obj)
        except vnc_exc.RefsExistError as e:
            self._raise_contrail_exception(
                'BadRequest',
                resource='route_table', msg=str(e))
        ret_rt_q = self._route_table_vnc_to_neutron(rt_obj)
        return ret_rt_q
Beispiel #2
0
 def get_or_create_rt(self, vn_obj, subnet_id):
     rt_fq_name = self.subnet_rt_fq_name(vn_obj.fq_name[:-1], subnet_id)
     try:
         rt_obj = self._vnc_lib.route_table_read(fq_name=rt_fq_name,
                                                 fields=['virtual_network_back_refs'])
         # check RT is correctly linked to the VN
         if not any([vn_obj.uuid == vn_ref['uuid']
                     for vn_ref in rt_obj.get_virtual_network_back_refs() or []]):
             self._associate_vn_rt(vn_obj, rt_obj)
     except vnc_exc.NoIdError:
         project_obj = self._project_read(proj_id=vn_obj.parent_uuid)
         route_table = vnc_api.RouteTable(name=rt_fq_name[-1],
                                          parent_obj=project_obj)
         rt_uuid = self._vnc_lib.route_table_create(route_table)
         rt_obj = self._vnc_lib.route_table_read(id=rt_uuid,
                                                 fields=['virtual_network_back_refs'])
         self._associate_vn_rt(vn_obj, rt_obj)
     return rt_obj
    def handle_create(self):
        parent_obj = None
        if parent_obj is None and self.properties.get(self.PROJECT):
            try:
                parent_obj = self.vnc_lib().project_read(
                    id=self.properties.get(self.PROJECT))
            except vnc_api.NoIdError:
                parent_obj = self.vnc_lib().project_read(
                    fq_name_str=self.properties.get(self.PROJECT))
            except:
                parent_obj = None

        if parent_obj is None:
            tenant_id = self.stack.context.tenant_id
            parent_obj = self.vnc_lib().project_read(
                id=str(uuid.UUID(tenant_id)))

        if parent_obj is None:
            raise Exception('Error: parent is not specified in template!')

        obj_0 = vnc_api.RouteTable(name=self.properties[self.NAME],
                                   parent_obj=parent_obj)

        if self.properties.get(self.ROUTES) is not None:
            obj_1 = vnc_api.RouteTableType()
            if self.properties.get(self.ROUTES,
                                   {}).get(self.ROUTES_ROUTE) is not None:
                for index_1 in range(
                        len(
                            self.properties.get(self.ROUTES,
                                                {}).get(self.ROUTES_ROUTE))):
                    obj_2 = vnc_api.RouteType()
                    if self.properties.get(self.ROUTES, {}).get(
                            self.ROUTES_ROUTE,
                        {})[index_1].get(self.ROUTES_ROUTE_PREFIX) is not None:
                        obj_2.set_prefix(
                            self.properties.get(self.ROUTES, {}).get(
                                self.ROUTES_ROUTE,
                                {})[index_1].get(self.ROUTES_ROUTE_PREFIX))
                    if self.properties.get(self.ROUTES, {}).get(
                            self.ROUTES_ROUTE, {})[index_1].get(
                                self.ROUTES_ROUTE_NEXT_HOP) is not None:
                        obj_2.set_next_hop(
                            self.properties.get(self.ROUTES, {}).get(
                                self.ROUTES_ROUTE,
                                {})[index_1].get(self.ROUTES_ROUTE_NEXT_HOP))
                    if self.properties.get(self.ROUTES, {}).get(
                            self.ROUTES_ROUTE, {})[index_1].get(
                                self.ROUTES_ROUTE_NEXT_HOP_TYPE) is not None:
                        obj_2.set_next_hop_type(
                            self.properties.get(self.ROUTES, {}).get(
                                self.ROUTES_ROUTE, {})[index_1].get(
                                    self.ROUTES_ROUTE_NEXT_HOP_TYPE))
                    if self.properties.get(self.ROUTES, {}).get(
                            self.ROUTES_ROUTE,
                        {})[index_1].get(self.ROUTES_ROUTE_COMMUNITY_ATTRIBUTES
                                         ) is not None:
                        obj_3 = vnc_api.CommunityAttributes()
                        if self.properties.get(self.ROUTES, {}).get(
                                self.ROUTES_ROUTE, {}
                        )[index_1].get(
                                self.ROUTES_ROUTE_COMMUNITY_ATTRIBUTES, {}
                        ).get(self.
                              ROUTES_ROUTE_COMMUNITY_ATTRIBUTES_COMMUNITY_ATTRIBUTE
                              ) is not None:
                            for index_3 in range(
                                    len(
                                        self.properties.get(
                                            self.ROUTES,
                                            {}).get(self.ROUTES_ROUTE, {})
                                        [index_1].get(
                                            self.
                                            ROUTES_ROUTE_COMMUNITY_ATTRIBUTES,
                                            {}).
                                        get(self.
                                            ROUTES_ROUTE_COMMUNITY_ATTRIBUTES_COMMUNITY_ATTRIBUTE
                                            ))):
                                obj_3.add_community_attribute(
                                    self.properties.get(self.ROUTES, {}).get(
                                        self.ROUTES_ROUTE, {})
                                    [index_1].get(
                                        self.ROUTES_ROUTE_COMMUNITY_ATTRIBUTES,
                                        {}).
                                    get(self.
                                        ROUTES_ROUTE_COMMUNITY_ATTRIBUTES_COMMUNITY_ATTRIBUTE
                                        )[index_3])
                        obj_2.set_community_attributes(obj_3)
                    obj_1.add_route(obj_2)
            obj_0.set_routes(obj_1)
        if self.properties.get(self.DISPLAY_NAME) is not None:
            obj_0.set_display_name(self.properties.get(self.DISPLAY_NAME))

        try:
            obj_uuid = super(ContrailRouteTable, self).resource_create(obj_0)
        except:
            raise Exception(
                _('route-table %s could not be updated.') % self.name)

        self.resource_id_set(obj_uuid)