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 |
| 51 | SVlan uint32 |
| 52 | CVlan uint32 |
| 53 | SerialNumber string |
| 54 | NasPortID string |
| 55 | CircuitID string |
| 56 | } |
| 57 | |
| 58 | func GatherAllInventory() string { |
| 59 | chassisMap := models.GetChassisMap() |
| 60 | chassis_s := []Chassis{} |
| 61 | for clli, chassisHolder := range *chassisMap { |
| 62 | chassis := parseClli(clli, chassisHolder) |
| 63 | chassis_s = append(chassis_s, chassis) |
| 64 | } |
| 65 | bytes, _ := json.Marshal(chassis_s) |
| 66 | return string(bytes) |
| 67 | } |
| 68 | |
Don Newton | 0766ec2 | 2018-11-06 15:18:05 -0500 | [diff] [blame] | 69 | func GatherInventory(clli string) (string, error) { |
| 70 | if clli == "" { |
| 71 | return "", errors.New("You must provide a CLLI") |
| 72 | } |
donNewtonAlpha | 27ad20c | 2018-10-30 16:18:42 -0400 | [diff] [blame] | 73 | chassisMap := models.GetChassisMap() |
| 74 | chassisHolder := (*chassisMap)[clli] |
Don Newton | 0766ec2 | 2018-11-06 15:18:05 -0500 | [diff] [blame] | 75 | if chassisHolder == nil { |
| 76 | errorMsg := fmt.Sprintf("No Chassis Holder found for CLLI %s", clli) |
| 77 | return "", errors.New(errorMsg) |
| 78 | } |
donNewtonAlpha | 27ad20c | 2018-10-30 16:18:42 -0400 | [diff] [blame] | 79 | chassis := parseClli(clli, chassisHolder) |
| 80 | bytes, _ := json.Marshal(chassis) |
Don Newton | 0766ec2 | 2018-11-06 15:18:05 -0500 | [diff] [blame] | 81 | return string(bytes), nil |
donNewtonAlpha | 27ad20c | 2018-10-30 16:18:42 -0400 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | func parseClli(clli string, chassisHolder *models.ChassisHolder) Chassis { |
| 85 | abstract := chassisHolder.AbstractChassis |
| 86 | chassis := Chassis{} |
| 87 | chassis.Clli = clli |
| 88 | chassis.Rack = abstract.Rack |
| 89 | chassis.Shelf = abstract.Shelf |
| 90 | chassis.XOSAddr = chassisHolder.PhysicalChassis.XOSAddress |
| 91 | |
| 92 | lineCards := []LineCard{} |
| 93 | for index, slot := range abstract.Slots { |
| 94 | if slot.Ports[0].PhysPort != nil { |
| 95 | lineCard := LineCard{Number: index + 1} |
| 96 | var currentOLT *physical.SimpleOLT |
| 97 | var physicalOLT PhysicalOlt |
| 98 | var ports []Port |
| 99 | olts := []PhysicalOlt{} |
| 100 | for i := 0; i < 16; i++ { |
| 101 | ponPort := slot.Ports[i].PhysPort |
| 102 | if ponPort != nil { |
| 103 | parentOLT := ponPort.Parent |
| 104 | if currentOLT != parentOLT { |
| 105 | if currentOLT != nil { |
| 106 | physicalOLT.Ports = ports |
| 107 | olts = append(olts, physicalOLT) |
| 108 | } |
| 109 | physicalOLT = PhysicalOlt{Address: parentOLT.Address, Hostname: parentOLT.Hostname} |
| 110 | currentOLT = parentOLT |
| 111 | ports = []Port{} |
| 112 | |
| 113 | } |
| 114 | port := Port{AbstractNumber: i + 1, PhysicalNumber: ponPort.Number} |
| 115 | onts := []Ont{} |
| 116 | for _, physicalONT := range ponPort.Onts { |
| 117 | if physicalONT.Active { |
| 118 | ont := Ont{Number: physicalONT.Number, SVlan: physicalONT.Svlan, CVlan: physicalONT.Cvlan, SerialNumber: physicalONT.SerialNumber, |
| 119 | NasPortID: physicalONT.NasPortID, CircuitID: physicalONT.CircuitID} |
| 120 | onts = append(onts, ont) |
| 121 | } |
| 122 | } |
| 123 | port.Onts = onts |
| 124 | ports = append(ports, port) |
| 125 | } |
| 126 | |
| 127 | if i == 15 { // last one |
| 128 | physicalOLT.Ports = ports |
| 129 | olts = append(olts, physicalOLT) |
| 130 | } |
| 131 | } |
| 132 | lineCard.Olts = olts |
| 133 | lineCards = append(lineCards, lineCard) |
| 134 | } |
| 135 | } |
| 136 | chassis.LineCards = lineCards |
| 137 | return chassis |
| 138 | } |