From 9c3b0ca0535ddf6967983190986035b3890f3f5f Mon Sep 17 00:00:00 2001 From: Lvbo163 Date: Mon, 6 Aug 2018 13:46:09 +0800 Subject: compare data change Issue-ID: MSB-259 Change-Id: I390e242b219937bb6fcf59e698506cb96f41e527 Signed-off-by: Lvbo163 --- msb2pilot/src/msb2pilot/pilot/msb.go | 60 ++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 msb2pilot/src/msb2pilot/pilot/msb.go (limited to 'msb2pilot/src') diff --git a/msb2pilot/src/msb2pilot/pilot/msb.go b/msb2pilot/src/msb2pilot/pilot/msb.go new file mode 100644 index 0000000..c0a2dc9 --- /dev/null +++ b/msb2pilot/src/msb2pilot/pilot/msb.go @@ -0,0 +1,60 @@ +/** + * Copyright (c) 2018 ZTE Corporation. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and the Apache License 2.0 which both accompany this distribution, + * and are available at http://www.eclipse.org/legal/epl-v10.html + * and http://www.apache.org/licenses/LICENSE-2.0 + * + * Contributors: + * ZTE - initial Project + */ +package pilot + +var ( + cachedServices []*models.MsbService +) + +func SyncMsbData(newServices []*models.MsbService) { + log.Log.Debug("sync msb rewrite rule to pilot") + createServices, updateServices, deleteServices := compareServices(cachedServices, newServices) + + log.Log.Debug("SyncMsbData: ", len(createServices), len(updateServices), len(deleteServices)) + + cachedServices = newServices +} + +func compareServices(oldServices, newServices []*models.MsbService) (createServices, updateServices, deleteServices []*models.MsbService) { + oldServiceMap := toServiceMap(oldServices) + newServiceMap := toServiceMap(newServices) + + for key, newService := range newServiceMap { + if oldService, exist := oldServiceMap[key]; exist { + // service exist: check whether need to update + if oldService.ModifyIndex != newService.ModifyIndex { + updateServices = append(updateServices, newService) + } + } else { + // service not exist: add + createServices = append(createServices, newService) + } + + delete(oldServiceMap, key) + } + + for _, service := range oldServiceMap { + deleteServices = append(deleteServices, service) + } + + return +} + +func toServiceMap(services []*models.MsbService) map[string]*models.MsbService { + serviceMap := make(map[string]*models.MsbService) + + for _, service := range services { + serviceMap[service.ServiceName] = service + } + + return serviceMap +} -- cgit 1.2.3-korg