summaryrefslogtreecommitdiffstats
path: root/msb2pilot/src
diff options
context:
space:
mode:
authorLvbo163 <lv.bo163@zte.com.cn>2018-08-06 13:46:09 +0800
committerLvbo163 <lv.bo163@zte.com.cn>2018-08-06 13:46:09 +0800
commit9c3b0ca0535ddf6967983190986035b3890f3f5f (patch)
tree5c3eab3c95635515fa4ff7e8514f4de8d55b8dab /msb2pilot/src
parentdbb52cd1295fdc995873c7f1d6ce046e4e8da7c3 (diff)
compare data change
Issue-ID: MSB-259 Change-Id: I390e242b219937bb6fcf59e698506cb96f41e527 Signed-off-by: Lvbo163 <lv.bo163@zte.com.cn>
Diffstat (limited to 'msb2pilot/src')
-rw-r--r--msb2pilot/src/msb2pilot/pilot/msb.go60
1 files changed, 60 insertions, 0 deletions
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
+}