当前位置:   article > 正文

Corba开发总结(二)---中兴CORBA接口开发

python模拟 corba协议

    由于公司新项目要求,做了一段时间的CORBA接口(中兴+华为)开发,踩了各种大大小小的坑,也算积累了一些经验,特分享至此,经验所限难免会有疏漏,欢迎讨论完善。

一、中兴CORBA接口开发过程

  1. 建立连接、建立消息通道
  2. 获取名字服务
  3. 获取EMSSession工厂
  4. 登录并获取EMSSession
  5. 使用EMSSession初始化管理器对象
  6. 管理器对象调用接口获取数据

 

     注:调用corba接口需要用到EMESession对象,如果在你的应用里要多次访问corba接口,那么多次建立连接和获取EMSSession是非常浪费服务器资源且效率低下的,并且一个corba用户名和密码在同一时间只能被一个连接使用。为了优化访问效率、避免登录冲突和减少重连开销,此处应当在服务启动时就建立连接和获取EMSSession(即步骤1、2、3、4),并不断发送心跳维持连接。就这样就实现了一个corba连接的重复使用,可以用单例模式实现。

 

二、管理器对象定义

高层网管如果要通过NetNumen NBI CORBA接口获取数据或交互请求,必须首先获取到相应的CORBA接口管理对象,即CORBA对象,然后通过该管理对象提供的方法来交互请求。目前NetNumen NBI CORBA接口中定义的管理对象以及对应的管理对象的名字如表:

管理对象定义

管理对象名称

EMSMgr_I

"EMS"

EMSSubscriptionMgr_I

"Subscriber"

EquipmentInventoryMgr_I

"EquipmentInventory"

Ethernet_I

"Ethernet"

FileTransferMgr_I

"FileTransfer"

FlowDomainMgr_I

"FlowDomain"

HeartbeatServiceMgr_I

"CommService"

ManagedElementMgr_I

"ManagedElement"

MSTPCommon_I

"MSTPCommon"

MultiLayerSubnetworkMgr_I

"MultiLayerSubnetwork"

PerformanceManagementMgr_I

"PerformanceManagement"

ProtectionMgr_I

"Protection"

TrailNtwProtMgr_I

"TrailNetworkProtection"

MaintenanceMgr_I

"maintenance"

 

注:该管理器对象定义只适用于中兴corba接口。

 

三、中兴CORBA接口开发示例

demo码云地址:https://gitee.com/dx/CorbaDemo

 

调用EMS管理器对象的获取所有网元信息接口(getAllManagedElements)和获取所有拓扑连接接口(getAllTopLevelTopologicalLinks)示例代码:

  1. package com.dx.corba;
  2. import java.io.UnsupportedEncodingException;
  3. import org.omg.CORBA.ORB;
  4. import org.omg.CORBA.SystemException;
  5. import org.omg.CosNaming.NameComponent;
  6. import org.omg.CosNaming.NamingContext;
  7. import org.omg.CosNaming.NamingContextHelper;
  8. import org.omg.CosNaming.NamingContextPackage.NotFound;
  9. import org.omg.PortableServer.POA;
  10. import org.omg.PortableServer.POAHelper;
  11. import org.omg.zx.common.Common_IHolder;
  12. import org.omg.zx.emsMgr.EMSMgr_I;
  13. import org.omg.zx.emsMgr.EMSMgr_IHelper;
  14. import org.omg.zx.emsSession.EmsSession_I;
  15. import org.omg.zx.emsSession.EmsSession_IHolder;
  16. import org.omg.zx.emsSessionFactory.EmsSessionFactory_I;
  17. import org.omg.zx.emsSessionFactory.EmsSessionFactory_IHelper;
  18. import org.omg.zx.globaldefs.NameAndStringValue_T;
  19. import org.omg.zx.globaldefs.ProcessingFailureException;
  20. import org.omg.zx.managedElement.ManagedElementList_THolder;
  21. import org.omg.zx.managedElementManager.ManagedElementMgr_I;
  22. import org.omg.zx.nmsSession.NmsSession_I;
  23. import org.omg.zx.nmsSession.NmsSession_IPOATie;
  24. import org.omg.zx.subscription.EMSSubscriptionMgr_I;
  25. import org.omg.zx.topologicalLink.TopologicalLinkList_THolder;
  26. import org.omg.zx.topologicalLink.TopologicalLink_T;
  27. public class ZxDemo {
  28. static ORB orb;
  29. static EMSMgr_I emsMgr = null;
  30. static EmsSession_I emsSession = null;
  31. static ManagedElementMgr_I meMgr = null;
  32. static EMSSubscriptionMgr_I emsSpMgr = null;
  33. public boolean invoke() {
  34. // 建立连接登录、建立消息通道
  35. try {
  36. String[] args = new String[2];
  37. args[0] = "-ORBInitRef";
  38. args[1] = "NameService=corbaloc::" + "CORBA服务器IP" + ":"
  39. + "CORBA服务器端口" + "/NameService";
  40. for (int i = 0; i < args.length; i++) {
  41. System.out.println("初始化ORB对象的启动参数为: arg[" + i + "] = "
  42. + args[i]);
  43. }
  44. orb = org.omg.CORBA.ORB.init(args, null);
  45. System.out.println("成功初始化ORB对象!----Initialize Orb");
  46. } catch (SystemException ex) {
  47. System.out.println("初始化ORB对象异常!");
  48. System.out.println(ex.getMessage());
  49. }
  50. if (orb == null) {
  51. System.out.println("orb == null");
  52. return false;
  53. }
  54. // Get Nameservice reference
  55. NamingContext nsRootContext = null;
  56. try {
  57. org.omg.CORBA.Object objRef = orb
  58. .resolve_initial_references("NameService");
  59. nsRootContext = NamingContextHelper.narrow(objRef);
  60. System.out.println("成功获取取名字服务!----Get Nameservice reference");
  61. } catch (org.omg.CORBA.ORBPackage.InvalidName ex) {
  62. System.out.println("取名字服务索引异常!");
  63. System.out.println(ex.getMessage());
  64. }
  65. // 3.1 Get Reference to EMSSessionFactory
  66. NameComponent[] name = new NameComponent[1];
  67. name[0] = new NameComponent("ZTE/T3", "EMSFactory");
  68. System.out.println("NameComponent: '" + "ZTE/T3 " + "','EMSFactory'");
  69. org.omg.CORBA.Object obj = null;
  70. try {
  71. obj = nsRootContext.resolve(name);
  72. System.out
  73. .println("成功获取EMSSession工厂! Get Reference to EMSSessionFactory");
  74. } catch (NotFound ex) {
  75. System.out.println("取EMSSession工厂异常!---NotFound---");
  76. ex.printStackTrace();
  77. } catch (org.omg.CosNaming.NamingContextPackage.InvalidName ex) {
  78. System.out.println("取EMSSession工厂异常!---InvalidName---");
  79. ex.printStackTrace();
  80. } catch (org.omg.CosNaming.NamingContextPackage.CannotProceed ex) {
  81. System.out.println("取EMSSession工厂异常!---CannotProceed---");
  82. ex.printStackTrace();
  83. }
  84. // 开始准备登陆并且获取EmsSession!
  85. EmsSessionFactory_I m_emsFactory = EmsSessionFactory_IHelper
  86. .narrow(obj);
  87. NmsSession_I csession = null;
  88. POA rootpoa = null;
  89. try {
  90. // get reference to rootpoa & activate the POAManager
  91. System.out.println("取得rootpoa并激活POAManager!");
  92. rootpoa = POAHelper.narrow(orb
  93. .resolve_initial_references("RootPOA"));
  94. rootpoa.the_POAManager().activate();
  95. // create servant and register it with the ORB
  96. System.out.println("注册NmsSession到ORB!");
  97. NmsSessionImpl nmsSessionImpl = new NmsSessionImpl();
  98. // nmsSessionImpl.setORB(orb);
  99. byte[] objectID = rootpoa.activate_object(nmsSessionImpl);
  100. // create a tie, with servant being the delegate.
  101. System.out.println("创建NmsSession并且托管给POA!");
  102. NmsSession_IPOATie tie = new NmsSession_IPOATie(nmsSessionImpl,rootpoa);
  103. // obtain the objectRef for the tie
  104. // this step also implicitly activates the the object
  105. System.out.println("在orb上激活NmsSession对象!");
  106. csession = tie._this(orb);
  107. } catch (Exception ex) {
  108. System.out.println("创建NmsSession对象过程,执行异常!");
  109. System.out.println(ex.getMessage());
  110. }
  111. EmsSession_IHolder sessionHolder = new EmsSession_IHolder();
  112. try {
  113. System.out.println("获取EmsSession引用对象");
  114. m_emsFactory.getEmsSession("CORBA服务用户名", "CORBA服务密码", csession,
  115. sessionHolder);
  116. System.out.println("NMSsession ---" + csession.toString());
  117. } catch (org.omg.zx.globaldefs.ProcessingFailureException ex) {
  118. System.out
  119. .println("获取EmsSession引用对象,异常!---ProcessingFailureException---");
  120. System.out.println("可能是用户名或者密码错误,或者权限不够,或者已登陆的用户还未退出!");
  121. System.out.println(ex.toString());
  122. }
  123. emsSession = sessionHolder.value;
  124. System.out.println("EMSsession ---" + emsSession.toString());
  125. emsSession.ping();
  126. // 初始化 EMS 管理器
  127. try {
  128. System.out.println("初始化 EMS 管理器!");
  129. Common_IHolder mgrHolder = new Common_IHolder();
  130. emsSession.getManager("EMS", mgrHolder);
  131. emsMgr = EMSMgr_IHelper.narrow(mgrHolder.value);
  132. System.out.println("EMS_Manager To String ---" + emsMgr.toString());
  133. } catch (ProcessingFailureException pfe) {
  134. System.out
  135. .println("初始化 EMS 管理器异常!---ProcessingFailureException---");
  136. System.out.println(pfe.toString());
  137. }
  138. try {
  139. initEvent(emsMgr, rootpoa);
  140. } catch (Exception e) {
  141. e.printStackTrace();
  142. }
  143. return true;
  144. }
  145. private static void initEvent(EMSMgr_I emsMgr, POA rootpoa) {
  146. // 通过远程对象获取网元信息
  147. ManagedElementList_THolder meList = null;
  148. try {
  149. meList = new ManagedElementList_THolder();
  150. meMgr.getAllManagedElements(meList);
  151. for (int i = 0, size = meList.value.length; i < size; i++) {
  152. System.out.print(Stringformat(meList.value[i].meType) + "\t"
  153. + Stringformat(meList.value[i].userLabel) + "\t"
  154. + Stringformat(meList.value[i].nativeEMSName) + "\t"
  155. + meList.value[i].hardwareVersion + "\t"
  156. + meList.value[i].productName + "\t");
  157. NameAndStringValue_T[] arr = meList.value[i].name;
  158. for (int j = 0; j < arr.length; j++) {
  159. System.out.print(meList.value[i].name[j].name + ":"
  160. + meList.value[i].name[j].value + "\t");
  161. }
  162. System.out.println();
  163. }
  164. TopologicalLinkList_THolder topoList = new TopologicalLinkList_THolder();
  165. emsMgr.getAllTopLevelTopologicalLinks(topoList);
  166. TopologicalLink_T[] a = topoList.value;
  167. for (int i = 0, size = a.length; i < size; i++) {
  168. System.out.println(Stringformat(a[i].userLabel));
  169. NameAndStringValue_T[] aa = a[i].name;
  170. for (NameAndStringValue_T nameAndStringValue_T : aa) {
  171. System.out.println("aa:" + nameAndStringValue_T.name + "~"
  172. + nameAndStringValue_T.value);
  173. }
  174. NameAndStringValue_T[] b = a[i].aEndTP;
  175. for (NameAndStringValue_T nameAndStringValue_T : b) {
  176. System.out.println("a:" + nameAndStringValue_T.name + "~"
  177. + nameAndStringValue_T.value);
  178. }
  179. NameAndStringValue_T[] c = a[i].zEndTP;
  180. for (NameAndStringValue_T nameAndStringValue_T : c) {
  181. System.out.println("z:" + nameAndStringValue_T.name + "~"
  182. + nameAndStringValue_T.value);
  183. }
  184. }
  185. } catch (ProcessingFailureException ex) {
  186. System.out.println(ex.toString());
  187. } catch (Exception e) {
  188. // TODO Auto-generated catch block
  189. e.printStackTrace();
  190. }
  191. }
  192. /**
  193. * 字符集转换
  194. * @param value
  195. * @return
  196. */
  197. private static String Stringformat(String value) {
  198. try {
  199. return new String(value.getBytes("ISO8859_1"), "GB2312");
  200. } catch (UnsupportedEncodingException e) {
  201. e.printStackTrace();
  202. }
  203. return "";
  204. }
  205. public static void main(String args[]) {
  206. try {
  207. ZxDemo zxDemo = new ZxDemo();
  208. zxDemo.invoke();
  209. } catch (Exception e) {
  210. e.printStackTrace();
  211. }
  212. }
  213. }

 

 

转载于:https://my.oschina.net/dong706/blog/1785116

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/496881
推荐阅读
  

闽ICP备14008679号