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" |
donNewtonAlpha | 57aa2ff | 2018-10-01 16:45:32 -0400 | [diff] [blame^] | 22 | "io/ioutil" |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 23 | "log" |
| 24 | "net" |
donNewtonAlpha | 57aa2ff | 2018-10-01 16:45:32 -0400 | [diff] [blame^] | 25 | "net/http" |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 26 | "os" |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 27 | "strings" |
| 28 | |
| 29 | "gerrit.opencord.org/abstract-olt/internal/pkg/settings" |
| 30 | "gerrit.opencord.org/abstract-olt/models" |
| 31 | "gerrit.opencord.org/abstract-olt/models/abstract" |
| 32 | "gerrit.opencord.org/abstract-olt/models/physical" |
| 33 | context "golang.org/x/net/context" |
| 34 | ) |
| 35 | |
| 36 | /* |
| 37 | Server instance of the grpc server |
| 38 | */ |
| 39 | type Server struct { |
| 40 | } |
| 41 | |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 42 | /* |
| 43 | Echo - Tester function which just returns same string sent to it |
| 44 | */ |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 45 | func (s *Server) Echo(ctx context.Context, in *EchoMessage) (*EchoReplyMessage, error) { |
| 46 | ping := in.GetPing() |
| 47 | pong := EchoReplyMessage{Pong: ping} |
| 48 | return &pong, nil |
| 49 | } |
| 50 | |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 51 | /* |
| 52 | CreateChassis - allocates a new Chassis struct and stores it in chassisMap |
| 53 | */ |
| 54 | func (s *Server) CreateChassis(ctx context.Context, in *AddChassisMessage) (*AddChassisReturn, error) { |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 55 | chassisMap := models.GetChassisMap() |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 56 | clli := in.GetCLLI() |
donNewtonAlpha | 57aa2ff | 2018-10-01 16:45:32 -0400 | [diff] [blame^] | 57 | |
| 58 | xosIP := net.ParseIP(in.GetXOSIP()) |
| 59 | xosPort := int(in.GetXOSPort()) |
| 60 | if xosIP == nil { |
| 61 | errStr := fmt.Sprintf("Invalid IP %s supplied for XOSIP", in.GetXOSIP()) |
| 62 | return nil, errors.New(errStr) |
| 63 | } |
| 64 | |
| 65 | xosAddress := net.TCPAddr{IP: xosIP, Port: xosPort} |
| 66 | |
| 67 | xosUser := in.GetXOSUser() |
| 68 | xosPassword := in.GetXOSPassword() |
| 69 | if xosUser == "" || xosPassword == "" { |
| 70 | return nil, errors.New("Either XOSUser or XOSPassword supplied were empty") |
| 71 | } |
| 72 | loginWorked := testLogin(xosUser, xosPassword, xosIP, xosPort) |
| 73 | if !loginWorked { |
| 74 | return nil, errors.New("Unable to validate login not creating Abstract Chassis") |
| 75 | } |
| 76 | shelf := int(in.GetShelf()) |
| 77 | rack := int(in.GetRack()) |
| 78 | |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 79 | chassisHolder := (*chassisMap)[clli] |
| 80 | if chassisHolder != nil { |
| 81 | return &AddChassisReturn{DeviceID: chassisHolder.AbstractChassis.CLLI}, nil |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 82 | } |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 83 | |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 84 | abstractChassis := abstract.GenerateChassis(clli, int(in.GetRack()), int(in.GetShelf())) |
donNewtonAlpha | 57aa2ff | 2018-10-01 16:45:32 -0400 | [diff] [blame^] | 85 | phyChassis := physical.Chassis{CLLI: clli, XOSAddress: xosAddress, Rack: rack, Shelf: shelf} |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 86 | |
| 87 | chassisHolder = &models.ChassisHolder{AbstractChassis: abstractChassis, PhysicalChassis: phyChassis} |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 88 | if settings.GetDebug() { |
| 89 | output := fmt.Sprintf("%v", abstractChassis) |
| 90 | formatted := strings.Replace(output, "{", "\n{", -1) |
| 91 | log.Printf("new chassis %s\n", formatted) |
| 92 | } |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 93 | (*chassisMap)[clli] = chassisHolder |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 94 | return &AddChassisReturn{DeviceID: clli}, nil |
| 95 | } |
| 96 | |
| 97 | /* |
donNewtonAlpha | 57aa2ff | 2018-10-01 16:45:32 -0400 | [diff] [blame^] | 98 | ChangeXOSUserPassword - allows update of xos credentials |
| 99 | */ |
| 100 | func (s *Server) ChangeXOSUserPassword(ctx context.Context, in *ChangeXOSUserPasswordMessage) (*ChangeXOSUserPasswordReturn, error) { |
| 101 | clli := in.GetCLLI() |
| 102 | xosUser := in.GetXOSUser() |
| 103 | xosPassword := in.GetXOSPassword() |
| 104 | if xosUser == "" || xosPassword == "" { |
| 105 | return nil, errors.New("Either XOSUser or XOSPassword supplied were empty") |
| 106 | } |
| 107 | chassisMap := models.GetChassisMap() |
| 108 | chassisHolder := (*chassisMap)[clli] |
| 109 | if chassisHolder == nil { |
| 110 | errString := fmt.Sprintf("There is no chassis with CLLI of %s", clli) |
| 111 | return nil, errors.New(errString) |
| 112 | } |
| 113 | xosIP := chassisHolder.PhysicalChassis.XOSAddress.IP |
| 114 | xosPort := chassisHolder.PhysicalChassis.XOSAddress.Port |
| 115 | loginWorked := testLogin(xosUser, xosPassword, xosIP, xosPort) |
| 116 | if !loginWorked { |
| 117 | return nil, errors.New("Unable to validate login when changing password") |
| 118 | } |
| 119 | |
| 120 | chassisHolder.PhysicalChassis.XOSUser = xosUser |
| 121 | chassisHolder.PhysicalChassis.XOSPassword = xosPassword |
| 122 | return &ChangeXOSUserPasswordReturn{Success: true}, nil |
| 123 | |
| 124 | } |
| 125 | |
| 126 | /* |
donNewtonAlpha | b9b85eb | 2018-08-14 14:28:22 -0400 | [diff] [blame] | 127 | CreateOLTChassis adds an OLT chassis/line card to the Physical chassis |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 128 | */ |
| 129 | func (s *Server) CreateOLTChassis(ctx context.Context, in *AddOLTChassisMessage) (*AddOLTChassisReturn, error) { |
| 130 | fmt.Printf(" CreateOLTChassis %v \n", *in) |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 131 | chassisMap := models.GetChassisMap() |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 132 | clli := in.GetCLLI() |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 133 | chassisHolder := (*chassisMap)[clli] |
| 134 | if chassisHolder == nil { |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 135 | errString := fmt.Sprintf("There is no chassis with CLLI of %s", clli) |
| 136 | return &AddOLTChassisReturn{DeviceID: "", ChassisDeviceID: ""}, errors.New(errString) |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 137 | } |
| 138 | oltType := in.GetType() |
| 139 | address := net.TCPAddr{IP: net.ParseIP(in.GetSlotIP()), Port: int(in.GetSlotPort())} |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 140 | physicalChassis := &chassisHolder.PhysicalChassis |
| 141 | sOlt := physical.SimpleOLT{CLLI: clli, Hostname: in.GetHostname(), Address: address, Parent: physicalChassis} |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 142 | switch oltType { |
| 143 | case AddOLTChassisMessage_edgecore: |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 144 | sOlt.CreateEdgecore() |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 145 | case AddOLTChassisMessage_adtran: |
| 146 | case AddOLTChassisMessage_tibit: |
| 147 | } |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 148 | physicalChassis.AddOLTChassis(sOlt) |
| 149 | ports := sOlt.GetPorts() |
| 150 | for i := 0; i < len(ports); i++ { |
| 151 | absPort, _ := chassisHolder.AbstractChassis.NextPort() |
| 152 | |
| 153 | absPort.PhysPort = &ports[i] |
| 154 | //AssignTraits(&ports[i], absPort) |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 155 | } |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 156 | return &AddOLTChassisReturn{DeviceID: in.GetHostname(), ChassisDeviceID: clli}, nil |
| 157 | |
| 158 | } |
| 159 | |
| 160 | /* |
donNewtonAlpha | 5234b13 | 2018-08-16 14:12:28 -0400 | [diff] [blame] | 161 | ProvisionOnt provisions an ONT on a specific Chassis/LineCard/Port |
| 162 | */ |
| 163 | func (s *Server) ProvisionOnt(ctx context.Context, in *AddOntMessage) (*AddOntReturn, error) { |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 164 | chassisMap := models.GetChassisMap() |
donNewtonAlpha | 5234b13 | 2018-08-16 14:12:28 -0400 | [diff] [blame] | 165 | clli := in.GetCLLI() |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 166 | chassisHolder := (*chassisMap)[clli] |
| 167 | if chassisHolder == nil { |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 168 | errString := fmt.Sprintf("There is no chassis with CLLI of %s", clli) |
| 169 | return &AddOntReturn{Success: false}, errors.New(errString) |
| 170 | } |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 171 | err := chassisHolder.AbstractChassis.ActivateONT(int(in.GetSlotNumber()), int(in.GetPortNumber()), int(in.GetOntNumber()), in.GetSerialNumber()) |
donNewtonAlpha | 5234b13 | 2018-08-16 14:12:28 -0400 | [diff] [blame] | 172 | |
| 173 | if err != nil { |
| 174 | return nil, err |
| 175 | } |
| 176 | return &AddOntReturn{Success: true}, nil |
| 177 | } |
donNewtonAlpha | f7cc999 | 2018-08-29 14:23:02 -0400 | [diff] [blame] | 178 | |
| 179 | /* |
| 180 | DeleteOnt - deletes a previously provision ont |
| 181 | */ |
| 182 | func (s *Server) DeleteOnt(ctx context.Context, in *DeleteOntMessage) (*DeleteOntReturn, error) { |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 183 | chassisMap := models.GetChassisMap() |
donNewtonAlpha | f7cc999 | 2018-08-29 14:23:02 -0400 | [diff] [blame] | 184 | clli := in.GetCLLI() |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 185 | chassisHolder := (*chassisMap)[clli] |
| 186 | err := chassisHolder.AbstractChassis.DeleteONT(int(in.GetSlotNumber()), int(in.GetPortNumber()), int(in.GetOntNumber()), in.GetSerialNumber()) |
donNewtonAlpha | f7cc999 | 2018-08-29 14:23:02 -0400 | [diff] [blame] | 187 | if err != nil { |
| 188 | return nil, err |
| 189 | } |
| 190 | return &DeleteOntReturn{Success: true}, nil |
| 191 | } |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 192 | |
| 193 | func (s *Server) Output(ctx context.Context, in *OutputMessage) (*OutputReturn, error) { |
| 194 | chassisMap := models.GetChassisMap() |
| 195 | for clli, chassisHolder := range *chassisMap { |
| 196 | |
| 197 | json, _ := (chassisHolder).Serialize() |
| 198 | backupFile := fmt.Sprintf("backup/%s", clli) |
| 199 | f, _ := os.Create(backupFile) |
| 200 | defer f.Close() |
| 201 | _, _ = f.WriteString(string(json)) |
| 202 | f.Sync() |
| 203 | } |
| 204 | return &OutputReturn{Success: true}, nil |
| 205 | |
| 206 | } |
donNewtonAlpha | 57aa2ff | 2018-10-01 16:45:32 -0400 | [diff] [blame^] | 207 | func testLogin(xosUser string, xosPassword string, xosIP net.IP, xosPort int) bool { |
| 208 | var dummyYaml = ` |
| 209 | tosca_definitions_version: tosca_simple_yaml_1_0 |
| 210 | imports: |
| 211 | - custom_types/site.yaml |
| 212 | description: anything |
| 213 | topology_template: |
| 214 | node_templates: |
| 215 | mysite: |
| 216 | type: tosca.nodes.Site |
| 217 | properties: |
| 218 | must-exist: true |
| 219 | name: mysite |
| 220 | ` |
| 221 | client := &http.Client{} |
| 222 | requestList := fmt.Sprintf("http://%s:%d/run", xosIP, xosPort) |
| 223 | req, err := http.NewRequest("POST", requestList, strings.NewReader(dummyYaml)) |
| 224 | req.Header.Add("xos-username", xosUser) |
| 225 | req.Header.Add("xos-password", xosPassword) |
| 226 | resp, err := client.Do(req) |
| 227 | log.Printf("testLogin resp:%v", resp) |
| 228 | if err != nil { |
| 229 | log.Printf("Unable to validate XOS Login Information %v", err) |
| 230 | return false |
| 231 | } |
| 232 | defer resp.Body.Close() |
| 233 | |
| 234 | if resp.StatusCode == http.StatusOK { |
| 235 | bodyBytes, _ := ioutil.ReadAll(resp.Body) |
| 236 | bodyString := string(bodyBytes) |
| 237 | fmt.Println(bodyString) |
| 238 | return true |
| 239 | } |
| 240 | return false |
| 241 | |
| 242 | } |