blob: d327dae330d18b0f15969b0f2eba5ece69a5cd57 [file] [log] [blame]
Hyunsun Moon7ad92202016-04-20 10:36:02 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.xosclient.impl;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.google.common.base.Strings;
21import com.google.common.collect.Sets;
22import org.onosproject.xosclient.api.VtnServiceApi;
23import org.onosproject.xosclient.api.XosAccess;
24import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
26
27import java.io.IOException;
28import java.util.Iterator;
29import java.util.Map;
30import java.util.Set;
31
32import static com.google.common.base.Preconditions.checkArgument;
33import static com.google.common.base.Preconditions.checkNotNull;
34
35/**
36 * Provides CORD VTN service and service dependency APIs.
37 */
38public final class DefaultVtnServiceApi extends XosApi implements VtnServiceApi {
39
40 private final Logger log = LoggerFactory.getLogger(getClass());
41
42 private static DefaultVtnServiceApi instance = null;
43
44 /**
45 * Default constructor.
46 */
47 private DefaultVtnServiceApi(String baseUrl, XosAccess access) {
48 super(baseUrl, access);
49 }
50
51 /**
52 * Returns VTN service API instance. Creates a new instance only if base url or
53 * access has been changed.
54 *
55 * @param baseUrl base url
56 * @param access access
57 * @return vtn service api
58 */
59 public static synchronized DefaultVtnServiceApi getInstance(String baseUrl, XosAccess access) {
60 checkNotNull(access, "XOS access information is null");
61 checkArgument(!Strings.isNullOrEmpty(baseUrl), "VTN service API base url is null or empty");
62
63 if (instance == null ||
64 !instance.baseUrl.equals(baseUrl) ||
65 !instance.access.equals(access)) {
66 instance = new DefaultVtnServiceApi(baseUrl, access);
67 }
68 return instance;
69 }
70
71 @Override
72 public Set<String> services() {
73 String response = restGet(EMPTY_STRING);
74 log.trace("Get services {}", response);
75
76 ObjectMapper mapper = new ObjectMapper();
77 try {
78 JsonNode nodes = mapper.readTree(response);
79 return Sets.newHashSet(nodes.fieldNames());
80 } catch (IOException e) {
81 log.warn("Failed to get service list");
82 return Sets.newHashSet();
83 }
84 }
85
86 @Override
87 public Set<String> getProviderServices(String tServiceId) {
88 checkNotNull(tServiceId);
89
90 String response = restGet(tServiceId);
91 log.trace("Get provider services {}", response);
92
93 ObjectMapper mapper = new ObjectMapper();
94 Set<String> pServices = Sets.newHashSet();
95
96 try {
97 JsonNode nodes = mapper.readTree(response);
98 nodes.forEach(node -> pServices.add(node.asText()));
99 } catch (IOException e) {
100 log.warn("Failed to get service dependency");
101 }
102 return pServices;
103 }
104
105 @Override
106 public Set<String> getTenantServices(String tServiceId) {
107 checkNotNull(tServiceId);
108
109 String response = restGet(EMPTY_STRING);
110 log.trace("Get tenant services {}", response);
111
112 ObjectMapper mapper = new ObjectMapper();
113 Set<String> tServices = Sets.newHashSet();
114 try {
115 JsonNode nodes = mapper.readTree(response);
116 Iterator<Map.Entry<String, JsonNode>> iterator = nodes.fields();
117
118 while (iterator.hasNext()) {
119 Map.Entry<String, JsonNode> entry = iterator.next();
120 entry.getValue().forEach(pService -> {
121 if (pService.asText().equals(tServiceId)) {
122 tServices.add(entry.getKey());
123 }
124 });
125 }
126 } catch (IOException e) {
127 log.warn("Failed to get service list");
128 }
129 return tServices;
130 }
131}