-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* New target registry server pattern allowing future feature * Current chain: watch handler -> store (sqlite + keepalive)
- Loading branch information
1 parent
61c9766
commit 34b917a
Showing
7 changed files
with
356 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
Copyright (c) 2022 Nordix 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 next | ||
|
||
// BuildNextTargetRegistryChain chains the target registry servers together. | ||
// Each NextTargetRegistryServer must have a non nil NextTargetRegistryServerImpl. | ||
// If the list of nextTargetRegistryServers is nil or empty, a nil value will be returned. | ||
func BuildNextTargetRegistryChain(nextTargetRegistryServers ...NextTargetRegistryServer) NextTargetRegistryServer { | ||
if len(nextTargetRegistryServers) <= 0 { | ||
return nil | ||
} | ||
for i, ntrs := range nextTargetRegistryServers { | ||
if i >= (len(nextTargetRegistryServers) - 1) { | ||
break | ||
} | ||
ntrs.setNext(nextTargetRegistryServers[i+1]) | ||
} | ||
return nextTargetRegistryServers[0] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
Copyright (c) 2022 Nordix 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 next_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/nordix/meridio/pkg/nsp/next" | ||
) | ||
|
||
type fakeNextTargetRegistryServerImpl struct { | ||
*next.NextTargetRegistryServerImpl | ||
name string | ||
} | ||
|
||
func TestBuildNextTargetRegistryChain(t *testing.T) { | ||
fakeA := &fakeNextTargetRegistryServerImpl{ | ||
NextTargetRegistryServerImpl: &next.NextTargetRegistryServerImpl{}, | ||
name: "a", | ||
} | ||
type args struct { | ||
nextTargetRegistryServers []next.NextTargetRegistryServer | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want next.NextTargetRegistryServer | ||
}{ | ||
{ | ||
name: "empty", | ||
args: args{}, | ||
want: nil, | ||
}, | ||
{ | ||
name: "one", | ||
args: args{ | ||
[]next.NextTargetRegistryServer{ | ||
fakeA, | ||
}, | ||
}, | ||
want: fakeA, | ||
}, | ||
{ | ||
name: "multiple", | ||
args: args{ | ||
[]next.NextTargetRegistryServer{ | ||
fakeA, | ||
&fakeNextTargetRegistryServerImpl{ | ||
NextTargetRegistryServerImpl: &next.NextTargetRegistryServerImpl{}, | ||
name: "b", | ||
}, | ||
&fakeNextTargetRegistryServerImpl{ | ||
NextTargetRegistryServerImpl: &next.NextTargetRegistryServerImpl{}, | ||
name: "c", | ||
}, | ||
}, | ||
}, | ||
want: fakeA, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := next.BuildNextTargetRegistryChain(tt.args.nextTargetRegistryServers...); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("BuildNextTargetRegistryChain() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
Copyright (c) 2021 Nordix 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 next | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/golang/protobuf/ptypes/empty" | ||
nspAPI "github.com/nordix/meridio/api/nsp/v1" | ||
) | ||
|
||
// NextTargetRegistryServer is the interface representing TargetRegistryServer with the | ||
// support of the chaining feature. | ||
type NextTargetRegistryServer interface { | ||
nspAPI.TargetRegistryServer | ||
setNext(NextTargetRegistryServer) | ||
} | ||
|
||
type NextTargetRegistryServerImpl struct { | ||
nspAPI.UnimplementedTargetRegistryServer | ||
next NextTargetRegistryServer | ||
} | ||
|
||
// Register will call the Register function of the next chain element | ||
// If the next element is nil, then &empty.Empty{}, nil will be returned. | ||
func (ntrsi *NextTargetRegistryServerImpl) Register(ctx context.Context, target *nspAPI.Target) (*empty.Empty, error) { | ||
if ntrsi.next == nil { | ||
return &empty.Empty{}, nil | ||
} | ||
return ntrsi.next.Register(ctx, target) | ||
} | ||
|
||
// Unregister will call the Unregister function of the next chain element | ||
// If the next element is nil, then &empty.Empty{}, nil will be returned. | ||
func (ntrsi *NextTargetRegistryServerImpl) Unregister(ctx context.Context, target *nspAPI.Target) (*empty.Empty, error) { | ||
if ntrsi.next == nil { | ||
return &empty.Empty{}, nil | ||
} | ||
return ntrsi.next.Unregister(ctx, target) | ||
} | ||
|
||
// Watch will call the Watch function of the next chain element | ||
// If the next element is nil, then nil will be returned. | ||
func (ntrsi *NextTargetRegistryServerImpl) Watch(t *nspAPI.Target, watcher nspAPI.TargetRegistry_WatchServer) error { | ||
if ntrsi.next == nil { | ||
return nil | ||
} | ||
return ntrsi.next.Watch(t, watcher) | ||
} | ||
|
||
func (ntrsi *NextTargetRegistryServerImpl) setNext(ntrs NextTargetRegistryServer) { | ||
ntrsi.next = ntrs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
Copyright (c) 2021 Nordix 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 registry | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/golang/protobuf/ptypes/empty" | ||
nspAPI "github.com/nordix/meridio/api/nsp/v1" | ||
"github.com/nordix/meridio/pkg/nsp/next" | ||
"github.com/nordix/meridio/pkg/nsp/types" | ||
) | ||
|
||
type registry struct { | ||
TargetRegistry types.TargetRegistry | ||
*next.NextTargetRegistryServerImpl | ||
} | ||
|
||
// NewServer provides an implementation of TargetRegistryServer with the | ||
// support of the chaining feature. This implementation handles Register | ||
// and Unregister calls by adding or removing data into a storage (e.g | ||
// memory or sqlite) | ||
func NewServer(targetRegistry types.TargetRegistry) *registry { | ||
r := ®istry{ | ||
TargetRegistry: targetRegistry, | ||
NextTargetRegistryServerImpl: &next.NextTargetRegistryServerImpl{}, | ||
} | ||
return r | ||
} | ||
|
||
func (r *registry) Register(ctx context.Context, target *nspAPI.Target) (*empty.Empty, error) { | ||
err := r.TargetRegistry.Set(ctx, target) | ||
if err != nil { | ||
return &empty.Empty{}, err | ||
} | ||
return r.NextTargetRegistryServerImpl.Register(ctx, target) | ||
} | ||
|
||
func (r *registry) Unregister(ctx context.Context, target *nspAPI.Target) (*empty.Empty, error) { | ||
err := r.TargetRegistry.Remove(ctx, target) | ||
if err != nil { | ||
return &empty.Empty{}, err | ||
} | ||
return r.NextTargetRegistryServerImpl.Unregister(ctx, target) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.