Akash Soni | b3abf52 | 2022-12-19 13:20:02 +0530 | [diff] [blame^] | 1 | /* |
| 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 | |
| 16 | package onos_nbi |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "encoding/json" |
| 21 | "net/http" |
| 22 | "strconv" |
| 23 | app "voltha-go-controller/internal/pkg/controller" |
| 24 | "voltha-go-controller/log" |
| 25 | |
| 26 | "github.com/gorilla/mux" |
| 27 | ) |
| 28 | |
| 29 | type GroupsHandle struct { |
| 30 | } |
| 31 | |
| 32 | func init() { |
| 33 | // Setup this package so that it's log level can be modified at run time |
| 34 | var err error |
| 35 | logger, err = log.AddPackageWithDefaultParam() |
| 36 | if err != nil { |
| 37 | panic(err) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | // ServeHTTP to serve http request |
| 42 | func (gh *GroupsHandle) GroupServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 43 | logger.Infow(ctx, "Received-northbound-request", log.Fields{"Method": r.Method, "URL": r.URL}) |
| 44 | vars := mux.Vars(r) |
| 45 | groupID := vars["id"] |
| 46 | |
| 47 | switch r.Method { |
| 48 | case "GET": |
| 49 | if groupID != "" { |
| 50 | gh.GetGroupInfo(context.Background(), groupID, w, r) |
| 51 | } else { |
| 52 | gh.GetAllGroups(context.Background(), w, r) |
| 53 | } |
| 54 | |
| 55 | default: |
| 56 | logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method}) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func (gh *GroupsHandle) GetGroupInfo(cntx context.Context, groupId string, w http.ResponseWriter, r *http.Request) { |
| 61 | |
| 62 | groupResp := GroupList{} |
| 63 | groupResp.Groups = []*GroupsInfo{} |
| 64 | grpId, err := strconv.ParseUint(groupId, 10, 32) |
| 65 | if err != nil { |
| 66 | logger.Errorw(ctx, "Failed to parse string to uint32", log.Fields{"Reason": err.Error()}) |
| 67 | } |
| 68 | id := uint32(grpId) |
| 69 | |
| 70 | logger.Infow(ctx, "Inside GetGroupInfo method", log.Fields{"groupId": id}) |
| 71 | |
| 72 | Groups, err := app.GetController().GetGroups(ctx, id) |
| 73 | if err != nil { |
| 74 | logger.Errorw(ctx, "Failed to fetch group info", log.Fields{"Reason": err.Error()}) |
| 75 | w.WriteHeader(http.StatusNotFound) |
| 76 | return |
| 77 | } |
| 78 | |
| 79 | GroupResp := gh.convertGroupsToOnosGroup(Groups) |
| 80 | groupResp.Groups = append(groupResp.Groups, GroupResp) |
| 81 | |
| 82 | GroupsRespJSON, err := json.Marshal(groupResp) |
| 83 | if err != nil { |
| 84 | logger.Errorw(ctx, "Error occurred while marshaling group response", log.Fields{"Error": err}) |
| 85 | w.WriteHeader(http.StatusInternalServerError) |
| 86 | return |
| 87 | } |
| 88 | |
| 89 | w.Header().Add("Content-Type", "application/json") |
| 90 | _, err = w.Write(GroupsRespJSON) |
| 91 | if err != nil { |
| 92 | logger.Errorw(ctx, "error in sending group response", log.Fields{"Error": err}) |
| 93 | w.WriteHeader(http.StatusInternalServerError) |
| 94 | } |
| 95 | |
| 96 | } |
| 97 | |
| 98 | func (gh *GroupsHandle) GetAllGroups(cntx context.Context, w http.ResponseWriter, r *http.Request) { |
| 99 | |
| 100 | logger.Info(cntx, "Inside GetAllGroups method") |
| 101 | groupListResp := GroupList{} |
| 102 | groupListResp.Groups = []*GroupsInfo{} |
| 103 | |
| 104 | GroupsInfo, err := app.GetController().GetGroupList() |
| 105 | if err != nil { |
| 106 | logger.Errorw(ctx, "Failed to fetch group info", log.Fields{"Reason": err.Error()}) |
| 107 | w.WriteHeader(http.StatusNotFound) |
| 108 | return |
| 109 | } |
| 110 | |
| 111 | for _, groups := range GroupsInfo { |
| 112 | grpResp := gh.convertGroupsToOnosGroup(groups) |
| 113 | groupListResp.Groups = append(groupListResp.Groups, grpResp) |
| 114 | } |
| 115 | |
| 116 | GroupRespJSON, err := json.Marshal(groupListResp) |
| 117 | if err != nil { |
| 118 | logger.Errorw(ctx, "Error occurred while marshaling meter response", log.Fields{"Error": err}) |
| 119 | w.WriteHeader(http.StatusInternalServerError) |
| 120 | return |
| 121 | } |
| 122 | |
| 123 | w.Header().Add("Content-Type", "application/json") |
| 124 | _, err = w.Write(GroupRespJSON) |
| 125 | if err != nil { |
| 126 | logger.Errorw(ctx, "error in sending meter response", log.Fields{"Error": err}) |
| 127 | w.WriteHeader(http.StatusInternalServerError) |
| 128 | } |
| 129 | |
| 130 | } |