Skip to content
This repository has been archived by the owner on Mar 31, 2023. It is now read-only.

Commit

Permalink
Node Manager Interface for NCM Registration
Browse files Browse the repository at this point in the history
IMPORTANT NOTE:
This is a breaking change in the following way:
create/update node will fail if NodeInfo doesn't contain a valid
NCM uri.
This means, 1) An instance of NCM must be running, 2) The NCM
should be registered with NMM first. NCM Registration should
contain the valid http end-point (like, http://localhost:9014)
and an ID. NodeInfo should contain this NCM ID.

delete node will work only if an NCM is up and running at
http://localhost:9014. Currently this is an issue, it is defered
to next commit since it requires solving a rather tricky problem but
it will be fixed as soon as possible.

The same conditions apply to bulk/file upload methods to create,
update and delete nodes through NMM.

Fix known issues in previous commit
- Node creation returns error if the ncm_id has not been registered.
- NMM query by ncm_id is working.
- NMM is using NCM URI to talk to NCM, except for DELETE (pending issue)
- No Unit tests yet, only swagger UI has been used to validate the code.
  Unit tests don't seem to test the real API, the integration test
  script for testing NMM to NCM flow will be extended.
  • Loading branch information
pkommoju committed Apr 15, 2021
1 parent 5bd9709 commit a35b630
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,14 @@ public static void close() {

public static Ignite getIgnite(){
// if no need create a real ignite server, we return a mock Ignite client
// NOT_JAVA_11_COMPATIBLE: doesn't work in IntelliJ IDE
return Objects.requireNonNullElseGet(igniteServer, IgniteNodeClientMock::new);
// JAVA_11_COMPATIBLE: works in IntelliJ IDE
/*
if (igniteServer != null)
return igniteServer;
return new IgniteNodeClientMock();
*/
}

/**
Expand All @@ -107,4 +114,4 @@ public <K, V> IgniteCache<K, V> getOrCreateCache(CacheConfiguration<K, V> cacheC
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,14 @@ public void checkCreate(String resourceName, TokenEntity tokenEntity, List<Strin
private List<String> excludeFields(String actionName, TokenEntity tokenEntity, OwnerChecker ownerChecker) {
List<String> excludeFields = new ArrayList<>();
Optional<ActionRbacRule> rbacRuleOptional = serviceRbacRule.getActionRbacRule(actionName);
// NOT_JAVA_11_COMPATIBLE: doesn't work in IntelliJ IDE
if (rbacRuleOptional.isEmpty()) {
// if not rbac rule return true
return excludeFields;
// if not rbac rule return true
return excludeFields;
}
// JAVA_11_COMPATIBLE: works in IntelliJ IDE
// if (rbacRuleOptional == null)
// return excludeFields;

List<String> tokenRoles = tokenEntity.getRoles();
List<FieldRbacRule> fieldRbacRules = rbacRuleOptional.get().getFieldRbacRules();
Expand Down Expand Up @@ -165,11 +169,18 @@ private void processExcludeFields(List<String> excludeFields, Object obj) throws
private void checkAction(String actionName, TokenEntity tokenEntity, List<String> bodyFields,
OwnerChecker ownerChecker) throws Exception {
Optional<ActionRbacRule> rbacRuleOptional = serviceRbacRule.getActionRbacRule(actionName);
// NOT_JAVA_11_COMPATIBLE: doesn't work in IntelliJ IDE
if (rbacRuleOptional.isEmpty()) {
// if not rbac rule return true
return;
}

/*
// JAVA_11_COMPATIBLE: works in IntelliJ IDE
if (rbacRuleOptional == null)
return;
*/

String ruleType = rbacRuleOptional.get().getRuleType();
List<String> tokenRoles = tokenEntity.getRoles();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public NodesWebJson getAllNodes(@ApiParam(value = "node_name") @RequestParam(req
}

if (ncm_id != null)
queryParams.put("ncmId", new String[]{ncm_id});
queryParams.put("ncm_id", new String[]{ncm_id});

nodes = service.getAllNodes(queryParams);
} catch (ParameterNullOrEmptyException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,30 +100,21 @@ private void handleCreateNodeBulkRequest(List<NodeInfo> nodeInfos) {
}
}

private boolean augmentNodeInfosWithNcmUri(List<NodeInfo> nodeInfos) {
private void augmentNodeInfosWithNcmUri(List<NodeInfo> nodeInfos) throws Exception {
for (int i = 0; i < nodeInfos.size(); ++i) {
NodeInfo ni = nodeInfos.get(i);
if (addNcmUriToNodeInfo(ni) == false)
return false;
addNcmUriToNodeInfo(ni);
}

return true;
}

private boolean addNcmUriToNodeInfo(NodeInfo nodeInfo) {
try {
NcmInfo ncmInfo = ncmInfoRepository.getNcmInfoById(nodeInfo.getNcmId());
if (ncmInfo == null) {
logger.error("Network Configuration Manager with ncm_id " + nodeInfo.getNcmId() + " is not registered");
return false;
}
nodeInfo.setNcmUri(ncmInfo.getUri());
} catch (Exception e) {
logger.error("Caught Network Configuration Metadata exception: ", e);
return false;
private void addNcmUriToNodeInfo(NodeInfo nodeInfo) throws Exception {
NcmInfo ncmInfo = ncmInfoRepository.getNcmInfoById(nodeInfo.getNcmId());
if (ncmInfo == null || ncmInfo.getUri() == null) {
String except = NodeManagerConstant.NODE_EXCEPTION_NCM_NOT_FOUND + " ncmid = " + nodeInfo.getNcmId();
logger.error(except);
throw new Exception(except);
}

return true;
nodeInfo.setNcmUri(ncmInfo.getUri());
}


Expand All @@ -143,15 +134,9 @@ public int getNodeInfoFromUpload(MultipartFile file) throws IOException, NodeRep
Reader reader = new BufferedReader(new InputStreamReader(file.getInputStream()));
NodeFileLoader dataCenterConfigLoader = new NodeFileLoader();
nodeList = dataCenterConfigLoader.getHostNodeListFromUpload(reader);
if (nodeList != null) {
boolean ncmError = augmentNodeInfosWithNcmUri(nodeList);

if (ncmError)
throw new Exception(NodeManagerConstant.NODE_EXCEPTION_NCM_NOT_FOUND);

nodeRepository.addItemBulkTransaction(nodeList);
nReturn = nodeList.size();
}
augmentNodeInfosWithNcmUri(nodeList);
nodeRepository.addItemBulkTransaction(nodeList);
nReturn = nodeList.size();
} catch (IOException e) {
logger.error(strMethodName+e.getMessage());
throw e;
Expand Down Expand Up @@ -250,12 +235,7 @@ public NodeInfo createNodeInfo(NodeInfo nodeInfo) throws ParameterNullOrEmptyExc
String strMethodName = "createNodeInfo";
if (nodeInfo == null)
throw (new ParameterNullOrEmptyException(NodeManagerConstant.NODE_EXCEPTION_PARAMETER_NULL_EMPTY));
boolean ncmError = addNcmUriToNodeInfo(nodeInfo);

if (ncmError) {
throw new Exception(NodeManagerConstant.NODE_EXCEPTION_NCM_NOT_FOUND);
}

addNcmUriToNodeInfo(nodeInfo);
NodeInfo node = getNodeInfoById(nodeInfo.getId());
if (nodeInfo != null) {
try {
Expand Down Expand Up @@ -285,11 +265,7 @@ public List<NodeInfo> createNodeInfoBulk(List<NodeInfo> nodeInfo) throws Excepti
if (nodeInfo == null)
throw (new ParameterNullOrEmptyException(NodeManagerConstant.NODE_EXCEPTION_PARAMETER_NULL_EMPTY));
if (nodeInfo != null) {
boolean ncmError = augmentNodeInfosWithNcmUri(nodeInfo);

if (ncmError)
throw new Exception(NodeManagerConstant.NODE_EXCEPTION_NCM_NOT_FOUND);

augmentNodeInfosWithNcmUri(nodeInfo);
try {
nodeRepository.addItemBulkTransaction(nodeInfo);
this.handleCreateNodeBulkRequest(nodeInfo);
Expand Down Expand Up @@ -326,6 +302,7 @@ else if (nodeId.equals(node.getId()) == false) {
}
if (nodeInfo != null) {
try {
addNcmUriToNodeInfo(nodeInfo);
nodeRepository.addItem(nodeInfo);
this.handleUpdateNodeRequest(nodeInfo);
} catch (CacheException e) {
Expand Down Expand Up @@ -359,6 +336,7 @@ public String deleteNodeInfo(String nodeId) throws ParameterNullOrEmptyException
else if (nodeId.equals(node.getId()) == false)
throw (new ParameterNullOrEmptyException(NodeManagerConstant.NODE_EXCEPTION_NODE_NOT_EXISTING));
try {
addNcmUriToNodeInfo(node);
nodeRepository.deleteItem(nodeId);
this.handleDeleteNodeRequest(nodeId);
} catch (CacheException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,33 @@ public class NetworkConfigManagerRestClient extends AbstractRestClient {

@DurationStatistics
public void createNodeInfo(NodeInfoJson message) throws Exception {
String ncmUrl = message.getNodeInfo().getNcmUri();
if (ncmUrl == null)
throw new Exception("NetworkConfigManagerClient: Required ncm_uri is NULL");
HttpEntity<NodeInfoJson> request = new HttpEntity<>(message);
restTemplate.postForObject(ncmManagerUrl, request, Object.class);
}

@DurationStatistics
public void updateNodeInfo(NodeInfoJson message) throws Exception {
String ncmUrl = message.getNodeInfo().getNcmUri();
if (ncmUrl == null)
throw new Exception("NetworkConfigManagerClient: Required ncm_uri is NULL");
HttpEntity<NodeInfoJson> request = new HttpEntity<>(message);
restTemplate.postForObject(ncmManagerUrl, request, Object.class);
}

@DurationStatistics
public void deleteNodeInfo(String nodeId) throws Exception {
/*
* PROBLEM: To dispatch node deletion to the correct NCM, we need it's
* URI but we can't get it here.
* FIX: Change the API to pass in NodeInfo.
* String ncmUrl = message.getNodeInfo().getNcmUri();
*
if (ncmUrl == null)
throw new Exception("NetworkConfigManagerClient: Required ncm_uri is NULL");
*/
HttpEntity<String> request = new HttpEntity<>(nodeId);
restTemplate.postForObject(ncmManagerUrl, request, Object.class);
}
Expand All @@ -66,4 +81,4 @@ public void bulkCreatNodeInfo(BulkNodeInfoJson bulkNodeInfoJson) throws Exceptio
HttpEntity<BulkNodeInfoJson> request = new HttpEntity<>(bulkNodeInfoJson);
restTemplate.postForObject(ncmManagerUrl, request, Object.class);
}
}
}

0 comments on commit a35b630

Please sign in to comment.