blob: a0c2a01a8c179255932a4a8283ad3a7a0e39fcbf [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"
21)
22
23func (chassisHolder ChassisHolder) Serialize() ([]byte, error) {
24 return json.Marshal(chassisHolder)
25
26}
27
28func (chassisHolder *ChassisHolder) Deserialize(jsonData []byte) error {
29 err := json.Unmarshal(jsonData, chassisHolder)
30 if err != nil {
31 return err
32 }
33 physicalChassis := &chassisHolder.PhysicalChassis
34 abstractChassis := &chassisHolder.AbstractChassis
35 //first handle abstract parent pointers
36 for i := 0; i < len(abstractChassis.Slots); i++ {
37 slot := &abstractChassis.Slots[i]
38 slot.Parent = abstractChassis
39 for j := 0; j < len(slot.Ports); j++ {
40 port := &slot.Ports[j]
41 port.Parent = slot
42 for k := 0; k < len(port.Onts); k++ {
43 port.Onts[k].Parent = port
44 }
45 }
46 }
47 //second handle physical parent pointers
48 for i := 0; i < len(physicalChassis.Linecards); i++ {
49 slot := physicalChassis.Linecards[i]
50 slot.Parent = physicalChassis
51 for j := 0; j < len(slot.Ports); j++ {
52 port := &slot.Ports[j]
53 port.Parent = &slot
54 for k := 0; k < len(port.Onts); k++ {
55 port.Onts[k].Parent = port
56 }
57 }
58 }
59 //finally handle abstract.Port -> physical.PonPort pointers
60
61 for i := 0; i < len(physicalChassis.Linecards); i++ {
62 slot := physicalChassis.Linecards[i]
63 for j := 0; j < len(slot.Ports); j++ {
64 absPort, _ := chassisHolder.AbstractChassis.NextPort()
65 absPort.PhysPort = &slot.Ports[j]
66 }
67 }
68 return nil
69}