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