blob: 77a936749ee792c792688ccf4551b62574dea444 [file] [log] [blame]
Don Newtone973d342018-10-26 16:44:12 -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*/
16package impl
17
18import (
19 "errors"
20 "fmt"
21 "io/ioutil"
22 "log"
23 "net"
24 "net/http"
25 "strings"
26
27 "gerrit.opencord.org/abstract-olt/internal/pkg/settings"
28 "gerrit.opencord.org/abstract-olt/models"
29)
30
31/*
32ChangeXOSUserPassword - allows update of xos credentials
33*/
34func ChangeXOSUserPassword(clli string, xosUser string, xosPassword string) (bool, error) {
35 myChan := getSyncChannel()
36 <-myChan
37 defer done(myChan, true)
38 chassisMap := models.GetChassisMap()
39 chassisHolder := (*chassisMap)[clli]
40 if chassisHolder == nil {
41 errString := fmt.Sprintf("There is no chassis with CLLI of %s", clli)
42 return false, errors.New(errString)
43 }
44 xosIP := chassisHolder.PhysicalChassis.XOSAddress.IP
45 xosPort := chassisHolder.PhysicalChassis.XOSAddress.Port
46 loginWorked := testLogin(xosUser, xosPassword, xosIP, xosPort)
47 if !loginWorked {
48 return false, errors.New("Unable to validate login when changing password")
49 }
50
51 chassisHolder.PhysicalChassis.XOSUser = xosUser
52 chassisHolder.PhysicalChassis.XOSPassword = xosPassword
53 isDirty = true
54 return true, nil
55
56}
57
58func testLogin(xosUser string, xosPassword string, xosIP net.IP, xosPort int) bool {
59 if settings.GetDummy() {
60 return true
61 }
62 var dummyYaml = `
63tosca_definitions_version: tosca_simple_yaml_1_0
64imports:
65 - custom_types/site.yaml
66description: anything
67topology_template:
68 node_templates:
69 mysite:
70 type: tosca.nodes.Site
71 properties:
72 must-exist: true
73 name: mysite
74`
75 client := &http.Client{}
76 requestList := fmt.Sprintf("http://%s:%d/run", xosIP, xosPort)
77 req, err := http.NewRequest("POST", requestList, strings.NewReader(dummyYaml))
78 req.Header.Add("xos-username", xosUser)
79 req.Header.Add("xos-password", xosPassword)
80 resp, err := client.Do(req)
81 log.Printf("testLogin resp:%v", resp)
82 if err != nil {
83 log.Printf("Unable to validate XOS Login Information %v", err)
84 return false
85 }
86 defer resp.Body.Close()
87
88 if resp.StatusCode == http.StatusOK {
89 bodyBytes, _ := ioutil.ReadAll(resp.Body)
90 bodyString := string(bodyBytes)
91 fmt.Println(bodyString)
92 return true
93 }
94 return false
95
96}