// 2020 Rackspace, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"): you may // not use this file except in compliance with the License. You may obtain // a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the // License for the specific language governing permissions and limitations // under the License. // Package discover contains the business logic to pull switchport data for a device and // interface with Rackspace systems to add to an environment. package cmdb import ( "fmt" "sort" "strconv" "strings" "github.rackspace.com/DCM/discover/common" "github.rackspace.com/DCM/tools" "github.rackspace.com/gorax/clients/dcxAutohost" ) // Map method takes a cmdb. pointer receiver and serves as the method to // generate a port mapping of the source and migration ports for a Model. func (c *DP) Map(r *Model) error { // Sort the ports in a consistent manner. sort.Sort(r.Environment.Migration.PortsParsed) sort.Sort(r.Environment.Current.PortsParsed) // We need to allocate memory for these slices so that the append function // later does not attempt to append to a nil pointer to these variables and // result in a nil pointer error. unmappedSource := make([]string, 0) unmappedMigr := make([]string, 0) r.PortMap.Device = strconv.Itoa(r.ID) r.PortMap.PortMaps = make([]*dcxAutohost.PortMapPair, 0) r.PortMap.DirectMaps = make([]dcxAutohost.DirectMapPair, 0) realPorts := make([]tools.PortParsed, 0) // For each current port assignment for i := range r.Environment.Current.PortsParsed { // If the interface added in discovery was a mock port, then do not // use it to create a port-map. if r.Environment.Current.PortsParsed[i].MockPort { continue } realPorts = append(realPorts, r.Environment.Current.PortsParsed[i]) } // For each current real port assignment for i, curr := range realPorts { // If the interface added in discovery was a mock port, then do not // use it to create a port-map. if curr.MockPort { continue } r.PortMap.SourceDC = func() string { if common.IsStrNil(curr.DataCenter) { return getDCSymbol(r.Environment.Current) } return curr.DataCenter }() // If the length of the migration ports is less than the index of the current port assignment, then // we know that there are more current ports than there are migration ports, and we need to flag this // as a unmapped current port assignment if len(r.Environment.Migration.PortsParsed) < i+1 { unmappedSource = append(unmappedSource, fmt.Sprintf("%s.%s[%s]", curr.SwitchName, curr.DataCenter, curr.PortName)) continue } mig := r.Environment.Migration.PortsParsed[i] r.PortMap.MigrationDC = func() string { if common.IsStrNil(mig.DataCenter) { return getDCSymbol(r.Environment.Migration) } return mig.DataCenter }() r.PortMap.PortMaps = append(r.PortMap.PortMaps, &dcxAutohost.PortMapPair{ Source: dcxAutohost.PortMapDetail{ DeviceID: r.Linked.ID, Name: curr.PortName, DevicePort: curr.DevicePort, PortNumber: curr.PortNumber, Switch: struct { ConfigurationNumber string `json:"configuration_number" bson:"configuration_number"` ID int `json:"id" bson:"id"` Number string `json:"number" bson:"number"` }{ ID: curr.SwitchNum, Number: curr.SwitchName, }, SwitchID: curr.SwitchNum, SwitchPortType: curr.PortType, }, Migration: dcxAutohost.PortMapDetail{ DeviceID: r.ID, Name: mig.PortName, DevicePort: curr.DevicePort, PortNumber: mig.PortNumber, Switch: struct { ConfigurationNumber string `json:"configuration_number" bson:"configuration_number"` ID int `json:"id" bson:"id"` Number string `json:"number" bson:"number"` }{ ID: mig.SwitchNum, Number: mig.SwitchName, }, SwitchID: mig.SwitchNum, SwitchPortType: mig.PortType, }, }) } // Now go back through the migration ports, and try to determine if we reach a point // where there isn't a Current port index for the migration port. If we get there, // add it as a unmapped Migration port. for i, mig := range r.Environment.Migration.PortsParsed { if len(r.Environment.Current.PortsParsed) < i+1 { unmappedMigr = append(unmappedMigr, fmt.Sprintf("%s.%s[%d]", mig.SwitchName, mig.DataCenter, mig.PortNumber)) } } // Now lets add the unmapped ports to the parent struct r.PortMap.UnmappedSourcePorts = strings.Join(unmappedSource, ",") r.PortMap.UnmappedMigrationPorts = strings.Join(unmappedMigr, ",") // Finally, we need to map in the direct server-to-server mappings. Since we will always // assume that there will not be a mapping change for these, we will map the current configuration // as the source and destination. for _, direct := range r.Environment.Current.DirectPorts { r.PortMap.DirectMaps = append(r.PortMap.DirectMaps, dcxAutohost.DirectMapPair{ Source: dcxAutohost.DirectMapDetail{ DevicePort: direct.DevicePort, OtherDevice: dcxAutohost.OtherDevice{ DevicePort: direct.OtherDevice.DevicePort, ID: direct.OtherDevice.ID, Type: direct.OtherDevice.Type, LinkedID: direct.OtherDevice.LinkedID, }, }, Migration: dcxAutohost.DirectMapDetail{ DevicePort: direct.DevicePort, OtherDevice: dcxAutohost.OtherDevice{ DevicePort: direct.OtherDevice.DevicePort, ID: direct.OtherDevice.ID, Type: direct.OtherDevice.Type, LinkedID: direct.OtherDevice.LinkedID, }, }, }) } return nil } func getDCSymbol(l tools.Location) string { for _, port := range l.PortsParsed { if !common.IsStrNil(port.DataCenter) { return port.DataCenter } } return "" }