donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -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 | |
| 17 | package models |
| 18 | |
| 19 | import ( |
| 20 | "encoding/json" |
donNewtonAlpha | c997d64 | 2018-10-17 13:22:48 -0400 | [diff] [blame] | 21 | "log" |
| 22 | |
| 23 | "gerrit.opencord.org/abstract-olt/internal/pkg/settings" |
| 24 | "gerrit.opencord.org/abstract-olt/models/abstract" |
| 25 | "gerrit.opencord.org/abstract-olt/models/physical" |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | func (chassisHolder ChassisHolder) Serialize() ([]byte, error) { |
donNewtonAlpha | c997d64 | 2018-10-17 13:22:48 -0400 | [diff] [blame] | 29 | return json.Marshal(chassisHolder.PhysicalChassis) |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 30 | |
| 31 | } |
| 32 | |
| 33 | func (chassisHolder *ChassisHolder) Deserialize(jsonData []byte) error { |
donNewtonAlpha | c997d64 | 2018-10-17 13:22:48 -0400 | [diff] [blame] | 34 | physicalChassis := physical.Chassis{} |
| 35 | err := json.Unmarshal(jsonData, &physicalChassis) |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 36 | if err != nil { |
| 37 | return err |
| 38 | } |
donNewtonAlpha | c997d64 | 2018-10-17 13:22:48 -0400 | [diff] [blame] | 39 | abstractChassis := abstract.GenerateChassis(physicalChassis.CLLI, 1, 1) |
| 40 | chassisHolder.AbstractChassis = abstractChassis |
| 41 | chassisHolder.PhysicalChassis = physicalChassis |
| 42 | |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 43 | //first handle abstract parent pointers |
| 44 | for i := 0; i < len(abstractChassis.Slots); i++ { |
| 45 | slot := &abstractChassis.Slots[i] |
donNewtonAlpha | c997d64 | 2018-10-17 13:22:48 -0400 | [diff] [blame] | 46 | slot.Parent = &abstractChassis |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 47 | for j := 0; j < len(slot.Ports); j++ { |
| 48 | port := &slot.Ports[j] |
| 49 | port.Parent = slot |
| 50 | for k := 0; k < len(port.Onts); k++ { |
| 51 | port.Onts[k].Parent = port |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | //second handle physical parent pointers |
| 56 | for i := 0; i < len(physicalChassis.Linecards); i++ { |
| 57 | slot := physicalChassis.Linecards[i] |
donNewtonAlpha | c997d64 | 2018-10-17 13:22:48 -0400 | [diff] [blame] | 58 | slot.Parent = &physicalChassis |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 59 | for j := 0; j < len(slot.Ports); j++ { |
| 60 | port := &slot.Ports[j] |
| 61 | port.Parent = &slot |
| 62 | for k := 0; k < len(port.Onts); k++ { |
| 63 | port.Onts[k].Parent = port |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | //finally handle abstract.Port -> physical.PonPort pointers |
| 68 | |
| 69 | for i := 0; i < len(physicalChassis.Linecards); i++ { |
| 70 | slot := physicalChassis.Linecards[i] |
| 71 | for j := 0; j < len(slot.Ports); j++ { |
| 72 | absPort, _ := chassisHolder.AbstractChassis.NextPort() |
| 73 | absPort.PhysPort = &slot.Ports[j] |
| 74 | } |
| 75 | } |
donNewtonAlpha | c997d64 | 2018-10-17 13:22:48 -0400 | [diff] [blame] | 76 | if settings.GetDebug() { |
| 77 | log.Printf("created chassis %v\n", abstractChassis) |
| 78 | } |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 79 | return nil |
| 80 | } |