Skip to content
Snippets Groups Projects
submanagement.go 2.15 KiB
Newer Older
  • Learn to ignore specific revisions
  • package server
    
    import (
    	"context"
    	"time"
    
    	subpb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/subscriptionmanagement"
    	"code.fbi.h-da.de/danet/gosdn/controller/nucleus"
    )
    
    // SubManagementServer represents a SubsriptionManagementServer.
    type SubManagementServer struct {
    	subpb.UnimplementedSubscriptionManagementServiceServer
    	networkElementWatchter *nucleus.NetworkElementWatcher
    }
    
    // NewSubManagementServer returns a new SubManagementServer.
    func NewSubManagementServer(networkElementWatchter *nucleus.NetworkElementWatcher) *SubManagementServer {
    	return &SubManagementServer{
    		networkElementWatchter: networkElementWatchter,
    	}
    }
    
    // ResetAllSubscriptions stops all running subscriptions and restarts them the same way they ran before.
    func (s *SubManagementServer) ResetAllSubscriptions(ctx context.Context, request *subpb.ResetAllSubscriptionsRequest) (*subpb.ResetAllSubscriptionsResponse, error) {
    
    	subscriptions := s.networkElementWatchter.GetAllSubscriptionInformations()
    
    
    	s.networkElementWatchter.StopAndRemoveAllNetworkElementSubscriptions()
    
    
    	s.networkElementWatchter.SubscribeToNetworkElements(subscriptions)
    
    
    	return &subpb.ResetAllSubscriptionsResponse{
    		Timestamp: time.Now().UnixNano(),
    	}, nil
    }
    
    Fabian Seidl's avatar
    Fabian Seidl committed
    
    // GetAll returns information about all running subscriptions.
    func (s *SubManagementServer) GetAll(ctx context.Context, request *subpb.GetAllRequest) (*subpb.GetAllResponse, error) {
    	subInfos := s.networkElementWatchter.GetAllSubscriptionInformations()
    
    	subInfosToReturn := make([]*subpb.Subscription, 0)
    
    	for _, info := range subInfos {
    		tmpPaths := make([]*subpb.Path, 0)
    		for _, path := range info.Opts.Paths {
    			tmpPaths = append(tmpPaths, &subpb.Path{
    				Elem: path,
    			})
    		}
    
    		subInfosToReturn = append(subInfosToReturn, &subpb.Subscription{
    			Subid:   info.SubID,
    			Pid:     info.PndID,
    			Mneid:   info.MneID,
    			MneName: info.MneName,
    			Paths:   tmpPaths,
    			SubscribeOptions: &subpb.SubscribeOptions{
    				GnmiMode:       info.Opts.Mode,
    				GnmiStreamMode: info.Opts.StreamMode,
    				SampleInterval: info.Opts.SampleInterval,
    			},
    		})
    	}
    
    	return &subpb.GetAllResponse{
    		Timestamp:     time.Now().UnixNano(),
    		Subscriptions: subInfosToReturn,
    	}, nil
    }