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 | b8f3075 | 2018-10-04 11:57:41 -0400 | [diff] [blame] | 60 | fmt.Println("HELLO WTF") |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 61 | ping := in.GetPing() |
| 62 | pong := EchoReplyMessage{Pong: ping} |
| 63 | return &pong, nil |
| 64 | } |
| 65 | |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 66 | /* |
| 67 | CreateChassis - allocates a new Chassis struct and stores it in chassisMap |
| 68 | */ |
| 69 | func (s *Server) CreateChassis(ctx context.Context, in *AddChassisMessage) (*AddChassisReturn, error) { |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 70 | clli := in.GetCLLI() |
donNewtonAlpha | 57aa2ff | 2018-10-01 16:45:32 -0400 | [diff] [blame] | 71 | |
| 72 | xosIP := net.ParseIP(in.GetXOSIP()) |
| 73 | xosPort := int(in.GetXOSPort()) |
| 74 | if xosIP == nil { |
| 75 | errStr := fmt.Sprintf("Invalid IP %s supplied for XOSIP", in.GetXOSIP()) |
| 76 | return nil, errors.New(errStr) |
| 77 | } |
donNewtonAlpha | 57aa2ff | 2018-10-01 16:45:32 -0400 | [diff] [blame] | 78 | xosAddress := net.TCPAddr{IP: xosIP, Port: xosPort} |
donNewtonAlpha | 57aa2ff | 2018-10-01 16:45:32 -0400 | [diff] [blame] | 79 | xosUser := in.GetXOSUser() |
| 80 | xosPassword := in.GetXOSPassword() |
| 81 | if xosUser == "" || xosPassword == "" { |
| 82 | return nil, errors.New("Either XOSUser or XOSPassword supplied were empty") |
| 83 | } |
donNewtonAlpha | 57aa2ff | 2018-10-01 16:45:32 -0400 | [diff] [blame] | 84 | shelf := int(in.GetShelf()) |
| 85 | rack := int(in.GetRack()) |
Don Newton | e973d34 | 2018-10-26 16:44:12 -0400 | [diff] [blame] | 86 | deviceID, err := impl.CreateChassis(clli, xosAddress, xosUser, xosPassword, shelf, rack) |
| 87 | if err != nil { |
| 88 | return nil, err |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 89 | } |
Don Newton | e973d34 | 2018-10-26 16:44:12 -0400 | [diff] [blame] | 90 | return &AddChassisReturn{DeviceID: deviceID}, nil |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /* |
donNewtonAlpha | 57aa2ff | 2018-10-01 16:45:32 -0400 | [diff] [blame] | 94 | ChangeXOSUserPassword - allows update of xos credentials |
| 95 | */ |
| 96 | func (s *Server) ChangeXOSUserPassword(ctx context.Context, in *ChangeXOSUserPasswordMessage) (*ChangeXOSUserPasswordReturn, error) { |
| 97 | clli := in.GetCLLI() |
| 98 | xosUser := in.GetXOSUser() |
| 99 | xosPassword := in.GetXOSPassword() |
| 100 | if xosUser == "" || xosPassword == "" { |
| 101 | return nil, errors.New("Either XOSUser or XOSPassword supplied were empty") |
| 102 | } |
Don Newton | e973d34 | 2018-10-26 16:44:12 -0400 | [diff] [blame] | 103 | success, err := impl.ChangeXOSUserPassword(clli, xosUser, xosPassword) |
| 104 | return &ChangeXOSUserPasswordReturn{Success: success}, err |
donNewtonAlpha | 57aa2ff | 2018-10-01 16:45:32 -0400 | [diff] [blame] | 105 | |
| 106 | } |
| 107 | |
| 108 | /* |
donNewtonAlpha | b9b85eb | 2018-08-14 14:28:22 -0400 | [diff] [blame] | 109 | CreateOLTChassis adds an OLT chassis/line card to the Physical chassis |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 110 | */ |
| 111 | func (s *Server) CreateOLTChassis(ctx context.Context, in *AddOLTChassisMessage) (*AddOLTChassisReturn, error) { |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 112 | clli := in.GetCLLI() |
Don Newton | e973d34 | 2018-10-26 16:44:12 -0400 | [diff] [blame] | 113 | oltType := in.GetType().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() |
| 116 | clli, err := impl.CreateOLTChassis(clli, oltType, address, hostname) |
| 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 | /* |
| 151 | DeleteOnt - deletes a previously provision ont |
| 152 | */ |
| 153 | func (s *Server) DeleteOnt(ctx context.Context, in *DeleteOntMessage) (*DeleteOntReturn, error) { |
donNewtonAlpha | f7cc999 | 2018-08-29 14:23:02 -0400 | [diff] [blame] | 154 | clli := in.GetCLLI() |
Don Newton | e973d34 | 2018-10-26 16:44:12 -0400 | [diff] [blame] | 155 | slotNumber := int(in.GetSlotNumber()) |
| 156 | portNumber := int(in.GetPortNumber()) |
| 157 | ontNumber := int(in.GetOntNumber()) |
| 158 | serialNumber := in.GetSerialNumber() |
| 159 | success, err := impl.DeleteOnt(clli, slotNumber, portNumber, ontNumber, serialNumber) |
| 160 | return &DeleteOntReturn{Success: success}, err |
donNewtonAlpha | f7cc999 | 2018-08-29 14:23:02 -0400 | [diff] [blame] | 161 | } |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 162 | |
donNewtonAlpha | 27ad20c | 2018-10-30 16:18:42 -0400 | [diff] [blame^] | 163 | /* |
| 164 | Reflow - iterates through provisioning to rebuild Seba-Pod |
| 165 | */ |
Don Newton | e973d34 | 2018-10-26 16:44:12 -0400 | [diff] [blame] | 166 | func (s *Server) Reflow(ctx context.Context, in *ReflowMessage) (*ReflowReturn, error) { |
| 167 | success, err := impl.Reflow() |
| 168 | return &ReflowReturn{Success: success}, err |
| 169 | |
| 170 | } |
donNewtonAlpha | 27ad20c | 2018-10-30 16:18:42 -0400 | [diff] [blame^] | 171 | |
| 172 | /* |
| 173 | Output - causes an immediate backup to be created |
| 174 | */ |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 175 | func (s *Server) Output(ctx context.Context, in *OutputMessage) (*OutputReturn, error) { |
Don Newton | e973d34 | 2018-10-26 16:44:12 -0400 | [diff] [blame] | 176 | success, err := impl.DoOutput() |
| 177 | return &OutputReturn{Success: success}, err |
donNewtonAlpha | 57aa2ff | 2018-10-01 16:45:32 -0400 | [diff] [blame] | 178 | |
| 179 | } |
donNewtonAlpha | 27ad20c | 2018-10-30 16:18:42 -0400 | [diff] [blame^] | 180 | |
| 181 | /* |
| 182 | GetFullInventory - gets a full json dump of the currently provisioned equipment |
| 183 | */ |
| 184 | func (s *Server) GetFullInventory(ctx context.Context, in *FullInventoryMessage) (*InventoryReturn, error) { |
| 185 | json := inventory.GatherAllInventory() |
| 186 | return &InventoryReturn{JsonDump: json}, nil |
| 187 | } |
| 188 | |
| 189 | /* |
| 190 | GetInventory - returns a json dump of a particular seba-pod |
| 191 | */ |
| 192 | func (s *Server) GetInventory(ctx context.Context, in *InventoryMessage) (*InventoryReturn, error) { |
| 193 | json := inventory.GatherInventory(in.GetClli()) |
| 194 | return &InventoryReturn{JsonDump: json}, nil |
| 195 | } |