diff --git a/applications/inventory-manager-netbox/Main.go b/applications/inventory-manager-netbox/Main.go index 2e117967895092184f98af7ac021076ccf60b7b3..f4a46c57148f6061f670b4ee65e36985bb1a3440 100644 --- a/applications/inventory-manager-netbox/Main.go +++ b/applications/inventory-manager-netbox/Main.go @@ -47,10 +47,8 @@ func main() { if len(elements.Mne) != elementeFigure { //log.Printf("Es wurden %d Elemente gefunden", len(elementss.Mne)) //take the date into Netbox - - + err = netboxManager.FeedNetbox(elements, configPathNetbox) - if err != nil { log.Fatalf("Error creating device: %v", err) @@ -70,10 +68,8 @@ func main() { } - } - /* func main2() { var configPathGosdn = flag.String("configGosdn", "config/config.yaml", "Path to the Gosdn configuration file") @@ -102,10 +98,10 @@ func main2() { if len(elements.Mne) != elementeFigure { //log.Printf("Es wurden %d Elemente gefunden", len(elementss.Mne)) //take the date into Netbox - - + + err = netboxManager.FeedNetbox(elements, configPathNetbox) - + if err != nil { log.Fatalf("Error creating device: %v", err) @@ -146,6 +142,6 @@ func main2() { } else { log.Println("Es ist alles cool!") } - + } -*/ \ No newline at end of file +*/ diff --git a/applications/inventory-manager-netbox/inventoryManager/inventoryManager.go b/applications/inventory-manager-netbox/inventoryManager/inventoryManager.go index 82ca8a3b8a0c47b3b4360ea09cdae44ab29b57ae..1af91d32eb4bc8f4daccff2c5cf8ad53ce499668 100644 --- a/applications/inventory-manager-netbox/inventoryManager/inventoryManager.go +++ b/applications/inventory-manager-netbox/inventoryManager/inventoryManager.go @@ -2,13 +2,16 @@ package inventoryManager import ( "context" + //"errors" "log" "os" "time" + //"code.fbi.h-da.de/danet/gosdn/api/go/github.com/openconfig/gnmi/proto/gnmi" "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/networkelement" "code.fbi.h-da.de/danet/gosdn/applications/inventory-manager-netbox/config" "code.fbi.h-da.de/danet/gosdn/controller/api" + //"github.com/openconfig/ygot/ytypes" "github.com/sirupsen/logrus" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" @@ -23,13 +26,30 @@ type InventoryManager struct { sessionToken string } + + +/*func GetModelAsStruct(gnmiNotifications []*gnmi.Notification) (*temp.Device, error) { + modelAsSchema := ytypes.Schema{ + Root: &temp.Device{}, + SchemaTree: temp.SchemaTree, + } + + err := ytypes.UnmarshalNotifications(&modelAsSchema, gnmiNotifications, &ytypes.IgnoreExtraFields{}) + if err != nil { + return nil, err + } + + modelAsStruct, ok := modelAsSchema.Root.(*temp.Device) + if !ok { + return nil, errors.New("dynamic cast fail") + } + + return modelAsStruct, nil +}*/ + // createGosdnConnectionwitNetzworkElementslist creates a context with the given authorization token. And gives the list of network elements bag. func CreateGosdnConnectionwitNetzworkElementslist(configFile *string) (*networkelement.GetAllResponse, error) { - // Configpfad aus CLI-Flag holen - //configPathNetbox = flag.String("configNetbox", "config/configNetbox.yaml", "Path to the NetBox configuration file") - //flag.Parse() - //log.Printf("Config path: %s", *configFile) - + //Reading the config file data, err := os.ReadFile(*configFile) if err != nil { @@ -54,8 +74,10 @@ func CreateGosdnConnectionwitNetzworkElementslist(configFile *string) (*networke if err != nil { log.Fatal(err) } - //log.Print(invManager) + log.Print(invManager) + + //Returning the list of devices from the controller. elements := listRunningNetworkElements(invManager) diff --git a/applications/inventory-manager-netbox/netboxManager/netboxManager.go b/applications/inventory-manager-netbox/netboxManager/netboxManager.go index a41ccebca6b8b62ff4533fdd5fa092c2f7fa2513..2943fb3978f608e93f527dd3019c4f99b5371851 100644 --- a/applications/inventory-manager-netbox/netboxManager/netboxManager.go +++ b/applications/inventory-manager-netbox/netboxManager/netboxManager.go @@ -2,12 +2,16 @@ package netboxManager import ( "context" + "errors" "log" "os" "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/networkelement" "code.fbi.h-da.de/danet/gosdn/applications/inventory-manager-netbox/config" netboxapi "code.fbi.h-da.de/danet/gosdn/internal/netboxapi" + temp "code.fbi.h-da.de/danet/gosdn/models/generated/openconfig" + "github.com/openconfig/gnmi/proto/gnmi" + "github.com/openconfig/ygot/ytypes" "gopkg.in/yaml.v3" ) @@ -46,82 +50,57 @@ func FeedNetbox(elements *networkelement.GetAllResponse, configPathNetbox *strin siteID = makeSite(client) + log.Printf(" ") + log.Printf(" ") + if (manufacturerID == 0) || (deviceTypeID == 0) || (deviceRoleID == 0) || (siteID == 0) { log.Printf("ich kann kein Gerät hinzufügen, weil ich nicht alle ID habe") return err } else { if elements != nil { for _, element := range elements.Mne { - deviceID = makeDevice(client, element.Name, deviceTypeID, deviceRoleID, siteID, element.Id, element.Plugin.String(), "active") - - interfaceType := netboxapi.InterfaceTypeValue("1000base-t") - ifaceID = makeInterface(client, "eth0", deviceID, interfaceType, "Uplink-Port", true) - ipID = int32(makeIPAddress(client, "192.168.1.10/24", int64(ifaceID))) - - - log.Printf("Gerät erstellt createdDevice: %v ", deviceID) - log.Printf("Interface erstellt mit der ID: %v ", ifaceID) - log.Printf("IP Adresse erstellt mit der ID: %v ", ipID) - - } - - } - } - log.Printf("Gerät erstellt createdDevice: %v ", deviceID) - return nil -} - -/* if elements != nil { - for _, element := range elements.Mne { - - //log.Printf("Test Zeugs, ich will die IP: %v", element.GetMneNotification()) - - log.Printf("IP Adresse, check: %v", element.TransportAddress) - - //log.Printf("Testen String bekommen: %v", element.String()) - - device = createDeviceWithParameter(element.Name, strconv.FormatInt(deviceTypeID, 10), strconv.FormatInt(deviceRoleID, 10), strconv.FormatInt(siteID, 10), element.Id, element.Plugin.String(), "active") - id, err = Netzworhandling(netboxconfig.NetboxAddress, netboxconfig.ApiToken, "/dcim/devices/", device) - deviceID := id - if err != nil { - log.Printf("Error creating a Device: %v", err) - return err - } else { - log.Printf("Device successfully created or found with the deviceID: %d\n", deviceID) - } + modelFormGNMI, err := GetModelAsStruct(element.GetMneNotification()) + if err != nil { + log.Printf("Fehler beim Umwandeln des Element: %v", err) + log.Printf("ModelAsStruct modelFormGNMI: %v", modelFormGNMI) + } - Newinterface := createInterfaceWithParameter("Name-eth0", strconv.FormatInt(deviceID, 10), "1000base-t", "Main uplink", true) - id, err = Netzworhandling(netboxconfig.NetboxAddress, netboxconfig.ApiToken, "/ipam/ip-addresses/", Newinterface) - NewinterfaceID := id - if err != nil { - log.Printf("Error creating a Interface: %v", err) - return err - } else { - log.Printf("Interface successfully created or found with the interfaceID: %d\n", NewinterfaceID) - //IP hinzufügen - //NewIpAddress := createIPAddressWithParameter("192.168.1.10/24", strconv.FormatInt(NewinterfaceID, 10)) - NewIpAddress := createIPAddressWithParameter(element.TransportAddress+"/32", strconv.FormatInt(NewinterfaceID, 10)) - - id, err = Netzworhandling(netboxconfig.NetboxAddress, netboxconfig.ApiToken, "/dcim/devices/", NewIpAddress) - ipAddressID := id - if err != nil { - log.Printf("Error creating a IP Address: %v", err) - return err - } else { - if ipAddressID == 0 { - log.Printf("No IP Address created in Netbox. IP adress: %s\n", element.TransportAddress) - } else { - log.Printf("IP Address successfully created or found with the ipAddressID: %d\n", ipAddressID) + deviceID = makeDevice(client, element.Name, deviceTypeID, deviceRoleID, siteID, element.Id, element.Plugin.String(), "active") + //log.Printf("Gerät erstellt createdDevice: %v ", deviceID) + + for name, iface := range modelFormGNMI.Interfaces.Interface { + if iface == nil { + continue + } + + log.Printf("") + log.Printf("") + //log.Printf("Interface Name (Key): %s", name) + interfaceType := netboxapi.InterfaceTypeValue("1000base-t") + ifaceID = makeInterface(client, name, deviceID, interfaceType, "Beschreibungstext, hier kann was sinvolle rein", true) + log.Printf("Interface erstellt mit der ID: %v ", ifaceID) + + for idx, subif := range iface.Subinterfaces.Subinterface { + log.Printf("Subinterface [%d] index: %v", idx, *subif.Index) + if subif.Ipv4 != nil && subif.Ipv4.Addresses != nil { + for k := range subif.Ipv4.Addresses.Address { + ipID = int32(makeIPAddress(client, k+"/24", element.GetName()+"das ist der Beschreibungtext", "active", "dcim.interface", int64(ifaceID))) + log.Printf("IP Adresse erstellt mit der ID: %v ", ipID) + } + } + if subif.Ipv6 != nil && subif.Ipv6.Addresses != nil { + for k := range subif.Ipv6.Addresses.Address { + ipID = int32(makeIPAddress(client, k+"/64", element.GetName()+"das ist der beschreibungtext", "active", "dcim.interface", int64(ifaceID))) + log.Printf("IP Adresse erstellt mit der ID: %v ", ipID) + } + } + } } } } + return nil } -} else { - //log.Fatalf("The controller has no data for me. Continuing is pointless. ^^ elementss is empty.") } -return nil - -*/ func makeManufacturer(client *netboxapi.APIClient) int32 { var ManufacturerID int32 @@ -287,12 +266,12 @@ func makeDevice(client *netboxapi.APIClient, name string, deviceTypeID, roleID, Execute() if err != nil { - log.Printf("Fehler beim Erstellen des Geräts: %v", err) + //log.Printf("Fehler beim Erstellen des Geräts: %v", err) if resp != nil { log.Printf("HTTP-Status: %s", resp.Status) } - // Fallback: Suche nach vorhandenem Gerät anhand Name + // Fallback: Searching for the Device with the name existing, resp, err := client.DcimAPI. DcimDevicesList(context.Background()). Name([]string{name}). @@ -357,10 +336,13 @@ func makeInterface(client *netboxapi.APIClient, name string, deviceID int32, int return interfaceID } -func makeIPAddress(client *netboxapi.APIClient, address string, interfaceID int64) int64 { +func makeIPAddress(client *netboxapi.APIClient, address, description, status, assignedType string, interfaceID int64) int64 { var ipID int64 = 0 - ip := CreateIPAddress(address, interfaceID) + //addressWithoutPort := extractIP(address) + //addressWithoutPort = addressWithoutPort + "/32" + //ip := CreateIPAddress(address, description, "active", "dcim.interface", interfaceID) + ip := CreateIPAddress(address, description, status, assignedType, interfaceID) createdIP, resp, err := client.IpamAPI. IpamIpAddressesCreate(context.Background()). @@ -370,29 +352,66 @@ func makeIPAddress(client *netboxapi.APIClient, address string, interfaceID int6 if err != nil { log.Printf("Fehler beim Erstellen der IP-Adresse: %v", err) if resp != nil { + log.Printf("Fehlerhafte Antwort: %v", address) log.Printf("HTTP Status: %s", resp.Status) + log.Printf("HTTP Request: %v", resp.Request) + log.Printf("HTTP Body: %s", resp.Body) + log.Printf("HTTP StatusCode: %d", resp.StatusCode) } // Suche nach bereits existierender IP-Adresse - existing, resp, err := client.IpamAPI. - IpamIpAddressesList(context.Background()). - Address([]string{address}). - Execute() + existing, resp, err := client.IpamAPI.IpamIpAddressesList(context.Background()).Address([]string{address}).Execute() if err != nil { log.Printf("Fehler beim Suchen nach IP-Adresse '%s': %v", address, err) } else if len(existing.Results) > 0 { - ipID = int64(existing.Results[0].Id) // für Fehler 8 + ipID = int64(existing.Results[0].Id) // für Fehler 8 log.Printf("IP-Adresse existiert bereits: ID %d, URL: %s", ipID, existing.Results[0].Url) } else { log.Printf("Keine bestehende IP-Adresse gefunden: %s", address) log.Printf("Status: %d", resp.StatusCode) } } else { - ipID := int64(createdIP.Id) + ipID := int64(createdIP.Id) log.Printf("IP-Adresse erfolgreich erstellt: ID %d, URL: %s", ipID, createdIP.Url) } return ipID } +func GetModelAsStruct(gnmiNotifications []*gnmi.Notification) (*temp.Device, error) { + modelAsSchema := ytypes.Schema{ + Root: &temp.Device{}, + SchemaTree: temp.SchemaTree, + } + + err := ytypes.UnmarshalNotifications(&modelAsSchema, gnmiNotifications, &ytypes.IgnoreExtraFields{}) + if err != nil { + return nil, err + } + + modelAsStruct, ok := modelAsSchema.Root.(*temp.Device) + if !ok { + return nil, errors.New("dynamic cast fail") + } + + return modelAsStruct, nil +} + +/* + func extractPort(address string) string { + parts := strings.Split(address, ":") + if len(parts) == 2 { + return parts[1] + } + return address // falls kein Doppelpunkt vorhanden ist + } + +func extractIP(address string) string { + parts := strings.Split(address, ":") + if len(parts) == 2 { + return parts[0] + } + return address +} +*/ diff --git a/applications/inventory-manager-netbox/netboxManager/netboxUtility.go b/applications/inventory-manager-netbox/netboxManager/netboxUtility.go index 3c4ed84283f16f458599b0e196d9e178f6ad707f..cd4d1077ca11f2717954c100436b3a4395a7c5a2 100644 --- a/applications/inventory-manager-netbox/netboxManager/netboxUtility.go +++ b/applications/inventory-manager-netbox/netboxManager/netboxUtility.go @@ -75,8 +75,6 @@ func CreateSite(name, slug, description, physicalAddress, shippingAddress, comme PhysicalAddress: StringPtr(physicalAddress), ShippingAddress: StringPtr(shippingAddress), Comments: StringPtr(comments), - //Status: &status, vielleicht brauche ich es ja doch nicht?!?! - //, status netbox.LocationStatus } return site } @@ -95,235 +93,15 @@ func CreateInterface(name string, deviceID netbox.BriefInterfaceRequestDevice, i } // CreateIPAddress creates an IP address in NetBox -func CreateIPAddress(address string, interfaceID int64) *netbox.WritableIPAddressRequest { - assignedType := "ipam.interface" +func CreateIPAddress(address, description, status, assignedType string, interfaceID int64) *netbox.WritableIPAddressRequest { // /api/ipam/ip-addresses/ ip := &netbox.WritableIPAddressRequest{ Address: address, + Status: (*netbox.PatchedWritableIPAddressRequestStatus)(&status), AssignedObjectType: *netbox.NewNullableString(&assignedType), AssignedObjectId: *netbox.NewNullableInt64(&interfaceID), + Description: StringPtr(description), } return ip } - -/* -// CreateTag creates a tag in NetBox -func CreateTag(client *netbox.Client, name, slug, description string) (*models.Tag, error) { - tag := &models.Tag{ - Name: name, - Slug: slug, - Description: description, - } - - createdTag, err := client.Extras.ExtrasTagsCreate( - extras.NewExtrasTagsCreateParams().WithData(tag).WithContext(context.Background()), - nil, - ) - if err != nil { - return nil, fmt.Errorf("Fehler beim Erstellen des Tags: %w", err) - } - - return createdTag.Payload, nil -} - -//createmodultyp creates a modultyp in NetBox -func CreateModuleType(client *netbox.Client, model, partNumber, description, comments, manufacturerID string) (*models.ModuleType, error) { - ///api/dcim/module-types/ Modultypen - moduleType := &models.ModuleType{ - Model: model, - PartNumber: partNumber, - Description: description, - Comments: comments, - Manufacturer: &models.NestedManufacturer{ID: manufacturerID}, - } - - createdModuleType, err := client.Dcim.DcimModuleTypesCreate( - dcim.NewDcimModuleTypesCreateParams().WithData(moduleType).WithContext(context.Background()), - nil, - ) - if err != nil { - return nil, fmt.Errorf("Fehler beim Erstellen des Modultyps: %w", err) - } - - return createdModuleType.Payload, nil -} -/* - - -// createDevice creates a deicetype in NetBox -func CreateDeviceType(client *netbox.NetBoxAPI, manufacturerID int64, model, slug string, uHeight float64) (*models.DeviceType, error) { - // /dcim/device-types/ Gerätetyp - deviceType := &models.DeviceType{ - Manufacturer: manufacturerID, - Model: &model, - Slug: &slug, - UHeight: &uHeight, - } - params := &dcim.DcimDeviceTypesCreateParams{ - Data: deviceType, - Context: context.Background(), - } - - created, err := client.Dcim.DcimDeviceTypesCreate(params, nil) - if err != nil { - return nil, err - } - return created.Payload, nil -} - -// createDevice creates a manufacturer in NetBox -func CreateManufacturer(client *netbox.NetBoxAPI, name, slug, description string) (*models.Manufacturer, error) { - // /dcim/manufacturers/ Hersteller - manufacturer := &models.Manufacturer{ - Name: &name, - Slug: &slug, - Description: description, - } - params := &dcim.DcimManufacturersCreateParams{ - Data: manufacturer, - Context: context.Background(), - } - - created, err := client.Dcim.DcimManufacturersCreate(params, nil) - if err != nil { - return nil, err - } - return created.Payload, nil -} - -// createDevice creates a device role in NetBox -func CreateDeviceRole(client *netbox.NetBoxAPI, name, slug, description, color, status string) (*models.DeviceRole, error) { - // /dcim/device-roles/ Geräterolle - deviceRole := &models.DeviceRole{ - Name: &name, - Slug: &slug, - Description: description, - Color: &color, - Status: &status, - } - - params := &dcim.DcimDeviceRolesCreateParams{ - Data: deviceRole, - Context: context.Background(), - } - - created, err := client.Dcim.DcimDeviceRolesCreate(params, nil) - if err != nil { - return nil, err - } - return created.Payload, nil -} - -// createDevice creates a site in NetBox -func CreateSite(client *netbox.NetBoxAPI, name, slug, description, physicalAddress, shippingAddress, comments, status string) (*models.Site, error) { - // /dcim/sites/ Standorts - site := &models.Site{ - Name: &name, - Slug: &slug, - Description: description, - PhysicalAddress: physicalAddress, - ShippingAddress: shippingAddress, - Comments: comments, - Status: &status, - } - - params := &dcim.DcimSitesCreateParams{ - Data: site, - Context: context.Background(), - } - - created, err := client.Dcim.DcimSitesCreate(params, nil) - if err != nil { - return nil, err - } - return created.Payload, nil -} - -// CreateInterface creates an interface in NetBox -func CreateInterface(client *netbox.NetBoxAPI, name string, deviceID int64, interfaceType, description string, enabled bool) (*models.Interface, error) { - // /api/dcim/interfaces/ Schnittstelle Liste - - interrface := &models.Interface{ - Name: &name, - Device: &deviceID, - Type: &interfaceType, // z. B. "1000base-t" - Description: description, - Enabled: &enabled, - } - params := &dcim.DcimInterfacesCreateParams{ - Data: interrface, - Context: context.Background(), - } - - created, err := client.Dcim.DcimInterfacesCreate(params, nil) - if err != nil { - return nil, err - } - return created.Payload, nil -} - -// CreateIPAddress creates an IP address in NetBox -func CreateIPAddress(client *netbox.NetBoxAPI, address string, interfaceID int64) (*models.IPAddress, error) { - assignedType := "dcim.interface" - // /api/ipam/ip-addresses/ - - ip := &models.IPAddress{ - Address: &address, - AssignedObjectType: &assignedType, - AssignedObjectID: &interfaceID, - } - params := &ipam.IpamIPAddressesCreateParams{ - Data: ip, - Context: context.Background(), - } - - created, err := client.Ipam.IpamIPAddressesCreate(params, nil) - if err != nil { - return nil, err - } - return created.Payload, nil -} - -/* -// CreateTag creates a tag in NetBox -func CreateTag(client *netbox.Client, name, slug, description string) (*models.Tag, error) { - tag := &models.Tag{ - Name: name, - Slug: slug, - Description: description, - } - - createdTag, err := client.Extras.ExtrasTagsCreate( - extras.NewExtrasTagsCreateParams().WithData(tag).WithContext(context.Background()), - nil, - ) - if err != nil { - return nil, fmt.Errorf("Fehler beim Erstellen des Tags: %w", err) - } - - return createdTag.Payload, nil -} - -//createmodultyp creates a modultyp in NetBox -func CreateModuleType(client *netbox.Client, model, partNumber, description, comments, manufacturerID string) (*models.ModuleType, error) { - ///api/dcim/module-types/ Modultypen - moduleType := &models.ModuleType{ - Model: model, - PartNumber: partNumber, - Description: description, - Comments: comments, - Manufacturer: &models.NestedManufacturer{ID: manufacturerID}, - } - - createdModuleType, err := client.Dcim.DcimModuleTypesCreate( - dcim.NewDcimModuleTypesCreateParams().WithData(moduleType).WithContext(context.Background()), - nil, - ) - if err != nil { - return nil, fmt.Errorf("Fehler beim Erstellen des Modultyps: %w", err) - } - - return createdModuleType.Payload, nil -} -*/ diff --git a/netbox-openapi.yaml b/netbox-openapi.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1becba2bb0a80e7b9b0a7715f15e56512a7e5f7e --- /dev/null +++ b/netbox-openapi.yaml @@ -0,0 +1 @@ +404: Not Found \ No newline at end of file