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