Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame^] | 1 | /* |
| 2 | ================================================================================== |
| 3 | Copyright (c) 2019 AT&T Intellectual Property. |
| 4 | Copyright (c) 2019 Nokia |
| 5 | |
| 6 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | you may not use this file except in compliance with the License. |
| 8 | You may obtain a copy of the License at |
| 9 | |
| 10 | http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | |
| 12 | Unless required by applicable law or agreed to in writing, software |
| 13 | distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | See the License for the specific language governing permissions and |
| 16 | limitations under the License. |
| 17 | ================================================================================== |
| 18 | */ |
| 19 | /* |
| 20 | Mnemonic: rtmgr.go |
| 21 | Abstract: Routing Manager Main file. Implemets the following functions: |
| 22 | - parseArgs: reading command line arguments |
| 23 | - init:Rtmgr initializing the service modules |
| 24 | - serve: running the loop |
| 25 | Date: 12 March 2019 |
| 26 | */ |
| 27 | package main |
| 28 | |
| 29 | import ( |
| 30 | "flag" |
| 31 | "nbi" |
| 32 | "os" |
| 33 | "rpe" |
| 34 | "rtmgr" |
| 35 | "sbi" |
| 36 | "sdl" |
| 37 | "time" |
| 38 | ) |
| 39 | |
| 40 | const SERVICENAME = "rtmgr" |
| 41 | const INTERVAL time.Duration = 2 |
| 42 | |
| 43 | var ( |
| 44 | args *map[string]string |
| 45 | ) |
| 46 | |
| 47 | func parseArgs() { |
| 48 | a := make(map[string]string) |
| 49 | xmgeturl := flag.String("nbi-httpget", "http://localhost:3000/xapps", "xApp Manager URL") |
| 50 | nngpubsock := flag.String("sbi-nngsub", "tcp://0.0.0.0:4560", "NNG Subsciption Socket URI") |
| 51 | file := flag.String("sdl-file", "/db/rt.json", "Local file store location") |
| 52 | rpename := flag.String("rpe", "rmr", "Policy Engine Module name") |
| 53 | loglevel := flag.String("loglevel", "INFO", "INFO | WARN | ERROR | DEBUG") |
| 54 | flag.Parse() |
| 55 | if (*xmgeturl) != "" { |
| 56 | a["xmurl"] = (*xmgeturl) |
| 57 | a["nbiname"] = "httpGetter" |
| 58 | } |
| 59 | if (*nngpubsock) != "" { |
| 60 | a["socketuri"] = (*nngpubsock) |
| 61 | a["sbiname"] = "nngpub" |
| 62 | } |
| 63 | if (*file) != "" { |
| 64 | a["file"] = (*file) |
| 65 | a["sdlname"] = "file" |
| 66 | } |
| 67 | a["rpename"] = (*rpename) |
| 68 | a["loglevel"] = (*loglevel) |
| 69 | args = &a |
| 70 | } |
| 71 | |
| 72 | func initRtmgr() (*nbi.NbiEngineConfig, *sbi.SbiEngineConfig, *sdl.SdlEngineConfig, *rpe.RpeEngineConfig, error) { |
| 73 | var err error |
| 74 | if nbi, err := nbi.GetNbi((*args)["nbiname"]); err == nil && nbi != nil { |
| 75 | if sbi, err := sbi.GetSbi((*args)["sbiname"]); err == nil && sbi != nil { |
| 76 | if sdl, err := sdl.GetSdl((*args)["sdlname"]); err == nil && sdl != nil { |
| 77 | if rpe, err := rpe.GetRpe((*args)["rpename"]); err == nil && rpe != nil { |
| 78 | return nbi, sbi, sdl, rpe, nil |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | return nil, nil, nil, nil, err |
| 84 | } |
| 85 | |
| 86 | func serve(nbi *nbi.NbiEngineConfig, sbi *sbi.SbiEngineConfig, sdl *sdl.SdlEngineConfig, rpe *rpe.RpeEngineConfig) { |
| 87 | err := sbi.OpenSocket((*args)["socketuri"]) |
| 88 | if err != nil { |
| 89 | rtmgr.Logger.Info("fail to open pub socket due to: " + err.Error()) |
| 90 | return |
| 91 | } |
| 92 | defer sbi.CloseSocket() |
| 93 | for { |
| 94 | time.Sleep(INTERVAL * time.Second) |
| 95 | data, err := nbi.BatchFetch((*args)["xmurl"]) |
| 96 | if err != nil { |
| 97 | rtmgr.Logger.Error("cannot get data from " + nbi.Engine.Name + " interface dute to: " + err.Error()) |
| 98 | } else { |
| 99 | sdl.WriteAll((*args)["file"], data) |
| 100 | } |
| 101 | data, err = sdl.ReadAll((*args)["file"]) |
| 102 | if err != nil || data == nil { |
| 103 | rtmgr.Logger.Error("cannot get data from " + sdl.Engine.Name + " interface dute to: " + err.Error()) |
| 104 | continue |
| 105 | } |
| 106 | policies := rpe.GeneratePolicies(data) |
| 107 | err = sbi.DistributeAll(policies) |
| 108 | if err != nil { |
| 109 | rtmgr.Logger.Error("routing rable cannot be published due to: " + err.Error()) |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | func main() { |
| 115 | parseArgs() |
| 116 | rtmgr.SetLogLevel((*args)["loglevel"]) |
| 117 | nbi, sbi, sdl, rpe, err := initRtmgr() |
| 118 | if err != nil { |
| 119 | rtmgr.Logger.Error(err.Error()) |
| 120 | os.Exit(1) |
| 121 | } |
| 122 | rtmgr.Logger.Info("Start " + SERVICENAME + " service") |
| 123 | serve(nbi, sbi, sdl, rpe) |
| 124 | os.Exit(0) |
| 125 | } |