 Welcome to the network engineering video blog. I am your host, Michael Crane. In this first half of our two part video on config file open, we will be using the reflection property info for our new and improved config file save subroutine. Reflections get type dot get properties allows us to get all the properties for our node classes and our link classes without having to request them by name. This is a must have for large and dynamic configs. Let me tell you. Also some changes are needed to align the config save with the new config open. We will introduce a new IO module that will be the home of our new config file save and config file open subroutines. And you will see the code refactoring on the main window xaml menu system. It's code behind and noting link class properties. It's a lot. So let's get started. So in the last video, if you remember, we were implementing the file save and file save as. So in this video, we are going to implement the opposite of that or I don't know if it's opposite but the fruits of our labor with the file open. And we're going to get our config file which is a text file and open it. And I'm sorry it's running a little bit slow. It's my screen capture software that's making it kind of chug along. So you'll just have to bear with me. So first let's go over some code refactoring I did. And if you're not familiar with the term refactoring, Wikipedia defines it as the process of cleaning up code to make it more readable and logical without really changing its functionality. It's just a fancy term for housekeeping. So what did I change here? Let me get this guy out of the way. Okay, so the first thing I did is I changed our link class that was just, you know, link to class link, all right. And that kind of matched up with the class node, which is the base class for our nodes. So yeah, maybe in the future, this will become a base class for the links as well. So I thought I'd go ahead and name them all class link and to keep them the same, right. And I thought about calling them base link and base node, but I don't know. Well, we'll see the class class node and class link make make sense to me. Okay. Oh, I also I changed the name of Xena test set node user control to Xena test set test set node. So instead of Xena test node is Xena test set node dot XAML. And the reason for that is in the future, I'm probably going to have a generic test set node, right. That won't be a specifically a Xena or some other brand. It'll be generic, maybe like a Linux box type thing, right. So it's kind of like, so where we have the Cisco Cisco router node and Cisco switch node, we have the Xena test set node, right. And in the future, we'll probably have a generic router node, you know, just any no name router and a generic test set. Okay. And that's kind of why I was renaming them. So though they're refactoring I did or code cleanup, whatever you want to call it. I'll show you as we go along. There's just a few other things. The file open, we're going to start and I made quite a few changes, I guess. So so you just have to bear with me. I'm sorry. If you're following along and downloading this code is going to this version is going to change quite a bit, just besides the file names, right. Okay. So to start with our main class, our main window dot XAML, I changed our context menu from list box items to menu items. It kind of made more sense to me and it also was easier for me to add sub menu items. And I'll show you that here real quick. So if I right click on here, you can see I remember we had the add router in the past, but now we have sub menu items. So we have add router or Cisco or Juniper, add switch, Cisco or a metro ethernet switch, which is kind of generic, I guess. And then we have a add test set, a Zena or Linux workstation, a lot of Unix boxes or Linux boxes now are being used as test sets, right. Actually being used as routers too, for that matter. And the last thing I added was this add link. Now you can add an ethernet link or a fiber link, right. So I just added these sub menus to our main GUI here, right. Okay, so that's about it for XAML, everything else is about this, it's pretty much the same. Let's see here, and you can see some of the name changes are off in here. I'm not going to go over that, we already talked about that. So let's see, what else we got here? Oh, so the next thing we want to look at is the visual basic side of our WPF user control here, what Microsoft calls the code behind. So I'm just going to start from the top. The first thing you'll notice is I removed system IO and the system windows control primitives. I don't remember exactly what I was using that for, it might have just popped in there automatically by Visual Studio, I don't remember. Anyway, this right here, I did purposely move, and that got moved into a new file, we'll talk about here in a minute. Actually, this check directory, as you can see, I created a verify create directory and moved this functionality into that new file as well, and we'll look at that new file here in a minute. Okay, so let's keep going down here. This open file, of course, has a bunch of new code in it, that's the point of this whole video, right? And we're going to talk about that here in a minute, I'm going to come back to this, okay? I'm just going to show you the other changes real quick. Save testbed, okay? That got moved into this new IO file that I'm going to show you, the same place, this create directory is, I have a module now that we'll look at here in a minute. And this has also got changed, but we'll look at that here in a minute. One of the biggest, besides the file open changes in the code behind for the main window, it used to have just this add router, which has basically added a Cisco, you can see right here, a Cisco router, and I split it up into add Cisco router, add Juniper router, a generic add router. And so now when you click on the menu item, add Cisco router, add Juniper router, it calls either, you know, the subroutine or the Cisco subroutine just basically calls the subroutine add router and then passes what kind of router it is, right? This add router function, or subroutine, sorry. And so you can see it's a router, it's a class node and the make of the router. And this right here, it's pretty much the same as it was before the add router, except you'll notice, it's got this property object in there where it sets the, instead of just setting it right into the class node, it's setting it into this property object. You can see it's class node dot prop, right? And we'll look at those more here in a minute, okay? So it's just going to add the index, what category it is. And by the way, I changed the name, this used to be name type. If you look right here, node type, I didn't really want to call it node type anymore. I'd rather have it index and type and make and instead of adding node to everything, right? But type is a keyword and visual basic. Yes, I can overwrite it, but it's going to get too confusing and wasn't very clean in my opinion. So I just renamed type to category. So now instead of type router, it's a category router, okay? And the name is the same. It's just a category plus index number and then the make. This is something for in the future, the host name. Yeah, I'm not implementing host name yet because I'm still thinking about how I'm going to do this, all right? And then the rest of it's pretty much the same, okay? And the same, I did the same thing with the Cisco switch or the switches instead of ad switch, yeah, here we go. So here's instead of a generic ad switch, now it's ad Cisco switch. Oops, where are we? Sorry, I keep jumping around. Hey, I moved this get, get available node index and let's get available link index to the bottom of the file. So I could work on these ad switches and add routers all grouped together and it screws up my my diff here. So that's why I keep scrolling around. Anyway, we'll just look at the right side. Anyway, the switch is the same. So it's ad Cisco switch, ad Metro switch, which just basically calls ad switch somewhere. Here it is right here, okay? And it's kind of the same thing as the ad nodes. So we're not going to go over that. The ad Zena test set and ad Linux test set exact same thing. You just have a generic ad test set and they all have properties. It's all pretty much the same, right? And here's ad ether link and oh, yeah, the ad ethernet link and the ad fiber link just calls the generic ad link and passes in its category. So let's see what else we got in here. Oh, I did add a delete all node links. And you'll see why you're in a minute. That's not a that's that's nothing really. It's just going around deleting all the node links. I think a couple sub routines call it. All right, and here's the two subs I moved. Okay, so here's the sub routine open file. You can see most of it's the same. First, we need to delete all the nodes. And the reason why I'm doing this is if you open a configuration file and you already have a configuration in the display, it's basically going to wipe out the whole display clean so it can open and populate the new config, right? Then even nodes and links, all right? And that's pretty simple and and so that delete all links. When you delete this node, when you call this delete node, it just calls that delete all links new sub routine I added, all right? It's pretty straightforward. Then we have the open test bed and that is also in this new file. And I'll show you this new file. It's right here. It's called, it's a module and it's for IO, all right? And we'll look at that here in just a second anyway. That's where this open test bed is stored. That's also where the save test bed is now lives, okay? Open test bed returns a Boolean if it's successful. So if it finds at least one node, it's going to return a success that adds it to a collection of class nodes or a collection of nodes. And so then it's just going to go through all the nodes in the collection and add them to the canvas, which is pretty much the way it was before. It's pretty much the same way as with add node. And let's see. So it's just children add, you've seen that before. And we've got this set location. So once you open the node, then you got to put it in the right spot in the canvas, right? So this moves the node to the right spot. And we'll look at this when we look at the class node here in a minute. And then set the index you've seen before. Okay, so once it gets all the nodes on the canvas and then it just goes through the collection of links and does the same thing. It just says canvas children add class link and set the end points and it goes and gets the end points from its properties, right? And if you remember, the node A and node B in class link was actually being stored as an object, right? A node object, class node object. And now we have to store it as a string. I just add a little dialogue box right here. So if this is not successful, it's going to pop up this dialogue so it's config files, zero nodes, you want to open another file, right? Now let's look at this module IO. And this is a new file. You can see it's got the system IOs where I moved all the IO for the project. We also are using reflection, all right? Okay, and we'll look at where we're using reflection here in a sec. I'm also, this has a little private class in here. And this is something I forget where I learned how to do this, but I've read it somewhere and this works pretty good. I've used this in the past. It's a little class and it's got two properties there. And you know, so properties and not class members or members, I guess. And the reason why is because I want to be able to call get and set on these, on this key value. So this is basically a key value pair. That's what KVP stands for, key value pair. That's all I store in them is key value pairs, right? If you're wondering about, if you're not familiar with properties, properties in Visual Basic, so you can see right here, the following code example shows the equivalent code for the previous auto-implemented property example. So this is what I put in my code right here, the key value. And this is the equivalent code for that, right? Just so you know. Probably you see C sharp people. I think you have to still put this in. I don't really know. Okay, so enough about that little guy. I think first I'm going to show you the, before we get into Save Testbed, I want to show you the, I'm sorry, Open Testbed. I want to show you the Save Testbed, all right? Because I made some changes here from the previous video that you should know. So it's basically the same. So we're going through the nodes. So this is going to go through each node that's in class node. Now you have to remember class node is parent class of all the router nodes and test set nodes and that sort of thing, right? And switches and so we're going to use this property info and we're going to get all the properties out of the property object, which I'll show you that class here in a minute. And then we're going to get all the properties out of that. Okay. And so this make and type, we can just get it right out of the, that same class, node.propertymake and node.propertycategory. Remember I renamed type. For example, and we'll look at a file here in a minute. Maybe I can just pull it up now. It might make more sense if we're looking at it at the same time. If you look at it, so we got Cisco router start. And that's what this is doing. So it's a make Cisco and type. So Cisco router. And it's just depending this underscore start to it. Okay. And it's going to write this to the file. So the first, first line is Cisco router start. Okay. If that's what it is, of course. And then it's going to write the name of it. So here's this name name equals router underscore zero. And then it's going to loop through each element or each info element inside this property info object and basically write it to this file. So it's just going to loop through this properties file. And this is what's all in the properties file. And it's going to do that until, oh yeah. So it's going to loop through each one of these until it gets to the end of this property info object. And then it's just going to write end. All right. So let's go router end. Which is the make end and type under appended with underscore in. Okay. So let's go take a look at this property class. Okay. Where this reflection is get properties where it's getting its data from. Right. And so it's in class node somewhere. Oh, sorry. It's not in class node, but we can look at this real quick. You can see right here. So remember in the old one right here, we had public node type as node node index. We only had really two properties inside this. And they weren't even popular. They're just class members. And now we've got a property called prop, which is a class node properties object. Okay. And let's go look at the class node properties object. This guy right here. And this is it. It's basically a little class that has one, two, three, four, five, six properties in it and a future one. And we come back to this mod IO guy. That's all this guy is doing is he's getting all the properties out of that node properties class. All right. Just getting all this. Now you might be asking yourself, why didn't I just get it right out of the class node? All right. Why didn't I just put all those properties in here? And I tried that actually. And the problem is, is when you call this get properties, let's get type get properties, it finds everything in the class. It had to be 300 entries that it returned in this object. I mean, it had everything in there that for everything. And that was a problem. And I went to the document and there's ways to filter this. Okay. I found it. I was on the wrong, wrong web page. So there it is. It's type dot get properties method. So if you look at it, it's get type dot get properties. Right. So here it is in Microsoft's documentation. And here's an example of it. We've been looking at, but one of the things I wanted to show you is there's ways to, oops, filter it. So they get for the get properties binding floods. So if you only want to retrieve certain kinds of properties, and so you can say instance or static or only public properties or non-public properties or flatten hierarchy, blah, blah, blah. And this did not work at all. I screwed around with this for probably two or three hours. And it just doesn't work. And the only thing I can think of is because this is not a normal class. This class node is not a normal class. It's inheriting user control, which has all kinds of XML junk associated with it. And then who knows what that thing is getting when you do the get properties. I mean, it's literally just hundreds of objects that it finds. It was unusable, believe me. So anyway, that's the reason for me making this class node properties. So I could use that function to get all these properties out of this class. And so with that, I had to modify the class node class. And we'll look at that here in a minute. All right, so let's see. Wow, that was a... We really got diverted there, didn't we? See mod.io. Oh yeah, we're still saving the test bed. Okay, so... And the reason why I had to save this off as a make and type or make and category is when we go to open it, I had to know what kind of object to instantiate. So in other words, I had to know what the child class was going to be. See, I needed to know if it was a Cisco router node or a Cisco switch node or a Juniper node. I had to know what the child node was going to be. And the only way I could think of to do it, and you'll see why I did it this way, is just to put what class type it is at the very start. And then I just used it at the end. All right, that's why I added this make and type to the start. All right, we're just going through that, you know, each object I already talked about this, this property info collected, and writing it to the file, and then writing end. And basically, it's the exact same thing for the link. So I'm not going to go over that because the exact same thing is I was doing for the nodes, right? Don't forget, you can support the network engineering video blog by donation using a credit card and PayPal, or by purchasing products at the Muxall store. Details and links are in the description under this video. Well, that's it for this video. If you liked the video, give it a big thumbs up, that helps, and hit the subscribe button. That really helps. If you have any questions or comments, post them in the comments under this video. Thanks for watching, and I'll see you next time.