Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
prinikasn committed Sep 21, 2023
1 parent 080c75b commit 20c951b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 27 deletions.
40 changes: 15 additions & 25 deletions system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package system

import (
"context"
"fmt"
"io"
"time"

Expand Down Expand Up @@ -154,47 +153,38 @@ func (p *PingOperation) Execute(ctx context.Context, c *internal.Clients) ([]*sp

// SwitchControlProcessorOperation represents the parameters of a SwitchControlProcessor operation.
type SwitchControlProcessorOperation struct {
subcomponentName string
isPathSet bool
path *tpb.Path
req *spb.SwitchControlProcessorRequest
}

// NewSwitchControlProcessorOperation creates an empty SwitchControlProcessorOperation.
func NewSwitchControlProcessorOperation() *SwitchControlProcessorOperation {
return &SwitchControlProcessorOperation{}
return &SwitchControlProcessorOperation{req: &spb.SwitchControlProcessorRequest{}}
}

// PathFromSubcomponentName and Path set the route processor path. These functions override each other and only
// the last function used will be applied.

// PathFromSubcomponentName sets the path for route processor to switch from the subcomponentName.
func (s *SwitchControlProcessorOperation) PathFromSubcomponentName(n string) *SwitchControlProcessorOperation {
s.subcomponentName = n
s.req.ControlProcessor = &tpb.Path{
Origin: "openconfig",
Elem: []*tpb.PathElem{
{Name: "components"},
{Name: "component", Key: map[string]string{"name": n}},
},
}
return s
}

// Path specifies the path of the route processor to switch.
// Path sets the full path for the route processor to switch.
func (s *SwitchControlProcessorOperation) Path(p *tpb.Path) *SwitchControlProcessorOperation {
s.isPathSet = true
s.path = p
s.req.ControlProcessor = p
return s
}

// Execute performs the SwitchControlProcessor operation.
func (s *SwitchControlProcessorOperation) Execute(ctx context.Context, c *internal.Clients) (*spb.SwitchControlProcessorResponse, error) {
if s.subcomponentName != "" && s.isPathSet {
return nil, fmt.Errorf("cannot set both PathFromSubcomponentName %v and Path %v", s.subcomponentName, s.path.String())
}
req := &spb.SwitchControlProcessorRequest{ControlProcessor: s.path}
if s.subcomponentName != "" {
req = &spb.SwitchControlProcessorRequest{
ControlProcessor: &tpb.Path{
Origin: "openconfig",
Elem: []*tpb.PathElem{
{Name: "components"},
{Name: "component", Key: map[string]string{"name": s.subcomponentName}},
},
},
}
}
return c.System().SwitchControlProcessor(ctx, req)
return c.System().SwitchControlProcessor(ctx, s.req)
}

// TimeOperation represents the parameters of a Time operation.
Expand Down
3 changes: 1 addition & 2 deletions system/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,12 @@ func TestSwitchControlProcessor(t *testing.T) {
{
desc: "Test SwitchControlProcessor with PathFromSubcomponentName and Path returns error",
op: system.NewSwitchControlProcessorOperation().PathFromSubcomponentName("RP0").Path(&tpb.Path{
Origin: "openconfig",
Elem: []*tpb.PathElem{
{Name: "components"},
{Name: "component", Key: map[string]string{"name": "RP0"}},
},
}),
wantErr: "cannot set both PathFromSubcomponentName ",
want: &spb.SwitchControlProcessorResponse{Version: "new"},
},
{
desc: "SwitchControlProcessor returns error",
Expand Down

0 comments on commit 20c951b

Please sign in to comment.