Takahiro Suzuki | d7bf820 | 2020-12-17 20:21:59 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
Kengof20xx | 05d7402 | 2020-12-21 11:56:38 +0900 | [diff] [blame] | 16 | |
Takahiro Suzuki | d7bf820 | 2020-12-17 20:21:59 +0900 | [diff] [blame] | 17 | package core |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "encoding/json" |
| 22 | "fmt" |
| 23 | "io/ioutil" |
| 24 | "os" |
| 25 | "time" |
| 26 | ) |
| 27 | |
| 28 | type AddFlowParam struct { |
| 29 | Value1 string `json:"value1"` |
| 30 | Value2 string `json:"value2"` |
| 31 | Value3 string `json:"value3"` |
| 32 | } |
| 33 | type AddFlowDownParam struct { |
| 34 | Value1 string `json:"value1"` |
| 35 | Value2 string `json:"value2"` |
| 36 | Value3 string `json:"value3"` |
| 37 | } |
| 38 | type AddFlowUpParam struct { |
| 39 | Value1 string `json:"value1"` |
| 40 | Value2 string `json:"value2"` |
| 41 | Value3 string `json:"value3"` |
| 42 | } |
| 43 | |
| 44 | type OltCtlJson struct { |
| 45 | Command string `json:"command"` |
| 46 | Command_help []string `json:"command_help"` |
| 47 | AddFlowParam AddFlowParam `json:"addFlowParam"` |
| 48 | AddFlowDownParam AddFlowDownParam `json:"addFlowDownParam"` |
| 49 | AddFlowUpParam AddFlowUpParam `json:"addFlowUpParam"` |
| 50 | } |
| 51 | |
| 52 | func init() { |
| 53 | } |
| 54 | |
| 55 | func oltctl_main() { |
| 56 | for { |
| 57 | time.Sleep(1 * time.Second) |
| 58 | jsonData := readJson() |
| 59 | if jsonData == nil { |
| 60 | continue |
| 61 | } |
| 62 | |
| 63 | switch jsonData.Command { |
| 64 | case "AddFlow": |
| 65 | oltctl_AddFlow(&jsonData.AddFlowParam) |
| 66 | case "AddFlowDown": |
| 67 | oltctl_AddFlowDown(&jsonData.AddFlowDownParam) |
| 68 | case "AddFlowUp": |
| 69 | oltctl_AddFlowUp(&jsonData.AddFlowUpParam) |
| 70 | default: |
| 71 | |
| 72 | } |
| 73 | jsonData.Command = "None" |
| 74 | writeJson(jsonData) |
| 75 | |
| 76 | } |
| 77 | |
| 78 | } |
| 79 | func oltctl_AddFlow(param *AddFlowParam) { |
| 80 | logger.Debug(context.Background(), fmt.Sprintf("oltctl_AddFlow() %v", param)) |
| 81 | } |
| 82 | func oltctl_AddFlowDown(param *AddFlowDownParam) { |
| 83 | logger.Debug(context.Background(), fmt.Sprintf("oltctl_AddFlowDown() %v", param)) |
| 84 | } |
| 85 | func oltctl_AddFlowUp(param *AddFlowUpParam) { |
| 86 | logger.Debug(context.Background(), fmt.Sprintf("oltctl_AddFlowUp() %v", param)) |
| 87 | } |
| 88 | |
| 89 | func readJson() *OltCtlJson { |
| 90 | bytes, err := ioutil.ReadFile(os.Getenv("HOME") + "/oltctl.json") |
| 91 | if err != nil { |
| 92 | fmt.Printf("json read error. %v\n", err) |
| 93 | return nil |
| 94 | } |
| 95 | jsonData := &OltCtlJson{} |
| 96 | if err := json.Unmarshal(bytes, jsonData); err != nil { |
| 97 | fmt.Printf("json unmarshal error. %v\n", err) |
| 98 | return nil |
| 99 | } |
| 100 | return jsonData |
| 101 | } |
| 102 | func writeJson(jsonData *OltCtlJson) { |
| 103 | |
| 104 | bytes, err := json.Marshal(jsonData) |
| 105 | if err != nil { |
| 106 | return |
| 107 | } |
| 108 | ioutil.WriteFile(os.Getenv("HOME")+"/oltctl.json", bytes, 0644) |
| 109 | } |