aboutsummaryrefslogtreecommitdiffstats
path: root/uni/unibase/src/main/java/com/cablelabs/vcpe/uni/unibase/client/UniClient.java
blob: 08994fe3d80066821a445932bea8286ac820c1cd (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) 2015 CableLabs Inc. 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 com.cablelabs.vcpe.uni.unibase.client;

import com.cablelabs.vcpe.common.Dbg;
import com.cablelabs.vcpe.uni.unibase.model.Uni;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.json.*;
import javax.json.stream.*;
import java.io.StringReader;
import java.util.List;
import javax.xml.bind.DatatypeConverter;

/**
 * Created by steve on 5/28/15.
 */

public class UniClient {

    private String uniMgrServer   = "localhost";
    private String uniMgrPort     = "8181";
    private String uniMgrCfgRESTPath = "/restconf/config/cl-vcpe-mef:unis/";
    private String uniMgrOpRESTPath = "/restconf/operational/cl-vcpe-mef:unis/";

    private Client client; // provided by Jersey

    public UniClient() {
        client = ClientBuilder.newClient();
    }

  //--------------------------------------------------------
    public Response create(Uni uni ) throws Exception
  //--------------------------------------------------------
  // create Uni
    {
        WebTarget target =client.target("http://"+uniMgrServer+":" + uniMgrPort + uniMgrCfgRESTPath);

        String json = uni.toJson();
        Dbg.p("\nUNI Create JSON:");
        Dbg.p(json);

        String uNameAndPass = "admin:admin";
        String authorizationHeaderValue = "Basic " +
                                           DatatypeConverter.printBase64Binary(uNameAndPass.getBytes("UTF-8"));
        Response response = target.path("uni")
                .request(MediaType.APPLICATION_JSON)
                .header("Authorization", authorizationHeaderValue)
                .post(Entity.entity(json, MediaType.APPLICATION_JSON));

        if (response.getStatus() != 200 ) // figure out how to use Status.OK
        {
            // in production you can be more specific based on reponse code, id, etc
            throw new RuntimeException(response.getStatus() + ": there was an error on the server.");
        }
        //return response.readEntity(Uni.class);
        return response;
    }

  //--------------------------------------------------------
    public Response update(Uni uni) throws Exception
  //--------------------------------------------------------
  // create/update Uni.  This seems to be how OLD expects uni creation
    {
        WebTarget target =client.target("http://"+uniMgrServer+":" + uniMgrPort + uniMgrCfgRESTPath);

        String json = uni.toJson();
        Dbg.p("\nUNI Create/Update JSON:");
        Dbg.p(json);

        String uNameAndPass = "admin:admin";
        String authorizationHeaderValue = "Basic " +
                                           DatatypeConverter.printBase64Binary(uNameAndPass.getBytes("UTF-8"));
        Response response = target.path("uni/"+uni.getId())
                .request(MediaType.APPLICATION_JSON)
                .header("Authorization", authorizationHeaderValue)
                .put(Entity.entity(json, MediaType.APPLICATION_JSON));

        if (response.getStatus() != 200 ) // figure out how to use Status.OK
        {
            // in production you can be more specific based on reponse code, id, etc
            throw new RuntimeException(response.getStatus() + ": there was an error on the server.");
        }
        return response;
    }

  //--------------------------------------------------------
    public void delete(String uniId) throws Exception
  //--------------------------------------------------------
  // delete Uni of specified ID
    {
        WebTarget target =client.target("http://"+uniMgrServer+":" + uniMgrPort + uniMgrCfgRESTPath);

        String uNameAndPass = "admin:admin";
        String authorizationHeaderValue = "Basic " +
                DatatypeConverter.printBase64Binary(uNameAndPass.getBytes("UTF-8"));

        Response response = target.path("uni/" + uniId)
                .request(MediaType.APPLICATION_JSON)
                .header("Authorization", authorizationHeaderValue)
                .delete();

        if (response.getStatus() != 200) // figure out how to use Status.OK
        {
            // in production you can be more specific based on reponse code, id, etc
            throw new RuntimeException(response.getStatus() + ": there was an error on the server.");
        }
    }

   //
   // Code from here below requires work in order to work with ODL
   //

  //--------------------------------------------------------
    public Uni get(String uniId) throws Exception
  //--------------------------------------------------------
  // get Uni of specified ID
    {
        WebTarget target =client.target("http://"+uniMgrServer+":" + uniMgrPort + uniMgrOpRESTPath);
        Response response = target.path("uni/" + uniId).request(MediaType.APPLICATION_JSON).get(Response.class);
        if (response.getStatus() != 200) // figure out how to use Status.OK
        {
            // in production you can be more specific based on reponse code, id, etc
            throw new RuntimeException(response.getStatus() + ": there was an error on the server.");
        }

        Uni uni = new Uni();
        uni.setId(uniId);
        String uniJson = response.readEntity(String.class);
        return JsonToUni(uni, uniJson);
    }


  //--------------------------------------------------------
    public Uni JsonToUni(Uni uni, String uniJson) {
  //--------------------------------------------------------
  // Parser utility encapsulation

        try {
            JsonParser parser = Json.createParser(new StringReader(uniJson));
            JsonParser.Event event = null;

            while (parser.hasNext()) {
                event = parser.next();
                switch(event) {
                   case KEY_NAME:
                      if (parser.getString().equals("speed")) {
                          event = parser.next();
                          if (parser.getString().equals("speed-10M")) {
                              uni.setSpeed(Uni.SvcSpeed.TEN_MEG);
                          } else
                          if (parser.getString().equals("speed-100M")) {
                              uni.setSpeed(Uni.SvcSpeed.HUNDRED_MEG);
                          } else
                          if (parser.getString().equals("speed-1G")) {
                              uni.setSpeed(Uni.SvcSpeed.ONE_GIG);
                          } else
                          if (parser.getString().equals("speed-10G")) {
                              uni.setSpeed(Uni.SvcSpeed.TEN_GIG);
                          } else {
                              uni.setSpeed(Uni.SvcSpeed.UNASSIGNED);
                          }
                      }
                      if (parser.getString().equals("mac-layer")) {
                          event = parser.next();
                          uni.setMacLayer(Uni.MacLayer.UNASSIGNED);
                          if (parser.getString().equals("IEEE 802.3-2005")) {
                              uni.setMacLayer(Uni.MacLayer.IEEE_802_3);
                          }
                      }
                      if (parser.getString().equals("physical-medium")) {
                          event = parser.next();
                          if (parser.getString().equals("10BASE-T")) {
                              uni.setPhysicalMedium(Uni.PhysMedium.TEN_BASE_T);
                          } else
                          if (parser.getString().equals("100BASE-T")) {
                              uni.setPhysicalMedium(Uni.PhysMedium.HUNDERED_BASE_T);
                          } else
                          if (parser.getString().equals("100BASE-T")) {
                              uni.setPhysicalMedium(Uni.PhysMedium.GIG_BASE_T);
                          } else
                          if (parser.getString().equals("10GBASE-T")) {
                              uni.setPhysicalMedium(Uni.PhysMedium.TEN_GIG_BASE_T);
                          } else {
                              uni.setPhysicalMedium(Uni.PhysMedium.UNASSIGNED);
                          }
                      }
                      if (parser.getString().equals("mtu-size")) {
                          uni.setMtuSize(parser.getLong());
                          event = parser.next();
                      }
                      if (parser.getString().equals("type")) {
                          event = parser.next();
                          uni.setType(Uni.Type.UNASSIGNED);
                          if (parser.getString().equals("IEEE 802.3-2005")) {
                              uni.setType(Uni.Type.UNITYPE);
                          }
                      }
                      if (parser.getString().equals("mac-address")) {
                          uni.setMacAddress(parser.getString());
                          event = parser.next();
                      }
                      if (event.toString().equals("ip-address")) {
                          uni.setIpAddress(parser.getString());
                          event = parser.next();
                      }
                      if (event.toString().equals("mode")) {
                          event = parser.next();
                          if (parser.getString().equals("syncEnabled")) {
                              uni.setMode(Uni.SyncMode.ENABLED);
                          } else
                          if (parser.getString().equals("syncDisabled")) {
                              uni.setMode(Uni.SyncMode.DISABLED);
                          } else {
                              uni.setMode(Uni.SyncMode.UNASSIGNED);
                          }
                      }
                      break;
                   default:
                      break;
                }
            }
        } catch(Exception e) {
            Dbg.p("\n, JsonReader:" + e);
        }
        return uni;
    }


  //--------------------------------------------------------
    public List<Uni> getAll()
  //--------------------------------------------------------
  // get a list of all Uni instances
    {
        WebTarget target = client.target("http://localhost:9090/unimgr/webapi/");

        // Can I do this with a Response, so that I can check for errors
        List<Uni> response = target.path("uni/list")
                                   .request(MediaType.APPLICATION_JSON)
                                   .get(new GenericType<List<Uni>>() {
                                   });
        if (response == null) // figure out how to use Status.OK
        {
            // in production you can be more specific based on reponse code, id, etc
            throw new RuntimeException("there was an error on the server.");
        }
        return response;
    }

  //--------------------------------------------------------
    public Uni testGet()
  //--------------------------------------------------------
  // test marshaling of Uni class from server json
    {
        WebTarget target = client.target("http://localhost:9090/unimgr/webapi/");

        Response response = target.path("uni").request(MediaType.APPLICATION_JSON).get(Response.class);
        if (response.getStatus() != 200) // figure out how to use Status.OK
        {
            // in production you can be more specific based on reponse code, id, etc
            throw new RuntimeException(response.getStatus() + ": there was an error on the server.");
        }

        //return response;
        return response.readEntity(Uni.class);
    }

  //--------------------------------------------------------
    public String ping()
  //--------------------------------------------------------
  // test connectivity
    {

            WebTarget target = client.target("http://localhost:9090/unimgr/webapi/");

            Response response = target.path("uni").request(MediaType.TEXT_PLAIN).get();
            if (response.getStatus() != 200) // figure out how to use Status.OK
            {
                // in production you can be more specific based on reponse code, id, etc
                throw new RuntimeException(response.getStatus() + ": there was an error on the server.");
            }


            return response.readEntity(String.class);
    }
}