2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.sfc.tacker.api;
11 import com.google.common.base.Preconditions;
12 import com.google.gson.Gson;
13 import com.google.gson.GsonBuilder;
14 import com.google.gson.JsonObject;
15 import java.util.Date;
16 import javax.ws.rs.client.Client;
17 import javax.ws.rs.client.ClientBuilder;
18 import javax.ws.rs.client.Entity;
19 import javax.ws.rs.client.WebTarget;
20 import javax.ws.rs.core.MediaType;
21 import javax.ws.rs.core.Response;
22 import org.glassfish.jersey.client.ClientProperties;
23 import org.opendaylight.sfc.tacker.dto.Attributes;
24 import org.opendaylight.sfc.tacker.dto.Auth;
25 import org.opendaylight.sfc.tacker.dto.KeystoneRequest;
26 import org.opendaylight.sfc.tacker.dto.TackerError;
27 import org.opendaylight.sfc.tacker.dto.TackerRequest;
28 import org.opendaylight.sfc.tacker.dto.TackerResponse;
29 import org.opendaylight.sfc.tacker.dto.Token;
30 import org.opendaylight.sfc.tacker.dto.Vnf;
31 import org.opendaylight.sfc.tacker.util.DateDeserializer;
32 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sf.rev140701.service.functions.ServiceFunction;
33 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sft.rev140701.service.function.types.ServiceFunctionType;
34 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.ss.rev140701.service.statistics.group.StatisticByTimestamp;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public final class TackerManager implements AutoCloseableSfcVnfManager {
40 private static final Logger LOG = LoggerFactory.getLogger(TackerManager.class);
41 private static final DateDeserializer DATE_DESERIALIZER = new DateDeserializer();
42 private static final Gson GSON = new GsonBuilder().registerTypeAdapter(Date.class, DATE_DESERIALIZER).create();
43 private static final Integer CONNECT_TIMEOUT_MILLISEC = 7000;
44 private static final Integer READ_TIMEOUT_MILLISEC = 5000;
45 private final Client client;
46 private final String baseUri;
47 private final int tackerPort;
48 private final int keystonePort;
50 private final Auth auth;
52 private TackerManager(TackerManagerBuilder builder) {
53 Preconditions.checkNotNull(builder.getBaseUri());
54 Preconditions.checkArgument(builder.getTackerPort() != 0);
55 Preconditions.checkArgument(builder.getKeystonePort() != 0);
56 Preconditions.checkNotNull(builder.getAuth());
58 this.baseUri = builder.getBaseUri();
59 this.tackerPort = builder.getTackerPort();
60 this.keystonePort = builder.getKeystonePort();
61 this.auth = builder.getAuth();
63 client = ClientBuilder.newBuilder().property(ClientProperties.READ_TIMEOUT, READ_TIMEOUT_MILLISEC)
64 .property(ClientProperties.CONNECT_TIMEOUT, CONNECT_TIMEOUT_MILLISEC).build();
68 public boolean createSf(ServiceFunctionType sfType) {
70 Token authToken = getToken();
71 if (authToken == null) {
72 LOG.error("Failed to Acquire Authentication token!");
76 WebTarget webTarget = client.target(baseUri + ":" + tackerPort).path("/v1.0/vnfs");
77 TackerRequest tackerRequest = TackerRequest.builder()
79 .setName(sfType.getType().getValue())
80 .setAttributes(Attributes.builder().setServiceType(sfType.getType().getValue()).build())
84 Response response = webTarget.request(MediaType.APPLICATION_JSON)
85 .header("X-Auth-Token", authToken.getId())
86 .header("X-Auth-Project-Id", authToken.getTenant().getName()).post(Entity.entity(
87 GSON.toJson(tackerRequest), MediaType.APPLICATION_JSON));
89 if (response != null) {
90 switch (response.getStatus()) {
92 String json = response.readEntity(String.class);
93 TackerResponse tackerResponse = GSON.fromJson(json, TackerResponse.class);
94 LOG.info("VNF successfully created.");
95 LOG.debug(GSON.toJson(tackerResponse));
98 LOG.debug("Unauthorized! Wrong username or password.");
101 TackerError error = GSON.fromJson(response.readEntity(String.class), TackerError.class);
102 LOG.debug(error.toString());
110 public boolean deleteSf(ServiceFunction sf) {
112 Token authToken = getToken();
113 if (authToken == null) {
114 LOG.error("Failed to Acquire Authentication token!");
118 String vnfId = sf.getName().getValue();
119 WebTarget webTarget = client.target(baseUri + ":" + tackerPort).path("/v1.0/vnfs/" + vnfId);
120 Response response = webTarget.request(MediaType.APPLICATION_JSON)
121 .header("X-Auth-Token", authToken.getId())
122 .header("X-Auth-Project-Id", authToken.getTenant().getName())
125 if (response != null) {
126 switch (response.getStatus()) {
128 LOG.info("VNF:" + vnfId + " successfully deleted.");
131 LOG.debug("404 - Not Found:" + response.toString());
134 LOG.debug("405 - Method not found: " + response.toString());
137 TackerError error = GSON.fromJson(response.readEntity(String.class), TackerError.class);
138 LOG.debug(error.toString());
146 public StatisticByTimestamp getSfStatistics(ServiceFunction sf) {
147 // TODO implement method
152 public void close() {
156 private Token getToken() {
157 if (this.token == null) {
158 this.token = requestToken();
160 if (this.token != null) {
161 Date currentDate = new Date();
162 if (!(currentDate.getTime() < token.getExpires().getTime()
163 && currentDate.getTime() >= token.getIssuedAt().getTime())) {
170 private Token requestToken() {
171 WebTarget webTarget = client.target(baseUri + ":" + keystonePort).path("/v2.0/tokens");
172 KeystoneRequest keystoneRequest = new KeystoneRequest(this.auth);
174 Response response = webTarget.request(MediaType.APPLICATION_JSON)
175 .post(Entity.entity(GSON.toJson(keystoneRequest), MediaType.APPLICATION_JSON));
177 if (response != null) {
178 switch (response.getStatus()) {
180 String json = response.readEntity(String.class);
181 JsonObject jsonObject =
182 GSON.fromJson(json, JsonObject.class).getAsJsonObject("access").getAsJsonObject("token");
184 LOG.debug("Authentication token successfully created.");
185 return GSON.fromJson(jsonObject, Token.class);
187 LOG.debug(response.readEntity(String.class));
194 public static TackerManagerBuilder builder() {
195 return new TackerManagerBuilder();
198 public static class TackerManagerBuilder {
200 private String baseUri;
201 private int tackerPort;
202 private int keystonePort;
205 public String getBaseUri() {
209 public TackerManagerBuilder setBaseUri(String baseUri) {
210 this.baseUri = baseUri;
214 public int getTackerPort() {
218 public TackerManagerBuilder setTackerPort(int tackerPort) {
219 this.tackerPort = tackerPort;
223 public int getKeystonePort() {
227 public TackerManagerBuilder setKeystonePort(int keystonePort) {
228 this.keystonePort = keystonePort;
232 public Auth getAuth() {
236 public TackerManagerBuilder setAuth(Auth auth) {
241 public TackerManager build() {
242 return new TackerManager(this);