blob: ce01c98016ae8a34cddf6554ae27cedbce50954b [file] [log] [blame]
Thomas Vachuska0d933862018-04-06 00:29:30 -07001/*
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
17package org.onosproject.layout;
18
Thomas Vachuska5b48d6c2018-04-27 18:24:27 -070019import com.google.common.collect.ImmutableList;
Thomas Vachuska0d933862018-04-06 00:29:30 -070020import org.onosproject.net.config.NetworkConfigService;
21import org.onosproject.net.device.DeviceService;
22import org.onosproject.net.host.HostService;
23import org.onosproject.net.link.LinkService;
Thomas Vachuska5b48d6c2018-04-27 18:24:27 -070024import org.onosproject.ui.UiExtension;
25import org.onosproject.ui.UiExtensionService;
26import org.onosproject.ui.UiMessageHandlerFactory;
27import org.onosproject.ui.UiTopoOverlay;
28import org.onosproject.ui.UiTopoOverlayFactory;
29import org.onosproject.ui.UiView;
30import org.onosproject.ui.UiViewHidden;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070031import org.osgi.service.component.annotations.Activate;
32import org.osgi.service.component.annotations.Component;
33import org.osgi.service.component.annotations.Deactivate;
34import org.osgi.service.component.annotations.Reference;
35import org.osgi.service.component.annotations.ReferenceCardinality;
Thomas Vachuska0d933862018-04-06 00:29:30 -070036import org.slf4j.Logger;
37import org.slf4j.LoggerFactory;
38
Thomas Vachuska5b48d6c2018-04-27 18:24:27 -070039import java.util.List;
40
Thomas Vachuska0d933862018-04-06 00:29:30 -070041/**
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 Milkeyd84f89b2018-08-17 14:54:17 -070046@Component(immediate = true, service = RoleBasedLayoutManager.class)
Thomas Vachuska0d933862018-04-06 00:29:30 -070047public class RoleBasedLayoutManager {
48
49 private Logger log = LoggerFactory.getLogger(getClass());
50
Thomas Vachuska5b48d6c2018-04-27 18:24:27 -070051 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 Milkeyd84f89b2018-08-17 14:54:17 -070075 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska0d933862018-04-06 00:29:30 -070076 protected NetworkConfigService networkConfigService;
77
Ray Milkeyd84f89b2018-08-17 14:54:17 -070078 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska0d933862018-04-06 00:29:30 -070079 protected DeviceService deviceService;
80
Ray Milkeyd84f89b2018-08-17 14:54:17 -070081 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska0d933862018-04-06 00:29:30 -070082 protected HostService hostService;
83
Ray Milkeyd84f89b2018-08-17 14:54:17 -070084 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska0d933862018-04-06 00:29:30 -070085 protected LinkService linkService;
86
Ray Milkeyd84f89b2018-08-17 14:54:17 -070087 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska5b48d6c2018-04-27 18:24:27 -070088 protected UiExtensionService uiExtensionService;
89
Thomas Vachuska0d933862018-04-06 00:29:30 -070090 @Activate
91 protected void activate() {
Thomas Vachuska5b48d6c2018-04-27 18:24:27 -070092 uiExtensionService.register(extension);
Thomas Vachuska0d933862018-04-06 00:29:30 -070093 log.info("Started");
94 }
95
96 @Deactivate
97 protected void deactivate() {
Thomas Vachuska5b48d6c2018-04-27 18:24:27 -070098 uiExtensionService.unregister(extension);
Thomas Vachuska0d933862018-04-06 00:29:30 -070099 log.info("Stopped");
100 }
101
Thomas Vachuska0d933862018-04-06 00:29:30 -0700102 /**
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}