blob: ce5c7a15078d1e5e854d08c65559a8dd46defd7e [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) {
donNewtonAlphab8f30752018-10-04 11:57:41 -040046 fmt.Println("HELLO WTF")
donNewtonAlphab3279ea2018-09-18 15:55:32 -040047 ping := in.GetPing()
48 pong := EchoReplyMessage{Pong: ping}
49 return &pong, nil
50}
51
Author Namea594e632018-08-10 11:33:58 -040052/*
53CreateChassis - allocates a new Chassis struct and stores it in chassisMap
54*/
55func (s *Server) CreateChassis(ctx context.Context, in *AddChassisMessage) (*AddChassisReturn, error) {
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040056 chassisMap := models.GetChassisMap()
Author Namea594e632018-08-10 11:33:58 -040057 clli := in.GetCLLI()
donNewtonAlpha57aa2ff2018-10-01 16:45:32 -040058
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
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040080 chassisHolder := (*chassisMap)[clli]
81 if chassisHolder != nil {
82 return &AddChassisReturn{DeviceID: chassisHolder.AbstractChassis.CLLI}, nil
Author Namea594e632018-08-10 11:33:58 -040083 }
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040084
donNewtonAlpha1d2d6812018-09-14 16:00:02 -040085 abstractChassis := abstract.GenerateChassis(clli, int(in.GetRack()), int(in.GetShelf()))
donNewtonAlphab8f30752018-10-04 11:57:41 -040086 phyChassis := physical.Chassis{CLLI: clli, XOSUser: xosUser, XOSPassword: xosPassword, XOSAddress: xosAddress, Rack: rack, Shelf: shelf}
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040087
88 chassisHolder = &models.ChassisHolder{AbstractChassis: abstractChassis, PhysicalChassis: phyChassis}
Author Namea594e632018-08-10 11:33:58 -040089 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 }
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040094 (*chassisMap)[clli] = chassisHolder
Author Namea594e632018-08-10 11:33:58 -040095 return &AddChassisReturn{DeviceID: clli}, nil
96}
97
98/*
donNewtonAlpha57aa2ff2018-10-01 16:45:32 -040099ChangeXOSUserPassword - allows update of xos credentials
100*/
101func (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/*
donNewtonAlphab9b85eb2018-08-14 14:28:22 -0400128CreateOLTChassis adds an OLT chassis/line card to the Physical chassis
Author Namea594e632018-08-10 11:33:58 -0400129*/
130func (s *Server) CreateOLTChassis(ctx context.Context, in *AddOLTChassisMessage) (*AddOLTChassisReturn, error) {
131 fmt.Printf(" CreateOLTChassis %v \n", *in)
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400132 chassisMap := models.GetChassisMap()
Author Namea594e632018-08-10 11:33:58 -0400133 clli := in.GetCLLI()
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400134 chassisHolder := (*chassisMap)[clli]
135 if chassisHolder == nil {
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400136 errString := fmt.Sprintf("There is no chassis with CLLI of %s", clli)
137 return &AddOLTChassisReturn{DeviceID: "", ChassisDeviceID: ""}, errors.New(errString)
Author Namea594e632018-08-10 11:33:58 -0400138 }
139 oltType := in.GetType()
140 address := net.TCPAddr{IP: net.ParseIP(in.GetSlotIP()), Port: int(in.GetSlotPort())}
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400141 physicalChassis := &chassisHolder.PhysicalChassis
142 sOlt := physical.SimpleOLT{CLLI: clli, Hostname: in.GetHostname(), Address: address, Parent: physicalChassis}
Author Namea594e632018-08-10 11:33:58 -0400143 switch oltType {
144 case AddOLTChassisMessage_edgecore:
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400145 sOlt.CreateEdgecore()
Author Namea594e632018-08-10 11:33:58 -0400146 case AddOLTChassisMessage_adtran:
147 case AddOLTChassisMessage_tibit:
148 }
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400149 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 Namea594e632018-08-10 11:33:58 -0400156 }
Author Namea594e632018-08-10 11:33:58 -0400157 return &AddOLTChassisReturn{DeviceID: in.GetHostname(), ChassisDeviceID: clli}, nil
158
159}
160
161/*
donNewtonAlpha5234b132018-08-16 14:12:28 -0400162ProvisionOnt provisions an ONT on a specific Chassis/LineCard/Port
163*/
164func (s *Server) ProvisionOnt(ctx context.Context, in *AddOntMessage) (*AddOntReturn, error) {
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400165 chassisMap := models.GetChassisMap()
donNewtonAlpha5234b132018-08-16 14:12:28 -0400166 clli := in.GetCLLI()
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400167 chassisHolder := (*chassisMap)[clli]
168 if chassisHolder == nil {
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400169 errString := fmt.Sprintf("There is no chassis with CLLI of %s", clli)
170 return &AddOntReturn{Success: false}, errors.New(errString)
171 }
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400172 err := chassisHolder.AbstractChassis.ActivateONT(int(in.GetSlotNumber()), int(in.GetPortNumber()), int(in.GetOntNumber()), in.GetSerialNumber())
donNewtonAlpha5234b132018-08-16 14:12:28 -0400173
174 if err != nil {
175 return nil, err
176 }
177 return &AddOntReturn{Success: true}, nil
178}
donNewtonAlphaf7cc9992018-08-29 14:23:02 -0400179
180/*
181DeleteOnt - deletes a previously provision ont
182*/
183func (s *Server) DeleteOnt(ctx context.Context, in *DeleteOntMessage) (*DeleteOntReturn, error) {
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400184 chassisMap := models.GetChassisMap()
donNewtonAlphaf7cc9992018-08-29 14:23:02 -0400185 clli := in.GetCLLI()
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400186 chassisHolder := (*chassisMap)[clli]
187 err := chassisHolder.AbstractChassis.DeleteONT(int(in.GetSlotNumber()), int(in.GetPortNumber()), int(in.GetOntNumber()), in.GetSerialNumber())
donNewtonAlphaf7cc9992018-08-29 14:23:02 -0400188 if err != nil {
189 return nil, err
190 }
191 return &DeleteOntReturn{Success: true}, nil
192}
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400193
194func (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}
donNewtonAlpha57aa2ff2018-10-01 16:45:32 -0400208func testLogin(xosUser string, xosPassword string, xosIP net.IP, xosPort int) bool {
donNewtonAlphab1466392018-10-02 10:42:05 -0400209 if settings.GetDummy() {
210 return true
211 }
donNewtonAlpha57aa2ff2018-10-01 16:45:32 -0400212 var dummyYaml = `
213tosca_definitions_version: tosca_simple_yaml_1_0
214imports:
215 - custom_types/site.yaml
216description: anything
217topology_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}