-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
129 lines (116 loc) · 5.17 KB
/
controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Copyright 2021 InfAI (CC SES)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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.
*/
package controller
import (
"context"
"errors"
eventinterfaces "github.com/SENERGY-Platform/event-deployment/lib/interfaces"
"github.com/SENERGY-Platform/event-deployment/lib/model"
"github.com/SENERGY-Platform/models/go/models"
"github.com/SENERGY-Platform/process-deployment/lib/auth"
"github.com/SENERGY-Platform/process-deployment/lib/interfaces"
"github.com/SENERGY-Platform/process-deployment/lib/model/devicemodel"
"github.com/SENERGY-Platform/process-sync/pkg/configuration"
"github.com/SENERGY-Platform/process-sync/pkg/database"
"github.com/SENERGY-Platform/process-sync/pkg/database/mongo"
"github.com/SENERGY-Platform/process-sync/pkg/devices"
"github.com/SENERGY-Platform/process-sync/pkg/kafka"
"github.com/SENERGY-Platform/process-sync/pkg/mgw"
"github.com/SENERGY-Platform/process-sync/pkg/security"
"net/http"
"time"
)
type Controller struct {
config configuration.Config
mgw *mgw.Mgw
db database.Database
security Security
baseDeviceRepoFactory BaseDeviceRepoFactory
devicerepo Devices
deploymentDoneNotifier interfaces.Producer
}
type BaseDeviceRepoFactory = func(token string, deviceRepoUrl string, permissionsSearchUrl string) eventinterfaces.Devices
type DeviceProvider = func(token string, baseUrl string, deviceId string) (result models.Device, err error, code int)
type Devices interface {
GetDeviceInfosOfGroup(token auth.Token, groupId string) (devices []model.Device, deviceTypeIds []string, err error, code int)
GetDeviceInfosOfDevices(token auth.Token, deviceIds []string) (devices []model.Device, deviceTypeIds []string, err error, code int)
GetDeviceTypeSelectables(token auth.Token, criteria []model.FilterCriteria) (result []model.DeviceTypeSelectable, err error, code int)
GetConcept(token auth.Token, conceptId string) (result model.Concept, err error, code int)
GetFunction(token auth.Token, functionId string) (result model.Function, err error, code int)
GetService(token auth.Token, serviceId string) (result models.Service, err error, code int)
GetDevice(token auth.Token, id string) (devicemodel.Device, error, int)
}
type Security interface {
GetAdminToken() (token string, err error)
CheckBool(token string, kind string, id string, rights string) (allowed bool, err error)
CheckMultiple(token string, kind string, ids []string, rights string) (result map[string]bool, err error)
List(token string, resource string, limit string, offset string, rights string) (result []security.ListElement, err error)
ListElements(token string, resource string, limit string, offset string, rights string, result interface{}) (err error)
}
func NewDefault(conf configuration.Config, ctx context.Context) (ctrl *Controller, err error) {
db, err := mongo.New(conf)
if err != nil {
return ctrl, err
}
return New(conf, ctx, db, security.New(conf), devices.DefaultBaseDeviceRepoFactory, devices.DefaultDeviceProvider)
}
func New(config configuration.Config, ctx context.Context, db database.Database, security Security, baseDeviceRepoFactory BaseDeviceRepoFactory, deviceProvider DeviceProvider) (ctrl *Controller, err error) {
d, err := devices.New(config, baseDeviceRepoFactory, deviceProvider)
if err != nil {
return ctrl, err
}
ctrl = &Controller{config: config, db: db, security: security, baseDeviceRepoFactory: baseDeviceRepoFactory, devicerepo: d}
if err != nil {
return ctrl, err
}
if config.KafkaUrl != "" && config.KafkaUrl != "-" {
ctrl.deploymentDoneNotifier, err = kafka.NewProducer(ctx, config.KafkaUrl, config.ProcessDeploymentDoneTopic, config.Debug)
if err != nil {
return ctrl, err
}
}
ctrl.mgw, err = mgw.New(config, ctx, ctrl)
if err != nil {
return ctrl, err
}
err = ctrl.initDeviceGroupWatcher(ctx)
if err != nil {
return ctrl, err
}
return ctrl, nil
}
var IsPlaceholderProcessErr = errors.New("is placeholder process")
var IsMarkedForDeleteErr = errors.New("is market for deletion")
var HistoryMayOnlyDeletedIfFinishedOrPlaceholderErr = errors.New("history may only deleted if the process instance is finished or the element is a placeholder")
func (this *Controller) SetErrCode(err error) int {
switch err {
case nil:
return http.StatusOK
case database.ErrNotFound:
return http.StatusNotFound
case IsPlaceholderProcessErr:
return http.StatusBadRequest
case IsMarkedForDeleteErr:
return http.StatusBadRequest
case HistoryMayOnlyDeletedIfFinishedOrPlaceholderErr:
return http.StatusBadRequest
default:
return http.StatusInternalServerError
}
}
func (this *Controller) RemoveOldEntities(maxAge time.Duration) error {
return this.db.RemoveOldElements(maxAge)
}