blob: f6d4c5b9cfcaba5f9c7279c0230c21a5ea3bec21 [file] [log] [blame]
Author Namea594e632018-08-10 11:33:58 -04001/*
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
17package api
18
19import (
donNewtonAlpha1d2d6812018-09-14 16:00:02 -040020 "errors"
Author Namea594e632018-08-10 11:33:58 -040021 "fmt"
donNewtonAlpha57aa2ff2018-10-01 16:45:32 -040022 "io/ioutil"
Author Namea594e632018-08-10 11:33:58 -040023 "log"
24 "net"
donNewtonAlpha57aa2ff2018-10-01 16:45:32 -040025 "net/http"
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040026 "os"
Author Namea594e632018-08-10 11:33:58 -040027 "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/*
37Server instance of the grpc server
38*/
39type Server struct {
40}
41
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040042/*
43Echo - Tester function which just returns same string sent to it
44*/
donNewtonAlphab3279ea2018-09-18 15:55:32 -040045func (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 Namea594e632018-08-10 11:33:58 -040051/*
52CreateChassis - allocates a new Chassis struct and stores it in chassisMap
53*/
54func (s *Server) CreateChassis(ctx context.Context, in *AddChassisMessage) (*AddChassisReturn, error) {
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040055 chassisMap := models.GetChassisMap()
Author Namea594e632018-08-10 11:33:58 -040056 clli := in.GetCLLI()
donNewtonAlpha57aa2ff2018-10-01 16:45:32 -040057
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
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040079 chassisHolder := (*chassisMap)[clli]
80 if chassisHolder != nil {
81 return &AddChassisReturn{DeviceID: chassisHolder.AbstractChassis.CLLI}, nil
Author Namea594e632018-08-10 11:33:58 -040082 }
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040083
donNewtonAlpha1d2d6812018-09-14 16:00:02 -040084 abstractChassis := abstract.GenerateChassis(clli, int(in.GetRack()), int(in.GetShelf()))
donNewtonAlpha57aa2ff2018-10-01 16:45:32 -040085 phyChassis := physical.Chassis{CLLI: clli, XOSAddress: xosAddress, Rack: rack, Shelf: shelf}
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040086
87 chassisHolder = &models.ChassisHolder{AbstractChassis: abstractChassis, PhysicalChassis: phyChassis}
Author Namea594e632018-08-10 11:33:58 -040088 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 }
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040093 (*chassisMap)[clli] = chassisHolder
Author Namea594e632018-08-10 11:33:58 -040094 return &AddChassisReturn{DeviceID: clli}, nil
95}
96
97/*
donNewtonAlpha57aa2ff2018-10-01 16:45:32 -040098ChangeXOSUserPassword - allows update of xos credentials
99*/
100func (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/*
donNewtonAlphab9b85eb2018-08-14 14:28:22 -0400127CreateOLTChassis adds an OLT chassis/line card to the Physical chassis
Author Namea594e632018-08-10 11:33:58 -0400128*/
129func (s *Server) CreateOLTChassis(ctx context.Context, in *AddOLTChassisMessage) (*AddOLTChassisReturn, error) {
130 fmt.Printf(" CreateOLTChassis %v \n", *in)
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400131 chassisMap := models.GetChassisMap()
Author Namea594e632018-08-10 11:33:58 -0400132 clli := in.GetCLLI()
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400133 chassisHolder := (*chassisMap)[clli]
134 if chassisHolder == nil {
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400135 errString := fmt.Sprintf("There is no chassis with CLLI of %s", clli)
136 return &AddOLTChassisReturn{DeviceID: "", ChassisDeviceID: ""}, errors.New(errString)
Author Namea594e632018-08-10 11:33:58 -0400137 }
138 oltType := in.GetType()
139 address := net.TCPAddr{IP: net.ParseIP(in.GetSlotIP()), Port: int(in.GetSlotPort())}
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400140 physicalChassis := &chassisHolder.PhysicalChassis
141 sOlt := physical.SimpleOLT{CLLI: clli, Hostname: in.GetHostname(), Address: address, Parent: physicalChassis}
Author Namea594e632018-08-10 11:33:58 -0400142 switch oltType {
143 case AddOLTChassisMessage_edgecore:
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400144 sOlt.CreateEdgecore()
Author Namea594e632018-08-10 11:33:58 -0400145 case AddOLTChassisMessage_adtran:
146 case AddOLTChassisMessage_tibit:
147 }
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400148 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 Namea594e632018-08-10 11:33:58 -0400155 }
Author Namea594e632018-08-10 11:33:58 -0400156 return &AddOLTChassisReturn{DeviceID: in.GetHostname(), ChassisDeviceID: clli}, nil
157
158}
159
160/*
donNewtonAlpha5234b132018-08-16 14:12:28 -0400161ProvisionOnt provisions an ONT on a specific Chassis/LineCard/Port
162*/
163func (s *Server) ProvisionOnt(ctx context.Context, in *AddOntMessage) (*AddOntReturn, error) {
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400164 chassisMap := models.GetChassisMap()
donNewtonAlpha5234b132018-08-16 14:12:28 -0400165 clli := in.GetCLLI()
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400166 chassisHolder := (*chassisMap)[clli]
167 if chassisHolder == nil {
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400168 errString := fmt.Sprintf("There is no chassis with CLLI of %s", clli)
169 return &AddOntReturn{Success: false}, errors.New(errString)
170 }
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400171 err := chassisHolder.AbstractChassis.ActivateONT(int(in.GetSlotNumber()), int(in.GetPortNumber()), int(in.GetOntNumber()), in.GetSerialNumber())
donNewtonAlpha5234b132018-08-16 14:12:28 -0400172
173 if err != nil {
174 return nil, err
175 }
176 return &AddOntReturn{Success: true}, nil
177}
donNewtonAlphaf7cc9992018-08-29 14:23:02 -0400178
179/*
180DeleteOnt - deletes a previously provision ont
181*/
182func (s *Server) DeleteOnt(ctx context.Context, in *DeleteOntMessage) (*DeleteOntReturn, error) {
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400183 chassisMap := models.GetChassisMap()
donNewtonAlphaf7cc9992018-08-29 14:23:02 -0400184 clli := in.GetCLLI()
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400185 chassisHolder := (*chassisMap)[clli]
186 err := chassisHolder.AbstractChassis.DeleteONT(int(in.GetSlotNumber()), int(in.GetPortNumber()), int(in.GetOntNumber()), in.GetSerialNumber())
donNewtonAlphaf7cc9992018-08-29 14:23:02 -0400187 if err != nil {
188 return nil, err
189 }
190 return &DeleteOntReturn{Success: true}, nil
191}
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400192
193func (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}
donNewtonAlpha57aa2ff2018-10-01 16:45:32 -0400207func testLogin(xosUser string, xosPassword string, xosIP net.IP, xosPort int) bool {
208 var dummyYaml = `
209tosca_definitions_version: tosca_simple_yaml_1_0
210imports:
211 - custom_types/site.yaml
212description: anything
213topology_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}