Seyeon Jeong | ac12956 | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.t3.cli; |
| 18 | |
| 19 | import com.google.common.collect.Lists; |
| 20 | import com.google.common.collect.Sets; |
| 21 | import org.apache.karaf.shell.api.action.Command; |
| 22 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.cluster.NodeId; |
| 25 | import org.onosproject.mastership.MastershipService; |
| 26 | import org.onosproject.mcast.api.McastRoute; |
| 27 | import org.onosproject.mcast.api.McastRouteData; |
| 28 | import org.onosproject.mcast.api.MulticastRouteService; |
| 29 | import org.onosproject.net.ConnectPoint; |
| 30 | import org.onosproject.net.DefaultAnnotations; |
| 31 | import org.onosproject.net.DefaultDevice; |
| 32 | import org.onosproject.net.Device; |
| 33 | import org.onosproject.net.DeviceId; |
| 34 | import org.onosproject.net.Host; |
| 35 | import org.onosproject.net.Link; |
| 36 | import org.onosproject.net.Port; |
| 37 | import org.onosproject.net.config.Config; |
| 38 | import org.onosproject.net.config.NetworkConfigService; |
| 39 | import org.onosproject.net.config.basics.InterfaceConfig; |
| 40 | import org.onosproject.net.device.DeviceService; |
| 41 | import org.onosproject.net.driver.DriverService; |
| 42 | import org.onosproject.net.edge.EdgePortService; |
| 43 | import org.onosproject.net.flow.FlowEntry; |
| 44 | import org.onosproject.net.flow.FlowRuleService; |
| 45 | import org.onosproject.net.group.Group; |
| 46 | import org.onosproject.net.group.GroupService; |
| 47 | import org.onosproject.net.host.HostService; |
| 48 | import org.onosproject.net.link.LinkService; |
| 49 | import org.onosproject.routeservice.ResolvedRoute; |
| 50 | import org.onosproject.routeservice.RouteService; |
| 51 | import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig; |
| 52 | import org.onosproject.t3.api.DeviceNib; |
| 53 | import org.onosproject.t3.api.DriverNib; |
| 54 | import org.onosproject.t3.api.EdgePortNib; |
| 55 | import org.onosproject.t3.api.FlowNib; |
| 56 | import org.onosproject.t3.api.GroupNib; |
| 57 | import org.onosproject.t3.api.HostNib; |
| 58 | import org.onosproject.t3.api.LinkNib; |
| 59 | import org.onosproject.t3.api.MastershipNib; |
| 60 | import org.onosproject.t3.api.MulticastRouteNib; |
| 61 | import org.onosproject.t3.api.NetworkConfigNib; |
| 62 | import org.onosproject.t3.api.NibProfile; |
| 63 | import org.onosproject.t3.api.RouteNib; |
| 64 | |
| 65 | import java.util.HashMap; |
| 66 | import java.util.HashSet; |
| 67 | import java.util.Map; |
| 68 | import java.util.Set; |
| 69 | |
| 70 | /** |
| 71 | * T3 CLI command to load the NIB with snapshots of the network states that are fetched from ONOS stores. |
| 72 | */ |
| 73 | @Service |
| 74 | @Command(scope = "onos", name = "t3-load-snapshot", |
| 75 | description = "Load the NIB with the network states stored in the ONOS instance where the T3 is running") |
| 76 | public class TroubleshootLoadSnapshotCommand |
| 77 | extends AbstractShellCommand implements NibLoader { |
| 78 | |
| 79 | @Override |
| 80 | protected void doExecute() { |
| 81 | |
| 82 | print("Load current network states from ONOS stores"); |
| 83 | |
| 84 | loadFlowNib(); |
| 85 | loadGroupNib(); |
| 86 | loadLinkNib(); |
| 87 | loadHostNib(); |
| 88 | loadDeviceNib(); |
| 89 | loadDriverNib(); |
| 90 | loadMastershipNib(); |
| 91 | loadEdgePortNib(); |
| 92 | loadRouteNib(); |
| 93 | loadNetworkConfigNib(); |
| 94 | loadMulticastRouteNib(); |
| 95 | |
| 96 | Lists.newArrayList(FlowNib.getInstance(), GroupNib.getInstance(), LinkNib.getInstance(), |
| 97 | HostNib.getInstance(), DeviceNib.getInstance(), DriverNib.getInstance(), |
| 98 | MastershipNib.getInstance(), EdgePortNib.getInstance(), RouteNib.getInstance(), |
| 99 | NetworkConfigNib.getInstance(), MulticastRouteNib.getInstance()) |
| 100 | .forEach(nib -> { |
| 101 | // specify creation time and source which the NIB is filled with |
| 102 | nib.setProfile(new NibProfile(System.currentTimeMillis(), NibProfile.SourceType.SNAPSHOT)); |
| 103 | NibProfile profile = nib.getProfile(); |
| 104 | print(String.format( |
| 105 | nib.getClass().getSimpleName() + " created %s from %s", |
| 106 | profile.date(), profile.sourceType())); |
| 107 | }); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public void loadFlowNib() { |
| 112 | FlowRuleService flowRuleService = get(FlowRuleService.class); |
| 113 | DeviceService deviceService = get(DeviceService.class); |
| 114 | Set<FlowEntry> flows = new HashSet<>(); |
| 115 | |
| 116 | Lists.newArrayList(deviceService.getDevices().iterator()) |
| 117 | .forEach(device -> flows.addAll(Lists.newArrayList( |
| 118 | flowRuleService.getFlowEntries(device.id())))); |
| 119 | |
| 120 | FlowNib flowNib = FlowNib.getInstance(); |
| 121 | flowNib.setFlows(flows); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void loadGroupNib() { |
| 126 | GroupService groupService = get(GroupService.class); |
| 127 | DeviceService deviceService = get(DeviceService.class); |
| 128 | Set<Group> groups = new HashSet<>(); |
| 129 | |
| 130 | Lists.newArrayList(deviceService.getDevices().iterator()) |
| 131 | .forEach(device -> groups.addAll(Lists.newArrayList( |
| 132 | groupService.getGroups(device.id())))); |
| 133 | |
| 134 | GroupNib groupNib = GroupNib.getInstance(); |
| 135 | groupNib.setGroups(groups); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public void loadLinkNib() { |
| 140 | LinkService linkService = get(LinkService.class); |
| 141 | Set<Link> links = new HashSet<>(); |
| 142 | |
| 143 | links.addAll(Lists.newArrayList(linkService.getLinks())); |
| 144 | |
| 145 | LinkNib linkNib = LinkNib.getInstance(); |
| 146 | linkNib.setLinks(links); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public void loadHostNib() { |
| 151 | HostService hostService = get(HostService.class); |
| 152 | Set<Host> hosts = new HashSet<>(); |
| 153 | |
| 154 | hosts.addAll(Lists.newArrayList(hostService.getHosts())); |
| 155 | |
| 156 | HostNib hostNib = HostNib.getInstance(); |
| 157 | hostNib.setHosts(hosts); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public void loadDeviceNib() { |
| 162 | DeviceService deviceService = get(DeviceService.class); |
| 163 | Map<Device, Set<Port>> devicePortMap = new HashMap<>(); |
| 164 | |
| 165 | Lists.newArrayList(deviceService.getDevices().iterator()) |
| 166 | .forEach(device -> { |
| 167 | // current DeviceNib impl. checks the availability of devices from their annotations |
| 168 | DefaultAnnotations annotations = DefaultAnnotations.builder() |
| 169 | .set("available", String.valueOf(deviceService.isAvailable(device.id()))).build(); |
| 170 | DefaultDevice annotated = new DefaultDevice(device.providerId(), device.id(), device.type(), |
| 171 | device.manufacturer(), device.hwVersion(), device.swVersion(), device.serialNumber(), |
| 172 | device.chassisId(), annotations); |
| 173 | devicePortMap.put(annotated, Sets.newHashSet(deviceService.getPorts(device.id()))); |
| 174 | }); |
| 175 | |
| 176 | DeviceNib deviceNib = DeviceNib.getInstance(); |
| 177 | deviceNib.setDevicePortMap(devicePortMap); |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public void loadDriverNib() { |
| 182 | DriverService driverService = get(DriverService.class); |
| 183 | Map<DeviceId, String> deviceDriverMap = driverService.getDeviceDrivers(); |
| 184 | |
| 185 | DriverNib driverNib = DriverNib.getInstance(); |
| 186 | driverNib.setDeviceDriverMap(deviceDriverMap); |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public void loadMastershipNib() { |
| 191 | MastershipService mastershipService = get(MastershipService.class); |
| 192 | DeviceService deviceService = get(DeviceService.class); |
| 193 | Map<DeviceId, NodeId> deviceMasterMap = new HashMap<>(); |
| 194 | |
| 195 | Lists.newArrayList(deviceService.getDevices().iterator()) |
| 196 | .forEach(device -> deviceMasterMap.put(device.id(), mastershipService.getMasterFor(device.id()))); |
| 197 | |
| 198 | MastershipNib mastershipNib = MastershipNib.getInstance(); |
| 199 | mastershipNib.setDeviceMasterMap(deviceMasterMap); |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public void loadEdgePortNib() { |
| 204 | EdgePortService edgePortService = get(EdgePortService.class); |
| 205 | DeviceService deviceService = get(DeviceService.class); |
| 206 | Map<DeviceId, Set<ConnectPoint>> edgePorts = new HashMap<>(); |
| 207 | |
| 208 | Lists.newArrayList(deviceService.getDevices().iterator()) |
| 209 | .forEach(device -> edgePorts.put(device.id(), Sets.newHashSet(edgePortService.getEdgePoints()))); |
| 210 | |
| 211 | EdgePortNib edgePortNib = EdgePortNib.getInstance(); |
| 212 | edgePortNib.setEdgePorts(edgePorts); |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public void loadRouteNib() { |
| 217 | RouteService routeService = get(RouteService.class); |
| 218 | Set<ResolvedRoute> routes = new HashSet<>(); |
| 219 | |
| 220 | Lists.newArrayList(routeService.getRouteTables()) |
| 221 | .forEach(routeTableId -> routes.addAll(routeService.getResolvedRoutes(routeTableId))); |
| 222 | |
| 223 | RouteNib routeNib = RouteNib.getInstance(); |
| 224 | routeNib.setRoutes(routes); |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public void loadNetworkConfigNib() { |
| 229 | NetworkConfigService networkConfigService = get(NetworkConfigService.class); |
| 230 | DeviceService deviceService = get(DeviceService.class); |
| 231 | |
| 232 | // Map of str ConnectPoint : InterfaceConfig |
| 233 | Map<String, Config> portConfigMap = new HashMap<>(); |
| 234 | Lists.newArrayList(deviceService.getDevices().iterator()) |
| 235 | .forEach(device -> deviceService.getPorts(device.id()) |
| 236 | .forEach(port -> { |
| 237 | ConnectPoint cp = new ConnectPoint(device.id(), port.number()); |
| 238 | portConfigMap.put(cp.toString(), networkConfigService.getConfig(cp, InterfaceConfig.class)); |
| 239 | })); |
| 240 | |
| 241 | // Map of str DeviceId : SegmentRoutingDeviceConfig |
| 242 | Map<String, Config> deviceConfigMap = new HashMap<>(); |
| 243 | Lists.newArrayList(deviceService.getDevices().iterator()) |
| 244 | .forEach(device -> deviceConfigMap.put(device.id().toString(), |
| 245 | networkConfigService.getConfig(device.id(), SegmentRoutingDeviceConfig.class))); |
| 246 | |
| 247 | NetworkConfigNib networkConfigNib = NetworkConfigNib.getInstance(); |
| 248 | networkConfigNib.setPortConfigMap(portConfigMap); |
| 249 | networkConfigNib.setDeviceConfigMap(deviceConfigMap); |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public void loadMulticastRouteNib() { |
| 254 | MulticastRouteService mcastRouteService = get(MulticastRouteService.class); |
| 255 | Map<McastRoute, McastRouteData> mcastRoutes = new HashMap<>(); |
| 256 | |
| 257 | Lists.newArrayList(mcastRouteService.getRoutes()) |
| 258 | .forEach(mcastRoute -> mcastRoutes.put(mcastRoute, mcastRouteService.routeData(mcastRoute))); |
| 259 | |
| 260 | MulticastRouteNib mcastRouteNib = MulticastRouteNib.getInstance(); |
| 261 | mcastRouteNib.setMcastRoutes(mcastRoutes); |
| 262 | } |
| 263 | |
| 264 | } |