blob: 848cfc73f554a2cb24e50158e0055faee6dbf0a9 [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;
20import 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.ConnectPoint;
27import org.onosproject.net.Device;
28import org.onosproject.net.DeviceId;
Daniel Park819f4e82018-07-02 14:22:31 +090029import org.onosproject.net.Link.Type;
daniel park128c52c2017-09-04 13:15:51 +090030import org.onosproject.net.Port;
31import org.onosproject.net.device.DeviceService;
daniel park128c52c2017-09-04 13:15:51 +090032import org.onosproject.net.link.DefaultLinkDescription;
33import org.onosproject.net.link.LinkDescription;
34import org.onosproject.net.link.LinkStore;
35import org.onosproject.net.provider.ProviderId;
36import org.onosproject.ui.UiExtension;
37import org.onosproject.ui.UiExtensionService;
38import org.onosproject.ui.UiMessageHandlerFactory;
39import org.onosproject.ui.UiTopoOverlayFactory;
40import org.onosproject.ui.UiView;
41import org.onosproject.ui.UiViewHidden;
42import org.slf4j.Logger;
43import org.slf4j.LoggerFactory;
daniel park128c52c2017-09-04 13:15:51 +090044
45import java.util.List;
46import java.util.NoSuchElementException;
47import java.util.Optional;
48import java.util.Set;
49import java.util.stream.Collectors;
50
Daniel Park819f4e82018-07-02 14:22:31 +090051import static org.onosproject.net.Device.Type.SWITCH;
52
daniel park128c52c2017-09-04 13:15:51 +090053/**
54 * Implementation of OpenStack Networking UI service.
55 */
56@Service
57@Component(immediate = true)
58public class OpenstackNetworkingUiManager implements OpenstackNetworkingUiService {
59
60 private static final ClassLoader CL = OpenstackNetworkingUiManager.class.getClassLoader();
61 private static final String VIEW_ID = "sonaTopov";
62 private static final String PORT_NAME = "portName";
63 private static final String VXLAN = "vxlan";
daniel park128c52c2017-09-04 13:15:51 +090064 private static final String APP_ID = "org.onosproject.openstacknetworkingui";
65 private static final String SONA_GUI = "sonagui";
66
67 private final Logger log = LoggerFactory.getLogger(getClass());
68
69 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
70 protected UiExtensionService uiExtensionService;
71
72 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
73 protected DeviceService deviceService;
74
75 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
daniel park128c52c2017-09-04 13:15:51 +090076 protected LinkStore linkStore;
Daniel Park819f4e82018-07-02 14:22:31 +090077
daniel park128c52c2017-09-04 13:15:51 +090078 Set<Device> vDevices;
79
80 private OpenstackNetworkingUiMessageHandler messageHandler = new OpenstackNetworkingUiMessageHandler();
81
82 private final List<UiView> uiViews = ImmutableList.of(
83 new UiViewHidden(VIEW_ID)
84 );
85
86
87 private final UiMessageHandlerFactory messageHandlerFactory =
88 () -> ImmutableList.of(messageHandler);
89
90 private final UiTopoOverlayFactory topoOverlayFactory =
91 () -> ImmutableList.of(new OpenstackNetworkingUiOverlay());
92
93 protected UiExtension extension =
94 new UiExtension.Builder(CL, uiViews)
95 .resourcePath(VIEW_ID)
96 .messageHandlerFactory(messageHandlerFactory)
97 .topoOverlayFactory(topoOverlayFactory)
98 .build();
99
100 @Activate
101 protected void activate() {
102 uiExtensionService.register(extension);
103
104 vDevices = Streams.stream(deviceService.getAvailableDevices())
Daniel Park819f4e82018-07-02 14:22:31 +0900105 .filter(device -> device.type() == SWITCH)
daniel park128c52c2017-09-04 13:15:51 +0900106 .collect(Collectors.toSet());
107
108 vDevices.forEach(this::createLinksConnectedToTargetvDevice);
109
110 log.info("Started");
111 }
112
113 @Deactivate
114 protected void deactivate() {
115 uiExtensionService.unregister(extension);
116 log.info("Stopped");
117 }
118
daniel park128c52c2017-09-04 13:15:51 +0900119 private Optional<Port> vxlanPort(DeviceId deviceId) {
120 return deviceService.getPorts(deviceId)
121 .stream()
122 .filter(port -> port.annotations().value(PORT_NAME).equals(VXLAN))
123 .findAny();
124 }
daniel park128c52c2017-09-04 13:15:51 +0900125
126 private void createLinksConnectedToTargetvDevice(Device targetvDevice) {
127 vDevices.stream().filter(d -> !d.equals(targetvDevice))
128 .forEach(device -> {
129 if (vxlanPort(targetvDevice.id()).isPresent() && vxlanPort(device.id()).isPresent()) {
130 ConnectPoint srcConnectPoint = createConnectPoint(targetvDevice.id());
131
132 ConnectPoint dstConnectPoint = createConnectPoint(device.id());
133
134 LinkDescription linkDescription = createLinkDescription(srcConnectPoint, dstConnectPoint);
135
136 linkStore.createOrUpdateLink(new ProviderId(SONA_GUI, APP_ID),
137 linkDescription);
138 }
139 });
140 }
141
142 private ConnectPoint createConnectPoint(DeviceId deviceId) {
143 try {
144 return new ConnectPoint(deviceId, vxlanPort(deviceId).get().number());
145 } catch (NoSuchElementException exception) {
146 log.warn("Exception occured because of {}", exception.toString());
147 return null;
148 }
149 }
150
151 private LinkDescription createLinkDescription(ConnectPoint srcConnectPoint, ConnectPoint dstConnectPoint) {
152 return new DefaultLinkDescription(srcConnectPoint, dstConnectPoint, Type.DIRECT, true);
153 }
daniel park128c52c2017-09-04 13:15:51 +0900154}