11package org .zstack .network .l2 .vxlan .vxlanNetwork ;
22
33import org .springframework .beans .factory .annotation .Autowired ;
4+ import org .zstack .core .Platform ;
45import org .zstack .core .asyncbatch .While ;
56import org .zstack .core .cloudbus .CloudBus ;
67import org .zstack .core .cloudbus .CloudBusCallBack ;
78import org .zstack .core .db .DatabaseFacade ;
89import org .zstack .core .db .Q ;
10+ import org .zstack .core .db .SQLBatchWithReturn ;
911import org .zstack .core .db .SimpleQuery ;
1012import org .zstack .header .Component ;
1113import org .zstack .header .core .FutureCompletion ;
2325import org .zstack .header .vm .VmInstanceInventory ;
2426import org .zstack .header .vm .VmInstanceMigrateExtensionPoint ;
2527import org .zstack .header .vm .VmNicInventory ;
28+ import org .zstack .identity .AccountManager ;
2629import org .zstack .network .l2 .L2NetworkDefaultMtu ;
2730import org .zstack .network .l2 .vxlan .vxlanNetworkPool .AllocateVniMsg ;
2831import org .zstack .network .l2 .vxlan .vxlanNetworkPool .AllocateVniReply ;
@@ -51,6 +54,8 @@ public class VxlanNetworkFactory implements L2NetworkFactory, Component, VmInsta
5154 private CloudBus bus ;
5255 @ Autowired
5356 private QueryFacade qf ;
57+ @ Autowired
58+ protected AccountManager acntMgr ;
5459
5560 @ Override
5661 public L2NetworkType getType () {
@@ -60,7 +65,6 @@ public L2NetworkType getType() {
6065 @ Override
6166 public L2NetworkInventory createL2Network (L2NetworkVO ovo , APICreateL2NetworkMsg msg ) {
6267 APICreateL2VxlanNetworkMsg amsg = (APICreateL2VxlanNetworkMsg ) msg ;
63- VxlanNetworkVO vo = new VxlanNetworkVO (ovo );
6468
6569 AllocateVniMsg vniMsg = new AllocateVniMsg ();
6670 vniMsg .setL2NetworkUuid (amsg .getPoolUuid ());
@@ -70,26 +74,41 @@ public L2NetworkInventory createL2Network(L2NetworkVO ovo, APICreateL2NetworkMsg
7074 if (!reply .isSuccess ()) {
7175 throw new OperationFailureException (reply .getError ());
7276 }
73-
7477 AllocateVniReply r = reply .castReply ();
75- vo .setVni (r .getVni ());
76- vo .setPoolUuid ((amsg .getPoolUuid ()));
77- if (vo .getPhysicalInterface () == null ) {
78- vo .setPhysicalInterface ("" );
79- }
80- vo = dbf .persistAndRefresh (vo );
81-
82- SimpleQuery <L2NetworkClusterRefVO > q = dbf .createQuery (L2NetworkClusterRefVO .class );
83- q .add (L2NetworkClusterRefVO_ .l2NetworkUuid , SimpleQuery .Op .EQ , amsg .getPoolUuid ());
84- final List <L2NetworkClusterRefVO > refs = q .list ();
85- for (L2NetworkClusterRefVO ref : refs ) {
86- L2NetworkClusterRefVO rvo = new L2NetworkClusterRefVO ();
87- rvo .setClusterUuid (ref .getClusterUuid ());
88- rvo .setL2NetworkUuid (vo .getUuid ());
89- dbf .persist (rvo );
90- }
9178
92- vo = dbf .reload (vo );
79+ VxlanNetworkVO vo = new SQLBatchWithReturn <VxlanNetworkVO >() {
80+ @ Override
81+ protected VxlanNetworkVO scripts () {
82+ VxlanNetworkVO vo = new VxlanNetworkVO (ovo );
83+ String uuid = msg .getResourceUuid () == null ? Platform .getUuid () : msg .getResourceUuid ();
84+ vo .setUuid (uuid );
85+ vo .setVni (r .getVni ());
86+ vo .setPoolUuid ((amsg .getPoolUuid ()));
87+ if (vo .getPhysicalInterface () == null ) {
88+ vo .setPhysicalInterface ("" );
89+ }
90+ dbf .getEntityManager ().persist (vo );
91+
92+ SimpleQuery <L2NetworkClusterRefVO > q = dbf .createQuery (L2NetworkClusterRefVO .class );
93+ q .add (L2NetworkClusterRefVO_ .l2NetworkUuid , SimpleQuery .Op .EQ , amsg .getPoolUuid ());
94+ final List <L2NetworkClusterRefVO > refs = q .list ();
95+ for (L2NetworkClusterRefVO ref : refs ) {
96+ L2NetworkClusterRefVO rvo = new L2NetworkClusterRefVO ();
97+ rvo .setClusterUuid (ref .getClusterUuid ());
98+ rvo .setL2NetworkUuid (uuid );
99+ dbf .getEntityManager ().persist (rvo );
100+ dbf .getEntityManager ().flush ();
101+ dbf .getEntityManager ().refresh (rvo );
102+ }
103+
104+ acntMgr .createAccountResourceRef (msg .getSession ().getAccountUuid (), vo .getUuid (), VxlanNetworkVO .class );
105+
106+ dbf .getEntityManager ().flush ();
107+ dbf .getEntityManager ().refresh (vo );
108+
109+ return vo ;
110+ }
111+ }.execute ();
93112
94113 L2VxlanNetworkInventory inv = L2VxlanNetworkInventory .valueOf (vo );
95114 String info = String .format ("successfully create L2VxlanNetwork, %s" , JSONObjectUtil .toJsonString (inv ));
0 commit comments