blob: 2e6b9cc9435433587ca5d5dc926ce748cb7b8017 [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"
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040024 "os"
Author Namea594e632018-08-10 11:33:58 -040025 "strings"
26
27 "gerrit.opencord.org/abstract-olt/internal/pkg/settings"
28 "gerrit.opencord.org/abstract-olt/models"
29 "gerrit.opencord.org/abstract-olt/models/abstract"
30 "gerrit.opencord.org/abstract-olt/models/physical"
31 context "golang.org/x/net/context"
32)
33
34/*
35Server instance of the grpc server
36*/
37type Server struct {
38}
39
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040040/*
41Echo - Tester function which just returns same string sent to it
42*/
donNewtonAlphab3279ea2018-09-18 15:55:32 -040043func (s *Server) Echo(ctx context.Context, in *EchoMessage) (*EchoReplyMessage, error) {
44 ping := in.GetPing()
45 pong := EchoReplyMessage{Pong: ping}
46 return &pong, nil
47}
48
Author Namea594e632018-08-10 11:33:58 -040049/*
50CreateChassis - allocates a new Chassis struct and stores it in chassisMap
51*/
52func (s *Server) CreateChassis(ctx context.Context, in *AddChassisMessage) (*AddChassisReturn, error) {
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040053 chassisMap := models.GetChassisMap()
Author Namea594e632018-08-10 11:33:58 -040054 clli := in.GetCLLI()
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040055 chassisHolder := (*chassisMap)[clli]
56 if chassisHolder != nil {
57 return &AddChassisReturn{DeviceID: chassisHolder.AbstractChassis.CLLI}, nil
Author Namea594e632018-08-10 11:33:58 -040058 }
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040059
donNewtonAlpha1d2d6812018-09-14 16:00:02 -040060 abstractChassis := abstract.GenerateChassis(clli, int(in.GetRack()), int(in.GetShelf()))
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040061 phyChassis := physical.Chassis{CLLI: clli, VCoreAddress: net.TCPAddr{IP: net.ParseIP(in.GetVCoreIP()),
62 Port: int(in.GetVCorePort())}, Rack: int(in.GetRack()), Shelf: int(in.GetShelf())}
63
64 chassisHolder = &models.ChassisHolder{AbstractChassis: abstractChassis, PhysicalChassis: phyChassis}
Author Namea594e632018-08-10 11:33:58 -040065 if settings.GetDebug() {
66 output := fmt.Sprintf("%v", abstractChassis)
67 formatted := strings.Replace(output, "{", "\n{", -1)
68 log.Printf("new chassis %s\n", formatted)
69 }
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040070 (*chassisMap)[clli] = chassisHolder
Author Namea594e632018-08-10 11:33:58 -040071 return &AddChassisReturn{DeviceID: clli}, nil
72}
73
74/*
donNewtonAlphab9b85eb2018-08-14 14:28:22 -040075CreateOLTChassis adds an OLT chassis/line card to the Physical chassis
Author Namea594e632018-08-10 11:33:58 -040076*/
77func (s *Server) CreateOLTChassis(ctx context.Context, in *AddOLTChassisMessage) (*AddOLTChassisReturn, error) {
78 fmt.Printf(" CreateOLTChassis %v \n", *in)
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040079 chassisMap := models.GetChassisMap()
Author Namea594e632018-08-10 11:33:58 -040080 clli := in.GetCLLI()
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040081 chassisHolder := (*chassisMap)[clli]
82 if chassisHolder == nil {
donNewtonAlpha1d2d6812018-09-14 16:00:02 -040083 errString := fmt.Sprintf("There is no chassis with CLLI of %s", clli)
84 return &AddOLTChassisReturn{DeviceID: "", ChassisDeviceID: ""}, errors.New(errString)
Author Namea594e632018-08-10 11:33:58 -040085 }
86 oltType := in.GetType()
87 address := net.TCPAddr{IP: net.ParseIP(in.GetSlotIP()), Port: int(in.GetSlotPort())}
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040088 physicalChassis := &chassisHolder.PhysicalChassis
89 sOlt := physical.SimpleOLT{CLLI: clli, Hostname: in.GetHostname(), Address: address, Parent: physicalChassis}
Author Namea594e632018-08-10 11:33:58 -040090 switch oltType {
91 case AddOLTChassisMessage_edgecore:
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040092 sOlt.CreateEdgecore()
Author Namea594e632018-08-10 11:33:58 -040093 case AddOLTChassisMessage_adtran:
94 case AddOLTChassisMessage_tibit:
95 }
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040096 physicalChassis.AddOLTChassis(sOlt)
97 ports := sOlt.GetPorts()
98 for i := 0; i < len(ports); i++ {
99 absPort, _ := chassisHolder.AbstractChassis.NextPort()
100
101 absPort.PhysPort = &ports[i]
102 //AssignTraits(&ports[i], absPort)
Author Namea594e632018-08-10 11:33:58 -0400103 }
Author Namea594e632018-08-10 11:33:58 -0400104 return &AddOLTChassisReturn{DeviceID: in.GetHostname(), ChassisDeviceID: clli}, nil
105
106}
107
108/*
donNewtonAlpha5234b132018-08-16 14:12:28 -0400109ProvisionOnt provisions an ONT on a specific Chassis/LineCard/Port
110*/
111func (s *Server) ProvisionOnt(ctx context.Context, in *AddOntMessage) (*AddOntReturn, error) {
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400112 chassisMap := models.GetChassisMap()
donNewtonAlpha5234b132018-08-16 14:12:28 -0400113 clli := in.GetCLLI()
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400114 chassisHolder := (*chassisMap)[clli]
115 if chassisHolder == nil {
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400116 errString := fmt.Sprintf("There is no chassis with CLLI of %s", clli)
117 return &AddOntReturn{Success: false}, errors.New(errString)
118 }
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400119 err := chassisHolder.AbstractChassis.ActivateONT(int(in.GetSlotNumber()), int(in.GetPortNumber()), int(in.GetOntNumber()), in.GetSerialNumber())
donNewtonAlpha5234b132018-08-16 14:12:28 -0400120
121 if err != nil {
122 return nil, err
123 }
124 return &AddOntReturn{Success: true}, nil
125}
donNewtonAlphaf7cc9992018-08-29 14:23:02 -0400126
127/*
128DeleteOnt - deletes a previously provision ont
129*/
130func (s *Server) DeleteOnt(ctx context.Context, in *DeleteOntMessage) (*DeleteOntReturn, error) {
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400131 chassisMap := models.GetChassisMap()
donNewtonAlphaf7cc9992018-08-29 14:23:02 -0400132 clli := in.GetCLLI()
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400133 chassisHolder := (*chassisMap)[clli]
134 err := chassisHolder.AbstractChassis.DeleteONT(int(in.GetSlotNumber()), int(in.GetPortNumber()), int(in.GetOntNumber()), in.GetSerialNumber())
donNewtonAlphaf7cc9992018-08-29 14:23:02 -0400135 if err != nil {
136 return nil, err
137 }
138 return &DeleteOntReturn{Success: true}, nil
139}
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400140
141func (s *Server) Output(ctx context.Context, in *OutputMessage) (*OutputReturn, error) {
142 chassisMap := models.GetChassisMap()
143 for clli, chassisHolder := range *chassisMap {
144
145 json, _ := (chassisHolder).Serialize()
146 backupFile := fmt.Sprintf("backup/%s", clli)
147 f, _ := os.Create(backupFile)
148 defer f.Close()
149 _, _ = f.WriteString(string(json))
150 f.Sync()
151 }
152 return &OutputReturn{Success: true}, nil
153
154}