Repository navigation
Expand file tree
/
Copy pathinterface.go
More file actions
103 lines (90 loc) · 2.83 KB
/
Copy pathinterface.go
File metadata and controls
103 lines (90 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2026 Google LLC
//
// 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 monax
import (
"errors"
"fmt"
"sync"
"time"
log "github.com/golang/glog"
"google.golang.org/grpc"
monaxpb "github.com/openconfig/monax/proto"
)
const (
dialTimeout = 30 * time.Second
)
var (
// ErrRequestedInterfaceNotProvided indicates that the requested interface is
// not provided by the SUT because it was not specified as a required
// interface of the abstract SUT.
ErrRequestedInterfaceNotProvided = errors.New("requested interface not provided by the SUT")
)
// interfaceID is the id for an interface.
type interfaceID string
// componentInterface is a typed communication boundary between components. It
// corresponds to the Interface protobuf message, but "interface" cannot be used
// here because it is a reserved word.
type componentInterface interface {
fmt.Stringer
id() interfaceID
}
func newInterface(pb *monaxpb.Interface) componentInterface {
switch pb.WhichType() {
case monaxpb.Interface_Dhcp_case:
return &dhcpInterface{
serviceName: pb.GetDhcp().GetServiceName(),
}
case monaxpb.Interface_Grpc_case:
return &grpcInterface{
serviceName: pb.GetGrpc().GetServiceName(),
}
case monaxpb.Interface_Http_case:
return &httpInterface{
serviceName: pb.GetHttp().GetServiceName(),
}
case monaxpb.Interface_Https_case:
return &httpsInterface{
serviceName: pb.GetHttps().GetServiceName(),
}
default:
log.Fatalf("Unexpected interface type: %v", pb)
panic("unreachable")
}
}
// SUTInterfaces implements methods for retrieving different kinds of
// interfaces from the SUT.
type SUTInterfaces struct {
sut *SUT
grpcConnByServiceName map[string]*grpc.ClientConn
mu sync.Mutex
}
func newSUTInterfaces(sut *SUT) *SUTInterfaces {
return &SUTInterfaces{
sut: sut,
grpcConnByServiceName: make(map[string]*grpc.ClientConn),
}
}
func (i *SUTInterfaces) findComponent(intf componentInterface) (*Component, error) {
component, ok := i.sut.componentsByInterfaceID[intf.id()]
if !ok {
return nil, fmt.Errorf("%w: interface %v", ErrRequestedInterfaceNotProvided, intf)
}
return component, nil
}
func (i *SUTInterfaces) closeCachedInterfaces() {
for _, conn := range i.grpcConnByServiceName {
conn.Close()
}
i.grpcConnByServiceName = make(map[string]*grpc.ClientConn)
}