blob: bb1a9b3abd50cd14317e96e8b88b3c896b6bf798 [file] [log] [blame]
Author Namea594e632018-08-10 11:33:58 -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*/
16
17package api
18
19import (
donNewtonAlpha1d2d6812018-09-14 16:00:02 -040020 "errors"
Author Namea594e632018-08-10 11:33:58 -040021 "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/*
34Server instance of the grpc server
35*/
36type Server struct {
37}
38
39/*
40CreateChassis - allocates a new Chassis struct and stores it in chassisMap
41*/
42func (s *Server) CreateChassis(ctx context.Context, in *AddChassisMessage) (*AddChassisReturn, error) {
43 phyChassisMap := models.GetPhyChassisMap()
44 absChassisMap := models.GetAbstractChassisMap()
45 clli := in.GetCLLI()
Author Namea594e632018-08-10 11:33:58 -040046 chassis := (*phyChassisMap)[clli]
47 if chassis != nil {
48 return &AddChassisReturn{DeviceID: chassis.CLLI}, nil
49 }
donNewtonAlpha1d2d6812018-09-14 16:00:02 -040050 abstractChassis := abstract.GenerateChassis(clli, int(in.GetRack()), int(in.GetShelf()))
51 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 Namea594e632018-08-10 11:33:58 -040052 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 }
donNewtonAlphab9b85eb2018-08-14 14:28:22 -040057 (*phyChassisMap)[clli] = phyChassis
58 fmt.Printf("phy %v, abs %v\n", phyChassisMap, absChassisMap)
Author Namea594e632018-08-10 11:33:58 -040059 (*absChassisMap)[clli] = abstractChassis
60 return &AddChassisReturn{DeviceID: clli}, nil
61}
62
63/*
donNewtonAlphab9b85eb2018-08-14 14:28:22 -040064CreateOLTChassis adds an OLT chassis/line card to the Physical chassis
Author Namea594e632018-08-10 11:33:58 -040065*/
66func (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 {
donNewtonAlpha1d2d6812018-09-14 16:00:02 -040072 errString := fmt.Sprintf("There is no chassis with CLLI of %s", clli)
73 return &AddOLTChassisReturn{DeviceID: "", ChassisDeviceID: ""}, errors.New(errString)
Author Namea594e632018-08-10 11:33:58 -040074 }
75 oltType := in.GetType()
76 address := net.TCPAddr{IP: net.ParseIP(in.GetSlotIP()), Port: int(in.GetSlotPort())}
donNewtonAlpha5234b132018-08-16 14:12:28 -040077 sOlt := physical.SimpleOLT{CLLI: clli, Hostname: in.GetHostname(), Address: address, Parent: chassis}
Author Namea594e632018-08-10 11:33:58 -040078
79 var olt physical.OLT
80 switch oltType {
81 case AddOLTChassisMessage_edgecore:
82 olt = physical.CreateEdgecore(&sOlt)
83 case AddOLTChassisMessage_adtran:
84 case AddOLTChassisMessage_tibit:
85 }
Author Namea594e632018-08-10 11:33:58 -040086 err := AddCard(chassis, olt)
87 if err != nil {
88 //TODO do something
89 }
Author Namea594e632018-08-10 11:33:58 -040090 return &AddOLTChassisReturn{DeviceID: in.GetHostname(), ChassisDeviceID: clli}, nil
91
92}
93
94/*
95AddCard Adds an OLT card to an existing physical chassis, allocating ports
96in the physical card to those in the abstract model
97*/
98func AddCard(physChassis *physical.Chassis, olt physical.OLT) error {
donNewtonAlpha5234b132018-08-16 14:12:28 -040099 physChassis.AddOLTChassis(olt)
Author Namea594e632018-08-10 11:33:58 -0400100
101 ports := olt.GetPorts()
102 absChassis := (*models.GetAbstractChassisMap())[physChassis.CLLI]
103
104 for i := 0; i < len(ports); i++ {
105 absPort, _ := absChassis.NextPort()
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400106
Author Namea594e632018-08-10 11:33:58 -0400107 absPort.PhysPort = &ports[i]
108 //AssignTraits(&ports[i], absPort)
109 }
Author Namea594e632018-08-10 11:33:58 -0400110 //should probably worry about error at some point
111 return nil
112}
donNewtonAlpha5234b132018-08-16 14:12:28 -0400113
114/*
donNewtonAlpha5234b132018-08-16 14:12:28 -0400115ProvisionOnt provisions an ONT on a specific Chassis/LineCard/Port
116*/
117func (s *Server) ProvisionOnt(ctx context.Context, in *AddOntMessage) (*AddOntReturn, error) {
118 absChassisMap := models.GetAbstractChassisMap()
119 clli := in.GetCLLI()
120 chassis := (*absChassisMap)[clli]
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400121 if chassis == nil {
122 errString := fmt.Sprintf("There is no chassis with CLLI of %s", clli)
123 return &AddOntReturn{Success: false}, errors.New(errString)
124 }
donNewtonAlphaf7cc9992018-08-29 14:23:02 -0400125 err := chassis.ActivateONT(int(in.GetSlotNumber()), int(in.GetPortNumber()), int(in.GetOntNumber()), in.GetSerialNumber())
donNewtonAlpha5234b132018-08-16 14:12:28 -0400126
127 if err != nil {
128 return nil, err
129 }
130 return &AddOntReturn{Success: true}, nil
131}
donNewtonAlphaf7cc9992018-08-29 14:23:02 -0400132
133/*
134DeleteOnt - deletes a previously provision ont
135*/
136func (s *Server) DeleteOnt(ctx context.Context, in *DeleteOntMessage) (*DeleteOntReturn, error) {
137 absChassisMap := models.GetAbstractChassisMap()
138 clli := in.GetCLLI()
139 chassis := (*absChassisMap)[clli]
140 err := chassis.DeleteONT(int(in.GetSlotNumber()), int(in.GetPortNumber()), int(in.GetOntNumber()), in.GetSerialNumber())
141 if err != nil {
142 return nil, err
143 }
144 return &DeleteOntReturn{Success: true}, nil
145}