blob: 0ca47140a526ea7b7493a0de89e79f8d2f35d412 [file] [log] [blame]
Takahiro Suzukid7bf8202020-12-17 20:21:59 +09001/*
2 * Copyright 2020-present Open Networking Foundation
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 l2oam
18
19import (
20 "encoding/json"
21 "io/ioutil"
22 "os"
23)
24
25// JSON file name
26const jsonname = "/onu_list.json"
27
28// OnuStatus has ONU status
29type OnuStatus struct {
30 ID string `json:"id"`
31 AdminState string `json:"admin_state"`
32 OpeState string `json:"ope_state"`
33 ConnectState string `json:"con_state"`
34 MacAddress string `json:"mac_addr"`
35 RebootState string `json:"reboot_state"`
36}
37
38// ReadOnuStatusList reads JSON file
39func ReadOnuStatusList() ([]OnuStatus, error) {
40 bytes, err := ioutil.ReadFile(os.Getenv("HOME") + jsonname)
41 if err != nil && os.IsNotExist(err) {
42 return nil, nil
43 }
44 var onuList []OnuStatus
45 if err := json.Unmarshal(bytes, &onuList); err != nil {
46 return nil, err
47 }
48 return onuList, nil
49}
50
51// WriteOnuStatusList writes JSON file
52func WriteOnuStatusList(list []OnuStatus) error {
53 bytes, err := json.Marshal(list)
54 if err != nil {
55 return err
56 }
57 return ioutil.WriteFile(os.Getenv("HOME")+jsonname, bytes, 0644)
58}
59
60// AddOnu adds ONU to ONU status list
61func AddOnu(sts *OnuStatus) error {
62 list, err := ReadOnuStatusList()
63 if err != nil {
64 return err
65 }
66 if list == nil {
67 newList := []OnuStatus{*sts}
68 return WriteOnuStatusList(newList)
69 }
70 return WriteOnuStatusList(append(list, *sts))
71}
72
73// UpdateOnu updates ONU status
74func UpdateOnu(upSts *OnuStatus) error {
75 list, err := ReadOnuStatusList()
76 if (err != nil) || (list == nil) {
77 return err
78 }
79 newList := []OnuStatus{}
80 for _, sts := range list {
81 if sts.ID == upSts.ID {
82 newList = append(newList, *upSts)
83 } else {
84 newList = append(newList, sts)
85 }
86 }
87 return WriteOnuStatusList(newList)
88}
89
90// RemoveOnu removes ONU from ONU status list
91func RemoveOnu(id string) error {
92 list, err := ReadOnuStatusList()
93 if (err != nil) || (list == nil) {
94 return err
95 }
96 newList := []OnuStatus{}
97 for _, sts := range list {
98 if sts.ID != id {
99 newList = append(newList, sts)
100 }
101 }
102 return WriteOnuStatusList(newList)
103}
104
105// GetOnuFromDeviceID returns ONU status from ONU status list using its device ID
106func GetOnuFromDeviceID(id string) (*OnuStatus, error) {
107 list, err := ReadOnuStatusList()
108 if err != nil {
109 return nil, err
110 }
111 if list == nil {
112 return nil, nil
113 }
114 for _, sts := range list {
115 if sts.ID != id {
116 return &sts, nil
117 }
118 }
119 return nil, nil
120}
121
122// GetOnuFromMacAddr returns ONU status from ONU status list using its MAC address
123func GetOnuFromMacAddr(addr string) (*OnuStatus, error) {
124 list, err := ReadOnuStatusList()
125 if err != nil {
126 return nil, err
127 }
128 if list == nil {
129 return nil, nil
130 }
131 for _, sts := range list {
132 if sts.MacAddress == addr {
133 return &sts, nil
134 }
135 }
136 return nil, nil
137}