blob: 18365bf83c5d555a97a5bfc082dd72b0630fa3d6 [file] [log] [blame]
Akash Sonib3abf522022-12-19 13:20:02 +05301/*
2* Copyright 2022-present Open Networking Foundation
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7* http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14 */
15
16package onos_nbi
17
18import (
19 "context"
20 "encoding/json"
21 "net/http"
22 app "voltha-go-controller/internal/pkg/application"
23 "voltha-go-controller/log"
24
25 "github.com/gorilla/mux"
26)
27
28type MacLearnerHandle struct {
29}
30
31func init() {
32 // Setup this package so that it's log level can be modified at run time
33 var err error
34 logger, err = log.AddPackageWithDefaultParam()
35 if err != nil {
36 panic(err)
37 }
38}
39
40// ServeHTTP to serve http request
41func (mlh *MacLearnerHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
42 logger.Infow(ctx, "Received-northbound-request", log.Fields{"Method": r.Method, "URL": r.URL})
43 vars := mux.Vars(r)
44 deviceID := vars["deviceId"]
45 portNum := vars["portNumber"]
46 vlanId := vars["vlanId"]
47 switch r.Method {
48 case "GET":
49 if deviceID == "" && portNum == "" && vlanId == "" {
50 mlh.GetAllMacLearnerInfo(context.Background(), w, r)
51 } else {
52 mlh.GetMacLearnerInfo(context.Background(), deviceID, portNum, vlanId, w, r)
53 }
54 default:
55 logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method})
56 }
57}
58
59func (mlh *MacLearnerHandle) GetAllMacLearnerInfo(cntx context.Context, w http.ResponseWriter, r *http.Request) {
60
61 logger.Info(cntx, "Inside GetAllMacLearnerInfo method")
62 MacLearnerInfo, err := app.GetApplication().GetAllMacLearnerInfo()
63 if err != nil {
64 logger.Errorw(ctx, "Failed to get mac learning info", log.Fields{"Reason": err.Error()})
65 w.WriteHeader(http.StatusInternalServerError)
66 return
67 }
68
69 MliRespJSON, err := json.Marshal(MacLearnerInfo)
70 if err != nil {
71 logger.Errorw(ctx, "Error occurred while marshaling mac learner response", log.Fields{"Error": err})
72 w.WriteHeader(http.StatusInternalServerError)
73 return
74 }
75
76 w.Header().Add("Content-Type", "application/json")
77 _, err = w.Write(MliRespJSON)
78 if err != nil {
79 logger.Errorw(ctx, "error in sending mac learner response", log.Fields{"Error": err})
80 w.WriteHeader(http.StatusInternalServerError)
81 }
82
83}
84
85func (mlh *MacLearnerHandle) GetMacLearnerInfo(cntx context.Context, deviceID, portNum, vlanId string, w http.ResponseWriter, r *http.Request) {
86
87 logger.Infow(cntx, "Inside GetMacLearnerInfo method", log.Fields{"deviceID": deviceID, "portNum": portNum, "vlanId": vlanId})
88 MacLearnerInfo, err := app.GetApplication().GetMacLearnerInfo(cntx, deviceID, portNum, vlanId)
89 if err != nil {
90 logger.Errorw(ctx, "Failed to get mac learning info", log.Fields{"Reason": err.Error()})
91 w.WriteHeader(http.StatusInternalServerError)
92 return
93 }
94
95 MliRespJSON, err := json.Marshal(MacLearnerInfo)
96 if err != nil {
97 logger.Errorw(ctx, "Error occurred while marshaling mac learner response", log.Fields{"Error": err})
98 w.WriteHeader(http.StatusInternalServerError)
99 return
100 }
101
102 w.Header().Add("Content-Type", "application/json")
103 _, err = w.Write(MliRespJSON)
104 if err != nil {
105 logger.Errorw(ctx, "error in sending mac learner response", log.Fields{"Error": err})
106 w.WriteHeader(http.StatusInternalServerError)
107 }
108
109}