blob: 573fcb2b099a50895d6a72a7cfe02dc0293e815c [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"
Don Newton0766ec22018-11-06 15:18:05 -050020 "errors"
21 "fmt"
donNewtonAlpha27ad20c2018-10-30 16:18:42 -040022 "net"
23
24 "gerrit.opencord.org/abstract-olt/models"
25 "gerrit.opencord.org/abstract-olt/models/physical"
26)
27
28type Chassis struct {
29 Clli string
30 Rack int
31 Shelf int
32 XOSAddr net.TCPAddr
33 LineCards []LineCard
34}
35type LineCard struct {
36 Number int
37 Olts []PhysicalOlt
38}
39type PhysicalOlt struct {
40 Address net.TCPAddr
41 Hostname string
42 Ports []Port
43}
44type Port struct {
45 AbstractNumber int
46 PhysicalNumber int
47 Onts []Ont
48}
49type Ont struct {
50 Number int
Don Newton276cd1f2019-02-06 17:14:03 -050051 Active bool
donNewtonAlpha27ad20c2018-10-30 16:18:42 -040052 SVlan uint32
53 CVlan uint32
54 SerialNumber string
55 NasPortID string
56 CircuitID string
57}
58
59func GatherAllInventory() string {
60 chassisMap := models.GetChassisMap()
61 chassis_s := []Chassis{}
62 for clli, chassisHolder := range *chassisMap {
63 chassis := parseClli(clli, chassisHolder)
64 chassis_s = append(chassis_s, chassis)
65 }
66 bytes, _ := json.Marshal(chassis_s)
67 return string(bytes)
68}
69
Don Newton0766ec22018-11-06 15:18:05 -050070func GatherInventory(clli string) (string, error) {
71 if clli == "" {
72 return "", errors.New("You must provide a CLLI")
73 }
donNewtonAlpha27ad20c2018-10-30 16:18:42 -040074 chassisMap := models.GetChassisMap()
75 chassisHolder := (*chassisMap)[clli]
Don Newton0766ec22018-11-06 15:18:05 -050076 if chassisHolder == nil {
77 errorMsg := fmt.Sprintf("No Chassis Holder found for CLLI %s", clli)
78 return "", errors.New(errorMsg)
79 }
donNewtonAlpha27ad20c2018-10-30 16:18:42 -040080 chassis := parseClli(clli, chassisHolder)
81 bytes, _ := json.Marshal(chassis)
Don Newton0766ec22018-11-06 15:18:05 -050082 return string(bytes), nil
donNewtonAlpha27ad20c2018-10-30 16:18:42 -040083}
84
85func parseClli(clli string, chassisHolder *models.ChassisHolder) Chassis {
86 abstract := chassisHolder.AbstractChassis
87 chassis := Chassis{}
88 chassis.Clli = clli
89 chassis.Rack = abstract.Rack
90 chassis.Shelf = abstract.Shelf
91 chassis.XOSAddr = chassisHolder.PhysicalChassis.XOSAddress
92
93 lineCards := []LineCard{}
94 for index, slot := range abstract.Slots {
95 if slot.Ports[0].PhysPort != nil {
96 lineCard := LineCard{Number: index + 1}
97 var currentOLT *physical.SimpleOLT
98 var physicalOLT PhysicalOlt
99 var ports []Port
100 olts := []PhysicalOlt{}
101 for i := 0; i < 16; i++ {
102 ponPort := slot.Ports[i].PhysPort
103 if ponPort != nil {
104 parentOLT := ponPort.Parent
105 if currentOLT != parentOLT {
106 if currentOLT != nil {
107 physicalOLT.Ports = ports
108 olts = append(olts, physicalOLT)
109 }
110 physicalOLT = PhysicalOlt{Address: parentOLT.Address, Hostname: parentOLT.Hostname}
111 currentOLT = parentOLT
112 ports = []Port{}
113
114 }
115 port := Port{AbstractNumber: i + 1, PhysicalNumber: ponPort.Number}
116 onts := []Ont{}
117 for _, physicalONT := range ponPort.Onts {
Don Newton276cd1f2019-02-06 17:14:03 -0500118 if physicalONT.CircuitID != "" {
119 ont := Ont{Number: physicalONT.Number, Active: physicalONT.Active, SVlan: physicalONT.Svlan, CVlan: physicalONT.Cvlan, SerialNumber: physicalONT.SerialNumber,
donNewtonAlpha27ad20c2018-10-30 16:18:42 -0400120 NasPortID: physicalONT.NasPortID, CircuitID: physicalONT.CircuitID}
121 onts = append(onts, ont)
122 }
123 }
124 port.Onts = onts
125 ports = append(ports, port)
126 }
127
128 if i == 15 { // last one
129 physicalOLT.Ports = ports
130 olts = append(olts, physicalOLT)
131 }
132 }
133 lineCard.Olts = olts
134 lineCards = append(lineCards, lineCard)
135 }
136 }
137 chassis.LineCards = lineCards
138 return chassis
139}