blob: f4a2b2df6b17ac4f39b06591206158e0de378a74 [file] [log] [blame]
Tinoj Josephec742f62022-09-29 19:11:10 +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 "strconv"
23
24 "github.com/gorilla/mux"
25 "voltha-go-controller/internal/pkg/of"
26 "voltha-go-controller/log"
27 cntlr "voltha-go-controller/internal/pkg/controller"
28)
29
30// FlowHandle struct to handle flow related REST calls
31type FlowHandle struct {
32}
33
34// FlowHandle struct to handle flow related REST calls
35type PendingFlowHandle struct {
36}
37
38type TrafficSelector struct {
39
40}
41
42type TrafficTreatment struct {
43
44}
45
46/*
47type FlowEntry struct {
48 TrafficSelector
49 TrafficTreatment
50 FlowID int
51 AppID int
52 GroupID int
53 Priority int
54 DeviceID string
55 TimeOut int
56 TableID int
57}*/
58
59func (fh *FlowHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
60 logger.Infow(ctx, "Received-northbound-request", log.Fields{"Method": r.Method, "URL": r.URL})
61 switch r.Method {
62 case "GET":
63 fh.GetFlows(context.Background(), w, r)
64 default:
65 logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method})
66 }
67}
68
69func (pfh *PendingFlowHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
70 logger.Infow(ctx, "Received-northbound-request", log.Fields{"Method": r.Method, "URL": r.URL})
71 switch r.Method {
72 case "GET":
73 pfh.GetPendingFlows(context.Background(), w, r)
74 default:
75 logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method})
76 }
77}
78
79func (pfh *PendingFlowHandle) GetPendingFlows(cntx context.Context, w http.ResponseWriter, r *http.Request) {
80 flows, err := cntlr.GetController().GetAllPendingFlows()
81 if err != nil {
82 logger.Errorw(ctx, "Failed to get Pending flows", log.Fields{"Error": err})
83 return
84 }
85 flowResp := ConvertFlowsToFlowEntry(flows)
86 FlowRespJSON, err := json.Marshal(flowResp)
87 if err != nil {
88 logger.Errorw(ctx, "Failed to marshal pending flow response", log.Fields{"Error": err})
89 w.WriteHeader(http.StatusInternalServerError)
90 return
91 }
92
93 w.Header().Add("Content-Type", "application/json")
94 _, err = w.Write(FlowRespJSON)
95 if err != nil {
96 logger.Errorw(ctx, "Failed to write Pending Flow response", log.Fields{"Error": err})
97 w.WriteHeader(http.StatusInternalServerError)
98 }
99
100}
101
102func (fh *FlowHandle) GetFlows(cntx context.Context, w http.ResponseWriter, r *http.Request) {
103 vars := mux.Vars(r)
104 deviceID := vars["deviceId"]
105 flowIDStr := vars["flowId"]
106 flowID, _ := strconv.ParseUint(flowIDStr, 10, 64)
107 var flowResp FlowEntry
108 if len(deviceID) > 0 && len(flowIDStr) > 0 {
109 flow, err := fh.getFlow(deviceID, flowID)
110 if err != nil {
111 logger.Errorw(ctx, "Failed to Fetch flow", log.Fields{"Error": err})
112 return
113 }
114 flowResp = ConvertFlowToFlowEntry(flow)
115 //flowResp = append(flowResp, flow)
116 } else {
117 flows, err := fh.getAllFlows(deviceID)
118 if err != nil {
119 logger.Errorw(ctx, "Failed to Fetch flows", log.Fields{"Error": err})
120 return
121 }
122 flowResp = ConvertFlowsToFlowEntry(flows)
123 //..flowResp = append(flowResp, flows...)
124 }
125 FlowRespJSON, err := json.Marshal(flowResp)
126 if err != nil {
127 logger.Errorw(ctx, "Failed to marshal flow response", log.Fields{"Error": err})
128 w.WriteHeader(http.StatusInternalServerError)
129 return
130 }
131
132 w.Header().Add("Content-Type", "application/json")
133 _, err = w.Write(FlowRespJSON)
134 if err != nil {
135 logger.Errorw(ctx, "Failed to write flow response", log.Fields{"Error": err})
136 w.WriteHeader(http.StatusInternalServerError)
137 }
138}
139
140func (fh *FlowHandle) getAllFlows(deviceID string) ([]*of.VoltSubFlow, error) {
141 if len(deviceID) == 0 {
142 return cntlr.GetController().GetAllFlows()
143 }
144 return cntlr.GetController().GetFlows(deviceID)
145}
146
147func (fh *FlowHandle) getFlow(deviceID string, flowID uint64) (*of.VoltSubFlow, error) {
148 return cntlr.GetController().GetFlow(deviceID, flowID)
149}