blob: c985a842b0c42802236189e60d75dada5deb88df [file] [log] [blame]
slowr13fa5b02017-08-08 16:32:31 -07001/*
2 * Copyright 2015-present Open Networking Laboratory
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.xran.controller;
18
19import com.google.common.collect.Sets;
20import io.netty.channel.ChannelHandlerContext;
21import io.netty.channel.sctp.SctpMessage;
22import org.apache.commons.lang.exception.ExceptionUtils;
23import org.apache.felix.scr.annotations.*;
24import org.onosproject.core.ApplicationId;
25import org.onosproject.core.CoreService;
26import org.onosproject.net.config.*;
27import org.onosproject.net.config.basics.SubjectFactories;
28import org.onosproject.net.device.DeviceEvent;
29import org.onosproject.net.device.DeviceListener;
30import org.onosproject.net.device.DeviceService;
31import org.onosproject.net.host.HostEvent;
32import org.onosproject.net.host.HostListener;
33import org.onosproject.net.host.HostService;
34import org.onosproject.xran.XranStore;
35import org.onosproject.xran.codecs.api.*;
slowrc153ad92017-08-16 19:47:52 -070036import org.onosproject.xran.codecs.ber.types.BerInteger;
slowr13fa5b02017-08-08 16:32:31 -070037import org.onosproject.xran.codecs.pdu.*;
38import org.onosproject.xran.entities.RnibCell;
39import org.onosproject.xran.entities.RnibLink;
40import org.onosproject.xran.entities.RnibUe;
41import org.onosproject.xran.identifiers.LinkId;
42import org.onosproject.xran.impl.XranConfig;
43import org.onosproject.xran.providers.XranDeviceListener;
44import org.onosproject.xran.providers.XranHostListener;
slowr13fa5b02017-08-08 16:32:31 -070045import org.onosproject.xran.wrapper.CellMap;
46import org.onosproject.xran.wrapper.LinkMap;
47import org.onosproject.xran.wrapper.UeMap;
slowr13fa5b02017-08-08 16:32:31 -070048import org.slf4j.Logger;
49import org.slf4j.LoggerFactory;
50
51import java.io.IOException;
52import java.util.*;
slowr8ddc2b12017-08-14 14:13:38 -070053import java.util.concurrent.ConcurrentHashMap;
54import java.util.concurrent.ConcurrentMap;
55import java.util.concurrent.CopyOnWriteArraySet;
56import java.util.concurrent.SynchronousQueue;
slowr13fa5b02017-08-08 16:32:31 -070057import java.util.stream.Collectors;
58
59import static org.onosproject.net.DeviceId.deviceId;
slowr67d05e42017-08-11 20:37:22 -070060import static org.onosproject.xran.controller.XranChannelHandler.getSctpMessage;
slowr13fa5b02017-08-08 16:32:31 -070061import static org.onosproject.xran.entities.RnibCell.decodeDeviceId;
62import static org.onosproject.xran.entities.RnibCell.uri;
slowr13fa5b02017-08-08 16:32:31 -070063import static org.onosproject.xran.entities.RnibUe.hostIdtoMME;
64
65/**
66 * Created by dimitris on 7/20/17.
67 */
68@Component(immediate = true)
69@Service
70public class XranControllerImpl implements XranController {
71 private static final String XRAN_APP_ID = "org.onosproject.xran";
72 private static final Class<XranConfig> CONFIG_CLASS = XranConfig.class;
73
74 private static final Logger log =
75 LoggerFactory.getLogger(XranControllerImpl.class);
76 /* CONFIG */
77 private final InternalNetworkConfigListener configListener =
78 new InternalNetworkConfigListener();
79 /* VARIABLES */
80 private final Controller controller = new Controller();
81 private XranConfig xranConfig;
82 private ApplicationId appId;
83 /* Services */
84 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
85 private DeviceService deviceService;
86 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
87 private HostService hostService;
88 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
89 private NetworkConfigRegistry registry;
90 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
91 private NetworkConfigService configService;
92 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
93 private CoreService coreService;
94 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
95 private XranStore xranStore;
96 private ConfigFactory<ApplicationId, XranConfig> xranConfigFactory =
97 new ConfigFactory<ApplicationId, XranConfig>(
98 SubjectFactories.APP_SUBJECT_FACTORY, CONFIG_CLASS, "xran") {
99 @Override
100 public XranConfig createConfig() {
101 return new XranConfig();
102 }
103 };
104 /* WRAPPERS */
105 private CellMap cellMap;
106 private UeMap ueMap;
107 private LinkMap linkMap;
108 /* MAPS */
109 private ConcurrentMap<String, ECGI> legitCells = new ConcurrentHashMap<>();
slowr73b4eae2017-08-17 16:09:09 -0700110 private ConcurrentMap<ECGI, SynchronousQueue<String>> hoQueue = new ConcurrentHashMap<>();
slowr89c2ac12017-08-15 16:20:06 -0700111 private ConcurrentMap<ECGI, SynchronousQueue<String>> RRMCellQueue = new ConcurrentHashMap<>();
112 private ConcurrentMap<CRNTI, SynchronousQueue<String>> scellAddQueue = new ConcurrentHashMap<>();
113 private ConcurrentMap<CRNTI, SynchronousQueue<String>> scellDeleteQueue = new ConcurrentHashMap<>();
slowr13fa5b02017-08-08 16:32:31 -0700114 /* AGENTS */
115 private InternalXranDeviceAgent deviceAgent = new InternalXranDeviceAgent();
116 private InternalXranHostAgent hostAgent = new InternalXranHostAgent();
117 private InternalXranPacketAgent packetAgent = new InternalXranPacketAgent();
118 /* LISTENERS */
119 private Set<XranDeviceListener> xranDeviceListeners = new CopyOnWriteArraySet<>();
120 private Set<XranHostListener> xranHostListeners = new CopyOnWriteArraySet<>();
121 private InternalDeviceListener device_listener = new InternalDeviceListener();
122 private InternalHostListener host_listener = new InternalHostListener();
123
124 @Activate
125 public void activate() {
126 appId = coreService.registerApplication(XRAN_APP_ID);
127
128 configService.addListener(configListener);
129 registry.registerConfigFactory(xranConfigFactory);
130 deviceService.addListener(device_listener);
131 hostService.addListener(host_listener);
132
133 cellMap = new CellMap(xranStore);
134 ueMap = new UeMap(xranStore);
135 linkMap = new LinkMap(xranStore);
136
137 xranStore.setController(this);
138
139 log.info("XRAN Controller Started");
140 }
141
142 @Deactivate
143 public void deactivate() {
144 controller.stop();
145
146 deviceService.removeListener(device_listener);
147 hostService.removeListener(host_listener);
148
149 legitCells.clear();
150
151 configService.removeListener(configListener);
152 registry.unregisterConfigFactory(xranConfigFactory);
153
154 log.info("XRAN Controller Stopped");
155 }
156
157 @Override
slowr73b4eae2017-08-17 16:09:09 -0700158 public SynchronousQueue<String> sendHORequest(RnibLink link_t, RnibLink link_s) {
159 ECGI ecgi_t = link_t.getLinkId().getEcgi(),
160 ecgi_s = link_s.getLinkId().getEcgi();
161
162 CRNTI crnti = linkMap.getCrnti(link_t.getLinkId().getMmeues1apid());
163 ChannelHandlerContext ctx_t = cellMap.getCtx(ecgi_t),
164 ctx_s = cellMap.getCtx(ecgi_s);
slowr67d05e42017-08-11 20:37:22 -0700165
166 try {
slowr73b4eae2017-08-17 16:09:09 -0700167 XrancPdu xrancPdu = HORequest.constructPacket(crnti, ecgi_s, ecgi_t);
168 ctx_t.writeAndFlush(getSctpMessage(xrancPdu));
169 ctx_s.writeAndFlush(getSctpMessage(xrancPdu));
slowr67d05e42017-08-11 20:37:22 -0700170 } catch (IOException e) {
171 e.printStackTrace();
172 }
173
174 SynchronousQueue<String> queue = new SynchronousQueue<>();
slowr73b4eae2017-08-17 16:09:09 -0700175 hoQueue.put(ecgi_s, queue);
slowr67d05e42017-08-11 20:37:22 -0700176
177 return queue;
178 }
179
180 @Override
slowr13fa5b02017-08-08 16:32:31 -0700181 public void addListener(XranDeviceListener listener) {
182 xranDeviceListeners.add(listener);
183 }
184
185 @Override
186 public void addListener(XranHostListener listener) {
187 xranHostListeners.add(listener);
188 }
189
190 @Override
191 public void removeListener(XranDeviceListener listener) {
192 xranDeviceListeners.remove(listener);
193 }
194
195 @Override
196 public void removeListener(XranHostListener listener) {
197 xranHostListeners.remove(listener);
198 }
199
slowr67d05e42017-08-11 20:37:22 -0700200 @Override
slowr8ddc2b12017-08-14 14:13:38 -0700201 public SynchronousQueue<String> sendModifiedRRMConf(RRMConfig rrmConfig, boolean xICIC) {
202 ECGI ecgi = rrmConfig.getEcgi();
slowr67d05e42017-08-11 20:37:22 -0700203 ChannelHandlerContext ctx = cellMap.getCtx(ecgi);
204 try {
slowr73b4eae2017-08-17 16:09:09 -0700205 XrancPdu pdu = null;
206
slowr8ddc2b12017-08-14 14:13:38 -0700207 if (xICIC) {
slowr73b4eae2017-08-17 16:09:09 -0700208 CellConfigReport cellConfigReport = cellMap.get(ecgi).getConf();
209 if (cellConfigReport != null) {
210 pdu = XICICConfig.constructPacket(rrmConfig, cellConfigReport);
211 ctx.writeAndFlush(getSctpMessage(pdu));
212 }
slowr8ddc2b12017-08-14 14:13:38 -0700213 } else {
214 pdu = RRMConfig.constructPacket(rrmConfig);
slowr73b4eae2017-08-17 16:09:09 -0700215 ctx.writeAndFlush(getSctpMessage(pdu));
216 SynchronousQueue<String> queue = new SynchronousQueue<>();
217 RRMCellQueue.put(ecgi, queue);
218 return queue;
slowr8ddc2b12017-08-14 14:13:38 -0700219 }
slowr67d05e42017-08-11 20:37:22 -0700220 } catch (IOException e) {
221 e.printStackTrace();
222 }
slowr67d05e42017-08-11 20:37:22 -0700223
slowr73b4eae2017-08-17 16:09:09 -0700224 return null;
slowr67d05e42017-08-11 20:37:22 -0700225 }
226
slowr89c2ac12017-08-15 16:20:06 -0700227 @Override
228 public SynchronousQueue<String> sendScellAdd(RnibLink link) {
229 RnibCell secondaryCell = link.getLinkId().getCell(),
230 primaryCell = linkMap.getPrimaryCell(link.getLinkId().getUe());
231 ECGI primaryEcgi = primaryCell.getEcgi();
232 ChannelHandlerContext ctx = cellMap.getCtx(primaryEcgi);
233
234 CRNTI crnti = linkMap.getCrnti(link.getLinkId().getMmeues1apid());
235
236 CellConfigReport cellReport = secondaryCell.getConf();
237
238 if (cellReport != null) {
239 PCIARFCN pciarfcn = new PCIARFCN();
240 pciarfcn.setPci(cellReport.getPci());
241 pciarfcn.setEarfcnDl(cellReport.getEarfcnDl());
242
243 PropScell propScell = new PropScell();
244 propScell.setPciArfcn(pciarfcn);
245
246 XrancPdu pdu = ScellAdd.constructPacket(primaryEcgi, crnti, propScell);
247 try {
248 ctx.writeAndFlush(getSctpMessage(pdu));
249 SynchronousQueue<String> queue = new SynchronousQueue<>();
250 scellAddQueue.put(crnti, queue);
251
252 return queue;
253 } catch (IOException e) {
254 log.error(ExceptionUtils.getFullStackTrace(e));
255 e.printStackTrace();
256 }
257 }
258 return null;
259 }
260
261 @Override
262 public boolean sendScellDelete(RnibLink link) {
263 RnibCell secondaryCell = link.getLinkId().getCell(),
264 primaryCell = linkMap.getPrimaryCell(link.getLinkId().getUe());
265 ECGI primaryEcgi = primaryCell.getEcgi();
266 ChannelHandlerContext ctx = cellMap.getCtx(primaryEcgi);
267
268 CRNTI crnti = linkMap.getCrnti(link.getLinkId().getMmeues1apid());
269
270 CellConfigReport cellReport = secondaryCell.getConf();
271
272 if (cellReport != null) {
273 PCIARFCN pciarfcn = new PCIARFCN();
274 pciarfcn.setPci(cellReport.getPci());
275 pciarfcn.setEarfcnDl(cellReport.getEarfcnDl());
276
277 XrancPdu pdu = ScellDelete.constructPacket(primaryEcgi, crnti, pciarfcn);
278
279 try {
280 ctx.writeAndFlush(getSctpMessage(pdu));
281 link.setType(RnibLink.Type.NON_SERVING);
282 return true;
283 } catch (IOException e) {
284 log.error(ExceptionUtils.getFullStackTrace(e));
285 e.printStackTrace();
286 }
287 }
288 return false;
289 }
290
slowr13fa5b02017-08-08 16:32:31 -0700291 private void restartTimer(RnibUe ue) {
292 Timer timer = new Timer();
293 ue.setTimer(timer);
294 log.info("Starting UE timer...");
295 timer.schedule(new TimerTask() {
296 @Override
297 public void run() {
slowr67d05e42017-08-11 20:37:22 -0700298 if (ue.getState() == RnibUe.State.IDLE) {
slowr13fa5b02017-08-08 16:32:31 -0700299 hostAgent.removeConnectedHost(ue);
300 log.info("UE is removed after 10 seconds of IDLE");
301 } else {
302 log.info("UE not removed cause its ACTIVE");
303 }
304 }
305 }, 10000);
306 }
307
308 private void restartTimer(RnibLink link) {
309 Timer timer = new Timer();
310 link.setTimer(timer);
311 log.info("Starting Link timer...");
312 timer.schedule(new TimerTask() {
313 @Override
314 public void run() {
315 LinkId linkId = link.getLinkId();
316 xranStore.removeLink(linkId);
317 log.info("Link is removed after not receiving Meas Reports for 10 seconds");
318 }
319 }, 10000);
320
321 }
322
323 class InternalDeviceListener implements DeviceListener {
324
325 @Override
326 public void event(DeviceEvent event) {
327 log.info("Device Event {}", event);
328 switch (event.type()) {
329 case DEVICE_ADDED: {
330 try {
331 ECGI ecgi = decodeDeviceId(event.subject().id());
332 RnibCell cell = cellMap.get(ecgi);
333 if (cell != null) {
334 Timer timer = new Timer();
335 timer.scheduleAtFixedRate(
336 new TimerTask() {
337 @Override
338 public void run() {
339 CellConfigReport conf = cell.getConf();
340 if (conf == null) {
341 try {
342 ChannelHandlerContext ctx = cellMap.getCtx(ecgi);
slowr8ddc2b12017-08-14 14:13:38 -0700343 XrancPdu xrancPdu = CellConfigRequest.constructPacket(ecgi);
slowr67d05e42017-08-11 20:37:22 -0700344 ctx.writeAndFlush(getSctpMessage(xrancPdu));
slowr13fa5b02017-08-08 16:32:31 -0700345 } catch (IOException e) {
346 log.error(ExceptionUtils.getFullStackTrace(e));
347 e.printStackTrace();
348 }
349 } else {
350 // FIXME: maybe remove this map.
351 cellMap.putPciArfcn(cell);
352 try {
353 ChannelHandlerContext ctx = cellMap.
354 getCtx(ecgi);
slowr8ddc2b12017-08-14 14:13:38 -0700355 XrancPdu xrancPdu = L2MeasConfig.constructPacket(ecgi, xranConfig.getL2MeasInterval());
slowr13fa5b02017-08-08 16:32:31 -0700356 cell.setMeasConfig(xrancPdu.getBody().getL2MeasConfig());
slowr67d05e42017-08-11 20:37:22 -0700357 SctpMessage sctpMessage = getSctpMessage(xrancPdu);
slowr13fa5b02017-08-08 16:32:31 -0700358 ctx.writeAndFlush(sctpMessage);
359 } catch (IOException e) {
360 log.error(ExceptionUtils.getFullStackTrace(e));
361 e.printStackTrace();
362 }
363 timer.cancel();
364 timer.purge();
365 }
366 }
367 },
368 0,
369 xranConfig.getConfigRequestInterval() * 1000
370 );
371 }
372 } catch (IOException e) {
373 log.error(ExceptionUtils.getFullStackTrace(e));
374 e.printStackTrace();
375 }
376 break;
377 }
378 default: {
379 break;
380 }
381 }
382 }
383 }
384
385 class InternalHostListener implements HostListener {
386
387 @Override
388 public void event(HostEvent event) {
389 log.info("Host Event {}", event);
390 switch (event.type()) {
391 case HOST_ADDED:
392 case HOST_MOVED: {
393 RnibUe ue = ueMap.get(hostIdtoMME(event.subject().id()));
394 if (ue != null) {
slowr89c2ac12017-08-15 16:20:06 -0700395 ECGI ecgi_primary = linkMap.getPrimaryCell(ue).getEcgi();
slowr13fa5b02017-08-08 16:32:31 -0700396 RnibCell primary = cellMap.get(ecgi_primary);
397 ue.setMeasConfig(null);
398 if (primary != null) {
399 Timer timer = new Timer();
400 timer.scheduleAtFixedRate(
401 new TimerTask() {
402 @Override
403 public void run() {
slowred74ec72017-08-17 11:25:01 -0700404 if (ue.getCapability() == null && primary.getVersion() >= 3) {
slowr13fa5b02017-08-08 16:32:31 -0700405 try {
406 ChannelHandlerContext ctx = cellMap.getCtx(primary.getEcgi());
slowr8ddc2b12017-08-14 14:13:38 -0700407 XrancPdu xrancPdu = UECapabilityEnquiry.constructPacket(
slowr67d05e42017-08-11 20:37:22 -0700408 primary.getEcgi(),
409 ue.getRanId());
410 ctx.writeAndFlush(getSctpMessage(xrancPdu));
slowr13fa5b02017-08-08 16:32:31 -0700411 } catch (IOException e) {
412 log.warn(ExceptionUtils.getFullStackTrace(e));
413 e.printStackTrace();
414 }
415 } else {
slowr13fa5b02017-08-08 16:32:31 -0700416 timer.cancel();
417 timer.purge();
418 }
419 }
420 },
421 0,
422 xranConfig.getConfigRequestInterval() * 1000
423 );
slowred74ec72017-08-17 11:25:01 -0700424 if (ue.getMeasConfig() == null) {
425 try {
426 ChannelHandlerContext ctx = cellMap.getCtx(primary.getEcgi());
427 RXSigMeasConfig.MeasCells measCells = new RXSigMeasConfig.MeasCells();
428 xranStore.getCellNodes().forEach(cell -> {
429 CellConfigReport cellReport = ((RnibCell) cell).getConf();
430 if (cellReport != null) {
431 PCIARFCN pciarfcn = new PCIARFCN();
432 pciarfcn.setPci(cellReport.getPci());
433 pciarfcn.setEarfcnDl(cellReport.getEarfcnDl());
434 measCells.setPCIARFCN(pciarfcn);
435 }
436 });
437 XrancPdu xrancPdu = RXSigMeasConfig.constructPacket(
438 primary.getEcgi(),
439 ue.getRanId(),
440 measCells,
441 xranConfig.getRxSignalInterval()
442 );
443 ue.setMeasConfig(xrancPdu.getBody().getRXSigMeasConfig());
444 ctx.writeAndFlush(getSctpMessage(xrancPdu));
445 } catch (IOException e) {
446 log.warn(ExceptionUtils.getFullStackTrace(e));
447 e.printStackTrace();
448 }
449 }
slowr13fa5b02017-08-08 16:32:31 -0700450 }
451 }
452 break;
453 }
454 default: {
455 break;
456 }
457 }
458 }
459 }
460
461 public class InternalXranDeviceAgent implements XranDeviceAgent {
462
463 private final Logger log = LoggerFactory.getLogger(InternalXranDeviceAgent.class);
464
465 @Override
466 public boolean addConnectedCell(String host, ChannelHandlerContext ctx) {
467 ECGI ecgi = legitCells.get(host);
468
469 if (ecgi == null) {
470 log.error("Device is not a legit source; ignoring...");
471 } else {
472 log.info("Device exists in configuration; registering...");
473 RnibCell storeCell = cellMap.get(ecgi);
474 if (storeCell == null) {
475 storeCell = new RnibCell();
476 storeCell.setEcgi(ecgi);
477 cellMap.put(storeCell, ctx);
478
479 for (XranDeviceListener l : xranDeviceListeners) {
480 l.deviceAdded(storeCell);
481 }
482 return true;
483 } else {
484 log.error("Device already registered; ignoring...");
485 }
486 }
487 ctx.close();
488 return false;
489 }
490
491 @Override
492 public boolean removeConnectedCell(String host) {
493 ECGI ecgi = legitCells.get(host);
494 List<RnibLink> linksByECGI = xranStore.getLinksByECGI(ecgi);
495
496 linksByECGI.forEach(rnibLink -> xranStore.removeLink(rnibLink.getLinkId()));
497
498 if (cellMap.remove(ecgi)) {
499 for (XranDeviceListener l : xranDeviceListeners) {
500 l.deviceRemoved(deviceId(uri(ecgi)));
501 }
502 return true;
503 }
504 return false;
505 }
506 }
507
508 public class InternalXranHostAgent implements XranHostAgent {
509
510 @Override
511 public boolean addConnectedHost(RnibUe ue, RnibCell cell, ChannelHandlerContext ctx) {
512
513 if (ueMap.get(ue.getMmeS1apId()) != null) {
514 linkMap.putPrimaryLink(cell, ue);
515
516 Set<ECGI> ecgiSet = xranStore.getLinksByUeId(ue.getMmeS1apId().longValue())
517 .stream()
slowr8ddc2b12017-08-14 14:13:38 -0700518 .map(l -> l.getLinkId().getEcgi())
slowr13fa5b02017-08-08 16:32:31 -0700519 .collect(Collectors.toSet());
520
521 for (XranHostListener l : xranHostListeners) {
522 l.hostAdded(ue, ecgiSet);
523 }
524 return true;
525 } else {
526 ueMap.put(ue);
527 linkMap.putPrimaryLink(cell, ue);
528
529 Set<ECGI> ecgiSet = Sets.newConcurrentHashSet();
530 ecgiSet.add(cell.getEcgi());
531 for (XranHostListener l : xranHostListeners) {
532 l.hostAdded(ue, ecgiSet);
533 }
534 return true;
535 }
536
537 }
538
539 @Override
540 public boolean removeConnectedHost(RnibUe ue) {
541 List<RnibLink> links = xranStore.getLinksByUeId(ue.getMmeS1apId().longValue());
542 links.forEach(rnibLink -> xranStore.removeLink(rnibLink.getLinkId()));
543 if (ueMap.remove(ue.getMmeS1apId())) {
544 for (XranHostListener l : xranHostListeners) {
545 l.hostRemoved(ue.getHostId());
546 }
547 return true;
548 }
549 return false;
550 }
551 }
552
553 public class InternalXranPacketAgent implements XranPacketProcessor {
554 @Override
555 public void handlePacket(XrancPdu recv_pdu, ChannelHandlerContext ctx) throws IOException {
556 XrancPdu send_pdu;
557
558 int apiID = recv_pdu.getHdr().getApiId().intValue();
559 log.debug("Received message: {}", recv_pdu);
560 switch (apiID) {
561 case 1: {
562 // Decode Cell config report.
563 CellConfigReport report = recv_pdu.getBody().getCellConfigReport();
564
565 ECGI ecgi = report.getEcgi();
566
567 RnibCell cell = xranStore.getCell(ecgi);
slowr8ddc2b12017-08-14 14:13:38 -0700568 cell.setVersion(recv_pdu.getHdr().getVer().toString());
slowr13fa5b02017-08-08 16:32:31 -0700569 cell.setConf(report);
570
571 break;
572 }
573 case 2: {
574 // Decode UE Admission Request.
575 UEAdmissionRequest ueAdmissionRequest = recv_pdu.getBody().getUEAdmissionRequest();
576
577 ECGI ecgi = ueAdmissionRequest.getEcgi();
578 if (xranStore.getCell(ecgi) != null) {
579 CRNTI crnti = ueAdmissionRequest.getCrnti();
slowr8ddc2b12017-08-14 14:13:38 -0700580 send_pdu = UEAdmissionResponse.constructPacket(ecgi, crnti, xranConfig.admissionFlag());
slowr67d05e42017-08-11 20:37:22 -0700581 ctx.writeAndFlush(getSctpMessage(send_pdu));
slowr13fa5b02017-08-08 16:32:31 -0700582 } else {
583 log.warn("Could not find ECGI in registered cells: {}", ecgi);
584 }
585 break;
586 }
587 case 4: {
588 // Decode UE Admission Status.
589 UEAdmissionStatus ueAdmissionStatus = recv_pdu.getBody().getUEAdmissionStatus();
590
591 RnibUe ue = ueMap.get(ueAdmissionStatus.getCrnti());
592 if (ue != null) {
593 if (ueAdmissionStatus.getAdmEstStatus().value.intValue() == 0) {
slowr67d05e42017-08-11 20:37:22 -0700594 ue.setState(RnibUe.State.ACTIVE);
slowr13fa5b02017-08-08 16:32:31 -0700595 } else {
slowr67d05e42017-08-11 20:37:22 -0700596 ue.setState(RnibUe.State.IDLE);
slowr13fa5b02017-08-08 16:32:31 -0700597 }
598 }
599 break;
600 }
601 case 5: {
602 // Decode UE Admission Context Update.
slowr8ddc2b12017-08-14 14:13:38 -0700603 UEContextUpdate ueContextUpdate = recv_pdu.getBody().getUEContextUpdate();
slowr13fa5b02017-08-08 16:32:31 -0700604
slowr8ddc2b12017-08-14 14:13:38 -0700605 RnibUe ue = ueMap.get(ueContextUpdate.getMMEUES1APID());
slowr73b4eae2017-08-17 16:09:09 -0700606 RnibCell cell = xranStore.getCell(ueContextUpdate.getEcgi());
607 if (ue == null) {
608 ue = new RnibUe();
slowr13fa5b02017-08-08 16:32:31 -0700609 }
slowr73b4eae2017-08-17 16:09:09 -0700610
611 ue.setMmeS1apId(ueContextUpdate.getMMEUES1APID());
612 ue.setEnbS1apId(ueContextUpdate.getENBUES1APID());
613 ue.setRanId(ueContextUpdate.getCrnti());
614
615 hostAgent.addConnectedHost(ue, cell, ctx);
616
slowr13fa5b02017-08-08 16:32:31 -0700617 break;
618 }
619 case 6: {
620 // Decode UE Reconfig_Ind.
621 UEReconfigInd ueReconfigInd = recv_pdu.getBody().getUEReconfigInd();
622 RnibUe ue = ueMap.get(ueReconfigInd.getCrntiOld());
623
624 if (ue != null) {
625 ue.setRanId(ueReconfigInd.getCrntiNew());
626 } else {
627 log.warn("Could not find UE with this CRNTI: {}", ueReconfigInd.getCrntiOld());
628 }
629 break;
630 }
631 case 7: {
632 // If xRANc wants to deactivate UE, we pass UEReleaseInd from xRANc to eNB.
633 // Decode UE Release_Ind.
634 UEReleaseInd ueReleaseInd = recv_pdu.getBody().getUEReleaseInd();
635 RnibUe ue = ueMap.get(ueReleaseInd.getCrnti());
636 if (ue != null) {
slowr67d05e42017-08-11 20:37:22 -0700637 ue.setState(RnibUe.State.IDLE);
slowr13fa5b02017-08-08 16:32:31 -0700638 restartTimer(ue);
639 }
640 break;
641 }
642 case 8: {
643 // Decode Bearer Adm Request
644 BearerAdmissionRequest bearerAdmissionRequest = recv_pdu.getBody().getBearerAdmissionRequest();
645
646 ECGI ecgi = bearerAdmissionRequest.getEcgi();
647 CRNTI crnti = bearerAdmissionRequest.getCrnti();
648 ERABParams erabParams = bearerAdmissionRequest.getErabParams();
649 RnibLink link = linkMap.get(ecgi, crnti);
650 if (link != null) {
651 link.setBearerParameters(erabParams);
652 } else {
653 log.warn("Could not find link between {}-{}", ecgi, crnti);
654 }
655
656 BerInteger numErabs = bearerAdmissionRequest.getNumErabs();
slowr8ddc2b12017-08-14 14:13:38 -0700657 // Encode and send Bearer Admission Response
658 send_pdu = BearerAdmissionResponse.constructPacket(ecgi, crnti, erabParams, numErabs, xranConfig.bearerFlag());
slowr67d05e42017-08-11 20:37:22 -0700659 ctx.writeAndFlush(getSctpMessage(send_pdu));
slowr13fa5b02017-08-08 16:32:31 -0700660 break;
661 }
662 case 10: {
663 //Decode Bearer Admission Status
664 BearerAdmissionStatus bearerAdmissionStatus = recv_pdu.getBody().getBearerAdmissionStatus();
slowr8ddc2b12017-08-14 14:13:38 -0700665 break;
slowr13fa5b02017-08-08 16:32:31 -0700666// ECGI ecgi = bearerAdmissionStatus.getEcgi();
667// CRNTI crnti = bearerAdmissionStatus.getCrnti();
668//
669// RnibLink link = linkMap.get(ecgi, crnti);
slowr13fa5b02017-08-08 16:32:31 -0700670 }
671 case 11: {
672 //Decode Bearer Release Ind
673 BearerReleaseInd bearerReleaseInd = recv_pdu.getBody().getBearerReleaseInd();
674
675 ECGI ecgi = bearerReleaseInd.getEcgi();
676 CRNTI crnti = bearerReleaseInd.getCrnti();
677 RnibLink link = linkMap.get(ecgi, crnti);
678
679 List<ERABID> erabidsRelease = bearerReleaseInd.getErabIds().getERABID();
680 List<ERABParamsItem> erabParamsItem = link.getBearerParameters().getERABParamsItem();
681
682 List<ERABParamsItem> unreleased = erabParamsItem
683 .stream()
684 .filter(item -> {
685 Optional<ERABID> any = erabidsRelease.stream().filter(id -> id.equals(item.getId())).findAny();
686 return !any.isPresent();
687 }).collect(Collectors.toList());
688
689 link.getBearerParameters().setERABParamsItem(new ArrayList<>(unreleased));
slowr13fa5b02017-08-08 16:32:31 -0700690 break;
691 }
692 case 13: {
slowr67d05e42017-08-11 20:37:22 -0700693 HOFailure hoFailure = recv_pdu.getBody().getHOFailure();
694
695 try {
slowr73b4eae2017-08-17 16:09:09 -0700696 hoQueue.get(hoFailure.getEcgi())
slowr67d05e42017-08-11 20:37:22 -0700697 .put("Hand Over Failed with cause: " + hoFailure.getCause());
698 } catch (InterruptedException e) {
699 log.error(ExceptionUtils.getFullStackTrace(e));
700 e.printStackTrace();
701 } finally {
slowr73b4eae2017-08-17 16:09:09 -0700702 hoQueue.remove(hoFailure.getEcgi());
slowr67d05e42017-08-11 20:37:22 -0700703 }
704 break;
slowr8ddc2b12017-08-14 14:13:38 -0700705
slowr67d05e42017-08-11 20:37:22 -0700706 }
slowr8ddc2b12017-08-14 14:13:38 -0700707 case 14: {
slowr67d05e42017-08-11 20:37:22 -0700708 HOComplete hoComplete = recv_pdu.getBody().getHOComplete();
709
slowr67d05e42017-08-11 20:37:22 -0700710 try {
slowr73b4eae2017-08-17 16:09:09 -0700711 hoQueue.get(hoComplete.getEcgiS())
slowr67d05e42017-08-11 20:37:22 -0700712 .put("Hand Over Completed");
713 } catch (InterruptedException e) {
714 log.error(ExceptionUtils.getFullStackTrace(e));
715 e.printStackTrace();
716 } finally {
slowr73b4eae2017-08-17 16:09:09 -0700717 hoQueue.remove(hoComplete.getEcgiS());
slowr67d05e42017-08-11 20:37:22 -0700718 }
719 break;
720 }
slowr8ddc2b12017-08-14 14:13:38 -0700721
722 case 16: {
slowr13fa5b02017-08-08 16:32:31 -0700723 // Decode RX Sig Meas Report.
724 RXSigMeasReport rxSigMeasReport = recv_pdu.getBody().getRXSigMeasReport();
725 List<RXSigReport> rxSigReportList = rxSigMeasReport.getCellMeasReports().getRXSigReport();
726
727 if (!rxSigReportList.isEmpty()) {
728 rxSigReportList.forEach(rxSigReport -> {
729 RnibCell cell = cellMap.get(rxSigReport.getPciArfcn());
730 if (cell != null) {
731 ECGI ecgi = cell.getEcgi();
732 RnibLink link = linkMap.get(ecgi, rxSigMeasReport.getCrnti());
733 if (link == null) {
734 log.warn("Could not find link between: {}-{} | Creating non-serving link..", ecgi, rxSigMeasReport.getCrnti());
735 link = linkMap.putNonServingLink(cell, rxSigMeasReport.getCrnti());
736
737 if (link != null) {
738 restartTimer(link);
739 }
740 }
741
742 if (link != null) {
743 RSRQRange rsrq = rxSigReport.getRsrq();
744 RSRPRange rsrp = rxSigReport.getRsrp();
745
746 RnibLink.LinkQuality quality = link.getQuality();
slowrc153ad92017-08-16 19:47:52 -0700747 quality.setRX(new RnibLink.LinkQuality.RX(
748 rsrp.value.intValue() - 140,
749 (rsrq.value.intValue() * 0.5) - 19.5
750 ));
slowr13fa5b02017-08-08 16:32:31 -0700751 }
752 } else {
753 log.warn("Could not find cell with PCI-ARFCN: {}", rxSigReport.getPciArfcn());
754 }
755 });
756 }
757 break;
758 }
slowr8ddc2b12017-08-14 14:13:38 -0700759 case 18: {
slowr13fa5b02017-08-08 16:32:31 -0700760 RadioMeasReportPerUE radioMeasReportPerUE = recv_pdu.getBody().getRadioMeasReportPerUE();
761
762 List<RadioRepPerServCell> servCells = radioMeasReportPerUE.getRadioReportServCells().getRadioRepPerServCell();
763
764 servCells.forEach(servCell -> {
765 RnibCell cell = cellMap.get(servCell.getPciArfcn());
766 if (cell != null) {
767 RnibLink link = linkMap.get(cell.getEcgi(), radioMeasReportPerUE.getCrnti());
768 if (link != null) {
769 RadioRepPerServCell.CqiHist cqiHist = servCell.getCqiHist();
770 RnibLink.LinkQuality quality = link.getQuality();
slowr13fa5b02017-08-08 16:32:31 -0700771
772 final double[] values = {0, 0, 0};
slowrd3d7b412017-08-17 11:15:44 -0700773 final int[] i = {1};
slowr13fa5b02017-08-08 16:32:31 -0700774 cqiHist.getBerInteger().forEach(value -> {
775 values[0] = Math.max(values[0], value.intValue());
slowrd3d7b412017-08-17 11:15:44 -0700776 values[1] += i[0] * value.intValue();
slowr13fa5b02017-08-08 16:32:31 -0700777 values[2] += value.intValue();
slowrd3d7b412017-08-17 11:15:44 -0700778 i[0]++;
slowr13fa5b02017-08-08 16:32:31 -0700779 });
780
slowrc153ad92017-08-16 19:47:52 -0700781 quality.setCQI(new RnibLink.LinkQuality.CQI(
782 cqiHist,
783 values[0],
784 values[1] / values[0]
785 ));
slowr13fa5b02017-08-08 16:32:31 -0700786
787 } else {
788 log.warn("Could not find link between: {}-{}", cell.getEcgi(), radioMeasReportPerUE.getCrnti());
789 }
790 } else {
791 log.warn("Could not find cell with PCI-ARFCN: {}", servCell.getPciArfcn());
792 }
793 });
slowr67d05e42017-08-11 20:37:22 -0700794 break;
slowr13fa5b02017-08-08 16:32:31 -0700795 }
slowr8ddc2b12017-08-14 14:13:38 -0700796 case 19: {
slowr13fa5b02017-08-08 16:32:31 -0700797 RadioMeasReportPerCell radioMeasReportPerCell = recv_pdu.getBody().getRadioMeasReportPerCell();
798 break;
799 }
slowr8ddc2b12017-08-14 14:13:38 -0700800 case 20: {
slowr13fa5b02017-08-08 16:32:31 -0700801 SchedMeasReportPerUE schedMeasReportPerUE = recv_pdu.getBody().getSchedMeasReportPerUE();
slowr8ddc2b12017-08-14 14:13:38 -0700802 List<SchedMeasRepPerServCell> servCells = schedMeasReportPerUE.getSchedReportServCells()
803 .getSchedMeasRepPerServCell();
slowr13fa5b02017-08-08 16:32:31 -0700804
805 servCells.forEach(servCell -> {
806 RnibCell cell = cellMap.get(servCell.getPciArfcn());
807 if (cell != null) {
808 RnibLink link = linkMap.get(cell.getEcgi(), schedMeasReportPerUE.getCrnti());
809 if (link != null) {
slowrc153ad92017-08-16 19:47:52 -0700810 link.getQuality().setMCS(new RnibLink.LinkQuality.MCS(
811 servCell.getMcsDl(),
812 servCell.getMcsUl()
813 ));
slowr13fa5b02017-08-08 16:32:31 -0700814
slowrc153ad92017-08-16 19:47:52 -0700815 link.setResourceUsage(new RnibLink.ResourceUsage(
816 servCell.getPrbUsage().getPrbUsageDl(),
817 servCell.getPrbUsage().getPrbUsageUl()
818 ));
slowr13fa5b02017-08-08 16:32:31 -0700819 } else {
slowr8ddc2b12017-08-14 14:13:38 -0700820 log.warn("Could not find link between: {}-{}", cell.getEcgi(),
821 schedMeasReportPerUE.getCrnti());
slowr13fa5b02017-08-08 16:32:31 -0700822 }
823 } else {
824 log.warn("Could not find cell with PCI-ARFCN: {}", servCell.getPciArfcn());
825 }
826 });
827 break;
828 }
slowr8ddc2b12017-08-14 14:13:38 -0700829 case 21: {
slowr13fa5b02017-08-08 16:32:31 -0700830 SchedMeasReportPerCell schedMeasReportPerCell = recv_pdu.getBody().getSchedMeasReportPerCell();
slowr13fa5b02017-08-08 16:32:31 -0700831 RnibCell cell = cellMap.get(schedMeasReportPerCell.getEcgi());
832 if (cell != null) {
slowrc153ad92017-08-16 19:47:52 -0700833 cell.setPrbUsage(new RnibCell.PrbUsageContainer(
834 schedMeasReportPerCell.getPrbUsagePcell(),
835 schedMeasReportPerCell.getPrbUsageScell()
836 ));
837
slowr13fa5b02017-08-08 16:32:31 -0700838 cell.setQci(schedMeasReportPerCell.getQciVals());
839 } else {
840 log.warn("Could not find cell with ECGI: {}", schedMeasReportPerCell.getEcgi());
841 }
842 break;
843 }
slowr8ddc2b12017-08-14 14:13:38 -0700844 case 22: {
slowr13fa5b02017-08-08 16:32:31 -0700845 PDCPMeasReportPerUe pdcpMeasReportPerUe = recv_pdu.getBody().getPDCPMeasReportPerUe();
846
847 RnibLink link = linkMap.get(pdcpMeasReportPerUe.getEcgi(), pdcpMeasReportPerUe.getCrnti());
848 if (link != null) {
slowrc153ad92017-08-16 19:47:52 -0700849 link.setPdcpThroughput(new RnibLink.PDCPThroughput(
850 pdcpMeasReportPerUe.getThroughputDl(),
851 pdcpMeasReportPerUe.getThroughputUl()
852 ));
853
854 link.setPdcpPackDelay(new RnibLink.PDCPPacketDelay(
855 pdcpMeasReportPerUe.getPktDelayDl(),
856 pdcpMeasReportPerUe.getPktDelayUl()
857 ));
slowr13fa5b02017-08-08 16:32:31 -0700858 } else {
slowr8ddc2b12017-08-14 14:13:38 -0700859 log.warn("Could not find link between: {}-{}", pdcpMeasReportPerUe.getEcgi(),
860 pdcpMeasReportPerUe.getCrnti());
slowr13fa5b02017-08-08 16:32:31 -0700861 }
862 break;
863 }
slowr8ddc2b12017-08-14 14:13:38 -0700864 case 24: {
865 // Decode UE Capability Info
866 UECapabilityInfo capabilityInfo = recv_pdu.getBody().getUECapabilityInfo();
867
868 RnibUe ue = ueMap.get(capabilityInfo.getCrnti());
869 if (ue != null) {
870 ue.setCapability(capabilityInfo);
871 } else {
872 log.warn("Could not find UE with this CRNTI: {}", capabilityInfo.getCrnti());
873 }
874 break;
875 }
876 case 25: {
877 // Don't know what will invoke sending UE CAPABILITY ENQUIRY
878 // Encode and send UE CAPABILITY ENQUIRY
879 UECapabilityEnquiry ueCapabilityEnquiry = recv_pdu.getBody().getUECapabilityEnquiry();
880 XrancPdu xrancPdu = UECapabilityEnquiry.constructPacket(ueCapabilityEnquiry.getEcgi(), ueCapabilityEnquiry.getCrnti());
881 ctx.writeAndFlush(getSctpMessage(xrancPdu));
882 break;
883 }
slowr89c2ac12017-08-15 16:20:06 -0700884 case 27: {
885 //Decode ScellAddStatus
886 ScellAddStatus scellAddStatus = recv_pdu.getBody().getScellAddStatus();
887 try {
888 scellAddQueue.get(scellAddStatus.getCrnti()).put("Scell's status: " + scellAddStatus.getStatus());
889 if (scellAddStatus.getStatus().getBerEnum().get(0).value.intValue() == 0) {
890
891 scellAddStatus.getScellsInd().getPCIARFCN().forEach(
892 pciarfcn -> {
893 RnibCell cell = cellMap.get(pciarfcn);
894 RnibLink link = linkMap.get(cell.getEcgi(), scellAddStatus.getCrnti());
895 link.setType(RnibLink.Type.SERVING_SECONDARY_CA);
896 }
897 );
898 } else {
899 log.error("Scell addition failed.");
900 }
901 } catch (InterruptedException e) {
902 log.error(ExceptionUtils.getFullStackTrace(e));
903 e.printStackTrace();
904 } finally {
905 scellAddQueue.remove(scellAddStatus.getCrnti());
906 }
907 break;
908 }
909 // TODO: 28: ScellDelete
slowr8ddc2b12017-08-14 14:13:38 -0700910
911 case 30: {
912 // Decode RRMConfig Status
slowr67d05e42017-08-11 20:37:22 -0700913 RRMConfigStatus rrmConfigStatus = recv_pdu.getBody().getRRMConfigStatus();
914 try {
915 RRMCellQueue.get(rrmConfigStatus.getEcgi())
916 .put("RRM Config's status: " + rrmConfigStatus.getStatus());
917 } catch (InterruptedException e) {
918 log.error(ExceptionUtils.getFullStackTrace(e));
919 e.printStackTrace();
920 } finally {
921 RRMCellQueue.remove(rrmConfigStatus.getEcgi());
922 }
923 break;
924 }
slowr8ddc2b12017-08-14 14:13:38 -0700925 //TODO Case 31: SeNBAdd 32: SeNBAddStatus 33: SeNBDelete
slowr13fa5b02017-08-08 16:32:31 -0700926 case 34: {
927 TrafficSplitConfig trafficSplitConfig = recv_pdu.getBody().getTrafficSplitConfig();
928
929 List<TrafficSplitPercentage> splitPercentages = trafficSplitConfig.getTrafficSplitPercent().getTrafficSplitPercentage();
930
931 splitPercentages.forEach(trafficSplitPercentage -> {
932 RnibCell cell = cellMap.get(trafficSplitPercentage.getEcgi());
933 if (cell != null) {
934 RnibLink link = linkMap.get(cell.getEcgi(), trafficSplitConfig.getCrnti());
935 if (link != null) {
936 link.setTrafficPercent(trafficSplitPercentage);
937 } else {
938 log.warn("Could not find link between: {}-{}", cell.getEcgi(), trafficSplitConfig.getCrnti());
939 }
940 } else {
941 log.warn("Could not find cell with ECGI: {}", trafficSplitConfig.getEcgi());
942 }
943 });
slowr67d05e42017-08-11 20:37:22 -0700944 break;
slowr13fa5b02017-08-08 16:32:31 -0700945 }
946 default: {
slowr60d4d102017-08-16 18:33:58 -0700947 log.warn("Wrong API ID: {}", recv_pdu);
slowr67d05e42017-08-11 20:37:22 -0700948 break;
slowr13fa5b02017-08-08 16:32:31 -0700949 }
950 }
951
952 }
953 }
954
955 class InternalNetworkConfigListener implements NetworkConfigListener {
956
957 @Override
958 public void event(NetworkConfigEvent event) {
959 switch (event.type()) {
960 case CONFIG_REGISTERED:
961 break;
962 case CONFIG_UNREGISTERED:
963 break;
964 case CONFIG_ADDED:
965 case CONFIG_UPDATED:
966 if (event.configClass() == CONFIG_CLASS) {
967 handleConfigEvent(event.config());
968 }
969 break;
970 case CONFIG_REMOVED:
971 break;
972 default:
973 break;
974 }
975 }
976
977 private void handleConfigEvent(Optional<Config> config) {
978 if (!config.isPresent()) {
979 return;
980 }
981
982 xranConfig = (XranConfig) config.get();
983
984 legitCells.putAll(xranConfig.activeCellSet());
985
986 controller.start(deviceAgent, hostAgent, packetAgent, xranConfig.getXrancPort());
987 }
988 }
989}