grpc added get/delete node, create/edit/delete link apis

This commit is contained in:
bharnden 2019-02-26 14:34:52 -08:00
parent 6016152490
commit 460e5c04d0
3 changed files with 378 additions and 9 deletions

View file

@ -6,8 +6,8 @@ import grpc
import core_pb2
import core_pb2_grpc
from core.emulator.emudata import NodeOptions
from core.enumerations import NodeTypes
from core.emulator.emudata import NodeOptions, IpPrefixes, InterfaceData, LinkOptions
from core.enumerations import NodeTypes, LinkTypes, EventTypes
def update_proto(obj, **kwargs):
@ -49,7 +49,7 @@ class CoreApiClient(object):
request.id = _id
return self.stub.GetSessionLocation(request)
def set_session_location(self, _id, x=None, y=None , z=None, lat=None, lon=None, alt=None, scale=None):
def set_session_location(self, _id, x=None, y=None, z=None, lat=None, lon=None, alt=None, scale=None):
request = core_pb2.SetSessionLocationRequest()
request.id = _id
update_proto(request.position, x=x, y=y, z=z, lat=lat, lon=lon, alt=alt)
@ -59,7 +59,7 @@ class CoreApiClient(object):
def set_session_state(self, _id, state):
request = core_pb2.SetSessionStateRequest()
request.id = _id
request.state = state
request.state = state.value
return self.stub.SetSessionState(request)
def create_node(self, session, _type=NodeTypes.DEFAULT, _id=None, node_options=None, emane=None):
@ -89,6 +89,12 @@ class CoreApiClient(object):
request.services.extend(node_options.services)
return self.stub.CreateNode(request)
def get_node(self, session, _id):
request = core_pb2.GetNodeRequest()
request.session = session
request.id = _id
return self.stub.GetNode(request)
def edit_node(self, session, _id, node_options):
request = core_pb2.EditNodeRequest()
request.session = session
@ -103,6 +109,102 @@ class CoreApiClient(object):
)
return self.stub.EditNode(request)
def delete_node(self, session, _id):
request = core_pb2.DeleteNodeRequest()
request.session = session
request.id = _id
return self.stub.DeleteNode(request)
def create_link(self, session, node_one, node_two, interface_one=None, interface_two=None, link_options=None):
request = core_pb2.CreateLinkRequest()
request.session = session
update_proto(
request.link,
node_one=node_one,
node_two=node_two,
type=LinkTypes.WIRED.value
)
if interface_one is not None:
update_proto(
request.link.interface_one,
id=interface_one.id,
name=interface_one.name,
mac=interface_one.mac,
ip4=interface_one.ip4,
ip4mask=interface_one.ip4_mask,
ip6=interface_one.ip6,
ip6mask=interface_one.ip6_mask
)
if interface_two is not None:
update_proto(
request.link.interface_two,
id=interface_two.id,
name=interface_two.name,
mac=interface_two.mac,
ip4=interface_two.ip4,
ip4mask=interface_two.ip4_mask,
ip6=interface_two.ip6,
ip6mask=interface_two.ip6_mask
)
if link_options is not None:
update_proto(
request.link.options,
delay=link_options.delay,
bandwidth=link_options.bandwidth,
per=link_options.per,
dup=link_options.dup,
jitter=link_options.jitter,
mer=link_options.mer,
burst=link_options.burst,
mburst=link_options.mburst,
unidirectional=link_options.unidirectional,
key=link_options.key,
opaque=link_options.opaque
)
return self.stub.CreateLink(request)
def edit_link(self, session, node_one, node_two, link_options, interface_one=None, interface_two=None):
request = core_pb2.EditLinkRequest()
request.session = session
request.node_one = node_one
request.node_two = node_two
update_proto(
request,
interface_one=interface_one,
interface_two=interface_two
)
update_proto(
request.options,
delay=link_options.delay,
bandwidth=link_options.bandwidth,
per=link_options.per,
dup=link_options.dup,
jitter=link_options.jitter,
mer=link_options.mer,
burst=link_options.burst,
mburst=link_options.mburst,
unidirectional=link_options.unidirectional,
key=link_options.key,
opaque=link_options.opaque
)
return self.stub.EditLink(request)
def delete_link(self, session, node_one, node_two, interface_one=None, interface_two=None):
request = core_pb2.DeleteLinkRequest()
request.session = session
request.node_one = node_one
request.node_two = node_two
update_proto(
request,
interface_one=interface_one,
interface_two=interface_two
)
return self.stub.DeleteLink(request)
@contextmanager
def connect(self):
channel = grpc.insecure_channel(self.address)
@ -142,15 +244,40 @@ def main():
print("get location: %s" % client.get_session_location(session_data.id))
# change session state
print("set session state: %s" % client.set_session_state(session_data.id, core_pb2.INSTANTIATION))
print("set session state: %s" % client.set_session_state(session_data.id, EventTypes.CONFIGURATION_STATE))
# create switch node
response = client.create_node(session_data.id, _type=NodeTypes.SWITCH)
print("created switch: %s" % response)
switch_id = response.id
# ip generator for example
prefixes = IpPrefixes(ip4_prefix="10.83.0.0/16")
for i in xrange(2):
response = client.create_node(session_data.id)
print("created node: %s" % response)
node_id = response.id
node_options = NodeOptions()
node_options.x = 5
node_options.y = 5
print("edit node: %s" % client.edit_node(session_data.id, response.id, node_options))
print("edit node: %s" % client.edit_node(session_data.id, node_id, node_options))
print("get node: %s" % client.get_node(session_data.id, node_id))
# create link
interface_one = InterfaceData(
_id=None, name=None, mac=None,
ip4=str(prefixes.ip4.addr(node_id)), ip4_mask=prefixes.ip4.prefixlen,
ip6=None, ip6_mask=None
)
print("created link: %s" % client.create_link(session_data.id, node_id, switch_id, interface_one))
link_options = LinkOptions()
link_options.per = 50
print("edit link: %s" % client.edit_link(
session_data.id, node_id, switch_id, link_options, interface_one=0))
# change session state
print("set session state: %s" % client.set_session_state(session_data.id, EventTypes.INSTANTIATION_STATE))
# get session
print("get session: %s" % client.get_session(session_data.id))

View file

@ -1,7 +1,7 @@
import os
from core.emulator.emudata import NodeOptions
from core.enumerations import NodeTypes, EventTypes
from core.emulator.emudata import NodeOptions, InterfaceData, LinkOptions
from core.enumerations import NodeTypes, EventTypes, LinkTypes
from concurrent import futures
import time
@ -27,7 +27,6 @@ def update_proto(obj, **kwargs):
for key in kwargs:
value = kwargs[key]
if value is not None:
logging.info("setting proto key(%s) value(%s)", key, value)
setattr(obj, key, value)
@ -276,6 +275,52 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
response.id = node.objid
return response
def GetNode(self, request, context):
session = self.coreemu.sessions.get(request.session)
if not session:
raise Exception("no session found")
node = session.get_object(request.id)
if not node:
raise Exception("no node found")
response = core_pb2.GetNodeResponse()
for interface_id, interface in node._netif.iteritems():
net_id = None
if interface.net:
net_id = interface.net.objid
interface_proto = response.interfaces.add()
interface_proto.id = interface_id
interface_proto.netid = net_id
interface_proto.name = interface.name
interface_proto.mac = str(interface.hwaddr)
interface_proto.mtu = interface.mtu
interface_proto.flowid = interface.flow_id
emane_model = None
if nodeutils.is_node(node, NodeTypes.EMANE):
emane_model = node.model.name
update_proto(
response.node,
name=node.name,
type=nodeutils.get_node_type(node.__class__).value,
emane=emane_model,
model=node.type
)
update_proto(
response.node.position,
x=node.position.x,
y=node.position.y,
z=node.position.z,
)
services = [x.name for x in getattr(node, "services", [])]
response.node.services.extend(services)
return response
def EditNode(self, request, context):
session = self.coreemu.sessions.get(request.session)
if not session:
@ -297,6 +342,137 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
response.result = result
return response
def DeleteNode(self, request, context):
logging.info("delete node: %s", request)
session = self.coreemu.sessions.get(request.session)
if not session:
raise Exception("no session found")
response = core_pb2.DeleteNodeResponse()
response.result = session.delete_node(request.id)
return response
def CreateLink(self, request, context):
session = self.coreemu.sessions.get(request.session)
if not session:
raise Exception("no session found")
logging.info("adding link: %s", request)
node_one = request.link.node_one
node_two = request.link.node_two
interface_one = None
interface_one_data = request.link.interface_one
if interface_one_data:
name = interface_one_data.name
if name == "":
name = None
mac = interface_one_data.mac
if mac == "":
mac = None
interface_one = InterfaceData(
_id=interface_one_data.id,
name=name,
mac=mac,
ip4=interface_one_data.ip4,
ip4_mask=interface_one_data.ip4mask,
ip6=interface_one_data.ip6,
ip6_mask=interface_one_data.ip6mask,
)
interface_two = None
interface_two_data = request.link.interface_two
if interface_two_data:
name = interface_two_data.name
if name == "":
name = None
mac = interface_two_data.mac
if mac == "":
mac = None
interface_two = InterfaceData(
_id=interface_two_data.id,
name=name,
mac=mac,
ip4=interface_two_data.ip4,
ip4_mask=interface_two_data.ip4mask,
ip6=interface_two_data.ip6,
ip6_mask=interface_two_data.ip6mask,
)
link_type = None
link_type_value = request.link.type
if link_type_value is not None:
link_type = LinkTypes(link_type_value)
options_data = request.link.options
link_options = LinkOptions(_type=link_type)
if options_data:
link_options.delay = options_data.delay
link_options.bandwidth = options_data.bandwidth
link_options.per = options_data.per
link_options.dup = options_data.dup
link_options.jitter = options_data.jitter
link_options.mer = options_data.mer
link_options.burst = options_data.burst
link_options.mburst = options_data.mburst
link_options.unidirectional = options_data.unidirectional
link_options.key = options_data.key
link_options.opaque = options_data.opaque
session.add_link(node_one, node_two, interface_one, interface_two, link_options=link_options)
response = core_pb2.CreateLinkResponse()
response.result = True
return response
def EditLink(self, request, context):
logging.info("edit link: %s", request)
session = self.coreemu.sessions.get(request.session)
if not session:
raise Exception("no session found")
node_one = request.node_one
node_two = request.node_two
interface_one_id = request.interface_one
interface_two_id = request.interface_two
options_data = request.options
link_options = LinkOptions()
link_options.delay = options_data.delay
link_options.bandwidth = options_data.bandwidth
link_options.per = options_data.per
link_options.dup = options_data.dup
link_options.jitter = options_data.jitter
link_options.mer = options_data.mer
link_options.burst = options_data.burst
link_options.mburst = options_data.mburst
link_options.unidirectional = options_data.unidirectional
link_options.key = options_data.key
link_options.opaque = options_data.opaque
session.update_link(node_one, node_two, interface_one_id, interface_two_id, link_options)
response = core_pb2.EditLinkResponse()
response.result = True
return response
def DeleteLink(self, request, context):
logging.info("delete link: %s", request)
session = self.coreemu.sessions.get(request.session)
if not session:
raise Exception("no session found")
node_one = request.node_one
node_two = request.node_two
interface_one = request.interface_one
interface_two = request.interface_two
session.delete_link(node_one, node_two, interface_one, interface_two)
response = core_pb2.DeleteLinkResponse()
response.result = True
return response
def listen(coreemu, address="[::]:50051"):
logging.info("starting grpc api: %s", address)

View file

@ -21,8 +21,18 @@ service CoreApi {
}
rpc CreateNode (CreateNodeRequest) returns (CreateNodeResponse) {
}
rpc GetNode (GetNodeRequest) returns (GetNodeResponse) {
}
rpc EditNode (EditNodeRequest) returns (EditNodeResponse) {
}
rpc DeleteNode (DeleteNodeRequest) returns (DeleteNodeResponse) {
}
rpc CreateLink (CreateLinkRequest) returns (CreateLinkResponse) {
}
rpc EditLink (EditLinkRequest) returns (EditLinkResponse) {
}
rpc DeleteLink (DeleteLinkRequest) returns (DeleteLinkResponse) {
}
}
// rpc request/response messages
@ -112,6 +122,16 @@ message CreateNodeResponse {
int32 id = 1;
}
message GetNodeRequest {
int32 session = 1;
int32 id = 2;
}
message GetNodeResponse {
Node node = 1;
repeated Interface interfaces = 2;
}
message EditNodeRequest {
int32 session = 1;
int32 id = 2;
@ -122,6 +142,49 @@ message EditNodeResponse {
bool result = 1;
}
message DeleteNodeRequest {
int32 session = 1;
int32 id = 2;
}
message DeleteNodeResponse {
bool result = 1;
}
message CreateLinkRequest {
int32 session = 1;
Link link = 2;
}
message CreateLinkResponse {
bool result = 1;
}
message EditLinkRequest {
int32 session = 1;
int32 node_one = 2;
int32 node_two = 3;
int32 interface_one = 4;
int32 interface_two = 5;
LinkOptions options = 6;
}
message EditLinkResponse {
bool result = 1;
}
message DeleteLinkRequest {
int32 session = 1;
int32 node_one = 2;
int32 node_two = 3;
int32 interface_one = 4;
int32 interface_two = 5;
}
message DeleteLinkResponse {
bool result = 1;
}
// data structures for messages below
enum SessionState {
NONE = 0;
@ -210,6 +273,9 @@ message Interface {
int32 ip4mask = 5;
string ip6 = 6;
int32 ip6mask = 7;
int32 netid = 8;
int32 flowid = 9;
int32 mtu = 10;
}
message Position {