donNewtonAlpha | 27ad20c | 2018-10-30 16:18:42 -0400 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package inventory |
| 17 | |
| 18 | import ( |
| 19 | "encoding/json" |
Don Newton | 0766ec2 | 2018-11-06 15:18:05 -0500 | [diff] [blame] | 20 | "errors" |
| 21 | "fmt" |
donNewtonAlpha | 27ad20c | 2018-10-30 16:18:42 -0400 | [diff] [blame] | 22 | "net" |
| 23 | |
| 24 | "gerrit.opencord.org/abstract-olt/models" |
| 25 | "gerrit.opencord.org/abstract-olt/models/physical" |
| 26 | ) |
| 27 | |
| 28 | type Chassis struct { |
| 29 | Clli string |
| 30 | Rack int |
| 31 | Shelf int |
| 32 | XOSAddr net.TCPAddr |
| 33 | LineCards []LineCard |
| 34 | } |
| 35 | type LineCard struct { |
| 36 | Number int |
| 37 | Olts []PhysicalOlt |
| 38 | } |
| 39 | type PhysicalOlt struct { |
| 40 | Address net.TCPAddr |
| 41 | Hostname string |
| 42 | Ports []Port |
| 43 | } |
| 44 | type Port struct { |
| 45 | AbstractNumber int |
| 46 | PhysicalNumber int |
| 47 | Onts []Ont |
| 48 | } |
| 49 | type Ont struct { |
| 50 | Number int |
Don Newton | 276cd1f | 2019-02-06 17:14:03 -0500 | [diff] [blame] | 51 | Active bool |
donNewtonAlpha | 27ad20c | 2018-10-30 16:18:42 -0400 | [diff] [blame] | 52 | SVlan uint32 |
| 53 | CVlan uint32 |
| 54 | SerialNumber string |
| 55 | NasPortID string |
| 56 | CircuitID string |
| 57 | } |
| 58 | |
| 59 | func 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 Newton | 0766ec2 | 2018-11-06 15:18:05 -0500 | [diff] [blame] | 70 | func GatherInventory(clli string) (string, error) { |
| 71 | if clli == "" { |
| 72 | return "", errors.New("You must provide a CLLI") |
| 73 | } |
donNewtonAlpha | 27ad20c | 2018-10-30 16:18:42 -0400 | [diff] [blame] | 74 | chassisMap := models.GetChassisMap() |
| 75 | chassisHolder := (*chassisMap)[clli] |
Don Newton | 0766ec2 | 2018-11-06 15:18:05 -0500 | [diff] [blame] | 76 | if chassisHolder == nil { |
| 77 | errorMsg := fmt.Sprintf("No Chassis Holder found for CLLI %s", clli) |
| 78 | return "", errors.New(errorMsg) |
| 79 | } |
donNewtonAlpha | 27ad20c | 2018-10-30 16:18:42 -0400 | [diff] [blame] | 80 | chassis := parseClli(clli, chassisHolder) |
| 81 | bytes, _ := json.Marshal(chassis) |
Don Newton | 0766ec2 | 2018-11-06 15:18:05 -0500 | [diff] [blame] | 82 | return string(bytes), nil |
donNewtonAlpha | 27ad20c | 2018-10-30 16:18:42 -0400 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | func 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 Newton | 276cd1f | 2019-02-06 17:14:03 -0500 | [diff] [blame] | 118 | if physicalONT.CircuitID != "" { |
| 119 | ont := Ont{Number: physicalONT.Number, Active: physicalONT.Active, SVlan: physicalONT.Svlan, CVlan: physicalONT.Cvlan, SerialNumber: physicalONT.SerialNumber, |
donNewtonAlpha | 27ad20c | 2018-10-30 16:18:42 -0400 | [diff] [blame] | 120 | 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 | } |