2017-07-06 08:31:49 +03:00
|
|
|
// Copyright 2017 Microsoft. All rights reserved.
|
|
|
|
// MIT License
|
|
|
|
|
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2020-11-06 20:51:30 +03:00
|
|
|
"github.com/Azure/azure-container-networking/server/tls"
|
2017-07-06 08:31:49 +03:00
|
|
|
|
2020-01-25 00:29:52 +03:00
|
|
|
"github.com/Azure/azure-container-networking/cns/logger"
|
2017-07-06 08:31:49 +03:00
|
|
|
acn "github.com/Azure/azure-container-networking/common"
|
|
|
|
"github.com/Azure/azure-container-networking/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Service implements behavior common to all services.
|
|
|
|
type Service struct {
|
2020-07-23 23:03:10 +03:00
|
|
|
Name string
|
|
|
|
Version string
|
|
|
|
Options map[string]interface{}
|
|
|
|
ErrChan chan error
|
|
|
|
Store store.KeyValueStore
|
|
|
|
ChannelMode string
|
2017-07-06 08:31:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceAPI defines base interface.
|
|
|
|
type ServiceAPI interface {
|
|
|
|
Start(*ServiceConfig) error
|
|
|
|
Stop()
|
|
|
|
GetOption(string) interface{}
|
|
|
|
SetOption(string, interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceConfig specifies common configuration.
|
|
|
|
type ServiceConfig struct {
|
2020-07-23 23:03:10 +03:00
|
|
|
Name string
|
|
|
|
Version string
|
|
|
|
Listener *acn.Listener
|
|
|
|
ErrChan chan error
|
|
|
|
Store store.KeyValueStore
|
|
|
|
ChannelMode string
|
2020-11-06 20:51:30 +03:00
|
|
|
TlsSettings tls.TlsSettings
|
2017-07-06 08:31:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewService creates a new Service object.
|
2020-07-23 23:03:10 +03:00
|
|
|
func NewService(name, version, channelMode string, store store.KeyValueStore) (*Service, error) {
|
2020-01-25 00:29:52 +03:00
|
|
|
logger.Debugf("[Azure CNS] Going to create a service object with name: %v. version: %v.", name, version)
|
2017-07-06 08:31:49 +03:00
|
|
|
|
|
|
|
svc := &Service{
|
2020-07-23 23:03:10 +03:00
|
|
|
Name: name,
|
|
|
|
Version: version,
|
|
|
|
ChannelMode: channelMode,
|
|
|
|
Options: make(map[string]interface{}),
|
|
|
|
Store: store,
|
2017-07-06 08:31:49 +03:00
|
|
|
}
|
|
|
|
|
2020-07-23 23:03:10 +03:00
|
|
|
logger.Debugf("[Azure CNS] Finished creating service object with name: %v. version: %v. managed: %s", name, version, channelMode)
|
2017-07-06 08:31:49 +03:00
|
|
|
return svc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize initializes the service.
|
|
|
|
func (service *Service) Initialize(config *ServiceConfig) error {
|
|
|
|
if config == nil {
|
|
|
|
err := "[Azure CNS Errror] Initialize called with nil ServiceConfig."
|
2020-01-25 00:29:52 +03:00
|
|
|
logger.Errorf(err)
|
2017-07-06 08:31:49 +03:00
|
|
|
return errors.New(err)
|
|
|
|
}
|
|
|
|
|
2020-01-25 00:29:52 +03:00
|
|
|
logger.Debugf("[Azure CNS] Going to initialize the service: %+v with config: %+v.", service, config)
|
2017-07-06 08:31:49 +03:00
|
|
|
|
|
|
|
service.ErrChan = config.ErrChan
|
|
|
|
service.Store = config.Store
|
|
|
|
service.Version = config.Version
|
2020-07-23 23:03:10 +03:00
|
|
|
service.ChannelMode = config.ChannelMode
|
2017-07-06 08:31:49 +03:00
|
|
|
|
2020-01-25 00:29:52 +03:00
|
|
|
logger.Debugf("[Azure CNS] nitialized service: %+v with config: %+v.", service, config)
|
2017-07-06 08:31:49 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uninitialize cleans up the service.
|
|
|
|
func (service *Service) Uninitialize() {
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOption gets the option value for the given key.
|
|
|
|
func (service *Service) GetOption(key string) interface{} {
|
|
|
|
return service.Options[key]
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOption sets the option value for the given key.
|
|
|
|
func (service *Service) SetOption(key string, value interface{}) {
|
|
|
|
service.Options[key] = value
|
|
|
|
}
|