m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java
blob: 0bf4338d5e3bc4a3d08a25229ddadbb8b7fa7635 (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
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
package pl.edu.mimuw.cloudatlas.agent;

import java.io.PrintStream;

import java.rmi.RemoteException;

import java.util.concurrent.CompletableFuture;
import java.util.List;
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

import pl.edu.mimuw.cloudatlas.agent.messages.*;
import pl.edu.mimuw.cloudatlas.interpreter.Interpreter;
import pl.edu.mimuw.cloudatlas.interpreter.InterpreterException;
import pl.edu.mimuw.cloudatlas.interpreter.Main;
import pl.edu.mimuw.cloudatlas.interpreter.QueryResult;
import pl.edu.mimuw.cloudatlas.model.*;
import pl.edu.mimuw.cloudatlas.api.Api;
import pl.edu.mimuw.cloudatlas.querysigner.QueryData;
import pl.edu.mimuw.cloudatlas.querysigner.QueryUtils;

public class NewApiImplementation implements Api {
    private EventBus eventBus;

    public NewApiImplementation(EventBus eventBus) {
        this.eventBus = eventBus;
    }

    public Set<String> getZoneSet() throws RemoteException {
        CompletableFuture<ResponseMessage> responseFuture = new CompletableFuture();
        RequestStateMessage message = new RequestStateMessage("", 0, responseFuture);
        try {
            eventBus.addMessage(message);
            ResponseMessage response = responseFuture.get();

            if (response.getType() == ResponseMessage.Type.STATE) {
                StateMessage stateMessage = (StateMessage) response;
                Set<String> zones = new HashSet<String>();
                collectZoneNames(stateMessage.getZMI(), zones);
                return zones;
            } else {
                System.out.println("ERROR: getZoneSet didn't receive a StateMessage");
                throw new Exception("Failed to retrieve zone set");
            }
        } catch (Exception e) {
            System.out.println("ERROR: exception caught in getZoneSet");
            throw new RemoteException(e.getMessage());
        }
    }

    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 {
        CompletableFuture<ResponseMessage> responseFuture = new CompletableFuture();
        RequestStateMessage message = new RequestStateMessage("", 0, responseFuture);
        try {
            eventBus.addMessage(message);
            ResponseMessage response = responseFuture.get();

            if (response.getType() == ResponseMessage.Type.STATE) {
                StateMessage stateMessage = (StateMessage) response;
                return stateMessage.getZMI().findDescendant(zoneName).getAttributes();
            } else {
                System.out.println("ERROR: getZoneSet didn't receive a StateMessage");
                throw new Exception("Failed to retrieve zone set");
            }
        } catch (Exception e) {
            System.out.println("ERROR: exception caught in getZoneSet");
            throw new RemoteException(e.getMessage());
        }
    }

    public void installQuery(String name, QueryData query) throws RemoteException {
        QueryUtils.validateQueryName(name);
        try {
            Attribute attributeName = new Attribute(name);
            ValueTime timestamp = new ValueTime(System.currentTimeMillis());
            Map<Attribute, Entry<ValueQuery, ValueTime>> queries = new HashMap();
            queries.put(attributeName, new SimpleImmutableEntry(new ValueQuery(query), timestamp));
            UpdateQueriesMessage message = new UpdateQueriesMessage("", 0, queries);
            eventBus.addMessage(message);
        } catch (Exception e) {
            throw new RemoteException("Failed to install query", e);
        }
    }

    public void uninstallQuery(String queryName, QueryData query) throws RemoteException {
        QueryUtils.validateQueryName(queryName);
        try {
            Attribute attributeName = new Attribute(queryName);
            ValueTime timestamp = new ValueTime(System.currentTimeMillis());
            Map<Attribute, Entry<ValueQuery, ValueTime>> queries = new HashMap();
            queries.put(attributeName, new SimpleImmutableEntry(null, timestamp));
            UpdateQueriesMessage message = new UpdateQueriesMessage("", 0, queries);
            eventBus.addMessage(message);
        } catch (Exception e) {
            System.out.println("ERROR: failed to remove query");
            throw new RemoteException("Failed to uninstall query", e);
        }
    }

    public void setAttributeValue(String zoneName, String attributeName, Value value) throws RemoteException {
        try {
            SetAttributeMessage message = new SetAttributeMessage("", 0, zoneName, new Attribute(attributeName), value, new ValueTime(System.currentTimeMillis()));
            eventBus.addMessage(message);
        } catch (Exception e) {
            System.out.println("ERROR: failed to set attribute");
            throw new RemoteException("Failed to set attribute", e);
        }
    }

    public void setFallbackContacts(Set<ValueContact> contacts) throws RemoteException {
        try {
            UpdateContactsMessage message = new UpdateContactsMessage("", System.currentTimeMillis(), contacts);
            eventBus.addMessage(message);
        } catch (Exception e) {
            System.out.println("ERROR: failed to set contacts");
            throw new RemoteException("Failed to set contacts", e);
        }
    }
}