Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.openstacknetworking.util; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
Jian Li | eb9f77d | 2018-02-20 11:25:45 +0900 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame^] | 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.openstacknode.api.OpenstackNode; |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 23 | import org.openstack4j.core.transport.ObjectMapperSingleton; |
| 24 | import org.openstack4j.model.ModelEntity; |
| 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | import java.io.InputStream; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame^] | 29 | import java.util.HashMap; |
| 30 | import java.util.Iterator; |
| 31 | import java.util.Map; |
| 32 | import java.util.Set; |
| 33 | import java.util.TreeMap; |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 34 | |
| 35 | import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT; |
| 36 | |
| 37 | /** |
| 38 | * An utility that used in openstack networking app. |
| 39 | */ |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 40 | public final class OpenstackNetworkingUtil { |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 41 | |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 42 | protected static final Logger log = LoggerFactory.getLogger(OpenstackNetworkingUtil.class); |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Prevents object instantiation from external. |
| 46 | */ |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 47 | private OpenstackNetworkingUtil() { |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 48 | } |
| 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 Li | eb9f77d | 2018-02-20 11:25:45 +0900 | [diff] [blame] | 69 | |
| 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 Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame^] | 89 | |
| 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 Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 128 | } |