Skip to content

Commit bcef513

Browse files
committed
Refactor the alto-core and alto-basic
Cleanup the codestyle and project structure. Change-Id: I6a54daff68a43dda7d31edfddac20657c6651510 Signed-off-by: jensenzhang <hack@jensen-zhang.site>
1 parent 5395ec7 commit bcef513

19 files changed

Lines changed: 353 additions & 312 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ yang-gen-code
4040
yang-gen-config
4141
yang-gen-sal
4242
.ensime
43+
.vscode

alto-basic/auto-maps/impl/src/main/java/org/opendaylight/alto/basic/impl/AltoAutoMapsProvider.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@ public class AltoAutoMapsProvider {
1515

1616
private static final Logger LOG = LoggerFactory.getLogger(AltoAutoMapsProvider.class);
1717

18-
private final DataBroker dataBroker;
19-
private AltoAutoMapsUpdateListener m_listener;
18+
private AltoAutoMapsUpdateListener listener;
2019

2120
public AltoAutoMapsProvider(final DataBroker dataBroker) {
22-
this.dataBroker = dataBroker;
23-
this.m_listener = new AltoAutoMapsUpdateListener(dataBroker);
21+
this.listener = new AltoAutoMapsUpdateListener(dataBroker);
2422
}
2523

2624
/**
@@ -33,7 +31,14 @@ public void init() {
3331
/**
3432
* Method called when the blueprint container is destroyed.
3533
*/
36-
public void close() {
34+
public void close() throws Exception {
35+
closeListener();
3736
LOG.info("AltoAutoMapsProvider Closed");
3837
}
38+
39+
private void closeListener() throws Exception {
40+
if (listener != null) {
41+
this.listener.close();
42+
}
43+
}
3944
}

alto-basic/auto-maps/impl/src/main/java/org/opendaylight/alto/basic/impl/AltoAutoMapsUpdateListener.java

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.Collection;
1111
import java.util.LinkedList;
1212
import java.util.List;
13+
1314
import org.opendaylight.alto.basic.manual.maps.ManualMapsUtils;
1415
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
1516
import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
@@ -45,8 +46,8 @@ public class AltoAutoMapsUpdateListener implements DataTreeChangeListener<Topolo
4546

4647
private static final String TOPOLOGY_NAME = "flow:1";
4748
private static final String DEFAULT_AUTO_NETWORKMAP = "default-auto-networkmap";
48-
private static final String DEFAULT_AUTO_COSTMAP = "default-auto-costmap";
49-
private static final String DEFAULT_PID = "PID0";
49+
// private static final String DEFAULT_AUTO_COSTMAP = "default-auto-costmap";
50+
// private static final String DEFAULT_PID = "PID0";
5051

5152
public AltoAutoMapsUpdateListener(final DataBroker dataBroker) {
5253
this.dataBroker = dataBroker;
@@ -62,28 +63,14 @@ public AltoAutoMapsUpdateListener(final DataBroker dataBroker) {
6263
@Override
6364
public void onDataTreeChanged(Collection<DataTreeModification<Topology>> changes) {
6465
WriteTransaction writeTx = null;
65-
for (DataTreeModification<Topology> change: changes) {
66+
for (DataTreeModification<Topology> change : changes) {
6667
final DataObjectModification<Topology> rootNode = change.getRootNode();
6768
switch (rootNode.getModificationType()) {
6869
case WRITE:
69-
if (writeTx == null) {
70-
writeTx = dataBroker.newWriteOnlyTransaction();
71-
}
72-
73-
if (rootNode.getDataBefore() == null) {
74-
createDefaultAutoNetworkMap(rootNode.getDataAfter(), writeTx);
75-
LOG.info("Create default auto networkmap");
76-
} else {
77-
updateDefaultAutoNetworkMap(rootNode.getDataAfter(), writeTx);
78-
LOG.info("Update default auto networkmap");
79-
}
70+
writeTx = handleDataWrite(rootNode, writeTx);
8071
break;
8172
case DELETE:
82-
if (writeTx == null) {
83-
writeTx = dataBroker.newWriteOnlyTransaction();
84-
}
85-
86-
emptyDefaultAutoNetworkMap(writeTx);
73+
writeTx = handleDataDelete(writeTx);
8774
LOG.info("Empty default auto networkmap");
8875
break;
8976
default:
@@ -96,6 +83,30 @@ public void onDataTreeChanged(Collection<DataTreeModification<Topology>> changes
9683
}
9784
}
9885

86+
private WriteTransaction handleDataWrite(DataObjectModification<Topology> rootNode, WriteTransaction writeTx) {
87+
if (writeTx == null) {
88+
writeTx = dataBroker.newWriteOnlyTransaction();
89+
}
90+
91+
if (rootNode.getDataBefore() == null) {
92+
createDefaultAutoNetworkMap(rootNode.getDataAfter(), writeTx);
93+
LOG.info("Create default auto networkmap");
94+
} else {
95+
updateDefaultAutoNetworkMap(rootNode.getDataAfter(), writeTx);
96+
LOG.info("Update default auto networkmap");
97+
}
98+
return writeTx;
99+
}
100+
101+
private WriteTransaction handleDataDelete(WriteTransaction writeTx) {
102+
if (writeTx == null) {
103+
writeTx = dataBroker.newWriteOnlyTransaction();
104+
}
105+
106+
emptyDefaultAutoNetworkMap(writeTx);
107+
return writeTx;
108+
}
109+
99110
private void createDefaultAutoNetworkMap(Topology topology, final WriteTransaction wx) {
100111
for (Node node : topology.getNode()) {
101112
HostNode hostNode = node.getAugmentation(HostNode.class);
@@ -156,7 +167,13 @@ private List<IpPrefix> aggregateAddressesList(List<Addresses> addressesList) {
156167

157168
@Override
158169
public void close() throws Exception {
159-
registration.close();
170+
closeRegistration();
160171
LOG.info("AltoAutoMapsUpdateListener Closed");
161172
}
173+
174+
private void closeRegistration() {
175+
if (registration != null) {
176+
registration.close();
177+
}
178+
}
162179
}

alto-basic/manual-maps/api/src/main/java/org/opendaylight/alto/basic/manual/maps/ManualMapsUtils.java

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,11 @@ public static InstanceIdentifier<ConfigContext> getDefaultContextIID() {
4242
}
4343

4444
public static InstanceIdentifier<ConfigContext> getContextIID(String cid) {
45-
Uuid _cid = new Uuid(cid);
46-
return getContextIID(_cid);
45+
return getContextIID(new Uuid(cid));
4746
}
4847

4948
public static InstanceIdentifier<ConfigContext> getContextIID(Uuid cid) {
50-
ConfigContextKey key = new ConfigContextKey(cid);
51-
return getContextIID(key);
49+
return getContextIID(new ConfigContextKey(cid));
5250
}
5351

5452
public static InstanceIdentifier<ConfigContext> getContextIID(ConfigContextKey key) {
@@ -64,31 +62,27 @@ public static InstanceIdentifier<ResourceNetworkMap> getResourceNetworkMapIID(St
6462
}
6563

6664
public static InstanceIdentifier<ResourceNetworkMap> getResourceNetworkMapIID(String cid, String rid) {
67-
Uuid _cid = new Uuid(cid);
68-
ResourceId _rid = new ResourceId(rid);
69-
return getResourceNetworkMapIID(_cid, _rid);
65+
return getResourceNetworkMapIID(new Uuid(cid), new ResourceId(rid));
7066
}
7167

7268
public static InstanceIdentifier<ResourceNetworkMap> getResourceNetworkMapIID(Uuid cid, ResourceId rid) {
73-
ConfigContextKey ckey = new ConfigContextKey(cid);
74-
ResourceNetworkMapKey rkey = new ResourceNetworkMapKey(rid);
75-
return getResourceIID(ckey, rkey, ResourceNetworkMap.class);
69+
return getResourceIID(new ConfigContextKey(cid), new ResourceNetworkMapKey(rid),
70+
ResourceNetworkMap.class);
7671
}
7772

7873
public static InstanceIdentifier<ResourceCostMap> getResourceCostMapIID(String rid) {
7974
return getResourceCostMapIID(DEFAULT_CONTEXT, rid);
8075
}
8176

8277
public static InstanceIdentifier<ResourceCostMap> getResourceCostMapIID(String cid, String rid) {
83-
Uuid _cid = new Uuid(cid);
84-
ResourceId _rid = new ResourceId(rid);
85-
return getResourceCostMapIID(_cid, _rid);
78+
return getResourceCostMapIID(new Uuid(cid), new ResourceId(rid));
8679
}
8780

8881
public static InstanceIdentifier<ResourceCostMap> getResourceCostMapIID(Uuid cid, ResourceId rid) {
8982
ConfigContextKey ckey = new ConfigContextKey(cid);
9083
ResourceCostMapKey rkey = new ResourceCostMapKey(rid);
91-
return getResourceIID(ckey, rkey, ResourceCostMap.class);
84+
return getResourceIID(new ConfigContextKey(cid), new ResourceCostMapKey(rid),
85+
ResourceCostMap.class);
9286
}
9387

9488
public static <T extends Identifiable<K> & ChildOf<? super ConfigContext>, K extends Identifier<T>>
@@ -108,8 +102,8 @@ public static Uuid createContext(String cid, final WriteTransaction wx) {
108102
public static Uuid createContext(Uuid cid, final WriteTransaction wx) {
109103
ConfigContextBuilder builder = new ConfigContextBuilder();
110104
builder.setContextId(cid);
111-
builder.setResourceNetworkMap(new LinkedList<ResourceNetworkMap>());
112-
builder.setResourceCostMap(new LinkedList<ResourceCostMap>());
105+
builder.setResourceNetworkMap(new LinkedList<>());
106+
builder.setResourceCostMap(new LinkedList<>());
113107

114108
wx.put(LogicalDatastoreType.CONFIGURATION, getContextIID(cid), builder.build());
115109
return cid;
@@ -119,10 +113,12 @@ public static InstanceIdentifier<ResourceNetworkMap> createResourceNetworkMap(St
119113
List<Map> networkMap,
120114
WriteTransaction wx) {
121115
InstanceIdentifier<ResourceNetworkMap> iid = getResourceNetworkMapIID(rid);
122-
ResourceNetworkMapBuilder builder = new ResourceNetworkMapBuilder();
123-
builder.setTag(new Tag(UUID.nameUUIDFromBytes(rid.getBytes()).toString().replaceAll("-", "")));
124-
builder.setResourceId(new ResourceId(rid));
125-
builder.setMap(networkMap);
116+
ResourceNetworkMapBuilder builder = new ResourceNetworkMapBuilder()
117+
.setTag(new Tag(UUID.nameUUIDFromBytes(rid.getBytes())
118+
.toString()
119+
.replaceAll("-", "")))
120+
.setResourceId(new ResourceId(rid))
121+
.setMap(networkMap);
126122
wx.put(LogicalDatastoreType.CONFIGURATION, iid, builder.build());
127123
return iid;
128124
}
@@ -132,11 +128,13 @@ public static InstanceIdentifier<ResourceCostMap> createResourceCostMap(String r
132128
List<org.opendaylight.yang.gen.v1.urn.alto.manual.maps.costmap.rev151021.cost.map.Map> costMap,
133129
WriteTransaction wx) {
134130
InstanceIdentifier<ResourceCostMap> iid = getResourceCostMapIID(rid);
135-
ResourceCostMapBuilder builder = new ResourceCostMapBuilder();
136-
builder.setTag(new Tag(UUID.nameUUIDFromBytes(rid.getBytes()).toString().replaceAll("-", "")));
137-
builder.setResourceId(new ResourceId(rid));
138-
builder.setMap(costMap);
139-
builder.setMeta(meta);
131+
ResourceCostMapBuilder builder = new ResourceCostMapBuilder()
132+
.setTag(new Tag(UUID.nameUUIDFromBytes(rid.getBytes())
133+
.toString()
134+
.replaceAll("-", "")))
135+
.setResourceId(new ResourceId(rid))
136+
.setMap(costMap)
137+
.setMeta(meta);
140138
wx.put(LogicalDatastoreType.CONFIGURATION, iid, builder.build());
141139
return iid;
142140
}
@@ -158,7 +156,8 @@ public static void deleteResourceNetworkMap(String cid, String rid, final WriteT
158156
deleteResourceNetworkMap(new Uuid(cid), new ResourceId(cid), wx);
159157
}
160158

161-
public static void deleteResourceNetworkMap(Uuid cid, ResourceId rid, final WriteTransaction wx) {
159+
public static void deleteResourceNetworkMap(Uuid cid, ResourceId rid,
160+
final WriteTransaction wx) {
162161
wx.delete(LogicalDatastoreType.CONFIGURATION, getResourceNetworkMapIID(cid, rid));
163162
}
164163

alto-basic/manual-maps/impl/src/main/java/org/opendaylight/alto/basic/impl/AltoManualMapsProvider.java

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
import java.util.LinkedList;
1111
import java.util.List;
12+
import java.util.concurrent.ExecutionException;
13+
1214
import org.opendaylight.alto.basic.manual.maps.ManualMapsUtils;
1315
import org.opendaylight.alto.core.northbound.api.AltoNorthboundRoute;
1416
import org.opendaylight.alto.core.northbound.api.AltoNorthboundRouter;
@@ -34,8 +36,7 @@ public class AltoManualMapsProvider implements AutoCloseable {
3436
private final BindingAwareBroker.RoutedRpcRegistration<AltoModelNetworkmapService> altoModelNetworkmapService;
3537
private final BindingAwareBroker.RoutedRpcRegistration<AltoModelCostmapService> altoModelCostmapService;
3638

37-
private AltoNorthboundRouter m_router = null;
38-
private List<Uuid> m_contexts = null;
39+
private List<Uuid> contextList = null;
3940

4041
public AltoManualMapsProvider(DataBroker dataBroker,
4142
AltoNorthboundRouter router,
@@ -57,16 +58,16 @@ protected void closeListener() throws Exception {
5758
manualMapsListener.close();
5859
}
5960

60-
protected void initializeConfigContext() throws Exception {
61-
m_contexts = new LinkedList<>();
61+
protected void initializeConfigContext() throws ExecutionException, InterruptedException {
62+
contextList = new LinkedList<>();
6263
WriteTransaction wx = dataBroker.newWriteOnlyTransaction();
63-
m_contexts.add(ManualMapsUtils.createContext(wx));
64+
contextList.add(ManualMapsUtils.createContext(wx));
6465
wx.submit().get();
6566
}
6667

67-
protected void clearConfigContext() throws Exception {
68+
protected void clearConfigContext() throws ExecutionException, InterruptedException {
6869
WriteTransaction wx = dataBroker.newWriteOnlyTransaction();
69-
for (Uuid context : m_contexts) {
70+
for (Uuid context : contextList) {
7071
ManualMapsUtils.deleteContext(context, wx);
7172
}
7273
wx.submit().get();
@@ -88,30 +89,24 @@ public void init() {
8889
@Override
8990
public void close() throws Exception {
9091
try {
91-
if (m_router != null) {
92-
m_router.removeRoute(RESOURCE_CONFIG_ROUTE_NAME);
92+
if (router != null) {
93+
router.removeRoute(RESOURCE_CONFIG_ROUTE_NAME);
9394
}
9495
clearConfigContext();
9596
closeListener();
9697
} catch (Exception e) {
97-
LOG.error("Failed to remove route");
98+
LOG.error("Failed to remove route", e);
9899
}
99100

100101
LOG.info("AltoManualMapsProvider Closed");
101102
}
102103

103104
private void setupRoute() {
104105
AltoNorthboundRoute route = new ManualMapsRoute(this);
105-
String base_url = router.addRoute(RESOURCE_CONFIG_ROUTE_NAME, route);
106-
if (base_url == null) {
106+
String baseUrl = router.addRoute(RESOURCE_CONFIG_ROUTE_NAME, route);
107+
if (baseUrl == null) {
107108
LOG.error("Failed to register route for AltoManualMaps");
108109
return;
109110
}
110-
111-
try {
112-
m_router = router;
113-
} catch (Exception e) {
114-
LOG.error("Failed to reigster route");
115-
}
116111
}
117112
}

0 commit comments

Comments
 (0)