summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHuabing Zhao <zhaohuabing@gmail.com>2018-08-06 07:13:11 +0000
committerGerrit Code Review <gerrit@onap.org>2018-08-06 07:13:11 +0000
commitfd5a73ed626884d37dff38ed2410bf7f3c4782d2 (patch)
tree8aac14d09cfc4ff671be08def4dfa6181d10dab0
parent0cd704e8b38f4c8fbf229e95404ffffe75e7cb20 (diff)
parent9c3b0ca0535ddf6967983190986035b3890f3f5f (diff)
Merge "compare data change"
-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
+}