• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025-2026 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef NETWORKSLICECOMMCONFIG_H
17 #define NETWORKSLICECOMMCONFIG_H
18 
19 #include <cstdint>
20 #include <string>
21 #include <vector>
22 #include <nlohmann/json.hpp>
23 #include "netmgr_ext_log_wrapper.h"
24 
25 namespace OHOS {
26 namespace NetManagerStandard {
27 class NetworkSliceCommConfig {
28 public:
29     /** move bit 8 */
30     static constexpr int BIT_MOVE_8 = 8;
31     /** move bit 16 */
32     static constexpr int BIT_MOVE_16 = 16;
33     static constexpr int LEN_BYTE = 1;
34     static constexpr int LEN_SHORT = 2;
35     static constexpr int LEN_THREE_BYTE = 3;
36     static constexpr int LEN_INT = 4;
37     static constexpr int LEN_SIX_BYTE = 6;
38     static constexpr int LEN_EIGHT_BYTE = 8;
39     static constexpr int LEN_SIXTEEN_BYTE = 16;
40     static constexpr int HASH_MAP_DEFAULT_CAPACITY = 16;
41     /** ursp version 1510 */
42     static constexpr int URSP_VERSION_1510 = 1510;
43     /** ursp version 1520 */
44     static constexpr int URSP_VERSION_1520 = 1520;
45     static constexpr int LEN_IPV6ADDR = 16;
46 };
47 
48 
49 class OsAppId {
50 public:
setOsId(std::string osId)51     void setOsId(std::string osId)
52     {
53         mOsId = osId;
54     }
55 
getOsId()56     const std::string& getOsId() const
57     {
58         return mOsId;
59     }
60 
setAppId(std::string appId)61     void setAppId(std::string appId)
62     {
63         mAppId = appId;
64     }
65 
getAppId()66     const std::string& getAppId() const
67     {
68         return mAppId;
69     }
to_json()70     nlohmann::json to_json() const
71     {
72         return nlohmann::json::object({
73             {"OsId", mOsId},
74             {"AppId", mAppId}
75         });
76     }
from_json(const nlohmann::json & jsonAppId)77     void from_json(const nlohmann::json& jsonAppId)
78     {
79         if (jsonAppId.find("OsId") != jsonAppId.end()) {
80             this->mOsId = jsonAppId.at("OsId").get<std::string>();
81             NETMGR_EXT_LOG_I("OsAppId find OsId = %{public}s", this->mOsId.c_str());
82         }
83         if (jsonAppId.find("AppId") != jsonAppId.end()) {
84             this->mAppId = jsonAppId.at("AppId").get<std::string>();
85             NETMGR_EXT_LOG_I("OsAppId find AppId = %{public}s", this->mAppId.c_str());
86         }
87     }
88 private:
89     std::string mOsId;
90     std::string mAppId;
91 };
92 
93 class Ipv4Addr {
94 public:
setIpv4Addr(const uint32_t ipv4Addr)95     void setIpv4Addr(const uint32_t ipv4Addr)
96     {
97         mIpv4Addr = ipv4Addr;
98     }
99 
getIpv4Addr()100     const uint32_t& getIpv4Addr() const
101     {
102         return mIpv4Addr;
103     }
104 
setIpv4Mask(const uint32_t ipv4Mask)105     void setIpv4Mask(const uint32_t ipv4Mask)
106     {
107         mIpv4Mask = ipv4Mask;
108     }
109 
getIpv4Mask()110     const uint32_t& getIpv4Mask() const
111     {
112         return mIpv4Mask;
113     }
114 
to_json()115     nlohmann::json to_json() const
116     {
117         return nlohmann::json::object({
118             {"Ipv4Addr", mIpv4Addr},
119             {"Ipv4Mask", mIpv4Mask}
120         });
121     }
122 
from_json(const nlohmann::json & jsonAddr)123     void from_json(const nlohmann::json& jsonAddr)
124     {
125         if (jsonAddr.find("ipv4Addr") != jsonAddr.end()) {
126             this->mIpv4Addr = jsonAddr.at("ipv4Addr").get<uint32_t>();
127         }
128         if (jsonAddr.find("ipv4Mask") != jsonAddr.end()) {
129             this->mIpv4Mask = jsonAddr.at("ipv4Mask").get<uint32_t>();
130         }
131     }
132 private:
133     uint32_t mIpv4Addr;
134     uint32_t mIpv4Mask;
135 };
136 
137 class Ipv6Addr {
138 public:
setIpv6Addr(const std::array<uint8_t,NetworkSliceCommConfig::LEN_IPV6ADDR> & ipv6Addr)139     void setIpv6Addr(const std::array<uint8_t, NetworkSliceCommConfig::LEN_IPV6ADDR>& ipv6Addr)
140     {
141         mIpv6Addr = ipv6Addr;
142     }
getIpv6Addr()143     const std::array<uint8_t, NetworkSliceCommConfig::LEN_IPV6ADDR>& getIpv6Addr() const
144     {
145         return mIpv6Addr;
146     }
setIpv6PrefixLen(int ipv6PrefixLen)147     void setIpv6PrefixLen(int ipv6PrefixLen)
148     {
149         mIpv6PrefixLen = ipv6PrefixLen;
150     }
getIpv6PrefixLen()151     int getIpv6PrefixLen() const
152     {
153         return mIpv6PrefixLen;
154     }
to_json()155     nlohmann::json to_json() const
156     {
157         return nlohmann::json::object({
158             {"Ipv6Addr", mIpv6Addr},
159             {"Ipv6PrefixLen", mIpv6PrefixLen}
160         });
161     }
from_json(const nlohmann::json & jsonAddr)162     void from_json(const nlohmann::json& jsonAddr)
163     {
164         if (jsonAddr.find("ipv6Addr") != jsonAddr.end()) {
165             const nlohmann::json& ipv6AddrJson = jsonAddr.at("ipv6Addr");
166             this->mIpv6Addr.fill(0);
167             for (size_t i = 0; i < ipv6AddrJson.size(); ++i) {
168                 this->mIpv6Addr[i] = ipv6AddrJson[i].get<uint8_t>();
169             }
170         }
171         if (jsonAddr.find("ipv6PrefixLen") != jsonAddr.end()) {
172             this->mIpv6PrefixLen = jsonAddr.at("ipv6PrefixLen").get<int>();
173         }
174     }
175 private:
176     std::array<uint8_t, NetworkSliceCommConfig::LEN_IPV6ADDR> mIpv6Addr;
177     int mIpv6PrefixLen;
178 };
179 class RemotePortRange {
180 public:
setPortRangeLowLimit(int portRangeLowLimit)181     void setPortRangeLowLimit(int portRangeLowLimit)
182     {
183         mPortRangeLowLimit = portRangeLowLimit;
184     }
185 
getPortRangeLowLimit()186     int getPortRangeLowLimit() const
187     {
188         return mPortRangeLowLimit;
189     }
190 
setPortRangeHighLimit(int portRangeHighLimit)191     void setPortRangeHighLimit(int portRangeHighLimit)
192     {
193         mPortRangeHighLimit = portRangeHighLimit;
194     }
195 
getPortRangeHighLimit()196     int getPortRangeHighLimit() const
197     {
198         return mPortRangeHighLimit;
199     }
to_json()200     nlohmann::json to_json() const
201     {
202         return nlohmann::json::object({
203             {"PortRangeLowLimit", mPortRangeLowLimit},
204             {"PortRangeHighLimit", mPortRangeHighLimit}
205         });
206     }
from_json(const nlohmann::json & jsonRange)207     void from_json(const nlohmann::json& jsonRange)
208     {
209         if (jsonRange.find("portRangeLowLimit") != jsonRange.end()) {
210             this->mPortRangeLowLimit = jsonRange.at("portRangeLowLimit").get<int>();
211         }
212         if (jsonRange.find("portRangeHighLimit") != jsonRange.end()) {
213             this->mPortRangeHighLimit = jsonRange.at("portRangeHighLimit").get<int>();
214         }
215     }
216 private:
217     int mPortRangeLowLimit;
218     int mPortRangeHighLimit;
219 };
220 
221 class GetSlicePara {
222 public:
223     bool isDone = false;
224     std::map<std::string, std::string> data;
225     std::map<std::string, std::string> ret;
226 };
227 
228 class AppDescriptor {
229 public:
230     int mUid = 0;
231     OsAppId mOsAppId;
232     uint32_t mIpv4Addr;
233     std::array<uint8_t, NetworkSliceCommConfig::LEN_IPV6ADDR> mIpv6Addr;
234     int mProtocolId = 0;
235     int mRemotePort = 0;
236     std::string mDnn = "";
237     std::string mFqdn = "";
238     int mConnectionCapability = 0;
239 
setUid(int uid)240     void setUid(int uid)
241     {
242         mUid = uid;
243     }
getUid()244     int getUid()
245     {
246         return mUid;
247     }
setOsAppId(const std::string & osId,const std::string & appId)248     void setOsAppId(const std::string& osId, const std::string& appId)
249     {
250         mOsAppId.setOsId(osId);
251         mOsAppId.setAppId(appId);
252     }
getOsAppId()253     const OsAppId& getOsAppId() const
254     {
255         return mOsAppId;
256     }
setIpv4Addr(const uint32_t ipv4Addr)257     void setIpv4Addr(const uint32_t ipv4Addr)
258     {
259         mIpv4Addr = ipv4Addr;
260     }
getIpv4Addr()261     const uint32_t& getIpv4Addr() const
262     {
263         return mIpv4Addr;
264     }
setIpv6Addr(const std::array<uint8_t,NetworkSliceCommConfig::LEN_IPV6ADDR> & ipv6Addr)265     void setIpv6Addr(const std::array<uint8_t, NetworkSliceCommConfig::LEN_IPV6ADDR>& ipv6Addr)
266     {
267         mIpv6Addr = ipv6Addr;
268     }
getIpv6Addr()269     const std::array<uint8_t, NetworkSliceCommConfig::LEN_IPV6ADDR>& getIpv6Addr() const
270     {
271         return mIpv6Addr;
272     }
setProtocolId(int protocolId)273     void setProtocolId(int protocolId)
274     {
275         mProtocolId = protocolId;
276     }
getProtocolId()277     int getProtocolId() const
278     {
279         return mProtocolId;
280     }
setRemotePort(int remotePort)281     void setRemotePort(int remotePort)
282     {
283         mRemotePort = remotePort;
284     }
getRemotePort()285     int getRemotePort() const
286     {
287         return mRemotePort;
288     }
setDnn(const std::string & dnn)289     void setDnn(const std::string& dnn)
290     {
291         mDnn = dnn;
292     }
getDnn()293     const std::string& getDnn() const
294     {
295         return mDnn;
296     }
setFqdn(const std::string & fqdn)297     void setFqdn(const std::string& fqdn)
298     {
299         mFqdn = fqdn;
300     }
getFqdn()301     const std::string& getFqdn() const
302     {
303         return mFqdn;
304     }
setConnectionCapability(int connCap)305     void setConnectionCapability(int connCap)
306     {
307         mConnectionCapability = connCap;
308     }
getConnectionCapability()309     int getConnectionCapability() const
310     {
311         return mConnectionCapability;
312     }
313 };
314 
315 class SelectedRouteDescriptor {
316 public:
317     uint8_t mSscMode = 0;
318     int mPduSessionType = -1;
319     std::string mSnssai = "";
320     std::string mDnn = "";
321 
322     /* bit0: isMatchAll bit1: isDnnMatched bit2: isFqdnMatched bit3: hasUrsp bit4: isCctMatched */
323     uint8_t mRouteBitmap = 0;
324     std::string mAppIds = "";
325     uint8_t mIpv4Num = 0;
326     std::vector<uint8_t> mIpv4AddrAndMask;
327     uint8_t mIpv6Num = 0;
328     std::vector<uint8_t> mIpv6AddrAndPrefix;
329     std::string mProtocolIds = "";
330     std::string mRemotePorts = "";
331     uint8_t mUrspPrecedence = 0;
332 
setSscMode(uint8_t sscMode)333     void setSscMode(uint8_t sscMode)
334     {
335         mSscMode = sscMode;
336     }
337 
getSscMode()338     uint8_t getSscMode() const
339     {
340         return mSscMode;
341     }
342 
setPduSessionType(int pduSessionType)343     void setPduSessionType(int pduSessionType)
344     {
345         mPduSessionType = pduSessionType;
346     }
347 
getPduSessionType()348     int getPduSessionType() const
349     {
350         return mPduSessionType;
351     }
352 
setSnssai(const std::string & snssai)353     void setSnssai(const std::string& snssai)
354     {
355         mSnssai = snssai;
356     }
357 
getSnssai()358     const std::string& getSnssai() const
359     {
360         return mSnssai;
361     }
362 
setDnn(const std::string & dnn)363     void setDnn(const std::string& dnn)
364     {
365         mDnn = dnn;
366     }
367 
getDnn()368     const std::string& getDnn() const
369     {
370         return mDnn;
371     }
372 
setRouteBitmap(uint8_t routeBitmap)373     void setRouteBitmap(uint8_t routeBitmap)
374     {
375         mRouteBitmap = routeBitmap;
376     }
377 
getRouteBitmap()378     uint8_t getRouteBitmap() const
379     {
380         return mRouteBitmap;
381     }
382 
setAppIds(const std::string & appIds)383     void setAppIds(const std::string& appIds)
384     {
385         mAppIds = appIds;
386     }
387 
getAppIds()388     const std::string& getAppIds() const
389     {
390         return mAppIds;
391     }
392 
setIpv4Num(uint8_t ipv4Num)393     void setIpv4Num(uint8_t ipv4Num)
394     {
395         mIpv4Num = ipv4Num;
396     }
397 
getIpv4Num()398     uint8_t getIpv4Num() const
399     {
400         return mIpv4Num;
401     }
402 
setIpv4AddrAndMask(const std::vector<uint8_t> & ipv4AddrAndMask)403     void setIpv4AddrAndMask(const std::vector<uint8_t>& ipv4AddrAndMask)
404     {
405         mIpv4AddrAndMask = ipv4AddrAndMask;
406     }
407 
getIpv4AddrAndMask()408     const std::vector<uint8_t>& getIpv4AddrAndMask() const
409     {
410         return mIpv4AddrAndMask;
411     }
412 
setIpv6Num(uint8_t ipv6Num)413     void setIpv6Num(uint8_t ipv6Num)
414     {
415         mIpv6Num = ipv6Num;
416     }
417 
getIpv6Num()418     uint8_t getIpv6Num() const
419     {
420         return mIpv6Num;
421     }
422 
setIpv6AddrAndPrefix(const std::vector<uint8_t> & ipv6AddrAndPrefix)423     void setIpv6AddrAndPrefix(const std::vector<uint8_t>& ipv6AddrAndPrefix)
424     {
425         mIpv6AddrAndPrefix = ipv6AddrAndPrefix;
426     }
427 
getIpv6AddrAndPrefix()428     const std::vector<uint8_t>& getIpv6AddrAndPrefix() const
429     {
430         return mIpv6AddrAndPrefix;
431     }
432 
setProtocolIds(const std::string & protocolIds)433     void setProtocolIds(const std::string& protocolIds)
434     {
435         mProtocolIds = protocolIds;
436     }
437 
getProtocolIds()438     const std::string& getProtocolIds() const
439     {
440         return mProtocolIds;
441     }
442 
setRemotePorts(const std::string & remotePorts)443     void setRemotePorts(const std::string& remotePorts)
444     {
445         mRemotePorts = remotePorts;
446     }
447 
getRemotePorts()448     const std::string& getRemotePorts() const
449     {
450         return mRemotePorts;
451     }
452 
setUrspPrecedence(uint8_t urspPrecedence)453     void setUrspPrecedence(uint8_t urspPrecedence)
454     {
455         mUrspPrecedence = urspPrecedence;
456     }
457 
getUrspPrecedence()458     uint8_t getUrspPrecedence() const
459     {
460         return mUrspPrecedence;
461     }
462 };
463 
464 class ForbiddenRouteDescriptor {
465 public:
ForbiddenRouteDescriptor()466     ForbiddenRouteDescriptor() : mSscMode(0), mPduSessionType(-1), mSnssai(""), mDnn("") {}
467 
setSscMode(unsigned char sscMode)468     void setSscMode(unsigned char sscMode)
469     {
470         mSscMode = sscMode;
471     }
472 
getSscMode()473     unsigned char getSscMode() const
474     {
475         return mSscMode;
476     }
477 
setPduSessionType(int pduSessionType)478     void setPduSessionType(int pduSessionType)
479     {
480         mPduSessionType = pduSessionType;
481     }
482 
getPduSessionType()483     int getPduSessionType() const
484     {
485         return mPduSessionType;
486     }
487 
setSnssai(const std::string & snssai)488     void setSnssai(const std::string& snssai)
489     {
490         mSnssai = snssai;
491     }
492 
getSnssai()493     const std::string& getSnssai() const
494     {
495         return mSnssai;
496     }
497 
setDnn(const std::string & dnn)498     void setDnn(const std::string& dnn)
499     {
500         mDnn = dnn;
501     }
502 
getDnn()503     const std::string& getDnn() const
504     {
505         return mDnn;
506     }
507 
setCurrentTimeMillies(long long currentTimeMillies)508     void setCurrentTimeMillies(long long currentTimeMillies)
509     {
510         mCurrentTimeMillies = currentTimeMillies;
511     }
512 
getCurrentTimeMillies()513     int64_t getCurrentTimeMillies() const
514     {
515         return mCurrentTimeMillies;
516     }
517 
518 private:
519     unsigned char mSscMode;
520     int mPduSessionType;
521     std::string mSnssai;
522     std::string mDnn;
523     int64_t mCurrentTimeMillies;
524 };
525 
526 class AddRoutePara {
527 public:
528     int netId = 0;
529     int uidNum = 0;
530     int protocolIdNum = 0;
531     int remotePortNum = 0;
532     int remotePortRangeNum = 0;
533     int singleRemotePortNum = 0;
534     short len = 0;
535     uint8_t urspPrecedence = 0;
536     uint8_t ipv4Num = 0;
537     uint8_t ipv6Num = 0;
538     std::vector<int> uidArrays;
539     std::vector<std::string> protocolIdArrays;
540     std::vector<std::string> remotePortsArrays;
541     std::vector<uint8_t> ipv4AddrAndMasks;
542     std::vector<uint8_t> ipv6AddrAndPrefixs;
543 };
544 
545 } // namespace NetManagerStandard
546 } // namespace OHOS
547 #endif  // NETWORKSLICECOMMCONFIG_H
548