slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.xran.entities; |
| 18 | |
| 19 | import org.onlab.packet.MacAddress; |
| 20 | import org.onosproject.net.HostId; |
| 21 | import org.onosproject.xran.codecs.api.CRNTI; |
| 22 | import org.onosproject.xran.codecs.api.ENBUES1APID; |
| 23 | import org.onosproject.xran.codecs.api.MMEUES1APID; |
| 24 | import org.onosproject.xran.codecs.pdu.RXSigMeasConfig; |
| 25 | import org.onosproject.xran.codecs.pdu.UECapabilityInfo; |
| 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
| 29 | import java.net.URI; |
| 30 | import java.net.URISyntaxException; |
| 31 | import java.util.Timer; |
| 32 | import java.util.stream.Collectors; |
| 33 | import java.util.stream.Stream; |
| 34 | |
| 35 | import static org.onosproject.net.HostId.hostId; |
| 36 | |
| 37 | /** |
| 38 | * Created by dimitris on 7/22/17. |
| 39 | */ |
| 40 | public class RnibUe { |
| 41 | |
| 42 | private static final String SCHEME = "xran"; |
| 43 | |
| 44 | private static final Logger log = |
| 45 | LoggerFactory.getLogger(RnibUe.class); |
| 46 | |
| 47 | private String imsi; |
| 48 | private ENBUES1APID enbS1apId; |
| 49 | private MMEUES1APID mmeS1apId; |
| 50 | private CRNTI ranId; |
slowr | 67d05e4 | 2017-08-11 20:37:22 -0700 | [diff] [blame] | 51 | private State state; |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 52 | private UECapabilityInfo capability; |
| 53 | private RXSigMeasConfig measConfig; |
| 54 | private Timer timer; |
| 55 | |
| 56 | public RnibUe() { |
slowr | 67d05e4 | 2017-08-11 20:37:22 -0700 | [diff] [blame] | 57 | state = State.ACTIVE; |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 58 | timer = new Timer(); |
| 59 | } |
| 60 | |
| 61 | public static URI uri(RnibUe ue) { |
| 62 | MMEUES1APID mmeS1apId = ue.getMmeS1apId(); |
| 63 | if (mmeS1apId != null) { |
| 64 | try { |
| 65 | return new URI(SCHEME, mmeS1apId.toString(), null); |
| 66 | } catch (URISyntaxException e) { |
| 67 | return null; |
| 68 | } |
| 69 | } |
| 70 | return null; |
| 71 | } |
| 72 | |
| 73 | public static MMEUES1APID hostIdtoMME(HostId hostId) { |
| 74 | String mac = hostId.mac().toString(); |
| 75 | mac = mac.replace(":", ""); |
| 76 | long l = Long.parseLong(mac, 16); |
| 77 | return new MMEUES1APID(l); |
| 78 | } |
| 79 | |
| 80 | public Timer getTimer() { |
| 81 | return timer; |
| 82 | } |
| 83 | |
| 84 | public void setTimer(Timer timer) { |
| 85 | this.timer.cancel(); |
| 86 | this.timer.purge(); |
| 87 | this.timer = timer; |
| 88 | } |
| 89 | |
| 90 | public MMEUES1APID getMmeS1apId() { |
| 91 | return mmeS1apId; |
| 92 | } |
| 93 | |
| 94 | public void setMmeS1apId(MMEUES1APID mmeS1apId) { |
| 95 | this.mmeS1apId = mmeS1apId; |
| 96 | } |
| 97 | |
| 98 | public ENBUES1APID getEnbS1apId() { |
| 99 | return enbS1apId; |
| 100 | } |
| 101 | |
| 102 | public void setEnbS1apId(ENBUES1APID enbS1apId) { |
| 103 | this.enbS1apId = enbS1apId; |
| 104 | } |
| 105 | |
| 106 | public CRNTI getRanId() { |
| 107 | return ranId; |
| 108 | } |
| 109 | |
| 110 | public void setRanId(CRNTI ranId) { |
| 111 | this.ranId = ranId; |
| 112 | } |
| 113 | |
| 114 | public String getImsi() { |
| 115 | return imsi; |
| 116 | } |
| 117 | |
| 118 | public void setImsi(String imsi) { |
| 119 | this.imsi = imsi; |
| 120 | } |
| 121 | |
| 122 | public HostId getHostId() { |
| 123 | try { |
| 124 | String text = this.mmeS1apId.value.toString(16), |
| 125 | res = ""; |
| 126 | int charsLeft = 12 - text.length(); |
| 127 | if (charsLeft > 0) { |
| 128 | res += Stream.generate(() -> "0").limit(charsLeft).collect(Collectors.joining("")); |
| 129 | } else if (charsLeft < 0) { |
| 130 | return null; |
| 131 | } |
| 132 | res += text; |
| 133 | |
| 134 | String insert = ":"; |
| 135 | int period = 2; |
| 136 | |
| 137 | StringBuilder builder = new StringBuilder( |
| 138 | res.length() + insert.length() * (res.length() / period) + 1); |
| 139 | |
| 140 | int index = 0; |
| 141 | String prefix = ""; |
| 142 | while (index < res.length()) { |
| 143 | // Don't putPrimaryLink the insert in the very first iteration. |
| 144 | // This is easier than appending it *after* each substring |
| 145 | builder.append(prefix); |
| 146 | prefix = insert; |
| 147 | builder.append(res.substring(index, |
| 148 | Math.min(index + period, res.length()))); |
| 149 | index += period; |
| 150 | } |
| 151 | |
| 152 | return hostId(MacAddress.valueOf(builder.toString())); |
| 153 | } catch (Exception e) { |
| 154 | log.warn(e.getMessage()); |
| 155 | e.printStackTrace(); |
| 156 | } |
| 157 | return null; |
| 158 | } |
| 159 | |
| 160 | |
| 161 | public RXSigMeasConfig getMeasConfig() { |
| 162 | return measConfig; |
| 163 | } |
| 164 | |
| 165 | public void setMeasConfig(RXSigMeasConfig measConfig) { |
| 166 | this.measConfig = measConfig; |
| 167 | } |
| 168 | |
| 169 | public UECapabilityInfo getCapability() { |
| 170 | return capability; |
| 171 | } |
| 172 | |
| 173 | public void setCapability(UECapabilityInfo capability) { |
| 174 | this.capability = capability; |
| 175 | } |
| 176 | |
slowr | 67d05e4 | 2017-08-11 20:37:22 -0700 | [diff] [blame] | 177 | public State getState() { |
| 178 | return state; |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 179 | } |
| 180 | |
slowr | 67d05e4 | 2017-08-11 20:37:22 -0700 | [diff] [blame] | 181 | public void setState(State state) { |
| 182 | this.state = state; |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public String toString() { |
| 187 | StringBuilder sb = new StringBuilder(); |
| 188 | sb.append("{\n") |
| 189 | .append(mmeS1apId != null ? "\n\"mme-s1-ap-id\":" + mmeS1apId : "") |
| 190 | .append(enbS1apId != null ? ",\n\"enb-s1-ap-id\":" + enbS1apId : "") |
| 191 | .append(imsi != null ? ",\"imsi\":" + imsi : "") |
| 192 | .append(ranId != null ? ",\n\"ran-id\":" + ranId : "") |
slowr | 67d05e4 | 2017-08-11 20:37:22 -0700 | [diff] [blame] | 193 | .append(state != null ? ",\n\"state\":" + state : "") |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 194 | .append(capability != null ? ",\n\"capability\":" + capability : "") |
| 195 | .append(measConfig != null ? ",\n\"meas-config\":" + measConfig : "") |
| 196 | .append("\n}\n"); |
| 197 | return sb.toString(); |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public boolean equals(Object o) { |
| 202 | if (this == o) return true; |
| 203 | if (o == null || getClass() != o.getClass()) return false; |
| 204 | |
| 205 | RnibUe rnibUe = (RnibUe) o; |
| 206 | |
| 207 | return mmeS1apId.equals(rnibUe.mmeS1apId) && ranId.equals(rnibUe.ranId); |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public int hashCode() { |
| 212 | int result = mmeS1apId.hashCode(); |
| 213 | result = 31 * result + ranId.hashCode(); |
| 214 | return result; |
| 215 | } |
| 216 | |
slowr | 67d05e4 | 2017-08-11 20:37:22 -0700 | [diff] [blame] | 217 | public enum State { |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 218 | ACTIVE { |
| 219 | @Override |
| 220 | public String toString() { |
| 221 | return "\"ACTIVE\""; |
| 222 | } |
| 223 | }, |
| 224 | IDLE { |
| 225 | @Override |
| 226 | public String toString() { |
| 227 | return "\"IDLE\""; |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | } |