// Copyright Istio Authors
//
// 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 virtualservice

import (
	"fmt"

	"istio.io/api/networking/v1alpha3"
	"istio.io/istio/pkg/config"
	"istio.io/istio/pkg/config/analysis"
	"istio.io/istio/pkg/config/analysis/analyzers/util"
	"istio.io/istio/pkg/config/analysis/msg"
	"istio.io/istio/pkg/config/resource"
	"istio.io/istio/pkg/config/schema/gvk"
)

// DestinationHostAnalyzer checks the destination hosts associated with each virtual service
type DestinationHostAnalyzer struct{}

var _ analysis.Analyzer = &DestinationHostAnalyzer{}

type hostAndSubset struct {
	host   resource.FullName
	subset string
}

// Metadata implements Analyzer
func (a *DestinationHostAnalyzer) Metadata() analysis.Metadata {
	return analysis.Metadata{
		Name:        "virtualservice.DestinationHostAnalyzer",
		Description: "Checks the destination hosts associated with each virtual service",
		Inputs: []config.GroupVersionKind{
			gvk.ServiceEntry,
			gvk.VirtualService,
			gvk.Service,
		},
	}
}

// Analyze implements Analyzer
func (a *DestinationHostAnalyzer) Analyze(ctx analysis.Context) {
	// Precompute the set of service entry hosts that exist (there can be more than one defined per ServiceEntry CRD)
	serviceEntryHosts := util.InitServiceEntryHostMap(ctx)
	virtualServiceDestinations := initVirtualServiceDestinations(ctx)

	ctx.ForEach(gvk.VirtualService, func(r *resource.Instance) bool {
		a.analyzeVirtualService(r, ctx, serviceEntryHosts)
		a.analyzeSubset(r, ctx, virtualServiceDestinations)
		return true
	})
}

func (a *DestinationHostAnalyzer) analyzeSubset(r *resource.Instance, ctx analysis.Context, vsDestinations map[resource.FullName][]*v1alpha3.Destination) {
	vs := r.Message.(*v1alpha3.VirtualService)

	// if there's no gateway specified, we're done
	if len(vs.GetGateways()) == 0 {
		return
	}

	for ruleIndex, http := range vs.GetHttp() {
		for routeIndex, route := range http.GetRoute() {
			if route.GetDestination().GetSubset() == "" {
				for virtualservice, destinations := range vsDestinations {
					for _, destination := range destinations {
						if destination.GetHost() == route.GetDestination().GetHost() {
							m := msg.NewIngressRouteRulesNotAffected(r, virtualservice.String(), r.Metadata.FullName.String())

							key := fmt.Sprintf(util.DestinationHost, http.Name, ruleIndex, routeIndex)
							if line, ok := util.ErrorLine(r, key); ok {
								m.Line = line
							}

							ctx.Report(gvk.VirtualService, m)
						}
					}
				}
			}
		}
	}
}

// get all virtualservice that have destination with subset
func initVirtualServiceDestinations(ctx analysis.Context) map[resource.FullName][]*v1alpha3.Destination {
	virtualservices := make(map[resource.FullName][]*v1alpha3.Destination)

	ctx.ForEach(gvk.VirtualService, func(r *resource.Instance) bool {
		virtualservice := r.Message.(*v1alpha3.VirtualService)
		for _, routes := range virtualservice.GetHttp() {
			for _, destinations := range routes.GetRoute() {
				// if there's no subset specified, we're done
				if destinations.GetDestination().GetSubset() != "" {
					for _, host := range virtualservice.GetHosts() {
						if destinations.GetDestination().GetHost() == host {
							virtualservices[r.Metadata.FullName] = append(virtualservices[r.Metadata.FullName], destinations.GetDestination())
						}
					}
				}
			}
		}

		return true
	})

	return virtualservices
}

func (a *DestinationHostAnalyzer) analyzeVirtualService(r *resource.Instance, ctx analysis.Context,
	serviceEntryHosts map[util.ScopedFqdn]*v1alpha3.ServiceEntry,
) {
	vs := r.Message.(*v1alpha3.VirtualService)

	for _, d := range getRouteDestinations(vs) {
		s := util.GetDestinationHost(r.Metadata.FullName.Namespace, vs.ExportTo, d.Destination.GetHost(), serviceEntryHosts)
		if s == nil {

			m := msg.NewReferencedResourceNotFound(r, "host", d.Destination.GetHost())

			key := fmt.Sprintf(util.DestinationHost, d.RouteRule, d.ServiceIndex, d.DestinationIndex)
			if line, found := util.ErrorLine(r, key); found {
				m.Line = line
			}

			ctx.Report(gvk.VirtualService, m)
			continue
		}
		checkServiceEntryPorts(ctx, r, d, s)
	}

	for _, d := range getHTTPMirrorDestinations(vs) {
		s := util.GetDestinationHost(r.Metadata.FullName.Namespace, vs.ExportTo, d.Destination.GetHost(), serviceEntryHosts)
		if s == nil {

			m := msg.NewReferencedResourceNotFound(r, "mirror host", d.Destination.GetHost())

			var key string
			if d.RouteRule == "http.mirror" {
				key = fmt.Sprintf(util.MirrorHost, d.ServiceIndex)
			} else {
				key = fmt.Sprintf(util.MirrorsHost, d.ServiceIndex, d.DestinationIndex)
			}
			if line, ok := util.ErrorLine(r, key); ok {
				m.Line = line
			}

			ctx.Report(gvk.VirtualService, m)
			continue
		}
		checkServiceEntryPorts(ctx, r, d, s)
	}
}

func checkServiceEntryPorts(ctx analysis.Context, r *resource.Instance, d *AnnotatedDestination, s *v1alpha3.ServiceEntry) {
	if d.Destination.GetPort() == nil {
		// If destination port isn't specified, it's only a problem if the service being referenced exposes multiple ports.
		if len(s.GetPorts()) > 1 {
			var portNumbers []int
			for _, p := range s.GetPorts() {
				portNumbers = append(portNumbers, int(p.GetNumber()))
			}

			m := msg.NewVirtualServiceDestinationPortSelectorRequired(r, d.Destination.GetHost(), portNumbers)

			var key string
			if d.RouteRule == "http.mirror" {
				key = fmt.Sprintf(util.MirrorHost, d.ServiceIndex)
			} else if d.RouteRule == "http.mirrors" {
				key = fmt.Sprintf(util.MirrorsHost, d.ServiceIndex, d.DestinationIndex)
			} else {
				key = fmt.Sprintf(util.DestinationHost, d.RouteRule, d.ServiceIndex, d.DestinationIndex)
			}
			if line, ok := util.ErrorLine(r, key); ok {
				m.Line = line
			}

			ctx.Report(gvk.VirtualService, m)
			return
		}

		// Otherwise, it's not needed and we're done here.
		return
	}

	foundPort := false
	for _, p := range s.GetPorts() {
		if d.Destination.GetPort().GetNumber() == p.GetNumber() {
			foundPort = true
			break
		}
	}
	if !foundPort {

		m := msg.NewReferencedResourceNotFound(r, "host:port",
			fmt.Sprintf("%s:%d", d.Destination.GetHost(), d.Destination.GetPort().GetNumber()))

		var key string
		if d.RouteRule == "http.mirror" {
			key = fmt.Sprintf(util.MirrorHost, d.ServiceIndex)
		} else if d.RouteRule == "http.mirrors" {
			key = fmt.Sprintf(util.MirrorsHost, d.ServiceIndex, d.DestinationIndex)
		} else {
			key = fmt.Sprintf(util.DestinationHost, d.RouteRule, d.ServiceIndex, d.DestinationIndex)
		}
		if line, ok := util.ErrorLine(r, key); ok {
			m.Line = line
		}

		ctx.Report(gvk.VirtualService, m)
	}
}
