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 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.Streams; |
| 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.apache.felix.scr.annotations.Service; |
| 27 | import org.onosproject.net.ConnectPoint; |
| 28 | import org.onosproject.net.Device; |
| 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.net.Port; |
| 31 | import org.onosproject.net.device.DeviceService; |
| 32 | import org.onosproject.net.driver.DriverService; |
| 33 | import org.onosproject.net.link.DefaultLinkDescription; |
| 34 | import org.onosproject.net.link.LinkDescription; |
| 35 | import org.onosproject.net.link.LinkStore; |
| 36 | import org.onosproject.net.provider.ProviderId; |
| 37 | import org.onosproject.ui.UiExtension; |
| 38 | import org.onosproject.ui.UiExtensionService; |
| 39 | import org.onosproject.ui.UiMessageHandlerFactory; |
| 40 | import org.onosproject.ui.UiTopoOverlayFactory; |
| 41 | import org.onosproject.ui.UiView; |
| 42 | import org.onosproject.ui.UiViewHidden; |
| 43 | import org.slf4j.Logger; |
| 44 | import org.slf4j.LoggerFactory; |
| 45 | import org.onosproject.net.Link.Type; |
| 46 | |
| 47 | import java.util.List; |
| 48 | import java.util.NoSuchElementException; |
| 49 | import java.util.Optional; |
| 50 | import java.util.Set; |
| 51 | import java.util.stream.Collectors; |
| 52 | |
| 53 | /** |
| 54 | * Implementation of OpenStack Networking UI service. |
| 55 | */ |
| 56 | @Service |
| 57 | @Component(immediate = true) |
| 58 | public class OpenstackNetworkingUiManager implements OpenstackNetworkingUiService { |
| 59 | |
| 60 | private static final ClassLoader CL = OpenstackNetworkingUiManager.class.getClassLoader(); |
| 61 | private static final String VIEW_ID = "sonaTopov"; |
| 62 | private static final String PORT_NAME = "portName"; |
| 63 | private static final String VXLAN = "vxlan"; |
| 64 | private static final String OVS = "ovs"; |
| 65 | private static final String APP_ID = "org.onosproject.openstacknetworkingui"; |
| 66 | private static final String SONA_GUI = "sonagui"; |
| 67 | |
| 68 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 69 | |
| 70 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 71 | protected UiExtensionService uiExtensionService; |
| 72 | |
| 73 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 74 | protected DeviceService deviceService; |
| 75 | |
| 76 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 77 | protected DriverService driverService; |
| 78 | |
| 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 80 | protected LinkStore linkStore; |
| 81 | Set<Device> vDevices; |
| 82 | |
| 83 | private OpenstackNetworkingUiMessageHandler messageHandler = new OpenstackNetworkingUiMessageHandler(); |
| 84 | |
| 85 | private final List<UiView> uiViews = ImmutableList.of( |
| 86 | new UiViewHidden(VIEW_ID) |
| 87 | ); |
| 88 | |
| 89 | |
| 90 | private final UiMessageHandlerFactory messageHandlerFactory = |
| 91 | () -> ImmutableList.of(messageHandler); |
| 92 | |
| 93 | private final UiTopoOverlayFactory topoOverlayFactory = |
| 94 | () -> ImmutableList.of(new OpenstackNetworkingUiOverlay()); |
| 95 | |
| 96 | protected UiExtension extension = |
| 97 | new UiExtension.Builder(CL, uiViews) |
| 98 | .resourcePath(VIEW_ID) |
| 99 | .messageHandlerFactory(messageHandlerFactory) |
| 100 | .topoOverlayFactory(topoOverlayFactory) |
| 101 | .build(); |
| 102 | |
| 103 | @Activate |
| 104 | protected void activate() { |
| 105 | uiExtensionService.register(extension); |
| 106 | |
| 107 | vDevices = Streams.stream(deviceService.getAvailableDevices()) |
| 108 | .filter(this::isVirtualDevice) |
| 109 | .collect(Collectors.toSet()); |
| 110 | |
| 111 | vDevices.forEach(this::createLinksConnectedToTargetvDevice); |
| 112 | |
| 113 | log.info("Started"); |
| 114 | } |
| 115 | |
| 116 | @Deactivate |
| 117 | protected void deactivate() { |
| 118 | uiExtensionService.unregister(extension); |
| 119 | log.info("Stopped"); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void sendMessage(String type, ObjectNode payload) { |
| 124 | messageHandler.sendMessagetoUi(type, payload); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void setRestServerIp(String ipAddress) { |
| 129 | messageHandler.setRestUrl(ipAddress); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public String restServerUrl() { |
| 134 | return messageHandler.restUrl(); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void setRestServerAuthInfo(String id, String password) { |
| 139 | messageHandler.setRestAuthInfo(id, password); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public String restServerAuthInfo() { |
| 144 | return messageHandler.restAuthInfo(); |
| 145 | } |
| 146 | |
| 147 | |
| 148 | private Optional<Port> vxlanPort(DeviceId deviceId) { |
| 149 | return deviceService.getPorts(deviceId) |
| 150 | .stream() |
| 151 | .filter(port -> port.annotations().value(PORT_NAME).equals(VXLAN)) |
| 152 | .findAny(); |
| 153 | } |
| 154 | private boolean isVirtualDevice(Device device) { |
| 155 | return driverService.getDriver(device.id()).name().equals(OVS); |
| 156 | } |
| 157 | |
| 158 | private void createLinksConnectedToTargetvDevice(Device targetvDevice) { |
| 159 | vDevices.stream().filter(d -> !d.equals(targetvDevice)) |
| 160 | .forEach(device -> { |
| 161 | if (vxlanPort(targetvDevice.id()).isPresent() && vxlanPort(device.id()).isPresent()) { |
| 162 | ConnectPoint srcConnectPoint = createConnectPoint(targetvDevice.id()); |
| 163 | |
| 164 | ConnectPoint dstConnectPoint = createConnectPoint(device.id()); |
| 165 | |
| 166 | LinkDescription linkDescription = createLinkDescription(srcConnectPoint, dstConnectPoint); |
| 167 | |
| 168 | linkStore.createOrUpdateLink(new ProviderId(SONA_GUI, APP_ID), |
| 169 | linkDescription); |
| 170 | } |
| 171 | }); |
| 172 | } |
| 173 | |
| 174 | private ConnectPoint createConnectPoint(DeviceId deviceId) { |
| 175 | try { |
| 176 | return new ConnectPoint(deviceId, vxlanPort(deviceId).get().number()); |
| 177 | } catch (NoSuchElementException exception) { |
| 178 | log.warn("Exception occured because of {}", exception.toString()); |
| 179 | return null; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | private LinkDescription createLinkDescription(ConnectPoint srcConnectPoint, ConnectPoint dstConnectPoint) { |
| 184 | return new DefaultLinkDescription(srcConnectPoint, dstConnectPoint, Type.DIRECT, true); |
| 185 | } |
| 186 | |
| 187 | } |