blob: fbcf4603396e72066c6d0d293580717cd8415571 [file] [log] [blame]
donNewtonAlphae7ab5b92018-09-27 15:09:14 -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*/
16
17package models
18
19import (
20 "encoding/json"
donNewtonAlphac997d642018-10-17 13:22:48 -040021 "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"
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040026)
27
28func (chassisHolder ChassisHolder) Serialize() ([]byte, error) {
donNewtonAlphac997d642018-10-17 13:22:48 -040029 return json.Marshal(chassisHolder.PhysicalChassis)
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040030
31}
32
33func (chassisHolder *ChassisHolder) Deserialize(jsonData []byte) error {
donNewtonAlphac997d642018-10-17 13:22:48 -040034 physicalChassis := physical.Chassis{}
35 err := json.Unmarshal(jsonData, &physicalChassis)
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040036 if err != nil {
37 return err
38 }
donNewtonAlphac997d642018-10-17 13:22:48 -040039 abstractChassis := abstract.GenerateChassis(physicalChassis.CLLI, 1, 1)
40 chassisHolder.AbstractChassis = abstractChassis
41 chassisHolder.PhysicalChassis = physicalChassis
42
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040043 //first handle abstract parent pointers
44 for i := 0; i < len(abstractChassis.Slots); i++ {
45 slot := &abstractChassis.Slots[i]
donNewtonAlphac997d642018-10-17 13:22:48 -040046 slot.Parent = &abstractChassis
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040047 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]
donNewtonAlphac997d642018-10-17 13:22:48 -040058 slot.Parent = &physicalChassis
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040059 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 }
donNewtonAlphac997d642018-10-17 13:22:48 -040076 if settings.GetDebug() {
77 log.Printf("created chassis %v\n", abstractChassis)
78 }
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040079 return nil
80}