-
Notifications
You must be signed in to change notification settings - Fork 12
/
ModelNexus.java
927 lines (792 loc) · 39.7 KB
/
ModelNexus.java
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
/*********************************************************************
* Copyright (c) 2023 Contributors to the Eclipse Foundation.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Data In Motion - initial API and implementation
* Kentyou - fixes and updates to include a basic sensiNact provider
**********************************************************************/
package org.eclipse.sensinact.core.model.nexus;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Instant;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EOperation;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.ETypedElement;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.impl.EFactoryImpl;
import org.eclipse.emf.ecore.impl.MinimalEObjectImpl;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.xmi.XMLResource;
import org.eclipse.sensinact.core.command.impl.ActionHandler;
import org.eclipse.sensinact.core.command.impl.ResourcePullHandler;
import org.eclipse.sensinact.core.command.impl.ResourcePushHandler;
import org.eclipse.sensinact.core.model.ResourceType;
import org.eclipse.sensinact.core.model.nexus.emf.EMFUtil;
import org.eclipse.sensinact.core.model.nexus.emf.compare.EMFCompareUtil;
import org.eclipse.sensinact.core.notification.NotificationAccumulator;
import org.eclipse.sensinact.core.twin.TimedValue;
import org.eclipse.sensinact.core.whiteboard.impl.SensinactWhiteboard;
import org.eclipse.sensinact.model.core.metadata.Action;
import org.eclipse.sensinact.model.core.metadata.ActionParameter;
import org.eclipse.sensinact.model.core.metadata.AnnotationMetadata;
import org.eclipse.sensinact.model.core.metadata.MetadataFactory;
import org.eclipse.sensinact.model.core.metadata.ResourceAttribute;
import org.eclipse.sensinact.model.core.metadata.ResourceMetadata;
import org.eclipse.sensinact.model.core.metadata.ServiceReference;
import org.eclipse.sensinact.model.core.provider.Admin;
import org.eclipse.sensinact.model.core.provider.FeatureCustomMetadata;
import org.eclipse.sensinact.model.core.provider.Provider;
import org.eclipse.sensinact.model.core.provider.ProviderFactory;
import org.eclipse.sensinact.model.core.provider.ProviderPackage;
import org.eclipse.sensinact.model.core.provider.Service;
import org.osgi.util.promise.Promise;
import org.osgi.util.promise.Promises;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A Central Nexus for Models
*
* @author Juergen Albert
* @since 26 Sep 2022
*/
public class ModelNexus {
private static final Logger LOG = LoggerFactory.getLogger(ModelNexus.class);
private static final String BASE = "data/";
private static final String INSTANCES = BASE + "instances/";
private static final String BASIC_BASE_ECORE = BASE + "models/basic/base.ecore";
/** HTTPS_ECLIPSE_ORG_SENSINACT_BASE */
private static final String DEFAULT_URI = "https://eclipse.org/sensinact/base";
private static final URI DEFAULT_URI_OBJECT = URI.createURI(DEFAULT_URI);
private final ResourceSet resourceSet;
private final ProviderPackage providerPackage;
private final Supplier<NotificationAccumulator> notificationAccumulator;
private Map<URI, EPackage> packageCache = new ConcurrentHashMap<>();
private EPackage defaultPackage;
private final Map<String, Provider> providers = new HashMap<>();
private final Map<String, EClass> models = new HashMap<>();
private final SensinactWhiteboard whiteboard;
public ModelNexus(ResourceSet resourceSet, ProviderPackage ProviderPackage,
Supplier<NotificationAccumulator> accumulator) {
this(resourceSet, ProviderPackage, accumulator, (SensinactWhiteboard) null);
}
public ModelNexus(ResourceSet resourceSet, ProviderPackage ProviderPackage,
Supplier<NotificationAccumulator> accumulator, ActionHandler actionHandler) {
this(resourceSet, ProviderPackage, accumulator, actionHandler, null, null);
}
public ModelNexus(ResourceSet resourceSet, ProviderPackage ProviderPackage,
Supplier<NotificationAccumulator> accumulator, ActionHandler actionHandler,
ResourcePullHandler resourceValuePullHandler, ResourcePushHandler resourceValuePushHandler) {
this(resourceSet, ProviderPackage, accumulator, new SensinactWhiteboard(null, null) {
@Override
public Promise<Object> act(String model, String provider, String service, String resource,
Map<String, Object> arguments) {
if (actionHandler != null) {
return actionHandler.act(model, provider, service, resource, arguments);
}
throw new RuntimeException("No action handler set");
}
@Override
public <T> Promise<TimedValue<T>> pullValue(String model, String provider, String service, String resource,
Class<T> type, TimedValue<T> cachedValue, Consumer<TimedValue<T>> gatewayUpdate) {
if (resourceValuePullHandler != null) {
return resourceValuePullHandler.pullValue(model, provider, service, resource, type, cachedValue,
gatewayUpdate);
}
throw new RuntimeException("No pullValue handler set");
}
@Override
public <T> Promise<TimedValue<T>> pushValue(String model, String provider, String service, String resource,
Class<T> type, TimedValue<T> cachedValue, TimedValue<T> newValue,
Consumer<TimedValue<T>> gatewayUpdate) {
if (resourceValuePushHandler != null) {
return resourceValuePushHandler.pushValue(model, provider, service, resource, type, cachedValue,
newValue, gatewayUpdate);
}
throw new RuntimeException("No pushValue handler set");
}
});
}
public ModelNexus(ResourceSet resourceSet, ProviderPackage ProviderPackage,
Supplier<NotificationAccumulator> accumulator, SensinactWhiteboard whiteboard) {
this.resourceSet = resourceSet;
this.providerPackage = ProviderPackage;
this.notificationAccumulator = accumulator;
this.whiteboard = whiteboard;
// TODO we need a general Working Directory for such data
Optional<EPackage> packageOptional = loadDefaultPackage(Paths.get(BASIC_BASE_ECORE));
defaultPackage = packageOptional
.orElseGet(() -> EMFUtil.createPackage("base", DEFAULT_URI, "sensinactBase", this.resourceSet));
defaultPackage.setEFactoryInstance(new EFactoryImpl() {
@Override
protected EObject basicCreate(EClass eClass) {
return eClass.getInstanceClassName() == "java.util.Map$Entry"
? new MinimalEObjectImpl.Container.Dynamic.BasicEMapEntry<String, String>(eClass)
: new MinimalEObjectImpl.Container.Dynamic.Permissive(eClass);
}
});
packageCache.put(DEFAULT_URI_OBJECT, defaultPackage);
loadInstances();
setupSensinactProvider();
}
private Optional<EPackage> loadDefaultPackage(Path fileName) {
Resource resource = resourceSet.createResource(URI.createFileURI(fileName.toString()));
if (Files.isRegularFile(fileName)) {
try {
resource.load(null);
if (!resource.getContents().isEmpty()) {
EPackage defaultPackage = (EPackage) resource.getContents().get(0);
resource.setURI(URI.createURI(defaultPackage.getNsURI()));
resourceSet.getResources().remove(resource);
resourceSet.getPackageRegistry().put(defaultPackage.getNsURI(), defaultPackage);
return Optional.of(defaultPackage);
}
} catch (IOException e) {
LOG.error(
"THIS WILL BE A RUNTIME EXCPETION FOR NOW: Error Loading default EPackage from persistent file: {}",
fileName, e);
throw new RuntimeException(e);
}
}
return Optional.empty();
}
private void loadInstances() {
Path instancesPath = Paths.get(INSTANCES);
if (Files.isDirectory(instancesPath)) {
try {
Files.walk(instancesPath).forEach(this::load);
} catch (IOException e) {
LOG.error("THIS WILL BE A RUNTIME EXCPETION FOR NOW: Error loading instances from Path: {}",
instancesPath, e);
throw new RuntimeException(e);
}
}
}
private void load(Path path) {
try {
if (Files.isDirectory(path)) {
return;
} else {
URI uri = URI.createFileURI(path.toString());
Resource resource = resourceSet.createResource(uri);
resource.load(null);
if (!resource.getContents().isEmpty()) {
Provider provider = (Provider) resource.getContents().get(0);
EClass eClass = provider.eClass();
models.putIfAbsent(EMFUtil.getModelName(eClass), eClass);
providers.put(provider.getId(), provider);
}
}
} catch (IOException e) {
LOG.error("THIS WILL BE A RUNTIME EXCPETION FOR NOW: Error loading provider from Path: {}", path, e);
throw new RuntimeException(e);
}
}
private void setupSensinactProvider() {
Instant now = Instant.now();
EClass sensiNactModel = getModel("sensinact").orElseGet(() -> createModel("sensinact", now));
EReference svc = Optional.ofNullable(getServiceForModel(sensiNactModel, "system"))
.orElseGet(() -> createService(sensiNactModel, "system", now));
EClass svcClass = svc.getEReferenceType();
EStructuralFeature versionResource = Optional.ofNullable(svcClass.getEStructuralFeature("version"))
.orElseGet(() -> createResource(svcClass, "version", double.class, now, null));
EStructuralFeature startedResource = Optional.ofNullable(svcClass.getEStructuralFeature("started"))
.orElseGet(() -> createResource(svcClass, "started", Instant.class, now, null));
Provider provider = Optional.ofNullable(getProvider("sensiNact"))
.orElseGet(() -> doCreateProvider("sensinact", sensiNactModel, "sensiNact", now));
handleDataUpdate("sensinact", provider, svc, versionResource, 0.1D, now);
handleDataUpdate("sensinact", provider, svc, startedResource, now, now);
}
public void shutDown() {
defaultPackage.eResource().setURI(URI.createFileURI(Path.of(BASIC_BASE_ECORE).toAbsolutePath().toString()));
try {
defaultPackage.eResource().save(null);
providers.values().forEach(this::saveInstance);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
resourceSet.getResources().clear();
}
/**
* Will associate the given Parent provider with the given child. If parent or
* child do not exist, they will be created.
*
* @param parentModel name of the type of providers for the parent. Can be
* <code>null</code>
* @param parentProvider The provider name of the parent. The name will be used
* as ID and in the first setup as the friendlyname for
* the Admin Service
* @param childModel name of the type of providers for the parent. Can be
* <code>null</code>
* @param childProvider The provider name of the child. The name will be used
* as ID and in the first setup as the friendlyname for
* the Admin service
* @param timestamp the timestamp when the link is created. If null, the
* current timestamp is used.
*/
public void linkProviders(String parentProvider, String childProvider, Instant timestamp) {
Instant metaTimestamp = timestamp == null ? Instant.now() : timestamp;
NotificationAccumulator accumulator = notificationAccumulator.get();
Provider parent = providers.get(parentProvider);
Provider child = providers.get(childProvider);
if (parent == null) {
throw new IllegalArgumentException("No parent provider " + parentProvider);
}
if (child == null) {
throw new IllegalArgumentException("No child provider " + childProvider);
}
((Provider) parent).getLinkedProviders().add((Provider) child);
// TODO link event
// accumulator.link(...)
}
/**
* Will disassociate the given Parent provider with the given child.
*
* @param parentModel name of the type of providers for the parent. Can be
* <code>null</code>.
* @param parentProvider The provider name of the parent.
* @param childModel name of the type of providers for the parent. Can be
* <code>null</code>.
* @param childProvider The provider name of the child.
* @param timestamp the timestamp when the link is created. If null, the
* current timestamp is used.
*/
public void unlinkProviders(String parentProvider, String childProvider, Instant timestamp) {
Instant metaTimestamp = timestamp == null ? Instant.now() : timestamp;
Provider parent = providers.get(parentProvider);
Provider child = providers.get(childProvider);
if (parent == null) {
throw new IllegalArgumentException("No parent provider " + parentProvider);
}
if (child == null) {
throw new IllegalArgumentException("No child provider " + childProvider);
}
((Provider) parent).getLinkedProviders().remove(child);
// TODO unlink event
// accumulator.unlink(...)
}
public void handleDataUpdate(String modelName, Provider provider, EStructuralFeature serviceFeature,
EStructuralFeature resourceFeature, Object data, Instant timestamp) {
Instant metaTimestamp = timestamp == null ? Instant.now() : timestamp;
String providerName = provider.getId();
NotificationAccumulator accumulator = notificationAccumulator.get();
Service service = (Service) provider.eGet(serviceFeature);
if (service == null) {
service = (Service) EcoreUtil.create((EClass) serviceFeature.getEType());
provider.eSet(serviceFeature, service);
accumulator.addService(modelName, providerName, serviceFeature.getName());
}
ResourceMetadata metadata = (ResourceMetadata) service.getMetadata().get(resourceFeature);
Map<String, Object> oldMetaData = null;
Object oldValue = service.eGet(resourceFeature);
if (metadata != null) {
oldMetaData = EMFCompareUtil.extractMetadataMap(oldValue, metadata, resourceFeature);
}
if (oldValue == null) {
accumulator.addResource(modelName, providerName, serviceFeature.getName(), resourceFeature.getName());
}
// Allow an update if the resource didn't exist or if the update timestamp is
// equal to or after the one of the current value
if (metadata == null || !metadata.getTimestamp().isAfter(timestamp.plusMillis(1))) {
EClassifier resourceType = resourceFeature.getEType();
if (metadata == null) {
metadata = MetadataFactory.eINSTANCE.createResourceMetadata();
service.getMetadata().put(resourceFeature, metadata);
}
metadata.setTimestamp(metaTimestamp);
if (data == null || resourceType.isInstance(data)) {
service.eSet(resourceFeature, data);
} else {
service.eSet(resourceFeature, EMFUtil.convertToTargetType(resourceType, data));
}
accumulator.resourceValueUpdate(modelName, providerName, serviceFeature.getName(),
resourceFeature.getName(), resourceType.getInstanceClass(), oldValue, data, timestamp);
} else {
return;
}
if (metadata == null) {
metadata = MetadataFactory.eINSTANCE.createResourceMetadata();
service.getMetadata().put(resourceFeature, metadata);
}
metadata.setTimestamp(timestamp);
Map<String, Object> newMetaData = EMFCompareUtil.extractMetadataMap(data, metadata, resourceFeature);
accumulator.metadataValueUpdate(modelName, providerName, serviceFeature.getName(), resourceFeature.getName(),
oldMetaData, newMetaData, timestamp);
}
/**
* Expects the caller to have checked that no provider exists for the given name
*
* @param modelName
* @param providerName
* @param timestamp
* @param accumulator
* @return
*/
private Provider doCreateProvider(String modelName, EClass model, String providerName, Instant timestamp) {
return doCreateProvider(modelName, model, providerName, timestamp, true);
}
private Provider doCreateProvider(String modelName, EClass model, String providerName, Instant timestamp,
boolean createAdmin) {
Provider provider = (Provider) EcoreUtil.create(model);
provider.setId(providerName);
notificationAccumulator.get().addProvider(modelName, providerName);
if (createAdmin) {
createAdminServiceForProvider(provider, timestamp);
}
providers.put(providerName, provider);
return provider;
}
private void createAdminServiceForProvider(Provider original, Instant timestamp) {
Provider provider = EcoreUtil.copy(original);
final Admin adminSvc = ProviderFactory.eINSTANCE.createAdmin();
provider.setAdmin(adminSvc);
// Set a timestamp to admin resources to indicate them as valued
for (EStructuralFeature resourceFeature : provider.getAdmin().eClass().getEStructuralFeatures()) {
if (resourceFeature == ProviderPackage.Literals.ADMIN__FRIENDLY_NAME
|| resourceFeature == ProviderPackage.Literals.ADMIN__MODEL_URI) {
ResourceMetadata metadata = MetadataFactory.eINSTANCE.createResourceMetadata();
metadata.setOriginalName(resourceFeature.getName());
metadata.setTimestamp(timestamp);
adminSvc.getMetadata().put(resourceFeature, metadata);
}
}
adminSvc.setFriendlyName(provider.getId());
adminSvc.setModelUri(EcoreUtil.getURI(provider.eClass()).toString());
EMFCompareUtil.compareAndSet(provider, original, notificationAccumulator.get());
}
public Provider createProviderInstance(String modelName, String providerName) {
return createProviderInstance(modelName, providerName, Instant.now());
}
public Provider createProviderInstance(String modelName, String providerName, Instant timestamp) {
Provider provider = getProvider(providerName);
if (provider != null) {
String m = EMFUtil.getModelName(provider.eClass());
if (!m.equals(modelName)) {
throw new IllegalArgumentException(
"The provider " + providerName + " already exists with a different model " + m);
} else {
throw new IllegalArgumentException(
"The provider " + providerName + " already exists with the model " + modelName);
}
} else {
provider = doCreateProvider(modelName, getMandatoryModel(modelName), providerName, timestamp);
}
return provider;
}
public Provider getProvider(String providerName) {
return providers.get(providerName);
}
public String getProviderModel(String providerName) {
return Optional.ofNullable(providers.get(providerName)).map(p -> EMFUtil.getModelName(p.eClass())).orElse(null);
}
public Provider getProvider(String model, String providerName) {
Provider p = providers.get(providerName);
if (p != null) {
String m = EMFUtil.getModelName(p.eClass());
if (!m.equals(model)) {
LOG.debug("Provider {} exists but with model {} not model {}", providerName, m, model);
p = null;
}
}
return p;
}
public Collection<Provider> getProviders() {
return Collections.unmodifiableCollection(providers.values());
}
/**
* Lists know providers
*/
public List<Provider> getProviders(String model) {
EClass m = getMandatoryModel(model);
// Don't use isInstance as subtypes have a different model
return providers.values().stream().filter(p -> EMFUtil.getModelName(p.eClass()).equals(m))
.collect(Collectors.toList());
}
public EAttribute createResource(EClass service, String resource, Class<?> type, Instant timestamp,
Object defaultValue) {
return createResource(service, resource, type, timestamp, defaultValue, false, 0, false);
}
public EAttribute createResource(EClass service, String resource, Class<?> type, Instant timestamp,
Object defaultValue, boolean hasGetter, long getterCacheMs, boolean hasSetter) {
// FIXME: WIP
FeatureCustomMetadata resourceType = ProviderFactory.eINSTANCE.createFeatureCustomMetadata();
resourceType.setName("resourceType");
resourceType.setValue(ResourceType.SENSOR);
resourceType.setTimestamp(Instant.EPOCH);
return doCreateResource(service, resource, type, timestamp, defaultValue, List.of(resourceType), hasGetter,
getterCacheMs, hasSetter);
}
private EAttribute doCreateResource(EClass service, String resource, Class<?> type, Instant timestamp,
Object defaultValue, List<FeatureCustomMetadata> metadata, boolean hasGetter, long getterCacheMs,
boolean hasSetter) {
assertResourceNotExist(service, resource);
ResourceAttribute feature = EMFUtil.createResourceAttribute(service, resource, type, defaultValue);
feature.setExternalGet(hasGetter);
feature.setExternalSet(hasSetter);
if (getterCacheMs > 0) {
feature.setExternalGetCacheMs(getterCacheMs);
}
EMFUtil.fillMetadata(feature, timestamp, false, resource, List.of());
return feature;
}
private void assertResourceNotExist(EClass service, String resource) {
ETypedElement element = service.getEOperations().stream().filter(o -> o.getName().equals(resource))
.map(ETypedElement.class::cast).findFirst().orElseGet(() -> service.getEStructuralFeature(resource));
if (element != null) {
throw new IllegalArgumentException(
"There is an existing resource with name " + resource + " in service " + service + " in model "
+ EMFUtil.getModelName(service.eContainingFeature().getEContainingClass()));
}
}
private EClass createModel(String modelName, String thePackageUri, Instant timestamp) {
String modelClassName = firstToUpper(modelName);
URI packageUri = URI.createURI(thePackageUri);
EPackage ePackage = packageCache.get(packageUri);
EClass model = EMFUtil.createEClass(modelClassName, ePackage,
(ec) -> createEClassAnnotations(modelName, timestamp), ProviderPackage.Literals.PROVIDER);
models.put(modelName, model);
return model;
}
private EReference doCreateService(EClass model, String name, Instant timestamp) {
EPackage ePackage = model.getEPackage();
EClass service = EMFUtil.createEClass(constructServiceEClassName(model.getName(), name), ePackage,
(ec) -> createEClassAnnotations(timestamp), ProviderPackage.Literals.SERVICE);
ServiceReference ref = EMFUtil.createServiceReference(model, name, service, true);
EMFUtil.fillMetadata(ref, timestamp, false, name, List.of());
return ref;
}
private List<EAnnotation> createEClassAnnotations(Instant timestamp) {
AnnotationMetadata meta = MetadataFactory.eINSTANCE.createAnnotationMetadata();
meta.setTimestamp(timestamp);
EAnnotation annotation = EMFUtil.createEAnnotation("metadata", Collections.singletonList(meta));
return Collections.singletonList(annotation);
}
private List<EAnnotation> createEClassAnnotations(String model, Instant timestamp) {
// TODO make this part of the ModelMetadata?
return List.of(createEClassAnnotations(timestamp).get(0),
EMFUtil.createEAnnotation("model", Map.of("name", model)));
}
/**
* We need a Unique name for the Service Class if they reside in the same
* Package. Thus we create a hopefully unique name.
*
* TODO: Place each Provider in its own Subpackage?
*
* @param providerName
* @param serviceName
* @return
*/
private String constructServiceEClassName(String providerName, String serviceName) {
return firstToUpper(providerName) + firstToUpper(serviceName);
}
private String firstToUpper(String str) {
return str.substring(0, 1).toUpperCase() + str.substring(1);
}
private void saveInstance(EObject p) {
URI baseUri = URI.createFileURI(Path.of(INSTANCES).toAbsolutePath().toString());
URI instanceUri = baseUri.appendSegment(EcoreUtil.getID(p)).appendFileExtension("xmi");
Resource res = resourceSet.createResource(instanceUri);
res.getContents().add(p);
try {
p.eResource().save(Collections.singletonMap(XMLResource.OPTION_SCHEMA_LOCATION, true));
} catch (IOException ex) {
LOG.error("THIS WILL BE A RUNTIME EXCPETION FOR NOW: Error saving provider fro URI: {}", instanceUri, p);
throw new RuntimeException(ex);
}
}
public Map<String, Object> getResourceMetadata(Provider provider, EStructuralFeature svcFeature,
final ETypedElement rcFeature) {
final Service svc = (Service) provider.eGet(svcFeature);
if (svc == null) {
return Map.of();
}
final ResourceMetadata metadata = (ResourceMetadata) svc.getMetadata().get(rcFeature);
if (metadata == null) {
return Map.of();
} else {
return toMetadataMap(rcFeature, metadata);
}
}
private Map<String, Object> toMetadataMap(final ETypedElement rcFeature, final ResourceMetadata metadata) {
final Map<String, Object> rcMeta = new HashMap<>();
rcMeta.putAll(EMFUtil.toMetadataAttributesToMap(metadata, rcFeature));
return rcMeta;
}
public void setResourceMetadata(Provider provider, EStructuralFeature svcFeature, ETypedElement resource,
String metadataKey, Object value, Instant timestamp) {
if (metadataKey == null || metadataKey.isEmpty()) {
throw new IllegalArgumentException("Empty metadata key");
}
if (timestamp == null) {
throw new IllegalArgumentException("Invalid timestamp");
}
final Service svc = (Service) provider.eGet(svcFeature);
ResourceMetadata metadata = svc == null ? null : (ResourceMetadata) svc.getMetadata().get(resource);
if (metadata == null) {
throw new IllegalStateException("No existing metadata for resource");
}
Map<String, Object> oldMetadata = toMetadataMap(resource, metadata);
metadata.setTimestamp(timestamp);
metadata.getExtra().stream().filter(fcm -> fcm.getName().equals(metadataKey)).findFirst()
.ifPresentOrElse(fcm -> handleFeatureCustomMetadata(fcm, metadataKey, timestamp, value),
() -> metadata.getExtra()
.add(handleFeatureCustomMetadata(
ProviderFactory.eINSTANCE.createFeatureCustomMetadata(), metadataKey, timestamp,
value)));
Map<String, Object> newMetadata = toMetadataMap(resource, metadata);
notificationAccumulator.get().metadataValueUpdate(EMFUtil.getModelName(provider.eClass()), provider.getId(),
svcFeature.getName(), resource.getName(), oldMetadata, newMetadata, timestamp);
}
private FeatureCustomMetadata handleFeatureCustomMetadata(FeatureCustomMetadata customMetadata, String metadataKey,
Instant timestamp, Object value) {
customMetadata.setName(metadataKey);
customMetadata.setTimestamp(timestamp);
customMetadata.setValue(value);
return customMetadata;
}
public Set<String> getModelNames() {
return Set.copyOf(models.keySet());
}
public Optional<EClass> getModel(String modelName) {
return Optional.ofNullable(models.get(modelName));
}
public boolean registered(EClass eClass) {
return models.containsValue(eClass);
}
private EClass getMandatoryModel(String modelName) {
return getModel(modelName).orElseThrow(() -> new IllegalArgumentException("No model with name " + modelName));
}
public EClass createModel(String modelName, Instant timestamp) {
if (getModel(modelName).isPresent()) {
throw new IllegalArgumentException("There is an existing model with name " + modelName);
}
return createModel(modelName, DEFAULT_URI, timestamp);
}
public EReference createService(EClass model, String service, Instant creationTimestamp) {
if (model.getEStructuralFeature(service) != null) {
throw new IllegalArgumentException(
"There is an existing service with name " + service + " in model " + model);
}
return doCreateService(model, service, creationTimestamp);
}
public Stream<EReference> getServicesForModel(EClass model) {
EClass svcClass = ProviderPackage.Literals.SERVICE;
return model.getEAllReferences().stream().filter(r -> svcClass.isSuperTypeOf(r.getEReferenceType()));
}
private EReference getServiceForModel(EClass model, String serviceName) {
EStructuralFeature feature = model.getEStructuralFeature(serviceName);
EClass serviceEClass = ProviderPackage.Literals.SERVICE;
if (feature != null && (!(feature instanceof EReference)
|| !serviceEClass.isSuperTypeOf(((EReference) feature).getEReferenceType()))) {
throw new IllegalArgumentException("The field " + serviceName + " exists in the model "
+ EMFUtil.getModelName(model) + " and is not a service");
}
return feature == null ? null : (EReference) feature;
}
public Stream<ETypedElement> getResourcesForService(EClass svcClass) {
return Stream.concat(
svcClass.getEAllAttributes().stream()
.filter(o -> o.getEContainingClass().getEPackage() != EcorePackage.eINSTANCE),
svcClass.getEAllOperations().stream()
.filter(o -> o.getEContainingClass().getEPackage() != EcorePackage.eINSTANCE)
.filter(Predicate.not(ProviderPackage.Literals.SERVICE___EIS_SET__ESTRUCTURALFEATURE::equals)));
}
public EOperation createActionResource(EClass serviceEClass, String name, Class<?> type,
List<Entry<String, Class<?>>> namedParameterTypes) {
assertResourceNotExist(serviceEClass, name);
List<ActionParameter> params = namedParameterTypes.stream().map(EMFUtil::createActionParameter)
.collect(Collectors.toList());
Action action = EMFUtil.createAction(serviceEClass, name, type, params);
return action;
}
/**
* Uses the white board to call an action handler
*
* @param provider Provider instance
* @param service Service instance
* @param resource Resource instance
* @param parameters Call parameters
* @return The promise of the result of the action
*/
public Promise<Object> act(Provider provider, EStructuralFeature service, ETypedElement resource,
Map<String, Object> parameters) {
if (whiteboard == null) {
return Promises.failed(new IllegalAccessError("Trying to act on a value without an action handler"));
}
try {
return whiteboard.act(EMFUtil.getModelName(provider.eClass()), provider.getId(), service.getName(),
resource.getName(), parameters);
} catch (Throwable t) {
return Promises.failed(t);
}
}
/**
* Uses the white board to pull the value from a resource external getter and
* updates the twin on success.
*
* @param <T> Resource data type
* @param provider Provider instance
* @param service Service instance
* @param resource Resource instance
* @param valueType Resource data class
* @param cachedValue Current twin value
* @return The promise of the new value
*/
public <T> Promise<TimedValue<T>> pullValue(Provider provider, EStructuralFeature service, ETypedElement resource,
Class<T> valueType, TimedValue<T> cachedValue) {
if (whiteboard == null) {
return Promises.failed(new IllegalAccessError("Trying to pull a value without a pull handler"));
}
try {
final String modelName = EMFUtil.getModelName(provider.eClass());
return whiteboard.pullValue(modelName, provider.getId(), service.getName(), resource.getName(), valueType,
cachedValue, (tv) -> {
if (tv != null) {
handleDataUpdate(modelName, provider, service, (EStructuralFeature) resource, tv.getValue(),
tv.getTimestamp());
}
});
} catch (Throwable t) {
return Promises.failed(t);
}
}
/**
* Uses the white board to push the value to a resource external setter and
* updates the twin on success.
*
* @param <T> Resource data type
* @param provider Provider instance
* @param service Service instance
* @param resource Resource instance
* @param valueType Resource data class
* @param cachedValue Current twin value
* @param newValue New value to be pushed
* @return The promise of a new resource value (can differ from
* <code>newValue</code>)
*/
public <T> Promise<TimedValue<T>> pushValue(Provider provider, EStructuralFeature service, ETypedElement resource,
Class<T> valueType, TimedValue<T> cachedValue, TimedValue<T> newValue) {
if (whiteboard == null) {
return Promises.failed(new IllegalAccessError("Trying to push a value without a push handler"));
}
try {
final String modelName = EMFUtil.getModelName(provider.eClass());
return whiteboard.pushValue(modelName, provider.getId(), service.getName(), resource.getName(), valueType,
cachedValue, newValue, (tv) -> {
if (tv != null) {
handleDataUpdate(modelName, provider, service, (EStructuralFeature) resource, tv.getValue(),
tv.getTimestamp());
}
});
} catch (Throwable t) {
return Promises.failed(t);
}
}
public void deleteProvider(String model, String name) {
String m = getProviderModel(name);
if (m != null) {
if (m.equals(model)) {
providers.remove(name);
notificationAccumulator.get().removeProvider(model, name);
} else {
LOG.warn("Unable to remove the provider {} with model {} as the actual model was {}", name, model, m);
}
} else {
LOG.info("The provider {} does not exist and cannot be removed", name);
}
}
public Provider save(Provider eObject) {
String id = EMFUtil.getProviderName(eObject);
Provider original = providers.get(id);
if (original == null) {
original = doCreateProvider(EMFUtil.getModelName(eObject.eClass()), eObject.eClass(), id, Instant.now(),
eObject.getAdmin() == null);
}
EMFCompareUtil.compareAndSet(eObject, original, notificationAccumulator.get());
return EcoreUtil.copy(original);
}
// private void printProvider(Provider provider) {
//
// Resource resource = resourceSet.createResource(URI.createURI("file://temp.xmi"));
// resource.getContents().add(EcoreUtil.copy(provider));
// try {
// resource.save(System.out, null);
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } finally {
// resourceSet.getResources().remove(resource);
// }
// }
public Provider getProvider(EClass model, String id) {
Provider provider = providers.get(id);
if (provider.eClass() != model) {
throw new IllegalArgumentException("Provider " + id + " does not have the same model expected "
+ EMFUtil.getModelName(model) + ", but provider is " + EMFUtil.getModelName(provider.eClass()));
}
return provider;
}
public EClass registerModel(EClass modelEClass, Instant timestamp) {
if (registered(modelEClass)) {
throw new IllegalArgumentException(
"There is an existing model with name " + EMFUtil.getModelName(modelEClass));
}
models.put(EMFUtil.getModelName(modelEClass), modelEClass);
return null;
}
/**
* @param ePackage a registered {@link EPackage} to scan for registered
* Provideres
*/
public void addEPackage(EPackage ePackage) {
if (ePackage != providerPackage) {
ePackage.getEClassifiers().stream().filter(EClass.class::isInstance).map(EClass.class::cast)
.filter(ProviderPackage.Literals.PROVIDER::isSuperTypeOf)
.forEach(ec -> models.put(EcoreUtil.getURI(ec).toString(), ec));
}
}
/**
* TODO how do we handle existing Instances of such a model?
*
* @param ePackage a registered {@link EPackage} to remove
*/
public void removeEPackage(EPackage ePackage) {
if (ePackage != providerPackage) {
ePackage.getEClassifiers().stream().filter(EClass.class::isInstance).map(EClass.class::cast)
.filter(ProviderPackage.Literals.PROVIDER::isSuperTypeOf)
.forEach(ec -> models.remove(EcoreUtil.getURI(ec).toString()));
}
}
}