blob: 427cf19faa7d1c2d68ca599fa8634bed8a838c03 [file] [log] [blame]
Takahiro Suzuki241c10e2020-12-17 20:17:57 +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 */
16package adaptercoreonu
17
18import (
19 "encoding/json"
20 "io/ioutil"
21 "os"
22)
23
24// Jsonファイル名
25const jsonname = "/onu_list.json"
26
27// OnuStatus ONU状態
28type OnuStatus struct {
29 ID string `json:"id"`
30 AdminState string `json:"admin_state"`
31 OpeState string `json:"ope_state"`
32 ConnectState string `json:"con_state"`
33 MacAddress string `json:"mac_addr"`
34 RebootState string `json:"reboot_state"`
35}
36
37//ReadOnuStatusList Jsonファイル読込み
38func ReadOnuStatusList() ([]OnuStatus, error) {
39 bytes, err := ioutil.ReadFile(os.Getenv("HOME") + jsonname)
40 if err != nil && os.IsNotExist(err) {
41 return nil, nil
42 }
43 var onuList []OnuStatus
44 if err := json.Unmarshal(bytes, &onuList); err != nil {
45 return nil, err
46 }
47 return onuList, nil
48}
49
50//WriteOnuStatusList Jsonファイル書き込み
51func WriteOnuStatusList(list []OnuStatus) error {
52 bytes, err := json.Marshal(list)
53 if err != nil {
54 return err
55 }
56 return ioutil.WriteFile(os.Getenv("HOME")+jsonname, bytes, 0644)
57}
58
59//AddOnu ONU追加
60func AddOnu(sts *OnuStatus) error {
61 list, err := ReadOnuStatusList()
62 if err != nil {
63 return err
64 }
65 if list == nil {
66 newList := []OnuStatus{*sts}
67 return WriteOnuStatusList(newList)
68 }
69 return WriteOnuStatusList(append(list, *sts))
70}
71
72//UpdateOnu ONU状態更新
73func UpdateOnu(upSts *OnuStatus) error {
74 list, err := ReadOnuStatusList()
75 if (err != nil) || (list == nil) {
76 return err
77 }
78 newList := []OnuStatus{}
79 for _, sts := range list {
80 if sts.ID == upSts.ID {
81 newList = append(newList, *upSts)
82 } else {
83 newList = append(newList, sts)
84 }
85 }
86 return WriteOnuStatusList(newList)
87}
88
89//RemoveOnu ONU削除
90func RemoveOnu(id string) error {
91 list, err := ReadOnuStatusList()
92 if (err != nil) || (list == nil) {
93 return err
94 }
95 newList := []OnuStatus{}
96 for _, sts := range list {
97 if sts.ID != id {
98 newList = append(newList, sts)
99 }
100 }
101 return WriteOnuStatusList(newList)
102}
103
104//GetOnuFromDeviceID ONU状態取得
105func GetOnuFromDeviceID(id string) (*OnuStatus, error) {
106 list, err := ReadOnuStatusList()
107 if err != nil {
108 return nil, err
109 }
110 if list == nil {
111 return nil, nil
112 }
113 for _, sts := range list {
114 if sts.ID != id {
115 return &sts, nil
116 }
117 }
118 return nil, nil
119}
120
121//GetOnuFromMacAddr ONU状態取得
122func GetOnuFromMacAddr(addr string) (*OnuStatus, error) {
123 list, err := ReadOnuStatusList()
124 if err != nil {
125 return nil, err
126 }
127 if list == nil {
128 return nil, nil
129 }
130 for _, sts := range list {
131 if sts.MacAddress == addr {
132 return &sts, nil
133 }
134 }
135 return nil, nil
136}