daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 | package org.onosproject.openstacknetworkingui; |
| 17 | |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.Streams; |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 20 | import org.onosproject.net.ConnectPoint; |
| 21 | import org.onosproject.net.Device; |
| 22 | import org.onosproject.net.DeviceId; |
Daniel Park | 819f4e8 | 2018-07-02 14:22:31 +0900 | [diff] [blame] | 23 | import org.onosproject.net.Link.Type; |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 24 | import org.onosproject.net.Port; |
| 25 | import org.onosproject.net.device.DeviceService; |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 26 | import org.onosproject.net.link.DefaultLinkDescription; |
| 27 | import org.onosproject.net.link.LinkDescription; |
| 28 | import org.onosproject.net.link.LinkStore; |
| 29 | import org.onosproject.net.provider.ProviderId; |
| 30 | import org.onosproject.ui.UiExtension; |
| 31 | import org.onosproject.ui.UiExtensionService; |
| 32 | import org.onosproject.ui.UiMessageHandlerFactory; |
| 33 | import org.onosproject.ui.UiTopoOverlayFactory; |
| 34 | import org.onosproject.ui.UiView; |
| 35 | import org.onosproject.ui.UiViewHidden; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 36 | import org.osgi.service.component.annotations.Activate; |
| 37 | import org.osgi.service.component.annotations.Component; |
| 38 | import org.osgi.service.component.annotations.Deactivate; |
| 39 | import org.osgi.service.component.annotations.Reference; |
| 40 | import org.osgi.service.component.annotations.ReferenceCardinality; |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 41 | import org.slf4j.Logger; |
| 42 | import org.slf4j.LoggerFactory; |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 43 | |
| 44 | import java.util.List; |
| 45 | import java.util.NoSuchElementException; |
| 46 | import java.util.Optional; |
| 47 | import java.util.Set; |
| 48 | import java.util.stream.Collectors; |
| 49 | |
Daniel Park | 819f4e8 | 2018-07-02 14:22:31 +0900 | [diff] [blame] | 50 | import static org.onosproject.net.Device.Type.SWITCH; |
| 51 | |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 52 | /** |
| 53 | * Implementation of OpenStack Networking UI service. |
| 54 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 55 | @Component(immediate = true, service = OpenstackNetworkingUiService.class) |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 56 | public class OpenstackNetworkingUiManager implements OpenstackNetworkingUiService { |
| 57 | |
| 58 | private static final ClassLoader CL = OpenstackNetworkingUiManager.class.getClassLoader(); |
| 59 | private static final String VIEW_ID = "sonaTopov"; |
| 60 | private static final String PORT_NAME = "portName"; |
| 61 | private static final String VXLAN = "vxlan"; |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 62 | private static final String APP_ID = "org.onosproject.openstacknetworkingui"; |
| 63 | private static final String SONA_GUI = "sonagui"; |
| 64 | |
| 65 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 66 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 67 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 68 | protected UiExtensionService uiExtensionService; |
| 69 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 70 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 71 | protected DeviceService deviceService; |
| 72 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 73 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 74 | protected LinkStore linkStore; |
Daniel Park | 819f4e8 | 2018-07-02 14:22:31 +0900 | [diff] [blame] | 75 | |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 76 | Set<Device> vDevices; |
| 77 | |
| 78 | private OpenstackNetworkingUiMessageHandler messageHandler = new OpenstackNetworkingUiMessageHandler(); |
| 79 | |
| 80 | private final List<UiView> uiViews = ImmutableList.of( |
| 81 | new UiViewHidden(VIEW_ID) |
| 82 | ); |
| 83 | |
| 84 | |
| 85 | private final UiMessageHandlerFactory messageHandlerFactory = |
| 86 | () -> ImmutableList.of(messageHandler); |
| 87 | |
| 88 | private final UiTopoOverlayFactory topoOverlayFactory = |
| 89 | () -> ImmutableList.of(new OpenstackNetworkingUiOverlay()); |
| 90 | |
| 91 | protected UiExtension extension = |
| 92 | new UiExtension.Builder(CL, uiViews) |
| 93 | .resourcePath(VIEW_ID) |
| 94 | .messageHandlerFactory(messageHandlerFactory) |
| 95 | .topoOverlayFactory(topoOverlayFactory) |
| 96 | .build(); |
| 97 | |
| 98 | @Activate |
| 99 | protected void activate() { |
| 100 | uiExtensionService.register(extension); |
| 101 | |
| 102 | vDevices = Streams.stream(deviceService.getAvailableDevices()) |
Daniel Park | 819f4e8 | 2018-07-02 14:22:31 +0900 | [diff] [blame] | 103 | .filter(device -> device.type() == SWITCH) |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 104 | .collect(Collectors.toSet()); |
| 105 | |
| 106 | vDevices.forEach(this::createLinksConnectedToTargetvDevice); |
| 107 | |
| 108 | log.info("Started"); |
| 109 | } |
| 110 | |
| 111 | @Deactivate |
| 112 | protected void deactivate() { |
| 113 | uiExtensionService.unregister(extension); |
| 114 | log.info("Stopped"); |
| 115 | } |
| 116 | |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 117 | private Optional<Port> vxlanPort(DeviceId deviceId) { |
| 118 | return deviceService.getPorts(deviceId) |
| 119 | .stream() |
| 120 | .filter(port -> port.annotations().value(PORT_NAME).equals(VXLAN)) |
| 121 | .findAny(); |
| 122 | } |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 123 | |
| 124 | private void createLinksConnectedToTargetvDevice(Device targetvDevice) { |
| 125 | vDevices.stream().filter(d -> !d.equals(targetvDevice)) |
| 126 | .forEach(device -> { |
| 127 | if (vxlanPort(targetvDevice.id()).isPresent() && vxlanPort(device.id()).isPresent()) { |
| 128 | ConnectPoint srcConnectPoint = createConnectPoint(targetvDevice.id()); |
| 129 | |
| 130 | ConnectPoint dstConnectPoint = createConnectPoint(device.id()); |
| 131 | |
| 132 | LinkDescription linkDescription = createLinkDescription(srcConnectPoint, dstConnectPoint); |
| 133 | |
| 134 | linkStore.createOrUpdateLink(new ProviderId(SONA_GUI, APP_ID), |
| 135 | linkDescription); |
| 136 | } |
| 137 | }); |
| 138 | } |
| 139 | |
| 140 | private ConnectPoint createConnectPoint(DeviceId deviceId) { |
| 141 | try { |
| 142 | return new ConnectPoint(deviceId, vxlanPort(deviceId).get().number()); |
| 143 | } catch (NoSuchElementException exception) { |
| 144 | log.warn("Exception occured because of {}", exception.toString()); |
| 145 | return null; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | private LinkDescription createLinkDescription(ConnectPoint srcConnectPoint, ConnectPoint dstConnectPoint) { |
| 150 | return new DefaultLinkDescription(srcConnectPoint, dstConnectPoint, Type.DIRECT, true); |
| 151 | } |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 152 | } |