blob: 9f2bd21f4a3fd17490465296c098069f814eaefe [file] [log] [blame]
donNewtonAlpha27ad20c2018-10-30 16:18:42 -04001/*
2 Copyright 2017 the original author or authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16package inventory
17
18import (
19 "encoding/json"
20 "net"
21
22 "gerrit.opencord.org/abstract-olt/models"
23 "gerrit.opencord.org/abstract-olt/models/physical"
24)
25
26type Chassis struct {
27 Clli string
28 Rack int
29 Shelf int
30 XOSAddr net.TCPAddr
31 LineCards []LineCard
32}
33type LineCard struct {
34 Number int
35 Olts []PhysicalOlt
36}
37type PhysicalOlt struct {
38 Address net.TCPAddr
39 Hostname string
40 Ports []Port
41}
42type Port struct {
43 AbstractNumber int
44 PhysicalNumber int
45 Onts []Ont
46}
47type Ont struct {
48 Number int
49 SVlan uint32
50 CVlan uint32
51 SerialNumber string
52 NasPortID string
53 CircuitID string
54}
55
56func GatherAllInventory() string {
57 chassisMap := models.GetChassisMap()
58 chassis_s := []Chassis{}
59 for clli, chassisHolder := range *chassisMap {
60 chassis := parseClli(clli, chassisHolder)
61 chassis_s = append(chassis_s, chassis)
62 }
63 bytes, _ := json.Marshal(chassis_s)
64 return string(bytes)
65}
66
67func GatherInventory(clli string) string {
68 chassisMap := models.GetChassisMap()
69 chassisHolder := (*chassisMap)[clli]
70 chassis := parseClli(clli, chassisHolder)
71 bytes, _ := json.Marshal(chassis)
72 return string(bytes)
73}
74
75func parseClli(clli string, chassisHolder *models.ChassisHolder) Chassis {
76 abstract := chassisHolder.AbstractChassis
77 chassis := Chassis{}
78 chassis.Clli = clli
79 chassis.Rack = abstract.Rack
80 chassis.Shelf = abstract.Shelf
81 chassis.XOSAddr = chassisHolder.PhysicalChassis.XOSAddress
82
83 lineCards := []LineCard{}
84 for index, slot := range abstract.Slots {
85 if slot.Ports[0].PhysPort != nil {
86 lineCard := LineCard{Number: index + 1}
87 var currentOLT *physical.SimpleOLT
88 var physicalOLT PhysicalOlt
89 var ports []Port
90 olts := []PhysicalOlt{}
91 for i := 0; i < 16; i++ {
92 ponPort := slot.Ports[i].PhysPort
93 if ponPort != nil {
94 parentOLT := ponPort.Parent
95 if currentOLT != parentOLT {
96 if currentOLT != nil {
97 physicalOLT.Ports = ports
98 olts = append(olts, physicalOLT)
99 }
100 physicalOLT = PhysicalOlt{Address: parentOLT.Address, Hostname: parentOLT.Hostname}
101 currentOLT = parentOLT
102 ports = []Port{}
103
104 }
105 port := Port{AbstractNumber: i + 1, PhysicalNumber: ponPort.Number}
106 onts := []Ont{}
107 for _, physicalONT := range ponPort.Onts {
108 if physicalONT.Active {
109 ont := Ont{Number: physicalONT.Number, SVlan: physicalONT.Svlan, CVlan: physicalONT.Cvlan, SerialNumber: physicalONT.SerialNumber,
110 NasPortID: physicalONT.NasPortID, CircuitID: physicalONT.CircuitID}
111 onts = append(onts, ont)
112 }
113 }
114 port.Onts = onts
115 ports = append(ports, port)
116 }
117
118 if i == 15 { // last one
119 physicalOLT.Ports = ports
120 olts = append(olts, physicalOLT)
121 }
122 }
123 lineCard.Olts = olts
124 lineCards = append(lineCards, lineCard)
125 }
126 }
127 chassis.LineCards = lineCards
128 return chassis
129}