|
| 1 | +"""Example of a DiffSync adapter implementation using new helper methods. |
| 2 | +
|
| 3 | +Copyright (c) 2021 Network To Code, LLC <[email protected]> |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +""" |
| 17 | + |
| 18 | +from models import Site, Device, Interface |
| 19 | +from diffsync import DiffSync |
| 20 | + |
| 21 | +BACKEND_DATA_A = [ |
| 22 | + { |
| 23 | + "name": "nyc-spine1", |
| 24 | + "role": "spine", |
| 25 | + "interfaces": {"eth0": "Interface 0", "eth1": "Interface 1"}, |
| 26 | + "site": "nyc", |
| 27 | + }, |
| 28 | + { |
| 29 | + "name": "nyc-spine2", |
| 30 | + "role": "spine", |
| 31 | + "interfaces": {"eth0": "Interface 0", "eth1": "Interface 1"}, |
| 32 | + "site": "nyc", |
| 33 | + }, |
| 34 | + { |
| 35 | + "name": "sfo-spine1", |
| 36 | + "role": "spine", |
| 37 | + "interfaces": {"eth0": "Interface 0", "eth1": "Interface 1"}, |
| 38 | + "site": "sfo", |
| 39 | + }, |
| 40 | + { |
| 41 | + "name": "sfo-spine2", |
| 42 | + "role": "spine", |
| 43 | + "interfaces": {"eth0": "TBD", "eth1": "ddd", "eth2": "Interface 2"}, |
| 44 | + "site": "sfo", |
| 45 | + }, |
| 46 | +] |
| 47 | +BACKEND_DATA_B = [ |
| 48 | + { |
| 49 | + "name": "atl-spine1", |
| 50 | + "role": "spine", |
| 51 | + "interfaces": {"eth0": "Interface 0", "eth1": "Interface 1"}, |
| 52 | + "site": "atl", |
| 53 | + }, |
| 54 | + { |
| 55 | + "name": "atl-spine2", |
| 56 | + "role": "spine", |
| 57 | + "interfaces": {"eth0": "Interface 0", "eth1": "Interface 1"}, |
| 58 | + "site": "atl", |
| 59 | + }, |
| 60 | + { |
| 61 | + "name": "nyc-spine1", |
| 62 | + "role": "spine", |
| 63 | + "interfaces": {"eth0": "Interface 0/0", "eth1": "Interface 1"}, |
| 64 | + "site": "nyc", |
| 65 | + }, |
| 66 | + { |
| 67 | + "name": "nyc-spine2", |
| 68 | + "role": "spine", |
| 69 | + "interfaces": {"eth0": "Interface 0", "eth1": "Interface 1"}, |
| 70 | + "site": "nyc", |
| 71 | + }, |
| 72 | + {"name": "sfo-spine1", "role": "leaf", "interfaces": {"eth0": "Interface 0", "eth1": "Interface 1"}, "site": "sfo"}, |
| 73 | + {"name": "sfo-spine2", "role": "spine", "interfaces": {"eth0": "TBD", "eth1": "ddd"}, "site": "sfo"}, |
| 74 | +] |
| 75 | + |
| 76 | + |
| 77 | +class BackendA(DiffSync): |
| 78 | + """Example of a DiffSync adapter implementation.""" |
| 79 | + |
| 80 | + site = Site |
| 81 | + device = Device |
| 82 | + interface = Interface |
| 83 | + |
| 84 | + top_level = ["device"] |
| 85 | + |
| 86 | + type = "Backend A" |
| 87 | + |
| 88 | + def load(self): |
| 89 | + """Initialize the BackendA Object by loading some site, device and interfaces from DATA.""" |
| 90 | + for device_data in BACKEND_DATA_A: |
| 91 | + device, instantiated = self.get_or_instantiate( |
| 92 | + self.device, {"name": device_data["name"]}, {"role": device_data["role"]} |
| 93 | + ) |
| 94 | + |
| 95 | + site, instantiated = self.get_or_instantiate(self.site, {"name": device_data["site"]}) |
| 96 | + if instantiated: |
| 97 | + device.add_child(site) |
| 98 | + |
| 99 | + for intf_name, desc in device_data["interfaces"].items(): |
| 100 | + intf, instantiated = self.update_or_instantiate( |
| 101 | + self.interface, {"name": intf_name, "device_name": device_data["name"]}, {"description": desc} |
| 102 | + ) |
| 103 | + if instantiated: |
| 104 | + device.add_child(intf) |
| 105 | + |
| 106 | + |
| 107 | +class BackendB(DiffSync): |
| 108 | + """Example of a DiffSync adapter implementation.""" |
| 109 | + |
| 110 | + site = Site |
| 111 | + device = Device |
| 112 | + interface = Interface |
| 113 | + |
| 114 | + top_level = ["device"] |
| 115 | + |
| 116 | + type = "Backend B" |
| 117 | + |
| 118 | + def load(self): |
| 119 | + """Initialize the BackendB Object by loading some site, device and interfaces from DATA.""" |
| 120 | + for device_data in BACKEND_DATA_B: |
| 121 | + device, instantiated = self.get_or_instantiate( |
| 122 | + self.device, {"name": device_data["name"]}, {"role": device_data["role"]} |
| 123 | + ) |
| 124 | + |
| 125 | + site, instantiated = self.get_or_instantiate(self.site, {"name": device_data["site"]}) |
| 126 | + if instantiated: |
| 127 | + device.add_child(site) |
| 128 | + |
| 129 | + for intf_name, desc in device_data["interfaces"].items(): |
| 130 | + intf, instantiated = self.get_or_instantiate( |
| 131 | + self.interface, {"name": intf_name, "device_name": device_data["name"]}, {"description": desc} |
| 132 | + ) |
| 133 | + if instantiated: |
| 134 | + device.add_child(intf) |
0 commit comments