blob: 54115a620289e87c76b0bd879878d3d61f468862 [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
donNewtonAlphab3279ea2018-09-18 15:55:32 -040039func (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 Namea594e632018-08-10 11:33:58 -040045/*
46CreateChassis - allocates a new Chassis struct and stores it in chassisMap
47*/
48func (s *Server) CreateChassis(ctx context.Context, in *AddChassisMessage) (*AddChassisReturn, error) {
49 phyChassisMap := models.GetPhyChassisMap()
50 absChassisMap := models.GetAbstractChassisMap()
51 clli := in.GetCLLI()
Author Namea594e632018-08-10 11:33:58 -040052 chassis := (*phyChassisMap)[clli]
53 if chassis != nil {
54 return &AddChassisReturn{DeviceID: chassis.CLLI}, nil
55 }
donNewtonAlpha1d2d6812018-09-14 16:00:02 -040056 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 Namea594e632018-08-10 11:33:58 -040058 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 }
donNewtonAlphab9b85eb2018-08-14 14:28:22 -040063 (*phyChassisMap)[clli] = phyChassis
64 fmt.Printf("phy %v, abs %v\n", phyChassisMap, absChassisMap)
Author Namea594e632018-08-10 11:33:58 -040065 (*absChassisMap)[clli] = abstractChassis
66 return &AddChassisReturn{DeviceID: clli}, nil
67}
68
69/*
donNewtonAlphab9b85eb2018-08-14 14:28:22 -040070CreateOLTChassis adds an OLT chassis/line card to the Physical chassis
Author Namea594e632018-08-10 11:33:58 -040071*/
72func (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 {
donNewtonAlpha1d2d6812018-09-14 16:00:02 -040078 errString := fmt.Sprintf("There is no chassis with CLLI of %s", clli)
79 return &AddOLTChassisReturn{DeviceID: "", ChassisDeviceID: ""}, errors.New(errString)
Author Namea594e632018-08-10 11:33:58 -040080 }
81 oltType := in.GetType()
82 address := net.TCPAddr{IP: net.ParseIP(in.GetSlotIP()), Port: int(in.GetSlotPort())}
donNewtonAlpha5234b132018-08-16 14:12:28 -040083 sOlt := physical.SimpleOLT{CLLI: clli, Hostname: in.GetHostname(), Address: address, Parent: chassis}
Author Namea594e632018-08-10 11:33:58 -040084
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 Namea594e632018-08-10 11:33:58 -040092 err := AddCard(chassis, olt)
93 if err != nil {
94 //TODO do something
95 }
Author Namea594e632018-08-10 11:33:58 -040096 return &AddOLTChassisReturn{DeviceID: in.GetHostname(), ChassisDeviceID: clli}, nil
97
98}
99
100/*
101AddCard Adds an OLT card to an existing physical chassis, allocating ports
102in the physical card to those in the abstract model
103*/
104func AddCard(physChassis *physical.Chassis, olt physical.OLT) error {
donNewtonAlpha5234b132018-08-16 14:12:28 -0400105 physChassis.AddOLTChassis(olt)
Author Namea594e632018-08-10 11:33:58 -0400106
107 ports := olt.GetPorts()
108 absChassis := (*models.GetAbstractChassisMap())[physChassis.CLLI]
109
110 for i := 0; i < len(ports); i++ {
111 absPort, _ := absChassis.NextPort()
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400112
Author Namea594e632018-08-10 11:33:58 -0400113 absPort.PhysPort = &ports[i]
114 //AssignTraits(&ports[i], absPort)
115 }
Author Namea594e632018-08-10 11:33:58 -0400116 //should probably worry about error at some point
117 return nil
118}
donNewtonAlpha5234b132018-08-16 14:12:28 -0400119
120/*
donNewtonAlpha5234b132018-08-16 14:12:28 -0400121ProvisionOnt provisions an ONT on a specific Chassis/LineCard/Port
122*/
123func (s *Server) ProvisionOnt(ctx context.Context, in *AddOntMessage) (*AddOntReturn, error) {
124 absChassisMap := models.GetAbstractChassisMap()
125 clli := in.GetCLLI()
126 chassis := (*absChassisMap)[clli]
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400127 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 }
donNewtonAlphaf7cc9992018-08-29 14:23:02 -0400131 err := chassis.ActivateONT(int(in.GetSlotNumber()), int(in.GetPortNumber()), int(in.GetOntNumber()), in.GetSerialNumber())
donNewtonAlpha5234b132018-08-16 14:12:28 -0400132
133 if err != nil {
134 return nil, err
135 }
136 return &AddOntReturn{Success: true}, nil
137}
donNewtonAlphaf7cc9992018-08-29 14:23:02 -0400138
139/*
140DeleteOnt - deletes a previously provision ont
141*/
142func (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}