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