Adding E2M reassoc of E2T when its already in DB
[ric-plt/e2mgr.git] / E2Manager / handlers / rmrmsghandlers / e2_term_init_notification_handler.go
index 5de5e2d..ca8a4c5 100644 (file)
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // See the License for the specific language governing permissions and
 // limitations under the License.
-//
+
+//  This source code is part of the near-RT RIC (RAN Intelligent Controller)
+//  platform project (RICP).
 
 package rmrmsghandlers
 
 import (
+       "e2mgr/clients"
        "e2mgr/logger"
        "e2mgr/managers"
        "e2mgr/models"
-       "e2mgr/services"
+       "encoding/json"
        "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
+       "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
 )
 
 type E2TermInitNotificationHandler struct {
-       logger                 *logger.Logger
-       rnibDataService        services.RNibDataService
-       ranReconnectionManager *managers.RanReconnectionManager
+       logger                  *logger.Logger
+       ranDisconnectionManager *managers.RanDisconnectionManager
+       e2tInstancesManager     managers.IE2TInstancesManager
+       routingManagerClient    clients.IRoutingManagerClient
 }
 
-func NewE2TermInitNotificationHandler(logger *logger.Logger, ranReconnectionManager *managers.RanReconnectionManager, rnibDataService services.RNibDataService) E2TermInitNotificationHandler {
+func NewE2TermInitNotificationHandler(logger *logger.Logger, ranDisconnectionManager *managers.RanDisconnectionManager, e2tInstancesManager managers.IE2TInstancesManager, routingManagerClient clients.IRoutingManagerClient) E2TermInitNotificationHandler {
        return E2TermInitNotificationHandler{
-               logger:                 logger,
-               rnibDataService:        rnibDataService,
-               ranReconnectionManager: ranReconnectionManager,
+               logger:                  logger,
+               ranDisconnectionManager: ranDisconnectionManager,
+               e2tInstancesManager:     e2tInstancesManager,
+               routingManagerClient:    routingManagerClient,
        }
 }
 
 func (h E2TermInitNotificationHandler) Handle(request *models.NotificationRequest) {
+       unmarshalledPayload := models.E2TermInitPayload{}
+       err := json.Unmarshal(request.Payload, &unmarshalledPayload)
+
+       if err != nil {
+               h.logger.Errorf("#E2TermInitNotificationHandler.Handle - Error unmarshaling E2 Term Init payload: %s", err)
+               return
+       }
+
+       e2tAddress := unmarshalledPayload.Address
+
+       if len(e2tAddress) == 0 {
+               h.logger.Errorf("#E2TermInitNotificationHandler.Handle - Empty E2T address received")
+               return
+       }
 
-       h.logger.Infof("#E2TermInitNotificationHandler.Handle - Handling E2_TERM_INIT")
+       h.logger.Infof("#E2TermInitNotificationHandler.Handle - E2T payload: %s - handling E2_TERM_INIT", unmarshalledPayload)
+
+       e2tInstance, err := h.e2tInstancesManager.GetE2TInstance(e2tAddress)
 
-       nbIdentityList, err := h.rnibDataService.GetListNodebIds()
        if err != nil {
-               h.logger.Errorf("#E2TermInitNotificationHandler.Handle - Failed to get nodes list from RNIB. Error: %s", err.Error())
+               _, ok := err.(*common.ResourceNotFoundError)
+
+               if !ok {
+                       h.logger.Errorf("#E2TermInitNotificationHandler.Handle - Failed retrieving E2TInstance. error: %s", err)
+                       return
+               }
+
+               h.HandleNewE2TInstance(e2tAddress, unmarshalledPayload.PodName)
+               return
+       }
+
+       if len(e2tInstance.AssociatedRanList) == 0 {
+               h.logger.Infof("#E2TermInitNotificationHandler.Handle - E2T Address: %s - E2T instance has no associated RANs", e2tInstance.Address)
+                h.UpdateExistingE2TInstanceToRtmgr(e2tAddress)
                return
        }
 
-       if len(nbIdentityList) == 0 {
-               h.logger.Warnf("#E2TermInitNotificationHandler.Handle - The Nodes list in RNIB is empty")
+       if e2tInstance.State == entities.ToBeDeleted {
+               h.logger.Infof("#E2TermInitNotificationHandler.Handle - E2T Address: %s - E2T instance status is: %s, ignore", e2tInstance.Address, e2tInstance.State)
                return
        }
 
-       for _, nbIdentity := range nbIdentityList {
+       h.HandleExistingE2TInstance(e2tInstance)
+
+       h.logger.Infof("#E2TermInitNotificationHandler.Handle - Completed handling of E2_TERM_INIT")
+}
+
+func (h E2TermInitNotificationHandler) HandleExistingE2TInstance(e2tInstance *entities.E2TInstance) {
+
+       for _, ranName := range e2tInstance.AssociatedRanList {
 
-               if err := h.ranReconnectionManager.ReconnectRan(nbIdentity.InventoryName); err != nil {
-                       h.logger.Errorf("#E2TermInitNotificationHandler.Handle - Ran name: %s - connection attempt failure, error: %s", (*nbIdentity).GetInventoryName(), err.Error())
-                       _, ok := err.(*common.ResourceNotFoundError)
-                       if !ok {
+               if err := h.ranDisconnectionManager.DisconnectRan(ranName); err != nil {
+                       if _, ok := err.(*common.ResourceNotFoundError); !ok {
                                break
                        }
                }
        }
+}
+
+func (h E2TermInitNotificationHandler) HandleNewE2TInstance(e2tAddress string, podName string) {
+
+    for i := 1; i < 4; i++ {
+
+        err := h.routingManagerClient.AddE2TInstance(e2tAddress)
+
+            if err == nil{
+
+                   _ = h.e2tInstancesManager.AddE2TInstance(e2tAddress, podName)
+
+                   break
+            } else {
+
+                h.logger.Errorf("#E2TermInitNotificationHandler.HandleNewE2TInstance - e2t address count - %d : %s - routing manager failure", i, e2tAddress)
+            }
+
+        }
+}
+
+func (h E2TermInitNotificationHandler) UpdateExistingE2TInstanceToRtmgr(e2tAddress string) {
+
+                _ = h.e2tInstancesManager.ResetKeepAliveTimestamp(e2tAddress)
+    for i := 1; i < 4; i++ {
+
+        err := h.routingManagerClient.AddE2TInstance(e2tAddress)
+
+            if err == nil{
+                   break
+            } else {
+
+                h.logger.Errorf("#E2TermInitNotificationHandler.UpdateExistingE2TInstanceToRtmgr - e2t address count - %d : %s - routing manager failure", i, e2tAddress)
+            }
+
+        }
 
-       h.logger.Infof("#E2TermInitNotificationHandler.Handle - Completed handling of E2_TERM_INIT")
 }
+