<commit_before>// Copyright Microsoft Corp.
// All rights reserved.

package network

import (
	"fmt"
	"net/http"
	"sync"

	"github.com/Azure/Aqua/core"
	"github.com/Azure/Aqua/log"
)

// Libnetwork network plugin name
const pluginName = "aqua"

// Libnetwork network plugin endpoint name
const endpointName = "NetworkDriver"

// NetPlugin object and its interface
type netPlugin struct {
	name     string
	version  string
	scope    string
	listener *core.Listener
	networks map[string]*azureNetwork
	sync.Mutex
}

type NetPlugin interface {
	Start(chan error) error
	Stop()

	GetListener() *core.Listener
}

// Creates a new NetPlugin object.
func NewPlugin(name string, version string) (NetPlugin, error) {
	return &netPlugin{
		name:    name,
		version: version,
		scope:   "local",
	}, nil
}

// Starts the plugin.
func (plugin *netPlugin) Start(errChan chan error) error {
	var socketName string
	if plugin.name != "test" {
		socketName = plugin.name
	}

	// Create the listener.
	listener, err := core.NewListener(socketName)
	if err != nil {
		log.Printf("Failed to create listener %v", err)
		return err
	}

	// Add protocol handlers.
	listener.AddHandler("Plugin", "Activate", plugin.activatePlugin)
	listener.AddHandler(endpointName, "GetCapabilities", plugin.getCapabilities)
	listener.AddHandler(endpointName, "CreateNetwork", plugin.createNetwork)
	listener.AddHandler(endpointName, "DeleteNetwork", plugin.deleteNetwork)
	listener.AddHandler(endpointName, "CreateEndpoint", plugin.createEndpoint)
	listener.AddHandler(endpointName, "DeleteEndpoint", plugin.deleteEndpoint)
	listener.AddHandler(endpointName, "Join", plugin.join)
	listener.AddHandler(endpointName, "Leave", plugin.leave)
	listener.AddHandler(endpointName, "EndpointOperInfo", plugin.endpointOperInfo)

	plugin.listener = listener

	err = listener.Start(errChan)
	if err != nil {
		log.Printf("Failed to start listener %v", err)
		return err
	}

	log.Printf("%s: Plugin started.", plugin.name)

	return nil
}

// Stops the plugin.
func (plugin *netPlugin) Stop() {
	plugin.listener.Stop()
	core.FreeSlaves()
	log.Printf("%s: Plugin stopped.\n", plugin.name)
}

// Returns the listener for the plugin.
func (plugin *netPlugin) GetListener() *core.Listener {
	return plugin.listener
}

func (plugin *netPlugin) networkExists(networkID string) bool {
	if plugin.networks[networkID] != nil {
		return true
	}
	return false
}

func (plugin *netPlugin) endpointExists(networkID string, endpointID string) bool {
	network := plugin.networks[networkID]
	if network == nil {
		return false
	}

	if network.endpoints[endpointID] == nil {
		return false
	}

	return true
}

type activateResponse struct {
	Implements []string
}

func (plugin *netPlugin) activatePlugin(w http.ResponseWriter, r *http.Request) {
	log.Request(plugin.name, "Activate", nil, nil)

	resp := &activateResponse{[]string{endpointName}}
	err := plugin.listener.Encode(w, resp)

	log.Response(plugin.name, "Activate", resp, err)
}

func (plugin *netPlugin) getCapabilities(w http.ResponseWriter, r *http.Request) {
	log.Request(plugin.name, "GetCapabilities", nil, nil)

	resp := map[string]string{"Scope": plugin.scope}
	err := plugin.listener.Encode(w, resp)

	log.Response(plugin.name, "GetCapabilities", resp, err)
}

// All request and response formats are well known and are published by libnetwork
type createNetworkRequestFormat struct {
	NetworkID string
	Options   map[string]interface{}
}

func (plugin *netPlugin) createNetwork(w http.ResponseWriter, r *http.Request) {<commit_msg>Minor renaming for consistency with libnetwork terminology<commit_after>// Copyright Microsoft Corp.
// All rights reserved.

package network

import (
	"fmt"
	"net/http"
	"sync"

	"github.com/Azure/Aqua/core"
	"github.com/Azure/Aqua/log"
)

// Libnetwork network plugin endpoint type
const endpointType = "NetworkDriver"

// NetPlugin object and its interface
type netPlugin struct {
	name     string
	version  string
	scope    string
	listener *core.Listener
	networks map[string]*azureNetwork
	sync.Mutex
}

type NetPlugin interface {
	Start(chan error) error
	Stop()
	GetListener() *core.Listener
}

// Creates a new NetPlugin object.
func NewPlugin(name string, version string) (NetPlugin, error) {
	return &netPlugin{
		name:    name,
		version: version,
		scope:   "local",
	}, nil
}

// Starts the plugin.
func (plugin *netPlugin) Start(errChan chan error) error {
	var socketName string
	if plugin.name != "test" {
		socketName = plugin.name
	}

	// Create the listener.
	listener, err := core.NewListener(socketName)
	if err != nil {
		log.Printf("Failed to create listener %v", err)
		return err
	}

	// Add protocol handlers.
	listener.AddHandler("Plugin", "Activate", plugin.activatePlugin)
	listener.AddHandler(endpointType, "GetCapabilities", plugin.getCapabilities)
	listener.AddHandler(endpointType, "CreateNetwork", plugin.createNetwork)
	listener.AddHandler(endpointType, "DeleteNetwork", plugin.deleteNetwork)
	listener.AddHandler(endpointType, "CreateEndpoint", plugin.createEndpoint)
	listener.AddHandler(endpointType, "DeleteEndpoint", plugin.deleteEndpoint)
	listener.AddHandler(endpointType, "Join", plugin.join)
	listener.AddHandler(endpointType, "Leave", plugin.leave)
	listener.AddHandler(endpointType, "EndpointOperInfo", plugin.endpointOperInfo)

	plugin.listener = listener

	err = listener.Start(errChan)
	if err != nil {
		log.Printf("Failed to start listener %v", err)
		return err
	}

	log.Printf("%s: Plugin started.", plugin.name)

	return nil
}

// Stops the plugin.
func (plugin *netPlugin) Stop() {
	plugin.listener.Stop()
	core.FreeSlaves()
	log.Printf("%s: Plugin stopped.\n", plugin.name)
}

// Returns the listener for the plugin.
func (plugin *netPlugin) GetListener() *core.Listener {
	return plugin.listener
}

func (plugin *netPlugin) networkExists(networkID string) bool {
	if plugin.networks[networkID] != nil {
		return true
	}
	return false
}

func (plugin *netPlugin) endpointExists(networkID string, endpointID string) bool {
	network := plugin.networks[networkID]
	if network == nil {
		return false
	}

	if network.endpoints[endpointID] == nil {
		return false
	}

	return true
}

type activateResponse struct {
	Implements []string
}

func (plugin *netPlugin) activatePlugin(w http.ResponseWriter, r *http.Request) {
	log.Request(plugin.name, "Activate", nil, nil)

	resp := &activateResponse{[]string{endpointType}}
	err := plugin.listener.Encode(w, resp)

	log.Response(plugin.name, "Activate", resp, err)
}

func (plugin *netPlugin) getCapabilities(w http.ResponseWriter, r *http.Request) {
	log.Request(plugin.name, "GetCapabilities", nil, nil)

	resp := map[string]string{"Scope": plugin.scope}
	err := plugin.listener.Encode(w, resp)

	log.Response(plugin.name, "GetCapabilities", resp, err)
}

// All request and response formats are well known and are published by libnetwork
type createNetworkRequestFormat struct {
	NetworkID string
	Options   map[string]interface{}
}

func (plugin *netPlugin) createNetwork(w http.ResponseWriter, r *http.Request) {
{'fraction_non_alphanumeric': 0.11785562331584172, 'fraction_numerical': 0.0, 'mean_word_length': 4.906197654941374, 'pattern_counts': {'":': 2, '<': 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': '303286', 'n_tokens_mistral': 2221, 'n_tokens_neox': 2162, 'n_words': 597}