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