Skip to content

Commit

Permalink
Support file-based configuration (onosproject#1108)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuujo authored Mar 27, 2020
1 parent 2705cce commit b80650e
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 60 deletions.
30 changes: 20 additions & 10 deletions cmd/onos-config/onos-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ package main

import (
"flag"
"fmt"
"github.com/onosproject/onos-config/pkg/config"
"os"
"time"

"github.com/onosproject/onos-config/pkg/manager"
Expand All @@ -57,8 +60,6 @@ import (
"github.com/onosproject/onos-lib-go/pkg/northbound"
)

var log = logging.GetLogger("main")

type arrayFlags []string

func (i *arrayFlags) String() string {
Expand All @@ -83,39 +84,48 @@ func main() {
flag.Bool("debug", false, "enable debug logging")
flag.Parse()

config, err := config.GetConfig()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

logging.Configure(config.Logging)

log := logging.GetLogger("main")
log.Info("Starting onos-config")

opts, err := certs.HandleCertArgs(keyPath, certPath)
if err != nil {
log.Fatal(err)
}

leadershipStore, err := leadership.NewAtomixStore()
leadershipStore, err := leadership.NewAtomixStore(config)
if err != nil {
log.Error("Cannot load leadership atomix store ", err)
}

mastershipStore, err := mastership.NewAtomixStore()
mastershipStore, err := mastership.NewAtomixStore(config)
if err != nil {
log.Error("Cannot load mastership atomix store ", err)
}

deviceChangesStore, err := device.NewAtomixStore()
deviceChangesStore, err := device.NewAtomixStore(config)
if err != nil {
log.Error("Cannot load device atomix store ", err)
}

networkChangesStore, err := network.NewAtomixStore()
networkChangesStore, err := network.NewAtomixStore(config)
if err != nil {
log.Error("Cannot load network atomix store ", err)
}

networkSnapshotStore, err := networksnap.NewAtomixStore()
networkSnapshotStore, err := networksnap.NewAtomixStore(config)
if err != nil {
log.Error("Cannot load network snapshot atomix store ", err)
}

deviceSnapshotStore, err := devicesnap.NewAtomixStore()
deviceSnapshotStore, err := devicesnap.NewAtomixStore(config)
if err != nil {
log.Error("Cannot load network atomix store ", err)
}
Expand Down Expand Up @@ -164,15 +174,15 @@ func main() {
}

mgr.Run()
err = startServer(*caPath, *keyPath, *certPath)
err = startServer(*caPath, *keyPath, *certPath, log)
if err != nil {
log.Fatal("Unable to start onos-config ", err)
}
}
}

// Creates gRPC server and registers various services; then serves.
func startServer(caPath string, keyPath string, certPath string) error {
func startServer(caPath string, keyPath string, certPath string, log *logging.Log) error {
s := northbound.NewServer(northbound.NewServerConfig(caPath, keyPath, certPath, 5150, true))
s.AddService(admin.Service{})
s.AddService(diags.Service{})
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/onosproject/config-models/modelplugin/devicesim-1.0.0 v0.0.0-20200303111912-723f2289d4c2
github.com/onosproject/config-models/modelplugin/testdevice-1.0.0 v0.0.0-20200303111912-723f2289d4c2
github.com/onosproject/config-models/modelplugin/testdevice-2.0.0 v0.0.0-20200304144136-6992f473b240
github.com/onosproject/onos-lib-go v0.0.0-20200311221003-fad88142208e
github.com/onosproject/onos-lib-go v0.0.0-20200318222217-3c0f4a7ed58b
github.com/onosproject/onos-test v0.0.0-20200306000348-1f2c86fc76c6
github.com/onosproject/onos-topo v0.0.0-20200218171206-55029b503689
github.com/openconfig/gnmi v0.0.0-20190823184014-89b2bf29312c
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,12 @@ github.com/onosproject/onos-lib-go v0.0.0-20200307155659-8ad70c22b981 h1:ao+nAtL
github.com/onosproject/onos-lib-go v0.0.0-20200307155659-8ad70c22b981/go.mod h1:tbdH7aqxvcOLSfz8cgiDwNP5szPnll1/m9HYKbXruSI=
github.com/onosproject/onos-lib-go v0.0.0-20200311221003-fad88142208e h1:gmR2xTdzpbSYgoPH3R83l8P/JnCUDK7eVaTqhWo9X6s=
github.com/onosproject/onos-lib-go v0.0.0-20200311221003-fad88142208e/go.mod h1:gnWmBzxrrUIdMPbwcBudAxIJCALzRj/CdSpaCfrgwkU=
github.com/onosproject/onos-lib-go v0.0.0-20200317205302-d54b7f33b9de h1:f95/+gMpll37SAemDmgWv5+jm794iopmtGGQoPykwEE=
github.com/onosproject/onos-lib-go v0.0.0-20200317205302-d54b7f33b9de/go.mod h1:gnWmBzxrrUIdMPbwcBudAxIJCALzRj/CdSpaCfrgwkU=
github.com/onosproject/onos-lib-go v0.0.0-20200318085111-7a1561372660 h1:3s4OB+oCoSrQa3Vhmgrf6Cpt9259jYJdootY04KhAPs=
github.com/onosproject/onos-lib-go v0.0.0-20200318085111-7a1561372660/go.mod h1:gnWmBzxrrUIdMPbwcBudAxIJCALzRj/CdSpaCfrgwkU=
github.com/onosproject/onos-lib-go v0.0.0-20200318222217-3c0f4a7ed58b h1:ZbXioCOdmU1C8wMToj/WtAaViPkn594qlc+xwuVg/0U=
github.com/onosproject/onos-lib-go v0.0.0-20200318222217-3c0f4a7ed58b/go.mod h1:gnWmBzxrrUIdMPbwcBudAxIJCALzRj/CdSpaCfrgwkU=
github.com/onosproject/onos-ran v0.0.0-20200205211410-0667c7147453/go.mod h1:mDd/65obXVUsEGFSLmxokw6QaW/3YCYal+HV1Y08kf8=
github.com/onosproject/onos-ran v0.0.0-20200210203428-df3953bf0fb1 h1:fXm7bKsrvhohMYk/YZtZ++sFZGhdEgPDvHJuPjw7FdQ=
github.com/onosproject/onos-ran v0.0.0-20200210203428-df3953bf0fb1/go.mod h1:mIi1jgByW+1Ns9vZg/lAXyTXDGi7wI6zWiqhnB9qiKU=
Expand Down
51 changes: 51 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2020-present Open Networking Foundation.
//
// 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 config

import (
"github.com/onosproject/onos-lib-go/pkg/atomix"
configlib "github.com/onosproject/onos-lib-go/pkg/config"
"github.com/onosproject/onos-lib-go/pkg/logging"
)

var config *Config

// Config is the onos-config configuration
type Config struct {
// Atomix is the Atomix configuration
Atomix atomix.Config `yaml:"atomix,omitempty"`
// Logging is the logging configuration
Logging logging.Config `yaml:"logging,omitempty"`
}

// GetConfig gets the onos-config configuration
func GetConfig() (Config, error) {
if config == nil {
config = &Config{}
if err := configlib.Load(config); err != nil {
return Config{}, err
}
}
return *config, nil
}

// GetConfigOrDie gets the onos-config configuration or panics
func GetConfigOrDie() Config {
config, err := GetConfig()
if err != nil {
panic(err)
}
return config
}
12 changes: 4 additions & 8 deletions pkg/store/change/device/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"errors"
"fmt"
"github.com/onosproject/onos-config/pkg/config"
"io"
"sync"
"time"
Expand All @@ -41,19 +42,14 @@ func getDeviceChangesName(deviceID device.VersionedID) string {
}

// NewAtomixStore returns a new persistent Store
func NewAtomixStore() (Store, error) {
client, err := atomix.GetAtomixClient()
if err != nil {
return nil, err
}

group, err := client.GetDatabase(context.Background(), atomix.GetAtomixRaftGroup())
func NewAtomixStore(config config.Config) (Store, error) {
database, err := atomix.GetDatabase(config.Atomix, config.Atomix.GetDatabase(atomix.DatabaseTypeConsensus))
if err != nil {
return nil, err
}

changesFactory := func(deviceID device.VersionedID) (indexedmap.IndexedMap, error) {
return group.GetIndexedMap(context.Background(), getDeviceChangesName(deviceID))
return database.GetIndexedMap(context.Background(), getDeviceChangesName(deviceID))
}

return &atomixStore{
Expand Down
12 changes: 4 additions & 8 deletions pkg/store/change/network/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/gogo/protobuf/proto"
"github.com/google/uuid"
networkchange "github.com/onosproject/onos-config/api/types/change/network"
"github.com/onosproject/onos-config/pkg/config"
"github.com/onosproject/onos-config/pkg/store/cluster"
"github.com/onosproject/onos-config/pkg/store/stream"
"github.com/onosproject/onos-lib-go/pkg/atomix"
Expand All @@ -37,18 +38,13 @@ func init() {
}

// NewAtomixStore returns a new persistent Store
func NewAtomixStore() (Store, error) {
client, err := atomix.GetAtomixClient()
func NewAtomixStore(config config.Config) (Store, error) {
database, err := atomix.GetDatabase(config.Atomix, config.Atomix.GetDatabase(atomix.DatabaseTypeConsensus))
if err != nil {
return nil, err
}

group, err := client.GetDatabase(context.Background(), atomix.GetAtomixRaftGroup())
if err != nil {
return nil, err
}

changes, err := group.GetIndexedMap(context.Background(), changesName)
changes, err := database.GetIndexedMap(context.Background(), changesName)
if err != nil {
return nil, err
}
Expand Down
12 changes: 4 additions & 8 deletions pkg/store/leadership/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/atomix/go-client/pkg/client/election"
"github.com/atomix/go-client/pkg/client/primitive"
"github.com/atomix/go-client/pkg/client/util/net"
"github.com/onosproject/onos-config/pkg/config"
"github.com/onosproject/onos-config/pkg/store/cluster"
"github.com/onosproject/onos-lib-go/pkg/atomix"
"io"
Expand Down Expand Up @@ -56,18 +57,13 @@ type Leadership struct {
}

// NewAtomixStore returns a new persistent Store
func NewAtomixStore() (Store, error) {
client, err := atomix.GetAtomixClient()
func NewAtomixStore(config config.Config) (Store, error) {
database, err := atomix.GetDatabase(config.Atomix, config.Atomix.GetDatabase(atomix.DatabaseTypeConsensus))
if err != nil {
return nil, err
}

group, err := client.GetDatabase(context.Background(), atomix.GetAtomixRaftGroup())
if err != nil {
return nil, err
}

election, err := group.GetElection(context.Background(), primitiveName, election.WithID(string(cluster.GetNodeID())))
election, err := database.GetElection(context.Background(), primitiveName, election.WithID(string(cluster.GetNodeID())))
if err != nil {
return nil, err
}
Expand Down
11 changes: 3 additions & 8 deletions pkg/store/mastership/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
package mastership

import (
"context"
"github.com/atomix/go-client/pkg/client/util/net"
"github.com/onosproject/onos-config/pkg/config"
"github.com/onosproject/onos-config/pkg/store/cluster"
"github.com/onosproject/onos-lib-go/pkg/atomix"
topodevice "github.com/onosproject/onos-topo/api/device"
Expand Down Expand Up @@ -54,13 +54,8 @@ type Mastership struct {
}

// NewAtomixStore returns a new persistent Store
func NewAtomixStore() (Store, error) {
client, err := atomix.GetAtomixClient()
if err != nil {
return nil, err
}

database, err := client.GetDatabase(context.Background(), atomix.GetAtomixRaftGroup())
func NewAtomixStore(config config.Config) (Store, error) {
database, err := atomix.GetDatabase(config.Atomix, config.Atomix.GetDatabase(atomix.DatabaseTypeConsensus))
if err != nil {
return nil, err
}
Expand Down
14 changes: 5 additions & 9 deletions pkg/store/snapshot/device/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/gogo/protobuf/proto"
"github.com/onosproject/onos-config/api/types/device"
devicesnapshot "github.com/onosproject/onos-config/api/types/snapshot/device"
"github.com/onosproject/onos-config/pkg/config"
"github.com/onosproject/onos-config/pkg/store/stream"
"github.com/onosproject/onos-lib-go/pkg/atomix"
"io"
Expand All @@ -33,23 +34,18 @@ const deviceSnapshotsName = "device-snapshots"
const snapshotsName = "snapshots"

// NewAtomixStore returns a new persistent Store
func NewAtomixStore() (Store, error) {
client, err := atomix.GetAtomixClient()
func NewAtomixStore(config config.Config) (Store, error) {
database, err := atomix.GetDatabase(config.Atomix, config.Atomix.GetDatabase(atomix.DatabaseTypeConsensus))
if err != nil {
return nil, err
}

group, err := client.GetDatabase(context.Background(), atomix.GetAtomixRaftGroup())
deviceSnapshots, err := database.GetMap(context.Background(), deviceSnapshotsName)
if err != nil {
return nil, err
}

deviceSnapshots, err := group.GetMap(context.Background(), deviceSnapshotsName)
if err != nil {
return nil, err
}

snapshots, err := group.GetMap(context.Background(), snapshotsName)
snapshots, err := database.GetMap(context.Background(), snapshotsName)
if err != nil {
return nil, err
}
Expand Down
12 changes: 4 additions & 8 deletions pkg/store/snapshot/network/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/gogo/protobuf/proto"
"github.com/google/uuid"
networksnapshot "github.com/onosproject/onos-config/api/types/snapshot/network"
"github.com/onosproject/onos-config/pkg/config"
"github.com/onosproject/onos-config/pkg/store/cluster"
"github.com/onosproject/onos-config/pkg/store/stream"
"github.com/onosproject/onos-lib-go/pkg/atomix"
Expand All @@ -37,18 +38,13 @@ func init() {
}

// NewAtomixStore returns a new persistent Store
func NewAtomixStore() (Store, error) {
client, err := atomix.GetAtomixClient()
func NewAtomixStore(config config.Config) (Store, error) {
database, err := atomix.GetDatabase(config.Atomix, config.Atomix.GetDatabase(atomix.DatabaseTypeConsensus))
if err != nil {
return nil, err
}

group, err := client.GetDatabase(context.Background(), atomix.GetAtomixRaftGroup())
if err != nil {
return nil, err
}

snapshots, err := group.GetIndexedMap(context.Background(), snapshotsName)
snapshots, err := database.GetIndexedMap(context.Background(), snapshotsName)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit b80650e

Please sign in to comment.