# Infrastructure Architecture - Final Design

**Date:** 2025-10-23
**Decision:** Use dedicated infrastructure tables with hierarchical relationships

---

## Existing Schema (Already in Database!)

### IDFs (Intermediate Distribution Frames)
```sql
idfs:
  - idfid INT(11) PK
  - idfname VARCHAR(100)
  - description VARCHAR(255)
  - maptop, mapleft INT(11) -- map coordinates
  - isactive BIT(1)
```
**No parent** - IDFs are top-level containers

### Cameras
```sql
cameras:
  - cameraid INT(11) PK
  - modelid INT(11) → models.modelnumberid → vendors
  - idfid INT(11) → idfs.idfid  Already has parent!
  - serialnumber VARCHAR(100)
  - macaddress VARCHAR(17)  Camera-specific
  - ipaddress VARCHAR(45)
  - description VARCHAR(255)
  - maptop, mapleft INT(11)
  - isactive BIT(1)
```

### Switches
```sql
switches:
  - switchid INT(11) PK
  - modelid INT(11) → models.modelnumberid → vendors
  - serialnumber VARCHAR(100)
  - ipaddress VARCHAR(45)
  - description VARCHAR(255)
  - maptop, mapleft INT(11)
  - isactive BIT(1)
```
**Missing:** `idfid` (switches should belong to IDFs)

### Servers
```sql
servers:
  - serverid INT(11) PK
  - modelid INT(11) → models.modelnumberid → vendors
  - serialnumber VARCHAR(100)
  - ipaddress VARCHAR(45)
  - description VARCHAR(255)
  - maptop, mapleft INT(11)
  - isactive BIT(1)
```
**Optional:** `idfid` (servers might be in IDFs)

---

## Hierarchical Relationships

```
IDFs (top level)
  ├─ Switches (belong to IDF)
  │   └─ Cameras (might connect to switch)
  └─ Cameras (belong to IDF)
  └─ Servers (might be in IDF)
```

---

## Migration Needed

### Step 1: Add idfid to switches (Required)
```sql
ALTER TABLE switches
  ADD COLUMN idfid INT(11) AFTER modelid,
  ADD INDEX idx_switches_idfid (idfid),
  ADD CONSTRAINT fk_switches_idf
    FOREIGN KEY (idfid) REFERENCES idfs(idfid) ON DELETE SET NULL;
```

### Step 2: Add idfid to servers (Optional)
```sql
ALTER TABLE servers
  ADD COLUMN idfid INT(11) AFTER modelid,
  ADD INDEX idx_servers_idfid (idfid),
  ADD CONSTRAINT fk_servers_idf
    FOREIGN KEY (idfid) REFERENCES idfs(idfid) ON DELETE SET NULL;
```

### Step 3: Ensure modelid exists (migration script handles this)
Run `add_infrastructure_vendor_model_support.sql`

---

## Page Architecture

### Unified List Page + Type-Specific Detail Pages

**Why:** Different device types have different fields, so unified edit forms would be messy.

### Files (7 total):

```
network_devices.asp                    → Unified list with tabs/filter
network_device_detail_idf.asp?id=5     → IDF detail/edit
network_device_detail_server.asp?id=3  → Server detail/edit
network_device_detail_switch.asp?id=2  → Switch detail/edit
network_device_detail_camera.asp?id=1  → Camera detail/edit
add_network_device.asp?type=idf        → Add form (type selector)
save_network_device.asp                → Universal save (routes by type)
```

---

## Page 1: network_devices.asp (Unified List)

### Features
- **Tabs:** All | IDFs | Servers | Switches | Cameras
- **Single table** showing all infrastructure
- **Click device** → routes to appropriate detail page based on type

### Routing Logic
```vbscript
Select Case rs("device_type")
    Case "IDF"
        detailUrl = "network_device_detail_idf.asp?id=" & rs("device_id")
    Case "Server"
        detailUrl = "network_device_detail_server.asp?id=" & rs("device_id")
    Case "Switch"
        detailUrl = "network_device_detail_switch.asp?id=" & rs("device_id")
    Case "Camera"
        detailUrl = "network_device_detail_camera.asp?id=" & rs("device_id")
End Select
```

---

## Page 2: network_device_detail_idf.asp

### Unique Fields
- **idfname** (no model/vendor - IDFs are just locations)
- **description**
- **Map coordinates**

### Form Fields
```html
<input type="text" name="idfname" required>
<textarea name="description"></textarea>
<input type="number" name="maptop" placeholder="Y coordinate">
<input type="number" name="mapleft" placeholder="X coordinate">
```

### No Parent Selection
IDFs are top-level, no parent dropdown needed.

---

## Page 3: network_device_detail_server.asp

### Fields
- **Model/Vendor dropdown** (modelid)
- **Serial number**
- **IP address**
- **Description**
- **IDF dropdown** (optional - which IDF is this server in?)
- **Map coordinates**

### IDF Dropdown
```vbscript
<div class="form-group">
    <label>IDF Location (Optional)</label>
    <select name="idfid" class="form-control">
        <option value="">-- Not in an IDF --</option>
        <%
        strSQL = "SELECT idfid, idfname FROM idfs WHERE isactive = 1 ORDER BY idfname"
        Set rsIDFs = objConn.Execute(strSQL)
        Do While Not rsIDFs.EOF
        %>
            <option value="<%=rsIDFs("idfid")%>"
                    <%If Not IsNull(rs("idfid")) And rs("idfid") = rsIDFs("idfid") Then Response.Write("selected")%>>
                <%=Server.HTMLEncode(rsIDFs("idfname"))%>
            </option>
        <%
            rsIDFs.MoveNext
        Loop
        %>
    </select>
</div>
```

---

## Page 4: network_device_detail_switch.asp

### Fields
- **Model/Vendor dropdown** (modelid)
- **Serial number**
- **IP address**
- **Description**
- **IDF dropdown** (required - which IDF is this switch in?)
- **Port count** (optional - could add this field)
- **Map coordinates**

### IDF Dropdown (Required for switches)
```vbscript
<div class="form-group">
    <label>IDF Location <span class="text-danger">*</span></label>
    <select name="idfid" required class="form-control">
        <option value="">-- Select IDF --</option>
        <!-- IDF options... -->
    </select>
    <small class="form-text text-muted">
        Switches must be assigned to an IDF
    </small>
</div>
```

---

## Page 5: network_device_detail_camera.asp

### Fields
- **Model/Vendor dropdown** (modelid)
- **Serial number**
- **MAC address** (cameras have this!)
- **IP address**
- **Description**
- **IDF dropdown** (required - which IDF does this camera connect to?)
- **Switch dropdown** (optional - which switch port?)
- **Map coordinates**

### IDF Dropdown (Required)
```vbscript
<div class="form-group">
    <label>IDF Location <span class="text-danger">*</span></label>
    <select name="idfid" required class="form-control">
        <option value="">-- Select IDF --</option>
        <!-- IDF options... -->
    </select>
</div>
```

### MAC Address Field (Unique to cameras)
```vbscript
<div class="form-group">
    <label>MAC Address</label>
    <input type="text" name="macaddress" class="form-control"
           pattern="^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$"
           placeholder="00:11:22:33:44:55">
</div>
```

---

## Page 6: add_network_device.asp

### Step 1: Device Type Selector
Show cards for IDF, Server, Switch, Camera

### Step 2: Type-Specific Form
Route to appropriate form based on selected type:
- `add_network_device.asp?type=idf` → IDF form (no model)
- `add_network_device.asp?type=server` → Server form (model + optional IDF)
- `add_network_device.asp?type=switch` → Switch form (model + required IDF)
- `add_network_device.asp?type=camera` → Camera form (model + required IDF + MAC)

---

## Page 7: save_network_device.asp

### Universal Save Endpoint

```vbscript
<%
Dim deviceType
deviceType = Request.Form("type")

' Route to appropriate table
Select Case deviceType
    Case "idf"
        tableName = "idfs"
        ' Save: idfname, description, maptop, mapleft
        ' No modelid

    Case "server"
        tableName = "servers"
        ' Save: modelid, idfid (optional), serialnumber, ipaddress, description, maptop, mapleft

    Case "switch"
        tableName = "switches"
        ' Save: modelid, idfid (required), serialnumber, ipaddress, description, maptop, mapleft

    Case "camera"
        tableName = "cameras"
        ' Save: modelid, idfid (required), serialnumber, macaddress, ipaddress, description, maptop, mapleft
End Select
%>
```

---

## Navigation Menu

```html
<li class="nav-header">INFRASTRUCTURE</li>
<li>
    <a href="network_devices.asp">
        <i class="zmdi zmdi-devices"></i> Network Devices
    </a>
</li>
<li>
    <a href="network_map.asp">
        <i class="zmdi zmdi-map"></i> Network Map
    </a>
</li>
```

---

## network_map.asp Integration

### Current State
Currently queries `machines` table filtering for infrastructure machine types.

### New Approach
Query both machines AND infrastructure tables:

```vbscript
<%
' Get infrastructure devices
strSQL = "SELECT 'IDF' as type, idfid as id, idfname as name, NULL as model, NULL as vendor, " & _
         "maptop, mapleft, 'IDF' as device_type " & _
         "FROM idfs WHERE isactive = 1 AND maptop IS NOT NULL " & _
         "UNION ALL " & _
         "SELECT 'Server' as type, serverid as id, description as name, m.modelnumber as model, v.vendor, " & _
         "s.maptop, s.mapleft, 'Server' as device_type " & _
         "FROM servers s " & _
         "LEFT JOIN models m ON s.modelid = m.modelnumberid " & _
         "LEFT JOIN vendors v ON m.vendorid = v.vendorid " & _
         "WHERE s.isactive = 1 AND s.maptop IS NOT NULL " & _
         "UNION ALL " & _
         "SELECT 'Switch' as type, switchid as id, description as name, m.modelnumber as model, v.vendor, " & _
         "sw.maptop, sw.mapleft, 'Switch' as device_type " & _
         "FROM switches sw " & _
         "LEFT JOIN models m ON sw.modelid = m.modelnumberid " & _
         "LEFT JOIN vendors v ON m.vendorid = v.vendorid " & _
         "WHERE sw.isactive = 1 AND sw.maptop IS NOT NULL " & _
         "UNION ALL " & _
         "SELECT 'Camera' as type, cameraid as id, description as name, m.modelnumber as model, v.vendor, " & _
         "c.maptop, c.mapleft, 'Camera' as device_type " & _
         "FROM cameras c " & _
         "LEFT JOIN models m ON c.modelid = m.modelnumberid " & _
         "LEFT JOIN vendors v ON m.vendorid = v.vendorid " & _
         "WHERE c.isactive = 1 AND c.maptop IS NOT NULL"

Set rs = objConn.Execute(strSQL)

' Output JSON for map markers
Response.Write("const devices = [")
Do While Not rs.EOF
    Response.Write("{")
    Response.Write("type: '" & rs("device_type") & "', ")
    Response.Write("id: " & rs("id") & ", ")
    Response.Write("name: '" & Replace(rs("name") & "", "'", "\'") & "', ")
    Response.Write("model: '" & Replace(rs("model") & "", "'", "\'") & "', ")
    Response.Write("vendor: '" & Replace(rs("vendor") & "", "'", "\'") & "', ")
    Response.Write("x: " & rs("mapleft") & ", ")
    Response.Write("y: " & rs("maptop"))
    Response.Write("},")
    rs.MoveNext
Loop
Response.Write("];")
%>
```

---

## Summary: Why This Approach?

 **Hierarchical relationships** - Cameras/switches belong to IDFs
 **Type-specific fields** - MAC address for cameras, idfname for IDFs
 **Flexible** - Can add more fields per type later
 **Clean data model** - Proper normalization
 **Unified list view** - See all infrastructure in one place
 **Type-specific edit** - Appropriate fields per device type
 **Map integration** - All devices can be mapped

**Total Files:** 7 ASP files (1 list + 4 detail + 1 add + 1 save)

---

**Next Step:** Run enhanced migration script to add `idfid` to switches/servers, then create the 7 pages.

