/// <summary>
 /// Create a new SwitchPort object.
 /// </summary>
 /// <param name="id">Initial value of the Id property.</param>
 /// <param name="switchModuleId">Initial value of the SwitchModuleId property.</param>
 /// <param name="port">Initial value of the Port property.</param>
 public static SwitchPort CreateSwitchPort(global::System.Int32 id, global::System.Int32 switchModuleId, global::System.Int32 port)
 {
     SwitchPort switchPort = new SwitchPort();
     switchPort.Id = id;
     switchPort.SwitchModuleId = switchModuleId;
     switchPort.Port = port;
     return switchPort;
 }
 /// <summary>
 /// Deprecated Method for adding a new object to the SwitchPorts EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToSwitchPorts(SwitchPort switchPort)
 {
     base.AddObject("SwitchPorts", switchPort);
 }
        public CommunicationAction ResolveRequest(int requestId, string switchIPAddress, string switchName, int? switchNumber,
            int? switchModuleNumber, int? switchPortNumber, string serverIPAddress, string notes)
        {
            var action = AssignCommunicationEngineer(requestId, notes);

            var securityAction = GetLastSecurityAction(action.Request);

            if(securityAction == null || !securityAction.Assigned || securityAction.Approved != true || securityAction.Vlan == null)
            {
                throw new VlanException("The request is not approved by the security team.");
            }

            var serverIP = action.ServerIP = new ServerIP();
            serverIP.Virtualization = action.Request.Virtualization;
            serverIP.IP = CreateIP(IPAddress.Parse(serverIPAddress));

            if (serverIP.IP.Segment == null || serverIP.IP.Segment.Vlan == null || serverIP.IP.Segment.Vlan.Id != securityAction.Vlan.Id)
            {
                throw new VlanException("The assigned IP address is not within the Vlan approved by the security team.");
            }

            if (action.Request.Virtualization.Server.ServerType.Id == (int)ServerTypes.Standalone)
            {
                if ((switchName != null && !string.IsNullOrEmpty(switchName.Trim()) && switchIPAddress != null &&
                    !string.IsNullOrEmpty(switchIPAddress.Trim()))
                    && switchModuleNumber.HasValue && switchPortNumber.HasValue)
                {
                    var switchIP = IPAddress.Parse(switchIPAddress);
                    var switchIPAddressArr = switchIP.GetAddressBytes();

                    var newSwitch = db.Switches.SingleOrDefault(i => i.IP.Address == switchIPAddressArr && i.StackableNumber.Equals(switchNumber));

                    if (newSwitch == null)
                    {
                        newSwitch = new Switch();
                        newSwitch.IP = CreateIP(switchIP);
                    }

                    newSwitch.Name = switchName;
                    newSwitch.StackableNumber = switchNumber;

                    var switchModule = newSwitch.SwitchModules.SingleOrDefault(x => x.Number == switchModuleNumber.Value);

                    if (switchModule == null)
                    {
                        switchModule = new SwitchModule();
                        switchModule.Number = switchModuleNumber.Value;
                        switchModule.Switch = newSwitch;
                    }

                    var switchPort = switchModule.SwitchPorts.SingleOrDefault(x => x.Port == switchPortNumber.Value);

                    if (switchPort == null)
                    {
                        switchPort = new SwitchPort();
                        switchPort.Port = switchPortNumber.Value;
                        switchPort.SwitchModule = switchModule;
                    }

                    serverIP.SwitchPort = switchPort;

                    action.Completed = true;
                }
                else
                {
                    action.Completed = false;
                }
            }
            else
            {
                action.Completed = true;
            }

            db.CommunicationActions.AddObject(action);

            return action;
        }