Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2017 the original author or authors. |
| 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 | |
| 17 | package api |
| 18 | |
| 19 | import ( |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 20 | "errors" |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 21 | "fmt" |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 22 | "net" |
donNewtonAlpha | c997d64 | 2018-10-17 13:22:48 -0400 | [diff] [blame] | 23 | "sync" |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 24 | |
Don Newton | e973d34 | 2018-10-26 16:44:12 -0400 | [diff] [blame] | 25 | "gerrit.opencord.org/abstract-olt/internal/pkg/impl" |
donNewtonAlpha | 27ad20c | 2018-10-30 16:18:42 -0400 | [diff] [blame] | 26 | "gerrit.opencord.org/abstract-olt/models/inventory" |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 27 | context "golang.org/x/net/context" |
| 28 | ) |
| 29 | |
| 30 | /* |
| 31 | Server instance of the grpc server |
| 32 | */ |
| 33 | type Server struct { |
| 34 | } |
| 35 | |
donNewtonAlpha | c997d64 | 2018-10-17 13:22:48 -0400 | [diff] [blame] | 36 | var syncChan chan bool |
| 37 | var isDirty bool |
| 38 | |
| 39 | var runOnce sync.Once |
| 40 | |
| 41 | func getSyncChannel() chan bool { |
| 42 | runOnce.Do(func() { |
| 43 | syncChan = make(chan bool, 1) |
| 44 | syncChan <- true |
| 45 | }) |
| 46 | |
| 47 | return syncChan |
| 48 | } |
| 49 | func done(myChan chan bool, done bool) { |
| 50 | myChan <- done |
| 51 | } |
| 52 | |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 53 | /* |
| 54 | Echo - Tester function which just returns same string sent to it |
| 55 | */ |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 56 | func (s *Server) Echo(ctx context.Context, in *EchoMessage) (*EchoReplyMessage, error) { |
donNewtonAlpha | c997d64 | 2018-10-17 13:22:48 -0400 | [diff] [blame] | 57 | myChan := getSyncChannel() |
| 58 | <-myChan |
| 59 | defer done(myChan, true) |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 60 | ping := in.GetPing() |
| 61 | pong := EchoReplyMessage{Pong: ping} |
| 62 | return &pong, nil |
| 63 | } |
| 64 | |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 65 | /* |
| 66 | CreateChassis - allocates a new Chassis struct and stores it in chassisMap |
| 67 | */ |
| 68 | func (s *Server) CreateChassis(ctx context.Context, in *AddChassisMessage) (*AddChassisReturn, error) { |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 69 | clli := in.GetCLLI() |
donNewtonAlpha | 57aa2ff | 2018-10-01 16:45:32 -0400 | [diff] [blame] | 70 | |
| 71 | xosIP := net.ParseIP(in.GetXOSIP()) |
| 72 | xosPort := int(in.GetXOSPort()) |
| 73 | if xosIP == nil { |
| 74 | errStr := fmt.Sprintf("Invalid IP %s supplied for XOSIP", in.GetXOSIP()) |
| 75 | return nil, errors.New(errStr) |
| 76 | } |
donNewtonAlpha | 57aa2ff | 2018-10-01 16:45:32 -0400 | [diff] [blame] | 77 | xosAddress := net.TCPAddr{IP: xosIP, Port: xosPort} |
donNewtonAlpha | 57aa2ff | 2018-10-01 16:45:32 -0400 | [diff] [blame] | 78 | xosUser := in.GetXOSUser() |
| 79 | xosPassword := in.GetXOSPassword() |
| 80 | if xosUser == "" || xosPassword == "" { |
| 81 | return nil, errors.New("Either XOSUser or XOSPassword supplied were empty") |
| 82 | } |
donNewtonAlpha | 57aa2ff | 2018-10-01 16:45:32 -0400 | [diff] [blame] | 83 | shelf := int(in.GetShelf()) |
| 84 | rack := int(in.GetRack()) |
Don Newton | e973d34 | 2018-10-26 16:44:12 -0400 | [diff] [blame] | 85 | deviceID, err := impl.CreateChassis(clli, xosAddress, xosUser, xosPassword, shelf, rack) |
| 86 | if err != nil { |
| 87 | return nil, err |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 88 | } |
Don Newton | e973d34 | 2018-10-26 16:44:12 -0400 | [diff] [blame] | 89 | return &AddChassisReturn{DeviceID: deviceID}, nil |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /* |
donNewtonAlpha | 57aa2ff | 2018-10-01 16:45:32 -0400 | [diff] [blame] | 93 | ChangeXOSUserPassword - allows update of xos credentials |
| 94 | */ |
| 95 | func (s *Server) ChangeXOSUserPassword(ctx context.Context, in *ChangeXOSUserPasswordMessage) (*ChangeXOSUserPasswordReturn, error) { |
| 96 | clli := in.GetCLLI() |
| 97 | xosUser := in.GetXOSUser() |
| 98 | xosPassword := in.GetXOSPassword() |
| 99 | if xosUser == "" || xosPassword == "" { |
| 100 | return nil, errors.New("Either XOSUser or XOSPassword supplied were empty") |
| 101 | } |
Don Newton | e973d34 | 2018-10-26 16:44:12 -0400 | [diff] [blame] | 102 | success, err := impl.ChangeXOSUserPassword(clli, xosUser, xosPassword) |
| 103 | return &ChangeXOSUserPasswordReturn{Success: success}, err |
donNewtonAlpha | 57aa2ff | 2018-10-01 16:45:32 -0400 | [diff] [blame] | 104 | |
| 105 | } |
| 106 | |
| 107 | /* |
donNewtonAlpha | b9b85eb | 2018-08-14 14:28:22 -0400 | [diff] [blame] | 108 | CreateOLTChassis adds an OLT chassis/line card to the Physical chassis |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 109 | */ |
| 110 | func (s *Server) CreateOLTChassis(ctx context.Context, in *AddOLTChassisMessage) (*AddOLTChassisReturn, error) { |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 111 | clli := in.GetCLLI() |
Don Newton | e973d34 | 2018-10-26 16:44:12 -0400 | [diff] [blame] | 112 | oltType := in.GetType().String() |
Don Newton | 5154fff | 2018-11-06 17:40:58 -0500 | [diff] [blame] | 113 | driver := in.GetDriver().String() |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 114 | address := net.TCPAddr{IP: net.ParseIP(in.GetSlotIP()), Port: int(in.GetSlotPort())} |
Don Newton | e973d34 | 2018-10-26 16:44:12 -0400 | [diff] [blame] | 115 | hostname := in.GetHostname() |
Don Newton | 5154fff | 2018-11-06 17:40:58 -0500 | [diff] [blame] | 116 | clli, err := impl.CreateOLTChassis(clli, oltType, driver, address, hostname) |
Don Newton | e973d34 | 2018-10-26 16:44:12 -0400 | [diff] [blame] | 117 | return &AddOLTChassisReturn{DeviceID: hostname, ChassisDeviceID: clli}, err |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /* |
donNewtonAlpha | 5234b13 | 2018-08-16 14:12:28 -0400 | [diff] [blame] | 121 | ProvisionOnt provisions an ONT on a specific Chassis/LineCard/Port |
| 122 | */ |
| 123 | func (s *Server) ProvisionOnt(ctx context.Context, in *AddOntMessage) (*AddOntReturn, error) { |
donNewtonAlpha | 5234b13 | 2018-08-16 14:12:28 -0400 | [diff] [blame] | 124 | clli := in.GetCLLI() |
Don Newton | e973d34 | 2018-10-26 16:44:12 -0400 | [diff] [blame] | 125 | slotNumber := int(in.GetSlotNumber()) |
| 126 | portNumber := int(in.GetPortNumber()) |
| 127 | ontNumber := int(in.GetOntNumber()) |
| 128 | serialNumber := in.GetSerialNumber() |
| 129 | success, err := impl.ProvisionOnt(clli, slotNumber, portNumber, ontNumber, serialNumber) |
| 130 | return &AddOntReturn{Success: success}, err |
donNewtonAlpha | 5234b13 | 2018-08-16 14:12:28 -0400 | [diff] [blame] | 131 | } |
Don Newton | 281e025 | 2018-10-22 14:38:50 -0400 | [diff] [blame] | 132 | |
Don Newton | e973d34 | 2018-10-26 16:44:12 -0400 | [diff] [blame] | 133 | /* |
donNewtonAlpha | 27ad20c | 2018-10-30 16:18:42 -0400 | [diff] [blame] | 134 | ProvisionOntFull - provisions ont using sTag,cTag,NasPortID, and CircuitID passed in |
Don Newton | e973d34 | 2018-10-26 16:44:12 -0400 | [diff] [blame] | 135 | */ |
| 136 | func (s *Server) ProvisionOntFull(ctx context.Context, in *AddOntFullMessage) (*AddOntReturn, error) { |
| 137 | clli := in.GetCLLI() |
| 138 | slotNumber := int(in.GetSlotNumber()) |
| 139 | portNumber := int(in.GetPortNumber()) |
| 140 | ontNumber := int(in.GetOntNumber()) |
| 141 | serialNumber := in.GetSerialNumber() |
| 142 | cTag := in.GetCTag() |
| 143 | sTag := in.GetSTag() |
| 144 | nasPortID := in.GetNasPortID() |
| 145 | circuitID := in.GetCircuitID() |
| 146 | success, err := impl.ProvisionOntFull(clli, slotNumber, portNumber, ontNumber, serialNumber, cTag, sTag, nasPortID, circuitID) |
| 147 | return &AddOntReturn{Success: success}, err |
Don Newton | 281e025 | 2018-10-22 14:38:50 -0400 | [diff] [blame] | 148 | } |
donNewtonAlpha | f7cc999 | 2018-08-29 14:23:02 -0400 | [diff] [blame] | 149 | |
| 150 | /* |
donNewtonAlpha | 292d143 | 2018-11-08 19:01:21 -0500 | [diff] [blame^] | 151 | PreProvisionOnt - provisions ont using sTag,cTag,NasPortID, and CircuitID passed in |
| 152 | */ |
| 153 | func (s *Server) PreProvisionOnt(ctx context.Context, in *PreProvisionOntMessage) (*AddOntReturn, error) { |
| 154 | clli := in.GetCLLI() |
| 155 | slotNumber := int(in.GetSlotNumber()) |
| 156 | portNumber := int(in.GetPortNumber()) |
| 157 | ontNumber := int(in.GetOntNumber()) |
| 158 | cTag := in.GetCTag() |
| 159 | sTag := in.GetSTag() |
| 160 | nasPortID := in.GetNasPortID() |
| 161 | circuitID := in.GetCircuitID() |
| 162 | techProfile := in.GetTechProfile() |
| 163 | speedProfile := in.GetSpeedProfile() |
| 164 | success, err := impl.PreProvisionOnt(clli, slotNumber, portNumber, ontNumber, cTag, sTag, nasPortID, circuitID, techProfile, speedProfile) |
| 165 | return &AddOntReturn{Success: success}, err |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | ActivateSerial provisions an ONT on a specific Chassis/LineCard/Port |
| 170 | */ |
| 171 | func (s *Server) ActivateSerial(ctx context.Context, in *AddOntMessage) (*AddOntReturn, error) { |
| 172 | clli := in.GetCLLI() |
| 173 | slotNumber := int(in.GetSlotNumber()) |
| 174 | portNumber := int(in.GetPortNumber()) |
| 175 | ontNumber := int(in.GetOntNumber()) |
| 176 | serialNumber := in.GetSerialNumber() |
| 177 | success, err := impl.ActivateSerial(clli, slotNumber, portNumber, ontNumber, serialNumber) |
| 178 | return &AddOntReturn{Success: success}, err |
| 179 | } |
| 180 | |
| 181 | /* |
donNewtonAlpha | f7cc999 | 2018-08-29 14:23:02 -0400 | [diff] [blame] | 182 | DeleteOnt - deletes a previously provision ont |
| 183 | */ |
| 184 | func (s *Server) DeleteOnt(ctx context.Context, in *DeleteOntMessage) (*DeleteOntReturn, error) { |
donNewtonAlpha | f7cc999 | 2018-08-29 14:23:02 -0400 | [diff] [blame] | 185 | clli := in.GetCLLI() |
Don Newton | e973d34 | 2018-10-26 16:44:12 -0400 | [diff] [blame] | 186 | slotNumber := int(in.GetSlotNumber()) |
| 187 | portNumber := int(in.GetPortNumber()) |
| 188 | ontNumber := int(in.GetOntNumber()) |
| 189 | serialNumber := in.GetSerialNumber() |
| 190 | success, err := impl.DeleteOnt(clli, slotNumber, portNumber, ontNumber, serialNumber) |
| 191 | return &DeleteOntReturn{Success: success}, err |
donNewtonAlpha | f7cc999 | 2018-08-29 14:23:02 -0400 | [diff] [blame] | 192 | } |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 193 | |
donNewtonAlpha | 27ad20c | 2018-10-30 16:18:42 -0400 | [diff] [blame] | 194 | /* |
| 195 | Reflow - iterates through provisioning to rebuild Seba-Pod |
| 196 | */ |
Don Newton | e973d34 | 2018-10-26 16:44:12 -0400 | [diff] [blame] | 197 | func (s *Server) Reflow(ctx context.Context, in *ReflowMessage) (*ReflowReturn, error) { |
| 198 | success, err := impl.Reflow() |
| 199 | return &ReflowReturn{Success: success}, err |
| 200 | |
| 201 | } |
donNewtonAlpha | 27ad20c | 2018-10-30 16:18:42 -0400 | [diff] [blame] | 202 | |
| 203 | /* |
| 204 | Output - causes an immediate backup to be created |
| 205 | */ |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 206 | func (s *Server) Output(ctx context.Context, in *OutputMessage) (*OutputReturn, error) { |
Don Newton | e973d34 | 2018-10-26 16:44:12 -0400 | [diff] [blame] | 207 | success, err := impl.DoOutput() |
| 208 | return &OutputReturn{Success: success}, err |
donNewtonAlpha | 57aa2ff | 2018-10-01 16:45:32 -0400 | [diff] [blame] | 209 | |
| 210 | } |
donNewtonAlpha | 27ad20c | 2018-10-30 16:18:42 -0400 | [diff] [blame] | 211 | |
| 212 | /* |
| 213 | GetFullInventory - gets a full json dump of the currently provisioned equipment |
| 214 | */ |
| 215 | func (s *Server) GetFullInventory(ctx context.Context, in *FullInventoryMessage) (*InventoryReturn, error) { |
| 216 | json := inventory.GatherAllInventory() |
| 217 | return &InventoryReturn{JsonDump: json}, nil |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | GetInventory - returns a json dump of a particular seba-pod |
| 222 | */ |
| 223 | func (s *Server) GetInventory(ctx context.Context, in *InventoryMessage) (*InventoryReturn, error) { |
Don Newton | 0766ec2 | 2018-11-06 15:18:05 -0500 | [diff] [blame] | 224 | json, err := inventory.GatherInventory(in.GetClli()) |
| 225 | return &InventoryReturn{JsonDump: json}, err |
donNewtonAlpha | 27ad20c | 2018-10-30 16:18:42 -0400 | [diff] [blame] | 226 | } |