internal static ClusterTopology GetClusterTopologyFromServiceRuntime(LocalNodeConfiguration localWindowsFabricNode)
        {
            List <InfrastructureNodeType> windowsFabricNodes = new List <InfrastructureNodeType>();

            int applicationPortCount = localWindowsFabricNode.ApplicationEndPort - localWindowsFabricNode.ApplicationStartPort + 1;
            Dictionary <string, WindowsFabricEndpointSet> roleNameToEndpointSetMap = GetRoleNameToEndpointSetMap(applicationPortCount);
            WindowsFabricEndpointSet currentRoleEndpointSet = roleNameToEndpointSetMap[RoleEnvironment.CurrentRoleInstance.Role.Name];

            foreach (KeyValuePair <string, Role> role in RoleEnvironment.Roles)
            {
                foreach (RoleInstance roleInstance in role.Value.Instances.Where(instance => IsWindowsFabricRoleInstance(instance)))
                {
                    var roleEndpointSet         = roleNameToEndpointSetMap[roleInstance.Role.Name];
                    InfrastructureNodeType node = new InfrastructureNodeType()
                    {
                        NodeName = GetNodeName(roleInstance),

                        UpgradeDomain = roleInstance.UpdateDomain.ToString(CultureInfo.InvariantCulture),

                        FaultDomain = string.Format(CultureInfo.InvariantCulture, WindowsFabricAzureWrapperServiceCommon.FaultDomainTemplate, roleInstance.FaultDomain),

                        IPAddressOrFQDN = GetRoleInstanceHostIPAddress(roleInstance).ToString(),

                        RoleOrTierName = roleInstance.Role.Name,

                        // The following two entries are unknown at this time and will be populated by FabricDeployer based on cluster manifest
                        NodeTypeRef = string.Empty,

                        IsSeedNode = false,

                        Endpoints = new FabricEndpointsType()
                        {
                            ClientConnectionEndpoint       = roleEndpointSet.ClientConnectionEndpoint ?? currentRoleEndpointSet.ClientConnectionEndpoint,
                            HttpGatewayEndpoint            = roleEndpointSet.HttpGatewayEndpoint ?? currentRoleEndpointSet.HttpGatewayEndpoint,
                            HttpApplicationGatewayEndpoint = roleEndpointSet.HttpApplicationGatewayEndpoint ?? currentRoleEndpointSet.HttpApplicationGatewayEndpoint,
                            ClusterConnectionEndpoint      = roleEndpointSet.ClusterConnectionEndpoint,
                            LeaseDriverEndpoint            = roleEndpointSet.LeaseDriverEndpoint,
                            ApplicationEndpoints           = roleEndpointSet.ApplicationEndpoints,
                        }
                    };

                    windowsFabricNodes.Add(node);
                }
            }

            // node list is always ordered by node name
            windowsFabricNodes.Sort(CompareNodeByName);

            return(new ClusterTopology
            {
                WindowsFabricNodes = windowsFabricNodes,

                CurrentNodeName = GetNodeName(RoleEnvironment.CurrentRoleInstance),

                CurrentNodeIPAddressOrFQDN = GetRoleInstanceHostIPAddress(RoleEnvironment.CurrentRoleInstance).ToString(),
            });
        }
        internal bool Equals(WindowsFabricEndpointSet windowsFabricEndpointSet)
        {
            if (windowsFabricEndpointSet == null)
            {
                return(false);
            }

            return
                (InputEndpointEquals(this.ClientConnectionEndpoint, windowsFabricEndpointSet.ClientConnectionEndpoint) &&
                 InputEndpointEquals(this.HttpGatewayEndpoint, windowsFabricEndpointSet.HttpGatewayEndpoint) &&
                 InputEndpointEquals(this.HttpApplicationGatewayEndpoint, windowsFabricEndpointSet.HttpApplicationGatewayEndpoint) &&
                 InternalEndpointEquals(this.ClusterConnectionEndpoint, windowsFabricEndpointSet.ClusterConnectionEndpoint) &&
                 InternalEndpointEquals(this.LeaseDriverEndpoint, windowsFabricEndpointSet.LeaseDriverEndpoint) &&
                 EndpointRangeEquals(this.ApplicationEndpoints, windowsFabricEndpointSet.ApplicationEndpoints));
        }
        private static bool FabricEndpointsEquals(FabricEndpointsType endpointSetA, FabricEndpointsType endpointSetB)
        {
            if (endpointSetA == null && endpointSetB == null)
            {
                return(true);
            }

            if (endpointSetA != null && endpointSetA != null)
            {
                return
                    (WindowsFabricEndpointSet.InputEndpointEquals(endpointSetA.ClientConnectionEndpoint, endpointSetB.ClientConnectionEndpoint) &&
                     WindowsFabricEndpointSet.InputEndpointEquals(endpointSetA.HttpGatewayEndpoint, endpointSetB.HttpGatewayEndpoint) &&
                     WindowsFabricEndpointSet.InputEndpointEquals(endpointSetA.HttpApplicationGatewayEndpoint, endpointSetB.HttpApplicationGatewayEndpoint) &&
                     WindowsFabricEndpointSet.InternalEndpointEquals(endpointSetA.ClusterConnectionEndpoint, endpointSetB.ClusterConnectionEndpoint) &&
                     WindowsFabricEndpointSet.InternalEndpointEquals(endpointSetA.LeaseDriverEndpoint, endpointSetB.LeaseDriverEndpoint) &&
                     WindowsFabricEndpointSet.EndpointRangeEquals(endpointSetA.ApplicationEndpoints, endpointSetB.ApplicationEndpoints));
            }

            return(false);
        }
        private static Dictionary <string, WindowsFabricEndpointSet> GetRoleNameToEndpointSetMap(int applicationPortCount)
        {
            Dictionary <string, WindowsFabricEndpointSet> roleNameToEndpointSetMap = new Dictionary <string, WindowsFabricEndpointSet>(StringComparer.OrdinalIgnoreCase);

            foreach (KeyValuePair <string, Role> role in RoleEnvironment.Roles)
            {
                // If it is the role of current role instance, get endpoints from current role instance; otherwise, use the first role instance in the collection
                if (RoleEnvironment.CurrentRoleInstance.Role.Name.Equals(role.Value.Name, StringComparison.OrdinalIgnoreCase))
                {
                    roleNameToEndpointSetMap[role.Value.Name] = WindowsFabricEndpointSet.GetWindowsFabricEndpointSet(RoleEnvironment.CurrentRoleInstance.InstanceEndpoints, applicationPortCount);
                }
                else
                {
                    foreach (RoleInstance roleInstance in role.Value.Instances.Where(instance => IsWindowsFabricRoleInstance(instance)))
                    {
                        roleNameToEndpointSetMap[role.Value.Name] = WindowsFabricEndpointSet.GetWindowsFabricEndpointSet(roleInstance.InstanceEndpoints, applicationPortCount);
                        break;
                    }
                }
            }

            return(roleNameToEndpointSetMap);
        }