blob: 98fc0a97a1421896df03b6d0b7d20627990f81ee [file] [log] [blame]
Jian Li091d8d22018-02-20 10:42:06 +09001/*
2 * Copyright 2018-present Open Networking Foundation
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.openstacknetworking.util;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
Jian Lieb9f77d2018-02-20 11:25:45 +090020import com.fasterxml.jackson.databind.node.ObjectNode;
Jian Li1064e4f2018-05-29 16:16:53 +090021import org.onosproject.net.DeviceId;
22import org.onosproject.openstacknode.api.OpenstackNode;
Jian Li091d8d22018-02-20 10:42:06 +090023import org.openstack4j.core.transport.ObjectMapperSingleton;
24import org.openstack4j.model.ModelEntity;
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
28import java.io.InputStream;
Jian Li1064e4f2018-05-29 16:16:53 +090029import java.util.HashMap;
30import java.util.Iterator;
31import java.util.Map;
32import java.util.Set;
33import java.util.TreeMap;
Jian Li091d8d22018-02-20 10:42:06 +090034
35import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
36
37/**
38 * An utility that used in openstack networking app.
39 */
Jian Lidea0fdb2018-04-02 19:02:48 +090040public final class OpenstackNetworkingUtil {
Jian Li091d8d22018-02-20 10:42:06 +090041
Jian Lidea0fdb2018-04-02 19:02:48 +090042 protected static final Logger log = LoggerFactory.getLogger(OpenstackNetworkingUtil.class);
Jian Li091d8d22018-02-20 10:42:06 +090043
44 /**
45 * Prevents object instantiation from external.
46 */
Jian Lidea0fdb2018-04-02 19:02:48 +090047 private OpenstackNetworkingUtil() {
Jian Li091d8d22018-02-20 10:42:06 +090048 }
49
50 /**
51 * Interprets JSON string to corresponding openstack model entity object.
52 *
53 * @param input JSON string
54 * @param entityClazz openstack model entity class
55 * @return openstack model entity object
56 */
57 public static ModelEntity jsonToModelEntity(InputStream input, Class entityClazz) {
58 ObjectMapper mapper = new ObjectMapper();
59 try {
60 JsonNode jsonTree = mapper.enable(INDENT_OUTPUT).readTree(input);
61 log.trace(new ObjectMapper().writeValueAsString(jsonTree));
62 return ObjectMapperSingleton.getContext(entityClazz)
63 .readerFor(entityClazz)
64 .readValue(jsonTree);
65 } catch (Exception e) {
66 throw new IllegalArgumentException();
67 }
68 }
Jian Lieb9f77d2018-02-20 11:25:45 +090069
70 /**
71 * Converts openstack model entity object into JSON object.
72 *
73 * @param entity openstack model entity object
74 * @param entityClazz openstack model entity class
75 * @return JSON object
76 */
77 public static ObjectNode modelEntityToJson(ModelEntity entity, Class entityClazz) {
78 ObjectMapper mapper = new ObjectMapper();
79 try {
80 String strModelEntity = ObjectMapperSingleton.getContext(entityClazz)
81 .writerFor(entityClazz)
82 .writeValueAsString(entity);
83 log.trace(strModelEntity);
84 return (ObjectNode) mapper.readTree(strModelEntity.getBytes());
85 } catch (Exception e) {
86 throw new IllegalStateException();
87 }
88 }
Jian Li1064e4f2018-05-29 16:16:53 +090089
90 /**
91 * Obtains the gateway node by device in compute node. Note that the gateway
92 * node is determined by device's device identifier.
93 *
94 * @param gws a collection of gateway nodes
95 * @param deviceId device identifier
96 * @return a gateway node
97 */
98 public static OpenstackNode getGwByComputeDevId(Set<OpenstackNode> gws, DeviceId deviceId) {
99 int numOfGw = gws.size();
100
101 if (numOfGw == 0) {
102 return null;
103 }
104
105 int gwIndex = Math.abs(deviceId.hashCode()) % numOfGw;
106
107 return getGwByIndex(gws, gwIndex);
108 }
109
110 private static OpenstackNode getGwByIndex(Set<OpenstackNode> gws, int index) {
111 Map<String, OpenstackNode> hashMap = new HashMap<>();
112 gws.forEach(gw -> hashMap.put(gw.hostname(), gw));
113 TreeMap<String, OpenstackNode> treeMap = new TreeMap<>(hashMap);
114 Iterator<String> iteratorKey = treeMap.keySet().iterator();
115
116 int intIndex = 0;
117 OpenstackNode gw = null;
118 while (iteratorKey.hasNext()) {
119 String key = iteratorKey.next();
120
121 if (intIndex == index) {
122 gw = treeMap.get(key);
123 }
124 intIndex++;
125 }
126 return gw;
127 }
Jian Li091d8d22018-02-20 10:42:06 +0900128}