Skip to content

Commit

Permalink
Merge pull request #3695 from cisagov/feature/CR-36b
Browse files Browse the repository at this point in the history
Feature/cr 36b
  • Loading branch information
randywoods authored Jan 11, 2024
2 parents 40641da + 63968b3 commit 4f0acef
Show file tree
Hide file tree
Showing 31 changed files with 6,497 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using CSETWebCore.DataLayer.Model;
using CSETWebCore.Interfaces.Malcolm;
using CSETWebCore.Model.Malcolm;
using System;
using System.Collections.Generic;

namespace CSETWebCore.Business.Malcolm
{
public class MalcolmBusiness : IMalcolmBusiness
{
private CSETContext _context;


private Dictionary<string, TempNode> networkOfNodes = new Dictionary<string, TempNode>();



public MalcolmBusiness(CSETContext context)
{
_context = context;
}

public List<MalcolmData> GetMalcolmJsonData(List<MalcolmData> datalist)
{
foreach (MalcolmData malcolmData in datalist)
{
if (malcolmData != null)
{
foreach (var bucket in malcolmData.Values.Buckets)
{
//if (!networkOfNodes.ContainsKey(bucket.Key))
//{
var buckets = new List<Buckets>() { bucket };
BuildNetwork(null, buckets);
//}
}
malcolmData.Graphs = networkOfNodes;
networkOfNodes = new Dictionary<string, TempNode>();
}
}
return datalist;

}
public List<MalcolmData> GetTreesFromMalcolmData(List<MalcolmData> datalist)
{
var malcolmDataList = new List<MalcolmData>();
foreach (MalcolmData malcolmData in datalist)
{
if (malcolmData != null)
{
MalcolmTree trees = new MalcolmTree();
malcolmData.Trees =trees.StartTheTreeWalk(malcolmData.Graphs);
malcolmDataList.Add(malcolmData);
}
}
return malcolmDataList;

}

private void BuildNetwork(TempNode parent, List<Buckets> buckets)
{
foreach (var bucket in buckets)
{
TempNode tnode;
if (String.IsNullOrEmpty(bucket.Key))
{
continue;
}
if (networkOfNodes.TryGetValue(bucket.Key, out tnode))
{
if (parent != null)
parent.AddChildGraphOnly(tnode);
}
else
{
tnode = new TempNode(bucket.Key);
networkOfNodes.TryAdd(bucket.Key, tnode);
if (parent != null)
parent.AddChildGraphOnly(tnode);
}
if (bucket.Values != null)
{
BuildNetwork(tnode, bucket.Values.Buckets);
}
}
}
}
}
129 changes: 129 additions & 0 deletions CSETWebApi/CSETWeb_Api/CSETWebCore.Business/Malcolm/MalcolmTree.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
using CSETWebCore.Model.Malcolm;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace CSETWebCore.Business.Malcolm
{
public class MalcolmTree
{

private HashSet<string> alreadySeenList = new HashSet<string>();



public MalcolmTree()
{

}

private Dictionary<string, TempNode> listOfAll = new Dictionary<string, TempNode>();

public List<TempNode> StartTheTreeWalk(Dictionary<string, TempNode> childrenDict)
{
List<TempNode> RootNodes = new List<TempNode>();
foreach (TempNode t in childrenDict.Values)
{
if (!alreadySeenList.Contains(t.Key))
{
TempNode root = new TempNode(t.Key);
listOfAll.Add(root.Key, root);
RootNodes.Add(root);
HashSet<string> seen = new HashSet<string>() { root.Key };
WalkTree(root,t,seen);
}

}
return RootNodes;
}

private void WalkTree(TempNode parent, TempNode graphNode, HashSet<string> alreadySeen)
{
//trying to do breadth first
//add all the children to the parent
//then recurse down the children
HashSet<string> visited = new HashSet<string>(alreadySeen);
visited.Add(parent.Key);
visited.Add(graphNode.Key);
//Trace.WriteLine("W: "+parent.Key+ "G: "+graphNode.Key);

Dictionary<TempNode, bool> children = new Dictionary<TempNode, bool>();
foreach (var c in graphNode.Children)
{

TempNode cnode;
if (listOfAll.TryGetValue(c.Key, out cnode))
{
if (graphNode.AlreadyWalked(cnode,visited))
{
children.TryAdd(c, false);
}
else
{
visited.Add(cnode.Key);
AddNode(parent, cnode);
children.TryAdd(c, true);
}

}
else
{
var newT = new TempNode(c.Key);
if (graphNode.AlreadyWalked(newT, visited))
{
children.TryAdd(c, false);
}
else
{
visited.Add(newT.Key);
listOfAll.TryAdd(newT.Key, newT);
AddNode(parent, newT);
children.TryAdd(c, true);
}
}
}
List<TempNode> childrenToWalk = children.Where(x => x.Value).Select(x=> x.Key).ToList();
foreach (var c in childrenToWalk)
{
TempNode cnode;
if (listOfAll.TryGetValue(c.Key, out cnode))
{
WalkTree(cnode, c,visited);
//Trace.WriteLine("rf:" + parent.Key + "-" + graphNode.Key);
}
else
{
var newT= new TempNode(c.Key);
listOfAll.Add(c.Key, newT);
WalkTree(newT, c, visited);
//Trace.WriteLine("rn:" + parent.Key + "-" + graphNode.Key);
}
}

}


/// <summary>
///
/// </summary>
/// <param name="parent"></param>
/// <param name="treeNode"></param>
/// <param name="graph"></param>
/// <returns>true if needs processed false if not</returns>
private bool AddNode(TempNode parent, TempNode treeNode)
{
if(alreadySeenList.Contains(treeNode.Key) && alreadySeenList.Contains(parent.Key))
{
return true;
}
//Trace.WriteLine("Adding: " + parent.Key+" ->" +treeNode.Key);
parent.Children.Add(treeNode);
alreadySeenList.Add(treeNode.Key);
alreadySeenList.Add(parent.Key);
return true;


}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,31 @@

<ItemGroup>
<ProjectReference Include="..\CSETWebCore.Business\CSETWebCore.Business.csproj" />
<ProjectReference Include="..\CSETWebCore.Model\CSETWebCore.Model.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="MalcolmJson\all_detected_software.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="MalcolmJson\all_network_protocols.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="MalcolmJson\cross_segment_traffic.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="MalcolmJson\direction_source_destination_protocol.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="MalcolmJson\source_destination_protocol_inbound_only.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="MalcolmJson\source_to_destination_ip.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="MalcolmJson\TestHarness.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Loading

0 comments on commit 4f0acef

Please sign in to comment.