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 | 5234b13 | 2018-08-16 14:12:28 -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 | |
| 39 | /* |
| 40 | CreateChassis - allocates a new Chassis struct and stores it in chassisMap |
| 41 | */ |
| 42 | func (s *Server) CreateChassis(ctx context.Context, in *AddChassisMessage) (*AddChassisReturn, error) { |
| 43 | phyChassisMap := models.GetPhyChassisMap() |
| 44 | absChassisMap := models.GetAbstractChassisMap() |
| 45 | clli := in.GetCLLI() |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 46 | chassis := (*phyChassisMap)[clli] |
| 47 | if chassis != nil { |
| 48 | return &AddChassisReturn{DeviceID: chassis.CLLI}, nil |
| 49 | } |
| 50 | abstractChassis := abstract.GenerateChassis(clli) |
donNewtonAlpha | 5234b13 | 2018-08-16 14:12:28 -0400 | [diff] [blame^] | 51 | phyChassis := &physical.Chassis{CLLI: clli, VCoreAddress: net.TCPAddr{IP: net.ParseIP(in.GetVCoreIP()), Port: int(in.GetVCorePort())}} |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 52 | if settings.GetDebug() { |
| 53 | output := fmt.Sprintf("%v", abstractChassis) |
| 54 | formatted := strings.Replace(output, "{", "\n{", -1) |
| 55 | log.Printf("new chassis %s\n", formatted) |
| 56 | } |
donNewtonAlpha | b9b85eb | 2018-08-14 14:28:22 -0400 | [diff] [blame] | 57 | (*phyChassisMap)[clli] = phyChassis |
| 58 | fmt.Printf("phy %v, abs %v\n", phyChassisMap, absChassisMap) |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 59 | (*absChassisMap)[clli] = abstractChassis |
| 60 | return &AddChassisReturn{DeviceID: clli}, nil |
| 61 | } |
| 62 | |
| 63 | /* |
donNewtonAlpha | b9b85eb | 2018-08-14 14:28:22 -0400 | [diff] [blame] | 64 | CreateOLTChassis adds an OLT chassis/line card to the Physical chassis |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 65 | */ |
| 66 | func (s *Server) CreateOLTChassis(ctx context.Context, in *AddOLTChassisMessage) (*AddOLTChassisReturn, error) { |
| 67 | fmt.Printf(" CreateOLTChassis %v \n", *in) |
| 68 | phyChassisMap := models.GetPhyChassisMap() |
| 69 | clli := in.GetCLLI() |
| 70 | chassis := (*phyChassisMap)[clli] |
| 71 | if chassis == nil { |
| 72 | } |
| 73 | oltType := in.GetType() |
| 74 | address := net.TCPAddr{IP: net.ParseIP(in.GetSlotIP()), Port: int(in.GetSlotPort())} |
donNewtonAlpha | 5234b13 | 2018-08-16 14:12:28 -0400 | [diff] [blame^] | 75 | sOlt := physical.SimpleOLT{CLLI: clli, Hostname: in.GetHostname(), Address: address, Parent: chassis} |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 76 | |
| 77 | var olt physical.OLT |
| 78 | switch oltType { |
| 79 | case AddOLTChassisMessage_edgecore: |
| 80 | olt = physical.CreateEdgecore(&sOlt) |
| 81 | case AddOLTChassisMessage_adtran: |
| 82 | case AddOLTChassisMessage_tibit: |
| 83 | } |
| 84 | |
| 85 | err := AddCard(chassis, olt) |
| 86 | if err != nil { |
| 87 | //TODO do something |
| 88 | } |
| 89 | |
| 90 | return &AddOLTChassisReturn{DeviceID: in.GetHostname(), ChassisDeviceID: clli}, nil |
| 91 | |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | AddCard Adds an OLT card to an existing physical chassis, allocating ports |
| 96 | in the physical card to those in the abstract model |
| 97 | */ |
| 98 | func AddCard(physChassis *physical.Chassis, olt physical.OLT) error { |
donNewtonAlpha | 5234b13 | 2018-08-16 14:12:28 -0400 | [diff] [blame^] | 99 | physChassis.AddOLTChassis(olt) |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 100 | |
| 101 | ports := olt.GetPorts() |
| 102 | absChassis := (*models.GetAbstractChassisMap())[physChassis.CLLI] |
| 103 | |
| 104 | for i := 0; i < len(ports); i++ { |
| 105 | absPort, _ := absChassis.NextPort() |
| 106 | absPort.PhysPort = &ports[i] |
| 107 | //AssignTraits(&ports[i], absPort) |
| 108 | } |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 109 | //should probably worry about error at some point |
| 110 | return nil |
| 111 | } |
donNewtonAlpha | 5234b13 | 2018-08-16 14:12:28 -0400 | [diff] [blame^] | 112 | |
| 113 | /* |
| 114 | EnableSlot - activates an OLT Chassis |
| 115 | */ |
| 116 | func (s *Server) EnableSlot(ctx context.Context, in *ActivateSlotMessage) (*ActivateSlotReturn, error) { |
| 117 | return nil, errors.New("garbage error") |
| 118 | } |
| 119 | |
| 120 | /* |
| 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] |
| 127 | err := chassis.ActivateONT(int(in.GetSlotNumber()), int(in.GetPortNumber()), int(in.GetPortNumber()), in.GetSerialNumber()) |
| 128 | |
| 129 | if err != nil { |
| 130 | return nil, err |
| 131 | } |
| 132 | return &AddOntReturn{Success: true}, nil |
| 133 | } |