<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package mapper

import (
	"github.com/ernestio/definition-mapper/libmapper/providers/vcloud/components"
	"github.com/ernestio/definition-mapper/libmapper/providers/vcloud/definition"
	"github.com/r3labs/graph"
)

// MapGateways : Maps input edge gateway to an ernest formatted edge gateway
func MapGateways(d *definition.Definition) []*components.Gateway {
	var routers []*components.Gateway

	for _, router := range d.Gateways {
		r := &components.Gateway{
			Name: router.Name,
		}

		// Map firewall rules
		for _, rule := range router.FirewallRules {
			snw := d.FindNetwork(rule.Source)
			if snw != nil {
				rule.Source = snw.Subnet
			}

			dnw := d.FindNetwork(rule.Destination)
			if dnw != nil {
				rule.Destination = dnw.Subnet
			}

			r.FirewallRules = append(r.FirewallRules, components.FirewallRule{
				Name:            rule.Name,
				SourceIP:        rule.Source,
				SourcePort:      rule.FromPort,
				DestinationIP:   rule.Destination,
				DestinationPort: rule.ToPort,
				Protocol:        rule.Protocol,
			})
		}

		// Map nat rules
		for _, rule := range router.NatRules {
			r.NatRules = append(r.NatRules, components.NatRule{
				Type:            rule.Type,
				OriginIP:        rule.Source,
				OriginPort:      rule.FromPort,
				TranslationIP:   rule.Destination,
				TranslationPort: rule.ToPort,
				Protocol:        rule.Protocol,
			})
		}

		r.SetDefaultVariables()

		routers = append(routers, r)
	}

	return routers
}

// MapDefinitionGateways : maps all networks, firewall and nat rules and gateway config to a definition gateway
func MapDefinitionGateways(g *graph.Graph) []definition.Gateway {
	var gateways []definition.Gateway

	for _, c := range g.GetComponents().ByType("router") {
		gw := c.(*components.Gateway)

		dgw := definition.Gateway{
			Name: gw.Name,
		}

		for _, c := range g.GetComponents().ByType("network") {
			n := c.(*components.Network)

			if n.EdgeGateway != dgw.Name {
				continue
			}

			dgw.Networks = append(dgw.Networks, definition.Network{
				Name:   n.Name,
				Subnet: n.Subnet,
				DNS:    n.DNS,
			})
		}

		for _, rule := range gw.FirewallRules {
			dgw.FirewallRules = append(dgw.FirewallRules, definition.FirewallRule{
				Name:        rule.Name,
				Source:      rule.SourceIP,
				FromPort:    rule.SourcePort,
				Destination: rule.DestinationIP,
				ToPort:      rule.DestinationPort,
				Protocol:    rule.Protocol,
			})
		}

		for _, rule := range gw.NatRules {
			dgw.NatRules = append(dgw.NatRules, definition.NatRule{
				Type:        rule.Type,
				Source:      rule.OriginIP,
				FromPort:    rule.OriginPort,
				Destination: rule.TranslationIP,<commit_msg>getting network cidr from gateway ip and netmask<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package mapper

import (
	"fmt"
	"net"
	"strconv"
	"strings"

	"github.com/ernestio/definition-mapper/libmapper/providers/vcloud/components"
	"github.com/ernestio/definition-mapper/libmapper/providers/vcloud/definition"
	"github.com/r3labs/graph"
)

// MapGateways : Maps input edge gateway to an ernest formatted edge gateway
func MapGateways(d *definition.Definition) []*components.Gateway {
	var routers []*components.Gateway

	for _, router := range d.Gateways {
		r := &components.Gateway{
			Name: router.Name,
		}

		// Map firewall rules
		for _, rule := range router.FirewallRules {
			snw := d.FindNetwork(rule.Source)
			if snw != nil {
				rule.Source = snw.Subnet
			}

			dnw := d.FindNetwork(rule.Destination)
			if dnw != nil {
				rule.Destination = dnw.Subnet
			}

			r.FirewallRules = append(r.FirewallRules, components.FirewallRule{
				Name:            rule.Name,
				SourceIP:        rule.Source,
				SourcePort:      rule.FromPort,
				DestinationIP:   rule.Destination,
				DestinationPort: rule.ToPort,
				Protocol:        rule.Protocol,
			})
		}

		// Map nat rules
		for _, rule := range router.NatRules {
			r.NatRules = append(r.NatRules, components.NatRule{
				Type:            rule.Type,
				OriginIP:        rule.Source,
				OriginPort:      rule.FromPort,
				TranslationIP:   rule.Destination,
				TranslationPort: rule.ToPort,
				Protocol:        rule.Protocol,
			})
		}

		r.SetDefaultVariables()

		routers = append(routers, r)
	}

	return routers
}

// MapDefinitionGateways : maps all networks, firewall and nat rules and gateway config to a definition gateway
func MapDefinitionGateways(g *graph.Graph) []definition.Gateway {
	var gateways []definition.Gateway

	for _, c := range g.GetComponents().ByType("router") {
		gw := c.(*components.Gateway)

		dgw := definition.Gateway{
			Name: gw.Name,
		}

		for _, c := range g.GetComponents().ByType("network") {
			n := c.(*components.Network)

			if n.EdgeGateway != dgw.Name {
				continue
			}

			mask := net.IPMask(net.ParseIP(n.Netmask).To4())
			prefixSize, _ := mask.Size()

			octets := strings.Split(n.Gateway, ".")
			octet, _ := strconv.Atoi(octets[3])
			octets[3] = strconv.Itoa(octet - 1)
			cidr := fmt.Sprintf("%s/%d", strings.Join(octets, "."), prefixSize)

			dgw.Networks = append(dgw.Networks, definition.Network{
				Name:   n.Name,
				Subnet: cidr,
				DNS:    n.DNS,
			})
		}

		for _, rule := range gw.FirewallRules {
			dgw.FirewallRules = append(dgw.FirewallRules, definition.FirewallRule{
				Name:        rule.Name,
				Source:      rule.SourceIP,
				FromPort:    rule.SourcePort,
				Destination: rule.DestinationIP,
				ToPort:      rule.DestinationPort,
				Protocol:    rule.Protocol,
			})
		}

		for _, rule := range gw.NatRules {
			dgw.NatRules = append(dgw.NatRules, definition.NatRule{
				Type:        rule.Type,
				Source:      rule.OriginIP,
				FromPort:    rule.OriginPort,
				Destination: rule.TranslationIP,
{'fraction_non_alphanumeric': 0.11893044619422573, 'fraction_numerical': 0.0022965879265091863, 'mean_word_length': 2.9008317338451697, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6530768', 'n_tokens_mistral': 2370, 'n_tokens_neox': 2016, 'n_words': 457}