m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java
blob: 12682a6536a4ed14d8354d2f4325e2a1329ab5be (plain)
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
package pl.edu.mimuw.cloudatlas.agent;

import java.rmi.RemoteException;

import java.util.Set;
import java.util.HashSet;

import pl.edu.mimuw.cloudatlas.model.Attribute;
import pl.edu.mimuw.cloudatlas.model.AttributesMap;
import pl.edu.mimuw.cloudatlas.model.PathName;
import pl.edu.mimuw.cloudatlas.model.ValueContact;
import pl.edu.mimuw.cloudatlas.model.Value;
import pl.edu.mimuw.cloudatlas.model.ValueQuery;
import pl.edu.mimuw.cloudatlas.model.ValueSet;
import pl.edu.mimuw.cloudatlas.model.ValueNull;
import pl.edu.mimuw.cloudatlas.model.TypePrimitive;
import pl.edu.mimuw.cloudatlas.model.ZMI;
import pl.edu.mimuw.cloudatlas.api.Api;

public class ApiImplementation implements Api {
    ZMI root;
    Set<ValueContact> contacts;

    public ApiImplementation(ZMI root) {
        this.root = root;
        this.contacts = new HashSet<ValueContact>();
    }

    public Set<String> getZoneSet() throws RemoteException {
        Set<String> zones = new HashSet<String>();
        collectZoneNames(root, zones);
        return zones;
    }

    private void collectZoneNames(ZMI zone, Set<String> names) {
        names.add(zone.getPathName().toString());
        for (ZMI son : zone.getSons()) {
            collectZoneNames(son, names);
        }
    }

    public AttributesMap getZoneAttributeValues(String zoneName) throws RemoteException {
        try {
            ZMI zmi = root.findDescendant(new PathName(zoneName));
            return zmi.getAttributes();
        } catch (ZMI.NoSuchZoneException e) {
            throw new RemoteException("Zone not found", e);
        }
    }

    public void installQuery(String name, String queryCode) throws RemoteException {
        try {
            ValueQuery query = new ValueQuery(queryCode);
            Attribute attributeName = new Attribute(name);
            installQueryInHierarchy(root, attributeName, query);
        } catch (Exception e) {
            throw new RemoteException("Failed to install query", e);
        }
    }

    private void installQueryInHierarchy(ZMI zmi, Attribute queryName, ValueQuery query) {
        if (!zmi.getSons().isEmpty()) {
            zmi.getAttributes().addOrChange(queryName, query);
            for (ZMI son : zmi.getSons()) {
                installQueryInHierarchy(son, queryName, query);
            }
        }
    }

    public void uninstallQuery(String queryName) throws RemoteException {
        uninstallQueryInHierarchy(root, new Attribute(queryName));
    }

    private void uninstallQueryInHierarchy(ZMI zmi, Attribute queryName) {
        if (!zmi.getSons().isEmpty()) {
            zmi.getAttributes().remove(queryName);
            for (ZMI son : zmi.getSons()) {
                uninstallQueryInHierarchy(son, queryName);
            }
        }
    }

    public void setAttributeValue(String zoneName, String attributeName, Value value) throws RemoteException {
        try {
            ZMI zmi = root.findDescendant(new PathName(zoneName));
            zmi.getAttributes().addOrChange(new Attribute(attributeName), value);
        } catch (ZMI.NoSuchZoneException e) {
            throw new RemoteException("Zone not found", e);
        }
    }

    public void setFallbackContacts(Set<ValueContact> contacts) throws RemoteException {
        this.contacts = contacts;
    }
}