blob: ca62a01e1885d1ee0ac9bbd941dbe766030c8a60 [file] [log] [blame]
cuilin20187b2a8c32019-03-26 19:52:28 -07001/*
2 * Copyright 2018-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 main
17
18import (
19 "context"
20 "errors"
21 "fmt"
22 "github.com/opencord/voltha-go/adapters"
23 com "github.com/opencord/voltha-go/adapters/common"
24 ac "github.com/opencord/voltha-go/adapters/openolt/adaptercore"
25 "github.com/opencord/voltha-go/adapters/openolt/config"
26 "github.com/opencord/voltha-go/common/log"
27 "github.com/opencord/voltha-go/db/kvstore"
28 "github.com/opencord/voltha-go/kafka"
29 ic "github.com/opencord/voltha-go/protos/inter_container"
30 "github.com/opencord/voltha-go/protos/voltha"
31 "os"
32 "os/signal"
33 "strconv"
34 "syscall"
35 "time"
36)
37
38type adapter struct {
39 instanceId string
40 config *config.AdapterFlags
41 iAdapter adapters.IAdapter
42 kafkaClient kafka.Client
43 kvClient kvstore.Client
44 kip *kafka.InterContainerProxy
45 coreProxy *com.CoreProxy
46 adapterProxy *com.AdapterProxy
47 halted bool
48 exitChannel chan int
49 receiverChannels []<-chan *ic.InterContainerMessage
50}
51
52func init() {
53 log.AddPackage(log.JSON, log.DebugLevel, nil)
54}
55
56func newAdapter(cf *config.AdapterFlags) *adapter {
57 var a adapter
58 a.instanceId = cf.InstanceID
59 a.config = cf
60 a.halted = false
61 a.exitChannel = make(chan int, 1)
62 a.receiverChannels = make([]<-chan *ic.InterContainerMessage, 0)
63 return &a
64}
65
66func (a *adapter) start(ctx context.Context) {
67 log.Info("Starting Core Adapter components")
68 var err error
69
70 // Setup KV Client
71 log.Debugw("create-kv-client", log.Fields{"kvstore": a.config.KVStoreType})
72 if err := a.setKVClient(); err != nil {
73 log.Fatal("error-setting-kv-client")
74 }
75
76 // Setup Kafka Client
77 if a.kafkaClient, err = newKafkaClient("sarama", a.config.KafkaAdapterHost, a.config.KafkaAdapterPort); err != nil {
78 log.Fatal("Unsupported-common-client")
79 }
80
81 // Start the common InterContainer Proxy - retries indefinitely
82 if a.kip, err = a.startInterContainerProxy(-1); err != nil {
83 log.Fatal("error-starting-inter-container-proxy")
84 }
85
86 // Create the core proxy to handle requests to the Core
87 a.coreProxy = com.NewCoreProxy(a.kip, a.config.Topic, a.config.CoreTopic)
88
89 // Create the adaptor proxy to handle request between olt and onu
90 a.adapterProxy = com.NewAdapterProxy(a.kip, "brcm_openomci_onu", a.config.CoreTopic)
91
92 // Create the open OLT adapter
93 if a.iAdapter, err = a.startOpenOLT(ctx, a.kip, a.coreProxy, a.adapterProxy, a.config.OnuNumber); err != nil {
94 log.Fatal("error-starting-inter-container-proxy")
95 }
96
97 // Register the core request handler
98 if err = a.setupRequestHandler(a.instanceId, a.iAdapter); err != nil {
99 log.Fatal("error-setting-core-request-handler")
100 }
101
102 // Register this adapter to the Core - retries indefinitely
103 if err = a.registerWithCore(-1); err != nil {
104 log.Fatal("error-registering-with-core")
105 }
106}
107
108func (rw *adapter) stop() {
109 // Stop leadership tracking
110 rw.halted = true
111
112 // send exit signal
113 rw.exitChannel <- 0
114
115 // Cleanup - applies only if we had a kvClient
116 if rw.kvClient != nil {
117 // Release all reservations
118 if err := rw.kvClient.ReleaseAllReservations(); err != nil {
119 log.Infow("fail-to-release-all-reservations", log.Fields{"error": err})
120 }
121 // Close the DB connection
122 rw.kvClient.Close()
123 }
124
125 // TODO: More cleanup
126}
127
128func newKVClient(storeType string, address string, timeout int) (kvstore.Client, error) {
129
130 log.Infow("kv-store-type", log.Fields{"store": storeType})
131 switch storeType {
132 case "consul":
133 return kvstore.NewConsulClient(address, timeout)
134 case "etcd":
135 return kvstore.NewEtcdClient(address, timeout)
136 }
137 return nil, errors.New("unsupported-kv-store")
138}
139
140func newKafkaClient(clientType string, host string, port int) (kafka.Client, error) {
141
142 log.Infow("common-client-type", log.Fields{"client": clientType})
143 switch clientType {
144 case "sarama":
145 return kafka.NewSaramaClient(
146 kafka.Host(host),
147 kafka.Port(port),
148 kafka.ProducerReturnOnErrors(true),
149 kafka.ProducerReturnOnSuccess(true),
150 kafka.ProducerMaxRetries(6),
151 kafka.ProducerRetryBackoff(time.Millisecond*30)), nil
152 }
153 return nil, errors.New("unsupported-client-type")
154}
155
156func (a *adapter) setKVClient() error {
157 addr := a.config.KVStoreHost + ":" + strconv.Itoa(a.config.KVStorePort)
158 client, err := newKVClient(a.config.KVStoreType, addr, a.config.KVStoreTimeout)
159 if err != nil {
160 a.kvClient = nil
161 log.Error(err)
162 return err
163 }
164 a.kvClient = client
165 return nil
166}
167
168func toString(value interface{}) (string, error) {
169 switch t := value.(type) {
170 case []byte:
171 return string(value.([]byte)), nil
172 case string:
173 return value.(string), nil
174 default:
175 return "", fmt.Errorf("unexpected-type-%T", t)
176 }
177}
178
179func (a *adapter) startInterContainerProxy(retries int) (*kafka.InterContainerProxy, error) {
180 log.Infow("starting-intercontainer-messaging-proxy", log.Fields{"host": a.config.KafkaAdapterHost,
181 "port": a.config.KafkaAdapterPort, "topic": a.config.Topic})
182 var err error
183 var kip *kafka.InterContainerProxy
184 if kip, err = kafka.NewInterContainerProxy(
185 kafka.InterContainerHost(a.config.KafkaAdapterHost),
186 kafka.InterContainerPort(a.config.KafkaAdapterPort),
187 kafka.MsgClient(a.kafkaClient),
188 kafka.DefaultTopic(&kafka.Topic{Name: a.config.Topic})); err != nil {
189 log.Errorw("fail-to-create-common-proxy", log.Fields{"error": err})
190 return nil, err
191 }
192 count := 0
193 for {
194 if err = kip.Start(); err != nil {
195 log.Warnw("error-starting-messaging-proxy", log.Fields{"error": err})
196 if retries == count {
197 return nil, err
198 }
199 count = +1
200 // Take a nap before retrying
201 time.Sleep(2 * time.Second)
202 } else {
203 break
204 }
205 }
206
207 log.Info("common-messaging-proxy-created")
208 return kip, nil
209}
210
211func (a *adapter) startOpenOLT(ctx context.Context, kip *kafka.InterContainerProxy, cp *com.CoreProxy, ap *com.AdapterProxy, onuNumber int) (*ac.OpenOLT, error) {
212 log.Info("starting-open-olt")
213 var err error
214 sOLT := ac.NewOpenOLT(ctx, a.kip, cp, ap, onuNumber)
215
216 if err = sOLT.Start(ctx); err != nil {
217 log.Fatalw("error-starting-messaging-proxy", log.Fields{"error": err})
218 return nil, err
219 }
220
221 log.Info("open-olt-started")
222 return sOLT, nil
223}
224
225func (a *adapter) setupRequestHandler(coreInstanceId string, iadapter adapters.IAdapter) error {
226 log.Info("setting-request-handler")
227 requestProxy := com.NewRequestHandlerProxy(coreInstanceId, iadapter, a.coreProxy)
228 if err := a.kip.SubscribeWithRequestHandlerInterface(kafka.Topic{Name: a.config.Topic}, requestProxy); err != nil {
229 log.Errorw("request-handler-setup-failed", log.Fields{"error": err})
230 return err
231
232 }
233 log.Info("request-handler-setup-done")
234 return nil
235}
236
237func (a *adapter) registerWithCore(retries int) error {
238 log.Info("registering-with-core")
239 adapterDescription := &voltha.Adapter{Id: "openolt", Vendor: "simulation Enterprise Inc"}
240 types := []*voltha.DeviceType{{Id: "openolt", Adapter: "openolt"}}
241 deviceTypes := &voltha.DeviceTypes{Items: types}
242 count := 0
243 for {
244 if err := a.coreProxy.RegisterAdapter(nil, adapterDescription, deviceTypes); err != nil {
245 log.Warnw("registering-with-core-failed", log.Fields{"error": err})
246 if retries == count {
247 return err
248 }
249 count += 1
250 // Take a nap before retrying
251 time.Sleep(2 * time.Second)
252 } else {
253 break
254 }
255 }
256 log.Info("registered-with-core")
257 return nil
258}
259
260func waitForExit() int {
261 signalChannel := make(chan os.Signal, 1)
262 signal.Notify(signalChannel,
263 syscall.SIGHUP,
264 syscall.SIGINT,
265 syscall.SIGTERM,
266 syscall.SIGQUIT)
267
268 exitChannel := make(chan int)
269
270 go func() {
271 s := <-signalChannel
272 switch s {
273 case syscall.SIGHUP,
274 syscall.SIGINT,
275 syscall.SIGTERM,
276 syscall.SIGQUIT:
277 log.Infow("closing-signal-received", log.Fields{"signal": s})
278 exitChannel <- 0
279 default:
280 log.Infow("unexpected-signal-received", log.Fields{"signal": s})
281 exitChannel <- 1
282 }
283 }()
284
285 code := <-exitChannel
286 return code
287}
288
289func printBanner() {
290 fmt.Println(" ____ ____ _ _______ ")
291 fmt.Println(" / _ \\ / __\\| | |__ __|")
292 fmt.Println(" | | | |_ __ ___ _ __ | | | | | | | ")
293 fmt.Println(" | | | | '_\\ / _\\ '_\\ | | | | | | | ")
294 fmt.Println(" | |__| | |_) | __/ | | || |__| | |____| | ")
295 fmt.Println(" \\____/| .__/\\___|_| |_|\\____/|______|_| ")
296 fmt.Println(" | | ")
297 fmt.Println(" |_| ")
298 fmt.Println(" ")
299}
300
301func main() {
302 start := time.Now()
303
304 cf := config.NewAdapterFlags()
305 cf.ParseCommandArguments()
306
307 //// Setup logging
308
309 //Setup default logger - applies for packages that do not have specific logger set
310 if _, err := log.SetDefaultLogger(log.JSON, cf.LogLevel, log.Fields{"instanceId": cf.InstanceID}); err != nil {
311 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
312 }
313
314 // Update all loggers (provisionned via init) with a common field
315 if err := log.UpdateAllLoggers(log.Fields{"instanceId": cf.InstanceID}); err != nil {
316 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
317 }
318
319 log.SetPackageLogLevel("github.com/opencord/voltha-go/adapters/common", log.DebugLevel)
320
321 defer log.CleanUp()
322
323 // Print banner if specified
324 if cf.Banner {
325 printBanner()
326 }
327
328 log.Infow("config", log.Fields{"config": *cf})
329
330 ctx, cancel := context.WithCancel(context.Background())
331 defer cancel()
332
333 ad := newAdapter(cf)
334 go ad.start(ctx)
335
336 code := waitForExit()
337 log.Infow("received-a-closing-signal", log.Fields{"code": code})
338
339 // Cleanup before leaving
340 ad.stop()
341
342 elapsed := time.Since(start)
343 log.Infow("run-time", log.Fields{"instanceId": ad.config.InstanceID, "time": elapsed / time.Second})
344}