grpc: removed old client, refactored wrapped client to be the new standard client, removed old client tests, updated documentation

This commit is contained in:
Blake Harnden 2021-05-04 13:29:22 -07:00
parent 924e86da2b
commit 15acdaa40f
14 changed files with 698 additions and 3008 deletions

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -12,7 +12,7 @@ from typing import TYPE_CHECKING, Dict, Iterable, List, Optional, Set, Tuple
import grpc
from core.api.grpc import clientw, configservices_pb2, core_pb2
from core.api.grpc import client, configservices_pb2, core_pb2
from core.api.grpc.wrappers import (
ConfigOption,
ConfigService,
@ -66,7 +66,7 @@ class CoreClient:
"""
self.app: "Application" = app
self.master: tk.Tk = app.master
self._client: clientw.CoreGrpcClient = clientw.CoreGrpcClient(proxy=proxy)
self._client: client.CoreGrpcClient = client.CoreGrpcClient(proxy=proxy)
self.session: Optional[Session] = None
self.user = getpass.getuser()
@ -95,7 +95,7 @@ class CoreClient:
self.handling_events: Optional[grpc.Future] = None
@property
def client(self) -> clientw.CoreGrpcClient:
def client(self) -> client.CoreGrpcClient:
if self.session:
if not self._client.check_session(self.session.id):
throughputs_enabled = self.handling_throughputs is not None

View file

@ -1,7 +1,7 @@
import argparse
import logging
from core.api.grpc import clientw
from core.api.grpc import client
from core.api.grpc.wrappers import NodeType, Position
@ -11,10 +11,10 @@ def log_event(event):
def main(args):
# helper to create interfaces
interface_helper = clientw.InterfaceHelper(ip4_prefix="10.83.0.0/16")
interface_helper = client.InterfaceHelper(ip4_prefix="10.83.0.0/16")
# create grpc client and connect
core = clientw.CoreGrpcClient()
core = client.CoreGrpcClient()
core.connect()
# create session

View file

@ -1,13 +1,13 @@
# required imports
from core.api.grpc import clientw
from core.api.grpc import client
from core.api.grpc.wrappers import NodeType, Position
from core.emane.ieee80211abg import EmaneIeee80211abgModel
# interface helper
iface_helper = clientw.InterfaceHelper(ip4_prefix="10.0.0.0/24", ip6_prefix="2001::/64")
iface_helper = client.InterfaceHelper(ip4_prefix="10.0.0.0/24", ip6_prefix="2001::/64")
# create grpc client and connect
core = clientw.CoreGrpcClient()
core = client.CoreGrpcClient()
core.connect()
# add session

View file

@ -1,11 +1,11 @@
from core.api.grpc import clientw
from core.api.grpc import client
from core.api.grpc.wrappers import Position
# interface helper
iface_helper = clientw.InterfaceHelper(ip4_prefix="10.0.0.0/24", ip6_prefix="2001::/64")
iface_helper = client.InterfaceHelper(ip4_prefix="10.0.0.0/24", ip6_prefix="2001::/64")
# create grpc client and connect
core = clientw.CoreGrpcClient()
core = client.CoreGrpcClient()
core.connect()
# add session

View file

@ -1,11 +1,11 @@
from core.api.grpc import clientw
from core.api.grpc import client
from core.api.grpc.wrappers import NodeType, Position
# interface helper
iface_helper = clientw.InterfaceHelper(ip4_prefix="10.0.0.0/24", ip6_prefix="2001::/64")
iface_helper = client.InterfaceHelper(ip4_prefix="10.0.0.0/24", ip6_prefix="2001::/64")
# create grpc client and connect
core = clientw.CoreGrpcClient()
core = client.CoreGrpcClient()
core.connect()
# add session

View file

@ -1,11 +1,11 @@
from core.api.grpc import clientw
from core.api.grpc import client
from core.api.grpc.wrappers import NodeType, Position
# interface helper
iface_helper = clientw.InterfaceHelper(ip4_prefix="10.0.0.0/24", ip6_prefix="2001::/64")
iface_helper = client.InterfaceHelper(ip4_prefix="10.0.0.0/24", ip6_prefix="2001::/64")
# create grpc client and connect
core = clientw.CoreGrpcClient()
core = client.CoreGrpcClient()
core.connect()
# add session

View file

@ -15,7 +15,7 @@ import grpc
import netaddr
from netaddr import EUI, AddrFormatError, IPNetwork
from core.api.grpc.clientw import CoreGrpcClient
from core.api.grpc.client import CoreGrpcClient
from core.api.grpc.wrappers import (
Geo,
Interface,

View file

@ -5,7 +5,7 @@ import sys
from pathlib import Path
from core import utils
from core.api.grpc.clientw import CoreGrpcClient
from core.api.grpc.client import CoreGrpcClient
from core.errors import CoreCommandError
if __name__ == "__main__":

View file

@ -15,7 +15,7 @@ from typing import Dict, Tuple
import grpc
from core import utils
from core.api.grpc.clientw import CoreGrpcClient
from core.api.grpc.client import CoreGrpcClient
from core.api.grpc.wrappers import NodeType
SDT_HOST = "127.0.0.1"

File diff suppressed because it is too large Load diff

View file

@ -9,7 +9,7 @@ import pytest
from mock import patch
from core.api.grpc import core_pb2
from core.api.grpc.clientw import CoreGrpcClient, InterfaceHelper, MoveNodesStreamer
from core.api.grpc.client import CoreGrpcClient, InterfaceHelper, MoveNodesStreamer
from core.api.grpc.server import CoreGrpcServer
from core.api.grpc.wrappers import (
ConfigOption,

View file

@ -53,15 +53,15 @@ and the services they map to.
There is an interface helper class that can be leveraged for convenience
when creating interface data for nodes. Alternatively one can manually create
a `core.api.grpc.core_pb2.Interface` class instead with appropriate information.
a `core.api.grpc.wrappers.Interface` class instead with appropriate information.
Manually creating gRPC interface data:
Manually creating gRPC client interface:
```python
from core.api.grpc import core_pb2
from core.api.grpc.wrappers import Interface
# id is optional and will set to the next available id
# name is optional and will default to eth<id>
# mac is optional and will result in a randomly generated mac
iface_data = core_pb2.Interface(
iface = Interface(
id=0,
name="eth0",
ip4="10.0.0.1",
@ -98,16 +98,24 @@ Event types:
* file - file events when the legacy gui joins a session
```python
from core.api.grpc import core_pb2
from core.api.grpc import client
from core.api.grpc.wrappers import EventType
def event_listener(event):
print(event)
# create grpc client and connect
core = client.CoreGrpcClient()
core.connect()
# add session
session = core.add_session()
# provide no events to listen to all events
core.events(session_id, event_listener)
core.events(session.id, event_listener)
# provide events to listen to specific events
core.events(session_id, event_listener, [core_pb2.EventType.NODE])
core.events(session.id, event_listener, [EventType.NODE])
```
### Configuring Links
@ -122,27 +130,47 @@ Currently supported configuration options:
* loss (%)
```python
from core.api.grpc import core_pb2
from core.api.grpc import client
from core.api.grpc.wrappers import LinkOptions, Position
# interface helper
iface_helper = client.InterfaceHelper(ip4_prefix="10.0.0.0/24", ip6_prefix="2001::/64")
# create grpc client and connect
core = client.CoreGrpcClient()
core.connect()
# add session
session = core.add_session()
# create nodes
position = Position(x=100, y=100)
node1 = session.add_node(1, position=position)
position = Position(x=300, y=100)
node2 = session.add_node(2, position=position)
# configuring when creating a link
options = core_pb2.LinkOptions(
options = LinkOptions(
bandwidth=54_000_000,
delay=5000,
dup=5,
loss=5.5,
jitter=0,
)
core.add_link(session_id, n1_id, n2_id, iface1_data, iface2_data, options)
iface1 = iface_helper.create_iface(node1.id, 0)
iface2 = iface_helper.create_iface(node2.id, 0)
link = session.add_link(node1=node1, node2=node2, iface1=iface1, iface2=iface2)
# configuring during runtime
core.edit_link(session_id, n1_id, n2_id, iface1_id, iface2_id, options)
link.options.loss = 10.0
core.edit_link(session.id, link)
```
### Peer to Peer Example
```python
# required imports
from core.api.grpc import client
from core.api.grpc.core_pb2 import Node, NodeType, Position, SessionState
from core.api.grpc.core_pb2 import Position
# interface helper
iface_helper = client.InterfaceHelper(ip4_prefix="10.0.0.0/24", ip6_prefix="2001::/64")
@ -151,39 +179,29 @@ iface_helper = client.InterfaceHelper(ip4_prefix="10.0.0.0/24", ip6_prefix="2001
core = client.CoreGrpcClient()
core.connect()
# create session and get id
response = core.create_session()
session_id = response.session_id
# add session
session = core.add_session()
# change session state to configuration so that nodes get started when added
core.set_session_state(session_id, SessionState.CONFIGURATION)
# create node one
# create nodes
position = Position(x=100, y=100)
n1 = Node(type=NodeType.DEFAULT, position=position, model="PC")
response = core.add_node(session_id, n1)
n1_id = response.node_id
# create node two
node1 = session.add_node(1, position=position)
position = Position(x=300, y=100)
n2 = Node(type=NodeType.DEFAULT, position=position, model="PC")
response = core.add_node(session_id, n2)
n2_id = response.node_id
node2 = session.add_node(2, position=position)
# links nodes together
iface1 = iface_helper.create_iface(n1_id, 0)
iface2 = iface_helper.create_iface(n2_id, 0)
core.add_link(session_id, n1_id, n2_id, iface1, iface2)
# create link
iface1 = iface_helper.create_iface(node1.id, 0)
iface2 = iface_helper.create_iface(node2.id, 0)
session.add_link(node1=node1, node2=node2, iface1=iface1, iface2=iface2)
# change session state
core.set_session_state(session_id, SessionState.INSTANTIATION)
# start session
core.start_session(session)
```
### Switch/Hub Example
```python
# required imports
from core.api.grpc import client
from core.api.grpc.core_pb2 import Node, NodeType, Position, SessionState
from core.api.grpc.core_pb2 import NodeType, Position
# interface helper
iface_helper = client.InterfaceHelper(ip4_prefix="10.0.0.0/24", ip6_prefix="2001::/64")
@ -192,46 +210,32 @@ iface_helper = client.InterfaceHelper(ip4_prefix="10.0.0.0/24", ip6_prefix="2001
core = client.CoreGrpcClient()
core.connect()
# create session and get id
response = core.create_session()
session_id = response.session_id
# add session
session = core.add_session()
# change session state to configuration so that nodes get started when added
core.set_session_state(session_id, SessionState.CONFIGURATION)
# create switch node
# create nodes
position = Position(x=200, y=200)
switch = Node(type=NodeType.SWITCH, position=position)
response = core.add_node(session_id, switch)
switch_id = response.node_id
# create node one
switch = session.add_node(1, _type=NodeType.SWITCH, position=position)
position = Position(x=100, y=100)
n1 = Node(type=NodeType.DEFAULT, position=position, model="PC")
response = core.add_node(session_id, n1)
n1_id = response.node_id
# create node two
node1 = session.add_node(2, position=position)
position = Position(x=300, y=100)
n2 = Node(type=NodeType.DEFAULT, position=position, model="PC")
response = core.add_node(session_id, n2)
n2_id = response.node_id
node2 = session.add_node(3, position=position)
# links nodes to switch
iface1 = iface_helper.create_iface(n1_id, 0)
core.add_link(session_id, n1_id, switch_id, iface1)
iface1 = iface_helper.create_iface(n2_id, 0)
core.add_link(session_id, n2_id, switch_id, iface1)
# create links
iface1 = iface_helper.create_iface(node1.id, 0)
session.add_link(node1=node1, node2=switch, iface1=iface1)
iface1 = iface_helper.create_iface(node2.id, 0)
session.add_link(node1=node2, node2=switch, iface1=iface1)
# change session state
core.set_session_state(session_id, SessionState.INSTANTIATION)
# start session
core.start_session(session)
```
### WLAN Example
```python
# required imports
from core.api.grpc import client
from core.api.grpc.core_pb2 import Node, NodeType, Position, SessionState
from core.api.grpc.core_pb2 import NodeType, Position
# interface helper
iface_helper = client.InterfaceHelper(ip4_prefix="10.0.0.0/24", ip6_prefix="2001::/64")
@ -240,49 +244,37 @@ iface_helper = client.InterfaceHelper(ip4_prefix="10.0.0.0/24", ip6_prefix="2001
core = client.CoreGrpcClient()
core.connect()
# create session and get id
response = core.create_session()
session_id = response.session_id
# add session
session = core.add_session()
# change session state to configuration so that nodes get started when added
core.set_session_state(session_id, SessionState.CONFIGURATION)
# create wlan node
# create nodes
position = Position(x=200, y=200)
wlan = Node(type=NodeType.WIRELESS_LAN, position=position)
response = core.add_node(session_id, wlan)
wlan_id = response.node_id
# create node one
wlan = session.add_node(1, _type=NodeType.WIRELESS_LAN, position=position)
position = Position(x=100, y=100)
n1 = Node(type=NodeType.DEFAULT, position=position, model="mdr")
response = core.add_node(session_id, n1)
n1_id = response.node_id
# create node two
node1 = session.add_node(2, model="mdr", position=position)
position = Position(x=300, y=100)
n2 = Node(type=NodeType.DEFAULT, position=position, model="mdr")
response = core.add_node(session_id, n2)
n2_id = response.node_id
node2 = session.add_node(3, model="mdr", position=position)
# configure wlan using a dict mapping currently
# create links
iface1 = iface_helper.create_iface(node1.id, 0)
session.add_link(node1=node1, node2=wlan, iface1=iface1)
iface1 = iface_helper.create_iface(node2.id, 0)
session.add_link(node1=node2, node2=wlan, iface1=iface1)
# set wlan config using a dict mapping currently
# support values as strings
core.set_wlan_config(session_id, wlan_id, {
"range": "280",
"bandwidth": "55000000",
"delay": "6000",
"jitter": "5",
"error": "5",
})
wlan.set_wlan(
{
"range": "280",
"bandwidth": "55000000",
"delay": "6000",
"jitter": "5",
"error": "5",
}
)
# links nodes to wlan
iface1 = iface_helper.create_iface(n1_id, 0)
core.add_link(session_id, n1_id, wlan_id, iface1)
iface1 = iface_helper.create_iface(n2_id, 0)
core.add_link(session_id, n2_id, wlan_id, iface1)
# change session state
core.set_session_state(session_id, SessionState.INSTANTIATION)
# start session
core.start_session(session)
```
### EMANE Example
@ -307,7 +299,7 @@ will use the defaults. When no configuration is used, the defaults are used.
```python
# required imports
from core.api.grpc import client
from core.api.grpc.core_pb2 import Node, NodeType, Position, SessionState
from core.api.grpc.core_pb2 import NodeType, Position
from core.emane.ieee80211abg import EmaneIeee80211abgModel
# interface helper
@ -317,68 +309,45 @@ iface_helper = client.InterfaceHelper(ip4_prefix="10.0.0.0/24", ip6_prefix="2001
core = client.CoreGrpcClient()
core.connect()
# create session and get id
response = core.create_session()
session_id = response.session_id
# add session
session = core.add_session()
# change session state to configuration so that nodes get started when added
core.set_session_state(session_id, SessionState.CONFIGURATION)
# create emane node
# create nodes
position = Position(x=200, y=200)
emane = Node(type=NodeType.EMANE, position=position, emane=EmaneIeee80211abgModel.name)
response = core.add_node(session_id, emane)
emane_id = response.node_id
# create node one
emane = session.add_node(
1, _type=NodeType.EMANE, position=position, emane=EmaneIeee80211abgModel.name
)
position = Position(x=100, y=100)
n1 = Node(type=NodeType.DEFAULT, position=position, model="mdr")
response = core.add_node(session_id, n1)
n1_id = response.node_id
# create node two
node1 = session.add_node(2, model="mdr", position=position)
position = Position(x=300, y=100)
n2 = Node(type=NodeType.DEFAULT, position=position, model="mdr")
response = core.add_node(session_id, n2)
n2_id = response.node_id
node2 = session.add_node(3, model="mdr", position=position)
# configure general emane settings
core.set_emane_config(session_id, {
"eventservicettl": "2"
})
# create links
iface1 = iface_helper.create_iface(node1.id, 0)
session.add_link(node1=node1, node2=emane, iface1=iface1)
iface1 = iface_helper.create_iface(node2.id, 0)
session.add_link(node1=node2, node2=emane, iface1=iface1)
# configure emane model settings
# using a dict mapping currently support values as strings
core.set_emane_model_config(session_id, emane_id, EmaneIeee80211abgModel.name, {
"unicastrate": "3",
})
# setting global emane configuration
session.set_emane({"eventservicettl": "2"})
# setting emane specific emane model configuration
emane.set_emane_model(EmaneIeee80211abgModel.name, {"unicastrate": "3"})
# links nodes to emane
iface1 = iface_helper.create_iface(n1_id, 0)
core.add_link(session_id, n1_id, emane_id, iface1)
iface1 = iface_helper.create_iface(n2_id, 0)
core.add_link(session_id, n2_id, emane_id, iface1)
# change session state
core.set_session_state(session_id, SessionState.INSTANTIATION)
# start session
core.start_session(session)
```
EMANE Model Configuration:
```python
# emane network specific config
core.set_emane_model_config(session_id, emane_id, EmaneIeee80211abgModel.name, {
"unicastrate": "3",
})
# emane network specific config, set on an emane node
# this setting applies to all nodes connected
emane.set_emane_model(EmaneIeee80211abgModel.name, {"unicastrate": "3"})
# node specific config
core.set_emane_model_config(session_id, node_id, EmaneIeee80211abgModel.name, {
"unicastrate": "3",
})
# node specific config for an individual node connected to an emane network
node.set_emane_model(EmaneIeee80211abgModel.name, {"unicastrate": "3"})
# node interface specific config
core.set_emane_model_config(session_id, node_id, EmaneIeee80211abgModel.name, {
"unicastrate": "3",
}, iface_id)
# node interface specific config for an individual node connected to an emane network
node.set_emane_model(EmaneIeee80211abgModel.name, {"unicastrate": "3"}, iface_id=0)
```
## Configuring a Service
@ -398,11 +367,8 @@ The following features can be configured for a service:
Editing service properties:
```python
# configure a service, for a node, for a given session
core.set_node_service(
session_id,
node_id,
service_name,
files=["file1.sh", "file2.sh"],
node.service_configs[service_name] = NodeServiceData(
configs=["file1.sh", "file2.sh"],
directories=["/etc/node"],
startup=["bash file1.sh"],
validate=[],
@ -417,13 +383,8 @@ Editing a service file:
```python
# to edit the contents of a generated file you can specify
# the service, the file name, and its contents
core.set_node_service_file(
session_id,
node_id,
service_name,
file_name,
"echo hello",
)
file_configs = node.service_file_configs.setdefault(service_name, {})
file_configs[file_name] = "echo hello world"
```
## File Examples