blob: 932e62601edbf54893e6bf4aae1f8c075ea003fd [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.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.apache.felix.scr.annotations.Service;
26import org.onosproject.net.config.NetworkConfigService;
27import org.onosproject.net.device.DeviceService;
28import org.onosproject.net.host.HostService;
29import org.onosproject.net.link.LinkService;
Thomas Vachuska5b48d6c2018-04-27 18:24:27 -070030import org.onosproject.ui.UiExtension;
31import org.onosproject.ui.UiExtensionService;
32import org.onosproject.ui.UiMessageHandlerFactory;
33import org.onosproject.ui.UiTopoOverlay;
34import org.onosproject.ui.UiTopoOverlayFactory;
35import org.onosproject.ui.UiView;
36import org.onosproject.ui.UiViewHidden;
Thomas Vachuska0d933862018-04-06 00:29:30 -070037import org.slf4j.Logger;
38import org.slf4j.LoggerFactory;
39
Thomas Vachuska5b48d6c2018-04-27 18:24:27 -070040import java.util.List;
41
Thomas Vachuska0d933862018-04-06 00:29:30 -070042/**
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)
49public class RoleBasedLayoutManager {
50
51 private Logger log = LoggerFactory.getLogger(getClass());
52
Thomas Vachuska5b48d6c2018-04-27 18:24:27 -070053 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 Vachuska0d933862018-04-06 00:29:30 -070077 @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 Vachuska5b48d6c2018-04-27 18:24:27 -070089 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
90 protected UiExtensionService uiExtensionService;
91
Thomas Vachuska0d933862018-04-06 00:29:30 -070092 @Activate
93 protected void activate() {
Thomas Vachuska5b48d6c2018-04-27 18:24:27 -070094 uiExtensionService.register(extension);
Thomas Vachuska0d933862018-04-06 00:29:30 -070095 log.info("Started");
96 }
97
98 @Deactivate
99 protected void deactivate() {
Thomas Vachuska5b48d6c2018-04-27 18:24:27 -0700100 uiExtensionService.unregister(extension);
Thomas Vachuska0d933862018-04-06 00:29:30 -0700101 log.info("Stopped");
102 }
103
Thomas Vachuska0d933862018-04-06 00:29:30 -0700104 /**
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}