Пример #1
0
    def _create_vn_ri_vmi(self, obj_count=1):
        vn_objs = []
        ipam_objs = []
        ri_objs = []
        vmi_objs = []
        for i in range(obj_count):
            vn_obj = vnc_api.VirtualNetwork('%s-vn-%s' % (self.id(), i))

            ipam_obj = vnc_api.NetworkIpam('%s-ipam-%s' % (self.id(), i))
            vn_obj.add_network_ipam(ipam_obj, vnc_api.VnSubnetsType())
            self._vnc_lib.network_ipam_create(ipam_obj)
            ipam_objs.append(ipam_obj)

            self._vnc_lib.virtual_network_create(vn_obj)
            vn_objs.append(vn_obj)

            ri_obj = vnc_api.RoutingInstance('%s-ri-%s' % (self.id(), i),
                                             parent_obj=vn_obj)
            self._vnc_lib.routing_instance_create(ri_obj)
            ri_objs.append(ri_obj)

            vmi_obj = vnc_api.VirtualMachineInterface(
                '%s-vmi-%s' % (self.id(), i), parent_obj=vnc_api.Project())
            vmi_obj.add_virtual_network(vn_obj)
            self._vnc_lib.virtual_machine_interface_create(vmi_obj)
            vmi_objs.append(vmi_obj)

        return vn_objs, ipam_objs, ri_objs, vmi_objs
    def handle_create(self):
        parent_obj = None
        if parent_obj is None and self.properties.get(self.VIRTUAL_NETWORK):
            try:
                parent_obj = self.vnc_lib().virtual_network_read(
                    id=self.properties.get(self.VIRTUAL_NETWORK))
            except vnc_api.NoIdError:
                parent_obj = self.vnc_lib().virtual_network_read(
                    fq_name_str=self.properties.get(self.VIRTUAL_NETWORK))
            except:
                parent_obj = None

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

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

        if self.properties.get(self.DISPLAY_NAME) is not None:
            obj_0.set_display_name(self.properties.get(self.DISPLAY_NAME))
        if self.properties.get(self.ROUTING_INSTANCE_HAS_PNF) is not None:
            obj_0.set_routing_instance_has_pnf(
                self.properties.get(self.ROUTING_INSTANCE_HAS_PNF))
        if self.properties.get(self.ROUTING_INSTANCE_IS_DEFAULT) is not None:
            obj_0.set_routing_instance_is_default(
                self.properties.get(self.ROUTING_INSTANCE_IS_DEFAULT))
        if self.properties.get(self.SERVICE_CHAIN_INFORMATION) is not None:
            obj_1 = vnc_api.ServiceChainInfo()
            if self.properties.get(self.SERVICE_CHAIN_INFORMATION, {}).get(
                    self.SERVICE_CHAIN_INFORMATION_ROUTING_INSTANCE
            ) is not None:
                obj_1.set_routing_instance(
                    self.properties.get(
                        self.SERVICE_CHAIN_INFORMATION, {}).get(
                            self.SERVICE_CHAIN_INFORMATION_ROUTING_INSTANCE))
            if self.properties.get(self.SERVICE_CHAIN_INFORMATION, {}).get(
                    self.SERVICE_CHAIN_INFORMATION_PREFIX) is not None:
                for index_1 in range(
                        len(
                            self.properties.get(
                                self.SERVICE_CHAIN_INFORMATION, {}).get(
                                    self.SERVICE_CHAIN_INFORMATION_PREFIX))):
                    obj_1.add_prefix(
                        self.properties.get(
                            self.SERVICE_CHAIN_INFORMATION,
                            {}).get(self.SERVICE_CHAIN_INFORMATION_PREFIX)
                        [index_1])
            if self.properties.get(self.SERVICE_CHAIN_INFORMATION, {}).get(
                    self.SERVICE_CHAIN_INFORMATION_SERVICE_CHAIN_ADDRESS
            ) is not None:
                obj_1.set_service_chain_address(
                    self.properties.get(self.SERVICE_CHAIN_INFORMATION, {}).
                    get(self.SERVICE_CHAIN_INFORMATION_SERVICE_CHAIN_ADDRESS))
            if self.properties.get(self.SERVICE_CHAIN_INFORMATION, {}).get(
                    self.SERVICE_CHAIN_INFORMATION_SERVICE_INSTANCE
            ) is not None:
                obj_1.set_service_instance(
                    self.properties.get(
                        self.SERVICE_CHAIN_INFORMATION, {}).get(
                            self.SERVICE_CHAIN_INFORMATION_SERVICE_INSTANCE))
            if self.properties.get(self.SERVICE_CHAIN_INFORMATION, {}).get(
                    self.SERVICE_CHAIN_INFORMATION_SOURCE_ROUTING_INSTANCE
            ) is not None:
                obj_1.set_source_routing_instance(
                    self.properties.get(
                        self.SERVICE_CHAIN_INFORMATION, {}).get(
                            self.
                            SERVICE_CHAIN_INFORMATION_SOURCE_ROUTING_INSTANCE))
            obj_0.set_service_chain_information(obj_1)
        if self.properties.get(
                self.IPV6_SERVICE_CHAIN_INFORMATION) is not None:
            obj_1 = vnc_api.ServiceChainInfo()
            if self.properties.get(
                    self.IPV6_SERVICE_CHAIN_INFORMATION,
                {}).get(self.IPV6_SERVICE_CHAIN_INFORMATION_ROUTING_INSTANCE
                        ) is not None:
                obj_1.set_routing_instance(
                    self.properties.get(self.IPV6_SERVICE_CHAIN_INFORMATION,
                                        {}).
                    get(self.IPV6_SERVICE_CHAIN_INFORMATION_ROUTING_INSTANCE))
            if self.properties.get(
                    self.IPV6_SERVICE_CHAIN_INFORMATION,
                {}).get(
                    self.IPV6_SERVICE_CHAIN_INFORMATION_PREFIX) is not None:
                for index_1 in range(
                        len(
                            self.properties.get(
                                self.IPV6_SERVICE_CHAIN_INFORMATION, {}).
                            get(self.IPV6_SERVICE_CHAIN_INFORMATION_PREFIX))):
                    obj_1.add_prefix(
                        self.properties.get(
                            self.IPV6_SERVICE_CHAIN_INFORMATION,
                            {}).get(self.IPV6_SERVICE_CHAIN_INFORMATION_PREFIX)
                        [index_1])
            if self.properties.get(
                    self.IPV6_SERVICE_CHAIN_INFORMATION,
                {}).get(
                    self.IPV6_SERVICE_CHAIN_INFORMATION_SERVICE_CHAIN_ADDRESS
                ) is not None:
                obj_1.set_service_chain_address(
                    self.properties.get(self.IPV6_SERVICE_CHAIN_INFORMATION,
                                        {}).
                    get(self.
                        IPV6_SERVICE_CHAIN_INFORMATION_SERVICE_CHAIN_ADDRESS))
            if self.properties.get(
                    self.IPV6_SERVICE_CHAIN_INFORMATION,
                {}).get(self.IPV6_SERVICE_CHAIN_INFORMATION_SERVICE_INSTANCE
                        ) is not None:
                obj_1.set_service_instance(
                    self.properties.get(self.IPV6_SERVICE_CHAIN_INFORMATION,
                                        {}).
                    get(self.IPV6_SERVICE_CHAIN_INFORMATION_SERVICE_INSTANCE))
            if self.properties.get(
                    self.IPV6_SERVICE_CHAIN_INFORMATION,
                {}).get(
                    self.IPV6_SERVICE_CHAIN_INFORMATION_SOURCE_ROUTING_INSTANCE
                ) is not None:
                obj_1.set_source_routing_instance(
                    self.properties.get(self.IPV6_SERVICE_CHAIN_INFORMATION,
                                        {}).
                    get(self.
                        IPV6_SERVICE_CHAIN_INFORMATION_SOURCE_ROUTING_INSTANCE)
                )
            obj_0.set_ipv6_service_chain_information(obj_1)
        if self.properties.get(self.STATIC_ROUTE_ENTRIES) is not None:
            obj_1 = vnc_api.StaticRouteEntriesType()
            if self.properties.get(self.STATIC_ROUTE_ENTRIES, {}).get(
                    self.STATIC_ROUTE_ENTRIES_ROUTE) is not None:
                for index_1 in range(
                        len(
                            self.properties.get(
                                self.STATIC_ROUTE_ENTRIES,
                                {}).get(self.STATIC_ROUTE_ENTRIES_ROUTE))):
                    obj_2 = vnc_api.StaticRouteType()
                    if self.properties.get(self.STATIC_ROUTE_ENTRIES, {}).get(
                            self.STATIC_ROUTE_ENTRIES_ROUTE,
                        {})[index_1].get(self.STATIC_ROUTE_ENTRIES_ROUTE_PREFIX
                                         ) is not None:
                        obj_2.set_prefix(
                            self.properties.get(
                                self.STATIC_ROUTE_ENTRIES,
                                {}).get(self.STATIC_ROUTE_ENTRIES_ROUTE, {})
                            [index_1].get(
                                self.STATIC_ROUTE_ENTRIES_ROUTE_PREFIX))
                    if self.properties.get(self.STATIC_ROUTE_ENTRIES, {}).get(
                            self.STATIC_ROUTE_ENTRIES_ROUTE, {})[index_1].get(
                                self.STATIC_ROUTE_ENTRIES_ROUTE_NEXT_HOP
                            ) is not None:
                        obj_2.set_next_hop(
                            self.properties.get(
                                self.STATIC_ROUTE_ENTRIES,
                                {}).get(self.STATIC_ROUTE_ENTRIES_ROUTE, {})
                            [index_1].get(
                                self.STATIC_ROUTE_ENTRIES_ROUTE_NEXT_HOP))
                    if self.properties.get(self.STATIC_ROUTE_ENTRIES, {}).get(
                            self.STATIC_ROUTE_ENTRIES_ROUTE, {})[index_1].get(
                                self.STATIC_ROUTE_ENTRIES_ROUTE_ROUTE_TARGET
                            ) is not None:
                        for index_2 in range(
                                len(
                                    self.properties.get(
                                        self.STATIC_ROUTE_ENTRIES, {}).get(
                                            self.STATIC_ROUTE_ENTRIES_ROUTE,
                                            {})
                                    [index_1].get(
                                        self.
                                        STATIC_ROUTE_ENTRIES_ROUTE_ROUTE_TARGET
                                    ))):
                            obj_2.add_route_target(
                                self.properties.get(
                                    self.STATIC_ROUTE_ENTRIES,
                                    {}).get(self.STATIC_ROUTE_ENTRIES_ROUTE,
                                            {})
                                [index_1].get(
                                    self.
                                    STATIC_ROUTE_ENTRIES_ROUTE_ROUTE_TARGET)
                                [index_2])
                    if self.properties.get(self.STATIC_ROUTE_ENTRIES, {}).get(
                            self.STATIC_ROUTE_ENTRIES_ROUTE, {})[index_1].get(
                                self.STATIC_ROUTE_ENTRIES_ROUTE_COMMUNITY
                            ) is not None:
                        for index_2 in range(
                                len(
                                    self.properties.get(
                                        self.STATIC_ROUTE_ENTRIES, {}).get(
                                            self.STATIC_ROUTE_ENTRIES_ROUTE,
                                            {})
                                    [index_1].get(
                                        self.
                                        STATIC_ROUTE_ENTRIES_ROUTE_COMMUNITY))
                        ):
                            obj_2.add_community(
                                self.properties.get(
                                    self.STATIC_ROUTE_ENTRIES,
                                    {}).get(self.STATIC_ROUTE_ENTRIES_ROUTE,
                                            {})
                                [index_1].get(
                                    self.STATIC_ROUTE_ENTRIES_ROUTE_COMMUNITY)
                                [index_2])
                    obj_1.add_route(obj_2)
            obj_0.set_static_route_entries(obj_1)
        if self.properties.get(self.DEFAULT_CE_PROTOCOL) is not None:
            obj_1 = vnc_api.DefaultProtocolType()
            if self.properties.get(self.DEFAULT_CE_PROTOCOL, {}).get(
                    self.DEFAULT_CE_PROTOCOL_BGP) is not None:
                obj_1.set_bgp(
                    self.properties.get(self.DEFAULT_CE_PROTOCOL,
                                        {}).get(self.DEFAULT_CE_PROTOCOL_BGP))
            if self.properties.get(self.DEFAULT_CE_PROTOCOL, {}).get(
                    self.DEFAULT_CE_PROTOCOL_OSPF) is not None:
                obj_2 = vnc_api.ProtocolOspfType()
                if self.properties.get(self.DEFAULT_CE_PROTOCOL, {}).get(
                        self.DEFAULT_CE_PROTOCOL_OSPF,
                    {}).get(self.DEFAULT_CE_PROTOCOL_OSPF_AREA) is not None:
                    obj_2.set_area(
                        self.properties.get(self.DEFAULT_CE_PROTOCOL, {}).get(
                            self.DEFAULT_CE_PROTOCOL_OSPF,
                            {}).get(self.DEFAULT_CE_PROTOCOL_OSPF_AREA))
                obj_1.set_ospf(obj_2)
            obj_0.set_default_ce_protocol(obj_1)

        # reference to routing_instance_refs
        obj_1 = None
        if self.properties.get(self.ROUTING_INSTANCE_REFS_DATA) is not None:
            for index_0 in range(
                    len(self.properties.get(self.ROUTING_INSTANCE_REFS_DATA))):
                obj_1 = vnc_api.ConnectionType()
                if self.properties.get(
                        self.ROUTING_INSTANCE_REFS_DATA,
                    {})[index_0].get(
                        self.ROUTING_INSTANCE_REFS_DATA_DESTINATION_INSTANCE
                    ) is not None:
                    obj_1.set_destination_instance(
                        self.properties.get(self.ROUTING_INSTANCE_REFS_DATA,
                                            {})
                        [index_0].get(
                            self.
                            ROUTING_INSTANCE_REFS_DATA_DESTINATION_INSTANCE))

                if self.properties.get(self.ROUTING_INSTANCE_REFS):
                    try:
                        ref_obj = self.vnc_lib().routing_instance_read(
                            id=self.properties.get(
                                self.ROUTING_INSTANCE_REFS)[index_0])
                    except vnc_api.NoIdError:
                        ref_obj = self.vnc_lib().routing_instance_read(
                            fq_name_str=self.properties.get(
                                self.ROUTING_INSTANCE_REFS)[index_0])
                    obj_0.add_routing_instance(ref_obj, obj_1)

        # reference to route_target_refs
        obj_1 = None
        if self.properties.get(self.ROUTE_TARGET_REFS_DATA) is not None:
            for index_0 in range(
                    len(self.properties.get(self.ROUTE_TARGET_REFS_DATA))):
                obj_1 = vnc_api.InstanceTargetType()
                if self.properties.get(
                        self.ROUTE_TARGET_REFS_DATA,
                    {})[index_0].get(
                        self.ROUTE_TARGET_REFS_DATA_IMPORT_EXPORT) is not None:
                    obj_1.set_import_export(
                        self.properties.get(
                            self.ROUTE_TARGET_REFS_DATA, {})[index_0].get(
                                self.ROUTE_TARGET_REFS_DATA_IMPORT_EXPORT))

                if self.properties.get(self.ROUTE_TARGET_REFS):
                    try:
                        ref_obj = self.vnc_lib().route_target_read(
                            id=self.properties.get(
                                self.ROUTE_TARGET_REFS)[index_0])
                    except vnc_api.NoIdError:
                        ref_obj = self.vnc_lib().route_target_read(
                            fq_name_str=self.properties.get(
                                self.ROUTE_TARGET_REFS)[index_0])
                    obj_0.add_route_target(ref_obj, obj_1)

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

        self.resource_id_set(obj_uuid)