summaryrefslogtreecommitdiffstats
path: root/verigraph/src/it/polito/verigraph/grpc/client/ToscaClient.java
blob: dbf84428091e42f64ee1f55cf516d985c1ed4d37 (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
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
/*******************************************************************************
 * Copyright (c) 2018 Politecnico di Torino and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License, Version 2.0
 * which accompanies this distribution, and is available at
 * http://www.apache.org/licenses/LICENSE-2.0
 *******************************************************************************/
package it.polito.verigraph.grpc.client;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.StatusRuntimeException;
import it.polito.verigraph.grpc.GetRequest;
import it.polito.verigraph.grpc.GraphGrpc;
import it.polito.verigraph.grpc.NewGraph;
import it.polito.verigraph.grpc.NewTopologyTemplate;
import it.polito.verigraph.grpc.NodeTemplateGrpc;
import it.polito.verigraph.grpc.RequestID;
import it.polito.verigraph.grpc.Status;
import it.polito.verigraph.grpc.TopologyTemplateGrpc;
import it.polito.verigraph.grpc.ToscaPolicy;
import it.polito.verigraph.grpc.ToscaRequestID;
import it.polito.verigraph.grpc.ToscaTestGrpc;
import it.polito.verigraph.grpc.ToscaVerificationGrpc;
import it.polito.verigraph.grpc.VerigraphGrpc;

public class ToscaClient {

  private final ManagedChannel channel;
  private final VerigraphGrpc.VerigraphBlockingStub blockingStub;

  public ToscaClient(String host, int port) {
    this(ManagedChannelBuilder.forAddress(host, port).usePlaintext(true));
  }

  /** Construct client for accessing toscaVerigraph server using the existing channel. */
  public ToscaClient(ManagedChannelBuilder<?> channelBuilder) {
    channel = channelBuilder.build();
    blockingStub = VerigraphGrpc.newBlockingStub(channel);
  }

  /** Close the channel */
  public void shutdown() throws InterruptedException {
    channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
  }

  /** Obtain a list of the available TopologyTemplates*/
  public List<TopologyTemplateGrpc> getTopologyTemplates(){
    List<TopologyTemplateGrpc> templates = new ArrayList<TopologyTemplateGrpc>();
    GetRequest request = GetRequest.newBuilder().build();
    boolean response_ok = true;

    /*Iterates on received topology templates, prints on log file in case of errors*/
    Iterator<TopologyTemplateGrpc> receivedTemplates;
    try {
      receivedTemplates = blockingStub.getTopologyTemplates(request);
      System.out.println("++ Receiving TopologyTemplates...");
      while(receivedTemplates.hasNext()) {
        TopologyTemplateGrpc received = receivedTemplates.next();
        if(received.getErrorMessage().equals("")) {
          System.out.println("++ Correctly received TopologyTemplate --> id:" + received.getId());
          templates.add(received);
        } else
          System.out.println("-- Received a TopologyTemplate with error: " + received.getErrorMessage());
      }
    } catch (StatusRuntimeException ex) {
      System.out.println("-- RPC failed: " + ex.getMessage());
      response_ok = false;
    }
    if(response_ok) {
      System.out.println("++ All TopologyTemplates correctly received.");
      return templates;
    } else {
      return null; //Function returns null in case of error to differentiate from empty list case.
    }
  }


  /** Obtain a TopologyTemplate by ID */
  public TopologyTemplateGrpc getTopologyTemplate(String id) {
    ToscaRequestID request = ToscaRequestID.newBuilder().setIdTopologyTemplate(id).build();
    TopologyTemplateGrpc response = TopologyTemplateGrpc.newBuilder().build();
    try {
      System.out.println("++ Receiving TopologyTemplate...");
      response = blockingStub.getTopologyTemplate(request);
      if(response.getErrorMessage().equals("")){
        System.out.println("++ Received TopologyTemplate --> id:" + response.getId());
        return response;
      } else {
        System.out.println("-- Error: " + response.getErrorMessage());
        return response;
      }
    } catch (StatusRuntimeException ex) {
      System.out.println("-- RPC failed: " + ex.getStatus());
      return TopologyTemplateGrpc.newBuilder().setErrorMessage(ex.getStatus().getDescription()).build();
    }
  }


  /** Creates a new TopologyTemplate, takes in input a TopologyTemplateGrpc  */
  public NewTopologyTemplate createTopologyTemplate(TopologyTemplateGrpc topol) {
    try {
      //Sending new Topology and analyzing response
      System.out.println("++ Sending the new TopologyTemplate...");
      NewTopologyTemplate response = blockingStub.createTopologyTemplate(topol);
      if(response.getSuccess())
        System.out.println("++ TopologyTemplate successfully created with id: "+ response.getTopologyTemplate().getId());
      else
        System.out.println("-- TopologyTemplate creation failed: " + response.getErrorMessage());
      return response;

    } catch (StatusRuntimeException ex) {
      System.out.println("-- RPC failed: " + ex.getStatus());
      return NewTopologyTemplate.newBuilder().setSuccess(false).setErrorMessage(ex.getStatus().getDescription()).build();
      //getDescription may be empty
    }
  }


  /** Update a TopologyTemplate, takes in input a TopologyTemplateGrpc and the Topology's ID to be updated*/
  public NewTopologyTemplate updateTopologyTemplate(TopologyTemplateGrpc topol, String id) {
    //Checking if the inserted string is an object
    try {
      Long.valueOf(id);
    } catch (NumberFormatException ex) {
      System.out.println("-- The ID must a number according to Verigraph implementation.");
      return NewTopologyTemplate.newBuilder().setSuccess(false)
              .setErrorMessage("The ID must a number according to Verigraph implementation.").build();
    }

    //Update the topology ID
    TopologyTemplateGrpc.Builder updTopol = TopologyTemplateGrpc.newBuilder();
    try {
      updTopol.setId(id)
      .addAllNodeTemplate(topol.getNodeTemplateList())
      .addAllRelationshipTemplate(topol.getRelationshipTemplateList());
    } catch (Exception ex) {
      System.out.println("-- Error: Incorrect fields implementation.");
      return NewTopologyTemplate.newBuilder().setSuccess(false).setErrorMessage("Error: Incorrect fields implementation.").build();
    }

    //Sending updated Topology and analyzing response
    try {

      System.out.println("++ Sending the updated TopologyTemplate...");
      NewTopologyTemplate response = blockingStub.updateTopologyTemplate(updTopol.build());
      if(response.getSuccess())
        System.out.println("++ TopologyTemplate successfully updated.");
      else
        System.out.println("-- TopologyTemplate not updated: " + response.getErrorMessage());
      return response;

    } catch (StatusRuntimeException ex) {
      System.out.println("-- RPC failed: " + ex.getStatus());
      return NewTopologyTemplate.newBuilder().setSuccess(false).setErrorMessage(ex.getStatus().getDescription()).build();
    }
  }


  /** Delete a TopologyTemplate by ID */
  public Status deleteTopologyTemplate(String id) {
    try {
      Long.valueOf(id);
    } catch (NumberFormatException ex) {
      System.out.println("-- The ID must a number according to Verigraph implementation.");
      return Status.newBuilder().setSuccess(false)
              .setErrorMessage("The ID must a number according to Verigraph implementation.").build();
    }
    ToscaRequestID request = ToscaRequestID.newBuilder().setIdTopologyTemplate(id).build();
    try {
      System.out.println("++ Sending delete request...");
      Status response = blockingStub.deleteTopologyTemplate(request);
      if(response.getSuccess())
        System.out.println("++ TopologyTemplate successfully deleted.");
      else
        System.out.println("-- Error deleting TopologyTemplate : " + response.getErrorMessage());
      return response;

    } catch (StatusRuntimeException ex) {
      System.out.println("-- RPC failed: " + ex.getStatus());
      return Status.newBuilder().setSuccess(false).setErrorMessage(ex.getStatus().getDescription()).build();
    }
  }


  /** VerifyPolicy */
  public ToscaVerificationGrpc verifyPolicy(ToscaPolicy policy){
    ToscaVerificationGrpc response;
    try {
      System.out.println("++ Sending ToscaPolicy...");
      response = blockingStub.verifyToscaPolicy(policy);
      if(!response.getErrorMessage().equals("")){
        System.out.println("-- Error in operation: " + response.getErrorMessage());
      }
      else {
        System.out.println("++ Result: " + response.getResult());
        System.out.println("++ Comment: " + response.getComment());

        for(ToscaTestGrpc test : response.getTestList()){
          System.out.println("++ Traversed nodes:");
          for(NodeTemplateGrpc node : test.getNodeTemplateList()){
            System.out.println("\t Node "+node.getName());
          }
        }
      }
      return response;
    } catch (StatusRuntimeException e) {
      System.out.println("-- RPC failed: " + e.getStatus());
      return ToscaVerificationGrpc.newBuilder().setSuccessOfOperation(false)
              .setErrorMessage(e.getStackTrace().toString()).build();
    }
  }


  //Methods added for backward compatibility with JSON grpc, only create and update methods need to be redefined
  //The reason is that the tosca Grpc converter requires a coherent numbering of IDs while the previous
  //implementations exploits names to identify nodes and can considers IDs as not strictly required attributes.

  public NewGraph createGraph(GraphGrpc gr) {
    NewGraph response;
    try {
      response = blockingStub.createGraph(gr);
      if(response.getSuccess())
        System.out.println("++ TopologyTemplate successfully created with id: "+ response.getGraph().getId());
      else
        System.out.println("-- TopologyTemplate creation failed: " + response.getErrorMessage());
      return response;
    } catch (StatusRuntimeException e) {
      System.err.println("-- RPC failed: " + e.getStatus());
      return NewGraph.newBuilder().setSuccess(false).setErrorMessage(e.getStatus().getDescription()).build();
    }

  }

  public NewGraph updateGraph(long idGraph, GraphGrpc newGraph) {

    GraphGrpc gr = GraphGrpc.newBuilder(newGraph).setId(idGraph).build();
    NewGraph response;
    try {
      response = blockingStub.updateGraph(gr);
      if(response.getSuccess())
        System.out.println("++ TopologyTemplate successfully created with id: "+ response.getGraph().getId());
      else
        System.out.println("-- TopologyTemplate creation failed: " + response.getErrorMessage());
      return response;
    } catch (StatusRuntimeException e) {
      System.err.println("-- RPC failed: " + e.getStatus());
      return NewGraph.newBuilder().setSuccess(false).setErrorMessage(e.getStatus().getDescription()).build();
    }
  }

  public GraphGrpc getGraph(long idGraph) {

    RequestID request = RequestID.newBuilder().setIdGraph(idGraph).build() ;
    try {
      System.out.println("++ Receiving TopologyTemplate...");
      GraphGrpc graph = blockingStub.getGraph(request);
      System.out.println("++ Received TopologyTemplate --> id:" + graph.getId());
      if(!graph.getErrorMessage().equals("")){
        System.out.println("-- Error : " + graph.getErrorMessage());
        return graph;
      }
      return graph;
    } catch (StatusRuntimeException ex) {
      System.err.println("-- RPC failed: " + ex.getStatus());
      return null;
    }
  }


  public List<GraphGrpc> getGraphs() {
    List<GraphGrpc> graphsReceived = new ArrayList<GraphGrpc>();
    GetRequest request = GetRequest.newBuilder().build();
    Iterator<GraphGrpc> graphs;
    try {
      graphs = blockingStub.getGraphs(request);
      System.out.println("++ Receiving TopologyTemplates...");
      while (graphs.hasNext()) {
        GraphGrpc graph = graphs.next();
        if(graph.getErrorMessage().equals("")){
          System.out.println("++ Correctly received graph --> id:" + graph.getId());
          graphsReceived.add(graph);
        }else{
          System.out.println("-- Received a graph with error : " + graph.getErrorMessage());
        }
      }
    } catch (StatusRuntimeException ex) {
      System.err.println("-- RPC failed : " + ex.getStatus());
      return null;
    }
    return graphsReceived;
  }
}