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" |
| 22 | "log" |
| 23 | "net" |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame^] | 24 | "os" |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 25 | "strings" |
| 26 | |
| 27 | "gerrit.opencord.org/abstract-olt/internal/pkg/settings" |
| 28 | "gerrit.opencord.org/abstract-olt/models" |
| 29 | "gerrit.opencord.org/abstract-olt/models/abstract" |
| 30 | "gerrit.opencord.org/abstract-olt/models/physical" |
| 31 | context "golang.org/x/net/context" |
| 32 | ) |
| 33 | |
| 34 | /* |
| 35 | Server instance of the grpc server |
| 36 | */ |
| 37 | type Server struct { |
| 38 | } |
| 39 | |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame^] | 40 | /* |
| 41 | Echo - Tester function which just returns same string sent to it |
| 42 | */ |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 43 | func (s *Server) Echo(ctx context.Context, in *EchoMessage) (*EchoReplyMessage, error) { |
| 44 | ping := in.GetPing() |
| 45 | pong := EchoReplyMessage{Pong: ping} |
| 46 | return &pong, nil |
| 47 | } |
| 48 | |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 49 | /* |
| 50 | CreateChassis - allocates a new Chassis struct and stores it in chassisMap |
| 51 | */ |
| 52 | func (s *Server) CreateChassis(ctx context.Context, in *AddChassisMessage) (*AddChassisReturn, error) { |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame^] | 53 | chassisMap := models.GetChassisMap() |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 54 | clli := in.GetCLLI() |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame^] | 55 | chassisHolder := (*chassisMap)[clli] |
| 56 | if chassisHolder != nil { |
| 57 | return &AddChassisReturn{DeviceID: chassisHolder.AbstractChassis.CLLI}, nil |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 58 | } |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame^] | 59 | |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 60 | abstractChassis := abstract.GenerateChassis(clli, int(in.GetRack()), int(in.GetShelf())) |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame^] | 61 | phyChassis := physical.Chassis{CLLI: clli, VCoreAddress: net.TCPAddr{IP: net.ParseIP(in.GetVCoreIP()), |
| 62 | Port: int(in.GetVCorePort())}, Rack: int(in.GetRack()), Shelf: int(in.GetShelf())} |
| 63 | |
| 64 | chassisHolder = &models.ChassisHolder{AbstractChassis: abstractChassis, PhysicalChassis: phyChassis} |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 65 | if settings.GetDebug() { |
| 66 | output := fmt.Sprintf("%v", abstractChassis) |
| 67 | formatted := strings.Replace(output, "{", "\n{", -1) |
| 68 | log.Printf("new chassis %s\n", formatted) |
| 69 | } |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame^] | 70 | (*chassisMap)[clli] = chassisHolder |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 71 | return &AddChassisReturn{DeviceID: clli}, nil |
| 72 | } |
| 73 | |
| 74 | /* |
donNewtonAlpha | b9b85eb | 2018-08-14 14:28:22 -0400 | [diff] [blame] | 75 | CreateOLTChassis adds an OLT chassis/line card to the Physical chassis |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 76 | */ |
| 77 | func (s *Server) CreateOLTChassis(ctx context.Context, in *AddOLTChassisMessage) (*AddOLTChassisReturn, error) { |
| 78 | fmt.Printf(" CreateOLTChassis %v \n", *in) |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame^] | 79 | chassisMap := models.GetChassisMap() |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 80 | clli := in.GetCLLI() |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame^] | 81 | chassisHolder := (*chassisMap)[clli] |
| 82 | if chassisHolder == nil { |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 83 | errString := fmt.Sprintf("There is no chassis with CLLI of %s", clli) |
| 84 | return &AddOLTChassisReturn{DeviceID: "", ChassisDeviceID: ""}, errors.New(errString) |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 85 | } |
| 86 | oltType := in.GetType() |
| 87 | address := net.TCPAddr{IP: net.ParseIP(in.GetSlotIP()), Port: int(in.GetSlotPort())} |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame^] | 88 | physicalChassis := &chassisHolder.PhysicalChassis |
| 89 | sOlt := physical.SimpleOLT{CLLI: clli, Hostname: in.GetHostname(), Address: address, Parent: physicalChassis} |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 90 | switch oltType { |
| 91 | case AddOLTChassisMessage_edgecore: |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame^] | 92 | sOlt.CreateEdgecore() |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 93 | case AddOLTChassisMessage_adtran: |
| 94 | case AddOLTChassisMessage_tibit: |
| 95 | } |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame^] | 96 | physicalChassis.AddOLTChassis(sOlt) |
| 97 | ports := sOlt.GetPorts() |
| 98 | for i := 0; i < len(ports); i++ { |
| 99 | absPort, _ := chassisHolder.AbstractChassis.NextPort() |
| 100 | |
| 101 | absPort.PhysPort = &ports[i] |
| 102 | //AssignTraits(&ports[i], absPort) |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 103 | } |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 104 | return &AddOLTChassisReturn{DeviceID: in.GetHostname(), ChassisDeviceID: clli}, nil |
| 105 | |
| 106 | } |
| 107 | |
| 108 | /* |
donNewtonAlpha | 5234b13 | 2018-08-16 14:12:28 -0400 | [diff] [blame] | 109 | ProvisionOnt provisions an ONT on a specific Chassis/LineCard/Port |
| 110 | */ |
| 111 | func (s *Server) ProvisionOnt(ctx context.Context, in *AddOntMessage) (*AddOntReturn, error) { |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame^] | 112 | chassisMap := models.GetChassisMap() |
donNewtonAlpha | 5234b13 | 2018-08-16 14:12:28 -0400 | [diff] [blame] | 113 | clli := in.GetCLLI() |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame^] | 114 | chassisHolder := (*chassisMap)[clli] |
| 115 | if chassisHolder == nil { |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 116 | errString := fmt.Sprintf("There is no chassis with CLLI of %s", clli) |
| 117 | return &AddOntReturn{Success: false}, errors.New(errString) |
| 118 | } |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame^] | 119 | 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] | 120 | |
| 121 | if err != nil { |
| 122 | return nil, err |
| 123 | } |
| 124 | return &AddOntReturn{Success: true}, nil |
| 125 | } |
donNewtonAlpha | f7cc999 | 2018-08-29 14:23:02 -0400 | [diff] [blame] | 126 | |
| 127 | /* |
| 128 | DeleteOnt - deletes a previously provision ont |
| 129 | */ |
| 130 | func (s *Server) DeleteOnt(ctx context.Context, in *DeleteOntMessage) (*DeleteOntReturn, error) { |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame^] | 131 | chassisMap := models.GetChassisMap() |
donNewtonAlpha | f7cc999 | 2018-08-29 14:23:02 -0400 | [diff] [blame] | 132 | clli := in.GetCLLI() |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame^] | 133 | chassisHolder := (*chassisMap)[clli] |
| 134 | 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] | 135 | if err != nil { |
| 136 | return nil, err |
| 137 | } |
| 138 | return &DeleteOntReturn{Success: true}, nil |
| 139 | } |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame^] | 140 | |
| 141 | func (s *Server) Output(ctx context.Context, in *OutputMessage) (*OutputReturn, error) { |
| 142 | chassisMap := models.GetChassisMap() |
| 143 | for clli, chassisHolder := range *chassisMap { |
| 144 | |
| 145 | json, _ := (chassisHolder).Serialize() |
| 146 | backupFile := fmt.Sprintf("backup/%s", clli) |
| 147 | f, _ := os.Create(backupFile) |
| 148 | defer f.Close() |
| 149 | _, _ = f.WriteString(string(json)) |
| 150 | f.Sync() |
| 151 | } |
| 152 | return &OutputReturn{Success: true}, nil |
| 153 | |
| 154 | } |