blob: e84deb2139274ed65b140e68fc71cb69271d14b7 [file] [log] [blame]
slowr13fa5b02017-08-08 16:32:31 -07001/*
slowr577f3222017-08-28 10:49:08 -07002 * Copyright 2015-present Open Networking Foundation
slowr13fa5b02017-08-08 16:32:31 -07003 *
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.xran.providers;
18
19import com.google.common.collect.Sets;
slowr577f3222017-08-28 10:49:08 -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;
slowr13fa5b02017-08-08 16:32:31 -070025import org.onlab.packet.VlanId;
slowr577f3222017-08-28 10:49:08 -070026import org.onosproject.net.AnnotationKeys;
27import org.onosproject.net.DefaultAnnotations;
28import org.onosproject.net.Host;
29import org.onosproject.net.HostId;
30import org.onosproject.net.HostLocation;
31import org.onosproject.net.PortNumber;
32import org.onosproject.net.SparseAnnotations;
slowr13fa5b02017-08-08 16:32:31 -070033import org.onosproject.net.host.DefaultHostDescription;
34import org.onosproject.net.host.HostProvider;
35import org.onosproject.net.host.HostProviderRegistry;
36import org.onosproject.net.host.HostProviderService;
37import org.onosproject.net.provider.AbstractProvider;
38import org.onosproject.net.provider.ProviderId;
39import org.onosproject.xran.codecs.api.ECGI;
40import org.onosproject.xran.controller.XranController;
41import org.onosproject.xran.entities.RnibUe;
42import org.slf4j.Logger;
43
44import java.util.Set;
45
slowr577f3222017-08-28 10:49:08 -070046import static org.onosproject.net.DeviceId.deviceId;
47import static org.onosproject.xran.entities.RnibCell.uri;
slowr13fa5b02017-08-08 16:32:31 -070048import static org.slf4j.LoggerFactory.getLogger;
49
50/**
slowr577f3222017-08-28 10:49:08 -070051 * UE Provider.
slowr13fa5b02017-08-08 16:32:31 -070052 */
slowr13fa5b02017-08-08 16:32:31 -070053@Component(immediate = true)
54public class UeProvider extends AbstractProvider implements HostProvider {
55
56 private static final Logger log = getLogger(UeProvider.class);
57 private final InternalHostListener listener = new InternalHostListener();
58 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
59 private HostProviderRegistry providerRegistry;
60 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
61 private XranController controller;
62 private HostProviderService providerService;
63
64 public UeProvider() {
slowrd337c932017-08-18 13:54:02 -070065 super(new ProviderId("xran", "org.onosproject.providers.ue"));
slowr13fa5b02017-08-08 16:32:31 -070066 }
67
68 @Activate
69 public void activate() {
70 providerService = providerRegistry.register(this);
71 controller.addListener(listener);
72
73 log.info("XRAN Host Provider Started");
74 }
75
76 @Deactivate
77 public void deactivate() {
78 controller.removeListener(listener);
79 providerRegistry.unregister(this);
80
81 providerService = null;
82 log.info("XRAN Host Provider Stopped");
83 }
84
85 @Override
86 public void triggerProbe(Host host) {
87
88 }
89
slowr577f3222017-08-28 10:49:08 -070090 /**
91 * Internal host listener.
92 */
slowr13fa5b02017-08-08 16:32:31 -070093 class InternalHostListener implements XranHostListener {
94
95 @Override
96 public void hostAdded(RnibUe ue, Set<ECGI> ecgiSet) {
97 if (providerService == null) {
98 return;
99 }
100
101 if (ue == null) {
slowrd337c932017-08-18 13:54:02 -0700102 log.error("UE is not found");
slowr13fa5b02017-08-08 16:32:31 -0700103 return;
104 }
105
106 try {
107 Set<HostLocation> hostLocations = Sets.newConcurrentHashSet();
108
slowr577f3222017-08-28 10:49:08 -0700109 ecgiSet.forEach(ecgi -> hostLocations
110 .add(new HostLocation(deviceId(uri(ecgi)),
111 PortNumber.portNumber(0), 0)));
slowr13fa5b02017-08-08 16:32:31 -0700112
113 SparseAnnotations annotations = DefaultAnnotations.builder()
slowrc86750e2017-08-22 17:26:47 -0700114 .set(AnnotationKeys.NAME, "UE " + ue.getId())
slowr13fa5b02017-08-08 16:32:31 -0700115 .build();
116
slowr577f3222017-08-28 10:49:08 -0700117 // Host ID is calculated from UE ID with some hacky function to represent a MAC address.
slowr13fa5b02017-08-08 16:32:31 -0700118 DefaultHostDescription desc = new DefaultHostDescription(
119 ue.getHostId().mac(),
120 VlanId.vlanId(VlanId.UNTAGGED),
121 hostLocations,
122 Sets.newConcurrentHashSet(),
123 true,
124 annotations
125 );
126
127 providerService.hostDetected(ue.getHostId(), desc, false);
128 } catch (Exception e) {
129 log.warn(e.getMessage());
130 e.printStackTrace();
131 }
132 }
133
134 @Override
135 public void hostRemoved(HostId id) {
136 providerService.hostVanished(id);
137 }
138 }
139}