blob: 4acf0aedbbe96ad0a22df0dd57a5948e59a23ab7 [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.wrapper;
18
slowr67d05e42017-08-11 20:37:22 -070019import com.google.common.collect.BiMap;
20import com.google.common.collect.HashBiMap;
slowr13fa5b02017-08-08 16:32:31 -070021import org.onosproject.xran.XranStore;
22import org.onosproject.xran.codecs.api.CRNTI;
23import org.onosproject.xran.codecs.api.ECGI;
24import org.onosproject.xran.codecs.api.MMEUES1APID;
25import org.onosproject.xran.entities.RnibCell;
26import org.onosproject.xran.entities.RnibLink;
27import org.onosproject.xran.entities.RnibUe;
28import org.onosproject.xran.identifiers.LinkId;
29import org.slf4j.Logger;
30
31import java.util.List;
32import java.util.Optional;
slowr13fa5b02017-08-08 16:32:31 -070033
34import static org.slf4j.LoggerFactory.getLogger;
35
36public class LinkMap {
37 private static final Logger log = getLogger(LinkMap.class);
slowr67d05e42017-08-11 20:37:22 -070038 private final XranStore xranStore;
39 private BiMap<CRNTI, MMEUES1APID> crntiMme = HashBiMap.create();
slowr13fa5b02017-08-08 16:32:31 -070040
41 public LinkMap(XranStore xranStore) {
42 this.xranStore = xranStore;
43 }
44
45 public void putPrimaryLink(RnibCell cell, RnibUe ue) {
slowr67d05e42017-08-11 20:37:22 -070046 RnibLink link = new RnibLink(cell, ue);
slowr13fa5b02017-08-08 16:32:31 -070047 link.setType(RnibLink.Type.SERVING_PRIMARY);
slowr67d05e42017-08-11 20:37:22 -070048 synchronized (xranStore) {
49 xranStore.getLinksByUeId(ue.getMmeS1apId().longValue())
50 .forEach(l -> {
51 if (l.getType().equals(RnibLink.Type.SERVING_PRIMARY)) {
52 l.setType(RnibLink.Type.SERVING_SECONDARY);
53 }
54 });
55 xranStore.storeLink(link);
56 }
slowr13fa5b02017-08-08 16:32:31 -070057 crntiMme.put(ue.getRanId(), ue.getMmeS1apId());
58 }
59
60 public RnibLink putNonServingLink(RnibCell cell, CRNTI crnti) {
61 RnibLink link = null;
62 MMEUES1APID mmeues1APID = crntiMme.get(crnti);
63
64 if (mmeues1APID != null) {
slowr13fa5b02017-08-08 16:32:31 -070065 RnibUe ue = xranStore.getUe(mmeues1APID);
slowr67d05e42017-08-11 20:37:22 -070066 link = new RnibLink(cell, ue);
slowr13fa5b02017-08-08 16:32:31 -070067 xranStore.storeLink(link);
68 } else {
69 log.error("Could not find mapping for CRNTI to UE. Aborting creation of non-serving link");
70 }
71 return link;
72 }
73
74 public RnibLink get(ECGI src, MMEUES1APID dst) {
75 if (src != null && dst != null) {
76 return xranStore.getLink(src, dst);
77 }
78 return null;
79 }
80
81 public RnibLink get(ECGI src, CRNTI dst) {
82 MMEUES1APID mmeues1APID = crntiMme.get(dst);
83
84 if (mmeues1APID != null) {
85 return xranStore.getLink(src, mmeues1APID);
86 }
87 return null;
88 }
89
slowr67d05e42017-08-11 20:37:22 -070090 public CRNTI getCrnti(MMEUES1APID mme) {
91 return crntiMme.inverse().get(mme);
92 }
93
slowr13fa5b02017-08-08 16:32:31 -070094 public boolean remove(ECGI src, MMEUES1APID dst) {
95 RnibLink link = xranStore.getLink(src, dst);
96
97 if (link != null) {
98 LinkId linkId = link.getLinkId();
99 if (linkId != null) {
100 return xranStore.removeLink(linkId);
101 }
102 }
103 return false;
104 }
105
106 public boolean remove(ECGI src, CRNTI dst) {
107 MMEUES1APID mmeues1APID = crntiMme.get(dst);
108
109 RnibLink link = xranStore.getLink(src, mmeues1APID);
110 if (link != null) {
111 LinkId linkId = link.getLinkId();
112 if (linkId != null) {
113 return xranStore.removeLink(linkId);
114 }
115 }
116 return false;
117 }
118
119 public ECGI getPrimaryCell(RnibUe ue) {
120 List<RnibLink> linksByUeId = xranStore.getLinksByUeId(ue.getMmeS1apId().longValue());
121 Optional<RnibLink> primary = linksByUeId.stream().filter(l -> l.getType().equals(RnibLink.Type.SERVING_PRIMARY)).findFirst();
122
123 if (primary.isPresent()) {
slowr67d05e42017-08-11 20:37:22 -0700124 return primary.get().getLinkId().getSourceId();
slowr13fa5b02017-08-08 16:32:31 -0700125 }
126 return null;
127 }
128}