khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package utils |
| 17 | |
| 18 | import ( |
| 19 | "bytes" |
| 20 | "github.com/cevaris/ordered_map" |
| 21 | "github.com/gogo/protobuf/proto" |
| 22 | ofp "github.com/opencord/voltha-go/protos/openflow_13" |
| 23 | ) |
| 24 | |
| 25 | type OfpFlowModArgs map[string]uint64 |
| 26 | |
| 27 | type FlowArgs struct { |
| 28 | MatchFields []*ofp.OfpOxmOfbField |
| 29 | Actions []*ofp.OfpAction |
| 30 | Command *ofp.OfpFlowModCommand |
| 31 | Priority uint32 |
| 32 | KV OfpFlowModArgs |
| 33 | } |
| 34 | |
| 35 | type FlowsAndGroups struct { |
| 36 | Flows *ordered_map.OrderedMap |
| 37 | Groups *ordered_map.OrderedMap |
| 38 | } |
| 39 | |
| 40 | func NewFlowsAndGroups() *FlowsAndGroups { |
| 41 | var fg FlowsAndGroups |
| 42 | fg.Flows = ordered_map.NewOrderedMap() |
| 43 | fg.Groups = ordered_map.NewOrderedMap() |
| 44 | return &fg |
| 45 | } |
| 46 | |
| 47 | func (fg *FlowsAndGroups) Copy() *FlowsAndGroups { |
| 48 | copyFG := NewFlowsAndGroups() |
| 49 | iter := fg.Flows.IterFunc() |
| 50 | for kv, ok := iter(); ok; kv, ok = iter() { |
| 51 | if protoMsg, isMsg := kv.Value.(*ofp.OfpFlowStats); isMsg { |
| 52 | copyFG.Flows.Set(kv.Key, proto.Clone(protoMsg)) |
| 53 | } |
| 54 | } |
| 55 | iter = fg.Groups.IterFunc() |
| 56 | for kv, ok := iter(); ok; kv, ok = iter() { |
| 57 | if protoMsg, isMsg := kv.Value.(*ofp.OfpGroupEntry); isMsg { |
| 58 | copyFG.Groups.Set(kv.Key, proto.Clone(protoMsg)) |
| 59 | } |
| 60 | } |
| 61 | return copyFG |
| 62 | } |
| 63 | |
| 64 | func (fg *FlowsAndGroups) GetFlow(index int) *ofp.OfpFlowStats { |
| 65 | iter := fg.Flows.IterFunc() |
| 66 | pos := 0 |
| 67 | for kv, ok := iter(); ok; kv, ok = iter() { |
| 68 | if pos == index { |
| 69 | if protoMsg, isMsg := kv.Value.(*ofp.OfpFlowStats); isMsg { |
| 70 | return protoMsg |
| 71 | } |
| 72 | return nil |
| 73 | } |
| 74 | pos += 1 |
| 75 | } |
| 76 | return nil |
| 77 | } |
| 78 | |
| 79 | func (fg *FlowsAndGroups) String() string { |
| 80 | var buffer bytes.Buffer |
| 81 | iter := fg.Flows.IterFunc() |
| 82 | for kv, ok := iter(); ok; kv, ok = iter() { |
| 83 | if protoMsg, isMsg := kv.Value.(*ofp.OfpFlowStats); isMsg { |
| 84 | buffer.WriteString("\nFlow:\n") |
| 85 | buffer.WriteString(proto.MarshalTextString(protoMsg)) |
| 86 | buffer.WriteString("\n") |
| 87 | } |
| 88 | } |
| 89 | iter = fg.Groups.IterFunc() |
| 90 | for kv, ok := iter(); ok; kv, ok = iter() { |
| 91 | if protoMsg, isMsg := kv.Value.(*ofp.OfpGroupEntry); isMsg { |
| 92 | buffer.WriteString("\nGroup:\n") |
| 93 | buffer.WriteString(proto.MarshalTextString(protoMsg)) |
| 94 | buffer.WriteString("\n") |
| 95 | } |
| 96 | } |
| 97 | return buffer.String() |
| 98 | } |
| 99 | |
| 100 | func (fg *FlowsAndGroups) AddFlow(flow *ofp.OfpFlowStats) { |
| 101 | if fg.Flows == nil { |
| 102 | fg.Flows = ordered_map.NewOrderedMap() |
| 103 | } |
| 104 | if fg.Groups == nil { |
| 105 | fg.Groups = ordered_map.NewOrderedMap() |
| 106 | } |
| 107 | //Add flow only if absent |
| 108 | if _, exist := fg.Flows.Get(flow.Id); !exist { |
| 109 | fg.Flows.Set(flow.Id, flow) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | //AddFrom add flows and groups from the argument into this structure only if they do not already exist |
| 114 | func (fg *FlowsAndGroups) AddFrom(from *FlowsAndGroups) { |
| 115 | iter := from.Flows.IterFunc() |
| 116 | for kv, ok := iter(); ok; kv, ok = iter() { |
| 117 | if protoMsg, isMsg := kv.Value.(*ofp.OfpFlowStats); isMsg { |
| 118 | if _, exist := fg.Flows.Get(protoMsg.Id); !exist { |
| 119 | fg.Flows.Set(protoMsg.Id, protoMsg) |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | iter = from.Groups.IterFunc() |
| 124 | for kv, ok := iter(); ok; kv, ok = iter() { |
| 125 | if protoMsg, isMsg := kv.Value.(*ofp.OfpGroupEntry); isMsg { |
| 126 | if _, exist := fg.Groups.Get(protoMsg.Stats.GroupId); !exist { |
| 127 | fg.Groups.Set(protoMsg.Stats.GroupId, protoMsg) |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | type DeviceRules struct { |
| 134 | Rules map[string]*FlowsAndGroups |
| 135 | } |
| 136 | |
| 137 | func NewDeviceRules() *DeviceRules { |
| 138 | var dr DeviceRules |
| 139 | dr.Rules = make(map[string]*FlowsAndGroups) |
| 140 | return &dr |
| 141 | } |
| 142 | |
| 143 | func (dr *DeviceRules) Copy() *DeviceRules { |
| 144 | copyDR := NewDeviceRules() |
| 145 | for key, val := range dr.Rules { |
| 146 | copyDR.Rules[key] = val.Copy() |
| 147 | } |
| 148 | return copyDR |
| 149 | } |
| 150 | |
| 151 | func (dr *DeviceRules) String() string { |
| 152 | var buffer bytes.Buffer |
| 153 | for key, value := range dr.Rules { |
| 154 | buffer.WriteString("DeviceId:") |
| 155 | buffer.WriteString(key) |
| 156 | buffer.WriteString(value.String()) |
| 157 | buffer.WriteString("\n\n") |
| 158 | } |
| 159 | return buffer.String() |
| 160 | } |
| 161 | |
| 162 | func (dr *DeviceRules) AddFlowsAndGroup(deviceId string, fg *FlowsAndGroups) { |
| 163 | dr.Rules[deviceId] = fg |
| 164 | } |