mirror of
https://github.com/hanzozt/zt.git
synced 2026-10-30 15:04:55 +00:00
- Repo names: ziti-* → zt-* - Go modules: github.com/hanzozt/ziti → github.com/hanzozt/zt - Docker images: hanzozt/ziti-* → hanzozt/zt-* - C/Java/JS symbols: ziti_* → zt_* - File and directory names updated
115 lines
3.2 KiB
Go
115 lines
3.2 KiB
Go
/*
|
|
Copyright NetFoundry Inc.
|
|
|
|
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
|
|
|
|
https://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 model
|
|
|
|
import (
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/hanzozt/foundation/v2/genext"
|
|
"github.com/hanzozt/foundation/v2/versions"
|
|
"github.com/hanzozt/storage/boltz"
|
|
"github.com/hanzozt/zt/v2/common/ctrlchan"
|
|
"github.com/hanzozt/zt/v2/common/pb/ctrl_pb"
|
|
"github.com/hanzozt/zt/v2/controller/db"
|
|
"github.com/hanzozt/zt/v2/controller/models"
|
|
"go.etcd.io/bbolt"
|
|
)
|
|
|
|
type Listener interface {
|
|
AdvertiseAddress() string
|
|
Protocol() string
|
|
Groups() []string
|
|
}
|
|
|
|
type Router struct {
|
|
models.BaseEntity
|
|
Name string
|
|
Fingerprint *string
|
|
Listeners []*ctrl_pb.Listener
|
|
Control ctrlchan.CtrlChannel
|
|
Connected atomic.Bool
|
|
ConnectTime time.Time
|
|
VersionInfo *versions.VersionInfo
|
|
routerLinks RouterLinks
|
|
Cost uint16
|
|
NoTraversal bool
|
|
Disabled bool
|
|
Metadata *ctrl_pb.RouterMetadata
|
|
Interfaces []*Interface
|
|
}
|
|
|
|
func (entity *Router) GetLinks() []*Link {
|
|
return entity.routerLinks.GetLinks()
|
|
}
|
|
|
|
func (entity *Router) toBoltEntityForUpdate(tx *bbolt.Tx, env Env, _ boltz.FieldChecker) (*db.Router, error) {
|
|
return entity.toBoltEntityForCreate(tx, env)
|
|
}
|
|
|
|
func (entity *Router) toBoltEntityForCreate(*bbolt.Tx, Env) (*db.Router, error) {
|
|
return &db.Router{
|
|
BaseExtEntity: *boltz.NewExtEntity(entity.Id, entity.Tags),
|
|
Name: entity.Name,
|
|
Fingerprint: entity.Fingerprint,
|
|
Cost: entity.Cost,
|
|
NoTraversal: entity.NoTraversal,
|
|
Disabled: entity.Disabled,
|
|
Interfaces: InterfacesToBolt(entity.Interfaces),
|
|
}, nil
|
|
}
|
|
|
|
func (entity *Router) fillFrom(_ Env, _ *bbolt.Tx, boltRouter *db.Router) error {
|
|
entity.Name = boltRouter.Name
|
|
entity.Fingerprint = boltRouter.Fingerprint
|
|
entity.Cost = boltRouter.Cost
|
|
entity.NoTraversal = boltRouter.NoTraversal
|
|
entity.Disabled = boltRouter.Disabled
|
|
entity.Interfaces = InterfacesFromBolt(boltRouter.Interfaces)
|
|
entity.FillCommon(boltRouter)
|
|
return nil
|
|
}
|
|
|
|
func (entity *Router) AddLinkListener(addr, linkProtocol string, linkCostTags []string, groups []string) {
|
|
entity.Listeners = append(entity.Listeners, &ctrl_pb.Listener{
|
|
Address: addr,
|
|
Protocol: linkProtocol,
|
|
CostTags: linkCostTags,
|
|
Groups: groups,
|
|
})
|
|
}
|
|
|
|
func (entity *Router) SetLinkListeners(listeners []*ctrl_pb.Listener) {
|
|
entity.Listeners = listeners
|
|
}
|
|
|
|
func (entity *Router) SetMetadata(metadata *ctrl_pb.RouterMetadata) {
|
|
entity.Metadata = metadata
|
|
}
|
|
|
|
func (entity *Router) HasCapability(capability ctrl_pb.RouterCapability) bool {
|
|
return entity.Metadata != nil && genext.Contains(entity.Metadata.Capabilities, capability)
|
|
}
|
|
|
|
func (entity *Router) SupportsRouterLinkMgmt() bool {
|
|
if entity.VersionInfo == nil {
|
|
return true
|
|
}
|
|
supportsLinkMgmt, err := entity.VersionInfo.HasMinimumVersion("0.32.1")
|
|
return err != nil || supportsLinkMgmt
|
|
}
|