blob: 4191503aeac3106c50e29627e149ac7e5acc6702 [file] [log] [blame]
daniel park128c52c2017-09-04 13:15:51 +09001/*
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 */
16package org.onosproject.openstacknetworkingui;
17
daniel park128c52c2017-09-04 13:15:51 +090018import com.google.common.collect.ImmutableList;
19import com.google.common.collect.Streams;
daniel park128c52c2017-09-04 13:15:51 +090020import org.onosproject.net.ConnectPoint;
21import org.onosproject.net.Device;
22import org.onosproject.net.DeviceId;
Daniel Park819f4e82018-07-02 14:22:31 +090023import org.onosproject.net.Link.Type;
daniel park128c52c2017-09-04 13:15:51 +090024import org.onosproject.net.Port;
25import org.onosproject.net.device.DeviceService;
daniel park128c52c2017-09-04 13:15:51 +090026import org.onosproject.net.link.DefaultLinkDescription;
27import org.onosproject.net.link.LinkDescription;
28import org.onosproject.net.link.LinkStore;
29import org.onosproject.net.provider.ProviderId;
30import org.onosproject.ui.UiExtension;
31import org.onosproject.ui.UiExtensionService;
32import org.onosproject.ui.UiMessageHandlerFactory;
33import org.onosproject.ui.UiTopoOverlayFactory;
34import org.onosproject.ui.UiView;
35import org.onosproject.ui.UiViewHidden;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070036import org.osgi.service.component.annotations.Activate;
37import org.osgi.service.component.annotations.Component;
38import org.osgi.service.component.annotations.Deactivate;
39import org.osgi.service.component.annotations.Reference;
40import org.osgi.service.component.annotations.ReferenceCardinality;
daniel park128c52c2017-09-04 13:15:51 +090041import org.slf4j.Logger;
42import org.slf4j.LoggerFactory;
daniel park128c52c2017-09-04 13:15:51 +090043
44import java.util.List;
45import java.util.NoSuchElementException;
46import java.util.Optional;
47import java.util.Set;
48import java.util.stream.Collectors;
49
Daniel Park819f4e82018-07-02 14:22:31 +090050import static org.onosproject.net.Device.Type.SWITCH;
51
daniel park128c52c2017-09-04 13:15:51 +090052/**
53 * Implementation of OpenStack Networking UI service.
54 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070055@Component(immediate = true, service = OpenstackNetworkingUiService.class)
daniel park128c52c2017-09-04 13:15:51 +090056public class OpenstackNetworkingUiManager implements OpenstackNetworkingUiService {
57
58 private static final ClassLoader CL = OpenstackNetworkingUiManager.class.getClassLoader();
59 private static final String VIEW_ID = "sonaTopov";
60 private static final String PORT_NAME = "portName";
61 private static final String VXLAN = "vxlan";
daniel park128c52c2017-09-04 13:15:51 +090062 private static final String APP_ID = "org.onosproject.openstacknetworkingui";
63 private static final String SONA_GUI = "sonagui";
64
65 private final Logger log = LoggerFactory.getLogger(getClass());
66
Ray Milkeyd84f89b2018-08-17 14:54:17 -070067 @Reference(cardinality = ReferenceCardinality.MANDATORY)
daniel park128c52c2017-09-04 13:15:51 +090068 protected UiExtensionService uiExtensionService;
69
Ray Milkeyd84f89b2018-08-17 14:54:17 -070070 @Reference(cardinality = ReferenceCardinality.MANDATORY)
daniel park128c52c2017-09-04 13:15:51 +090071 protected DeviceService deviceService;
72
Ray Milkeyd84f89b2018-08-17 14:54:17 -070073 @Reference(cardinality = ReferenceCardinality.MANDATORY)
daniel park128c52c2017-09-04 13:15:51 +090074 protected LinkStore linkStore;
Daniel Park819f4e82018-07-02 14:22:31 +090075
daniel park128c52c2017-09-04 13:15:51 +090076 Set<Device> vDevices;
77
78 private OpenstackNetworkingUiMessageHandler messageHandler = new OpenstackNetworkingUiMessageHandler();
79
80 private final List<UiView> uiViews = ImmutableList.of(
81 new UiViewHidden(VIEW_ID)
82 );
83
84
85 private final UiMessageHandlerFactory messageHandlerFactory =
86 () -> ImmutableList.of(messageHandler);
87
88 private final UiTopoOverlayFactory topoOverlayFactory =
89 () -> ImmutableList.of(new OpenstackNetworkingUiOverlay());
90
91 protected UiExtension extension =
92 new UiExtension.Builder(CL, uiViews)
93 .resourcePath(VIEW_ID)
94 .messageHandlerFactory(messageHandlerFactory)
95 .topoOverlayFactory(topoOverlayFactory)
96 .build();
97
98 @Activate
99 protected void activate() {
100 uiExtensionService.register(extension);
101
102 vDevices = Streams.stream(deviceService.getAvailableDevices())
Daniel Park819f4e82018-07-02 14:22:31 +0900103 .filter(device -> device.type() == SWITCH)
daniel park128c52c2017-09-04 13:15:51 +0900104 .collect(Collectors.toSet());
105
106 vDevices.forEach(this::createLinksConnectedToTargetvDevice);
107
108 log.info("Started");
109 }
110
111 @Deactivate
112 protected void deactivate() {
113 uiExtensionService.unregister(extension);
114 log.info("Stopped");
115 }
116
daniel park128c52c2017-09-04 13:15:51 +0900117 private Optional<Port> vxlanPort(DeviceId deviceId) {
118 return deviceService.getPorts(deviceId)
119 .stream()
120 .filter(port -> port.annotations().value(PORT_NAME).equals(VXLAN))
121 .findAny();
122 }
daniel park128c52c2017-09-04 13:15:51 +0900123
124 private void createLinksConnectedToTargetvDevice(Device targetvDevice) {
125 vDevices.stream().filter(d -> !d.equals(targetvDevice))
126 .forEach(device -> {
127 if (vxlanPort(targetvDevice.id()).isPresent() && vxlanPort(device.id()).isPresent()) {
128 ConnectPoint srcConnectPoint = createConnectPoint(targetvDevice.id());
129
130 ConnectPoint dstConnectPoint = createConnectPoint(device.id());
131
132 LinkDescription linkDescription = createLinkDescription(srcConnectPoint, dstConnectPoint);
133
134 linkStore.createOrUpdateLink(new ProviderId(SONA_GUI, APP_ID),
135 linkDescription);
136 }
137 });
138 }
139
140 private ConnectPoint createConnectPoint(DeviceId deviceId) {
141 try {
142 return new ConnectPoint(deviceId, vxlanPort(deviceId).get().number());
143 } catch (NoSuchElementException exception) {
144 log.warn("Exception occured because of {}", exception.toString());
145 return null;
146 }
147 }
148
149 private LinkDescription createLinkDescription(ConnectPoint srcConnectPoint, ConnectPoint dstConnectPoint) {
150 return new DefaultLinkDescription(srcConnectPoint, dstConnectPoint, Type.DIRECT, true);
151 }
daniel park128c52c2017-09-04 13:15:51 +0900152}