aboutsummaryrefslogtreecommitdiffstats
path: root/uni/unibase/src/main/java/com/cablelabs/vcpe/uni/unibase/client/EvcPathClient.java
blob: 50feb6cb1d574fd4e8a9e49e5293d6207777a891 (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
/*******************************************************************************
* 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.EvcPath;
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.xml.bind.DatatypeConverter;
import java.util.List;

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

public class EvcPathClient {

    // private String evcMgrServer   = "10.36.0.20";
    private String evcMgrServer   = "localhost";
    private String evcMgrPort     = "8181";
    private String evcMgrCfgRESTPath = "/restconf/config/network-topology:network-topology/topology/unimgr:evc/link/";

    private Client client; // provided by Jersey

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

  //--------------------------------------------------------
    public Response create(EvcPath evcPath )
  //--------------------------------------------------------
    {
        WebTarget target =client.target("http://"+evcMgrServer+":" + evcMgrPort + evcMgrCfgRESTPath);

        String json = evcPath.toJson();
        Dbg.p("\nEVC Create JSON:");
        Dbg.p(json);

        Response response = target.path("")
                .request(MediaType.APPLICATION_JSON)
                .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(EvcPath.class);
        return response;
    }

  //--------------------------------------------------------
    public Response update(EvcPath evcPath) throws Exception
  //--------------------------------------------------------
    {
        WebTarget target =client.target("http://"+evcMgrServer+":" + evcMgrPort + evcMgrCfgRESTPath);

        String json = evcPath.toJson();
        String unamepass = "admin:admin";
        String authorizationHeaderValue = "Basic " + DatatypeConverter.printBase64Binary(unamepass.getBytes("UTF-8"));
        Dbg.p("\nEVC Create/Update JSON:");
        Dbg.p(json);

        Response response = target.path(evcPath.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 evcPathId) throws Exception
  //--------------------------------------------------------
  // delete EvcPath of specified ID
    {
        WebTarget target =client.target("http://"+evcMgrServer+":" + evcMgrPort + evcMgrCfgRESTPath);

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

        Response response = target.path(evcPathId)
                .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 EvcPath get(String evcPathId)
  //--------------------------------------------------------
  // get EvcPath of specified ID
    {
        WebTarget target = client.target("http://localhost:9090/evcmgr/webapi/");

        Response response = target.path("evc/"+evcPathId).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.readEntity(EvcPath.class);
    }

  //--------------------------------------------------------
    public List<EvcPath> getAll()
  //--------------------------------------------------------
  // get a list of all EvcPath instances
    {

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

        // Can I do this with a Response, so that I can check for errors
        List<EvcPath> response = target.path("evc/list")
                                   .request(MediaType.APPLICATION_JSON)
                                   .get(new GenericType<List<EvcPath>>() {
                                   });
        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 EvcPath testGet()
  //--------------------------------------------------------
  // test marshaling of EvcPath class from server json
    {
        WebTarget target = client.target("http://localhost:9090/evcmgr/webapi/");

        Response response = target.path("evc").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(EvcPath.class);
    }

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

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

            Response response = target.path("evc").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);
    }
}