Thomas Vachuska | 0d93386 | 2018-04-06 00:29:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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.layout; |
| 18 | |
Thomas Vachuska | 5b48d6c | 2018-04-27 18:24:27 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
Thomas Vachuska | 0d93386 | 2018-04-06 00:29:30 -0700 | [diff] [blame] | 20 | import org.onosproject.net.config.NetworkConfigService; |
| 21 | import org.onosproject.net.device.DeviceService; |
| 22 | import org.onosproject.net.host.HostService; |
| 23 | import org.onosproject.net.link.LinkService; |
Thomas Vachuska | 5b48d6c | 2018-04-27 18:24:27 -0700 | [diff] [blame] | 24 | import org.onosproject.ui.UiExtension; |
| 25 | import org.onosproject.ui.UiExtensionService; |
| 26 | import org.onosproject.ui.UiMessageHandlerFactory; |
| 27 | import org.onosproject.ui.UiTopoOverlay; |
| 28 | import org.onosproject.ui.UiTopoOverlayFactory; |
| 29 | import org.onosproject.ui.UiView; |
| 30 | import org.onosproject.ui.UiViewHidden; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 31 | import org.osgi.service.component.annotations.Activate; |
| 32 | import org.osgi.service.component.annotations.Component; |
| 33 | import org.osgi.service.component.annotations.Deactivate; |
| 34 | import org.osgi.service.component.annotations.Reference; |
| 35 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Thomas Vachuska | 0d93386 | 2018-04-06 00:29:30 -0700 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
| 38 | |
Thomas Vachuska | 5b48d6c | 2018-04-27 18:24:27 -0700 | [diff] [blame] | 39 | import java.util.List; |
| 40 | |
Thomas Vachuska | 0d93386 | 2018-04-06 00:29:30 -0700 | [diff] [blame] | 41 | /** |
| 42 | * Manages automatic layout of the current network elements into one of several |
| 43 | * supported layout variants using roles assigned to network elements using |
| 44 | * network configuration. |
| 45 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 46 | @Component(immediate = true, service = RoleBasedLayoutManager.class) |
Thomas Vachuska | 0d93386 | 2018-04-06 00:29:30 -0700 | [diff] [blame] | 47 | public class RoleBasedLayoutManager { |
| 48 | |
| 49 | private Logger log = LoggerFactory.getLogger(getClass()); |
| 50 | |
Thomas Vachuska | 5b48d6c | 2018-04-27 18:24:27 -0700 | [diff] [blame] | 51 | private static final String VIEW_ID = "tlTopov"; |
| 52 | private static final String OVERLAY_ID = "tl-overlay"; |
| 53 | |
| 54 | // List of application views |
| 55 | private final List<UiView> uiViews = ImmutableList.of( |
| 56 | new UiViewHidden(VIEW_ID) |
| 57 | ); |
| 58 | |
| 59 | // Factory for UI message handlers |
| 60 | private final UiMessageHandlerFactory messageHandlerFactory = |
| 61 | () -> ImmutableList.of(new LayoutOverlayMessageHandler()); |
| 62 | |
| 63 | // Factory for UI topology overlays |
| 64 | private final UiTopoOverlayFactory topoOverlayFactory = |
| 65 | () -> ImmutableList.of(new UiTopoOverlay(OVERLAY_ID)); |
| 66 | |
| 67 | // Application UI extension |
| 68 | protected UiExtension extension = |
| 69 | new UiExtension.Builder(getClass().getClassLoader(), uiViews) |
| 70 | .resourcePath(VIEW_ID) |
| 71 | .messageHandlerFactory(messageHandlerFactory) |
| 72 | .topoOverlayFactory(topoOverlayFactory) |
| 73 | .build(); |
| 74 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | 0d93386 | 2018-04-06 00:29:30 -0700 | [diff] [blame] | 76 | protected NetworkConfigService networkConfigService; |
| 77 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | 0d93386 | 2018-04-06 00:29:30 -0700 | [diff] [blame] | 79 | protected DeviceService deviceService; |
| 80 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | 0d93386 | 2018-04-06 00:29:30 -0700 | [diff] [blame] | 82 | protected HostService hostService; |
| 83 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | 0d93386 | 2018-04-06 00:29:30 -0700 | [diff] [blame] | 85 | protected LinkService linkService; |
| 86 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 87 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | 5b48d6c | 2018-04-27 18:24:27 -0700 | [diff] [blame] | 88 | protected UiExtensionService uiExtensionService; |
| 89 | |
Thomas Vachuska | 0d93386 | 2018-04-06 00:29:30 -0700 | [diff] [blame] | 90 | @Activate |
| 91 | protected void activate() { |
Thomas Vachuska | 5b48d6c | 2018-04-27 18:24:27 -0700 | [diff] [blame] | 92 | uiExtensionService.register(extension); |
Thomas Vachuska | 0d93386 | 2018-04-06 00:29:30 -0700 | [diff] [blame] | 93 | log.info("Started"); |
| 94 | } |
| 95 | |
| 96 | @Deactivate |
| 97 | protected void deactivate() { |
Thomas Vachuska | 5b48d6c | 2018-04-27 18:24:27 -0700 | [diff] [blame] | 98 | uiExtensionService.unregister(extension); |
Thomas Vachuska | 0d93386 | 2018-04-06 00:29:30 -0700 | [diff] [blame] | 99 | log.info("Stopped"); |
| 100 | } |
| 101 | |
Thomas Vachuska | 0d93386 | 2018-04-06 00:29:30 -0700 | [diff] [blame] | 102 | /** |
| 103 | * Executes the specified layout algorithm. |
| 104 | * |
| 105 | * @param algorithm layout algorithm |
| 106 | */ |
| 107 | public void layout(LayoutAlgorithm algorithm) { |
| 108 | algorithm.init(deviceService, hostService, linkService, networkConfigService); |
| 109 | algorithm.classify(); |
| 110 | log.info("Layout classified: {}", algorithm); |
| 111 | algorithm.apply(); |
| 112 | } |
| 113 | |
| 114 | } |