Jian Li | bde20bf | 2019-01-25 17:34:43 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2019-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.k8snetworking.util; |
| 17 | |
| 18 | import com.fasterxml.jackson.core.JsonParseException; |
| 19 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 20 | import com.fasterxml.jackson.databind.JsonMappingException; |
| 21 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 22 | import org.slf4j.Logger; |
| 23 | import org.slf4j.LoggerFactory; |
| 24 | |
| 25 | import java.io.IOException; |
| 26 | |
| 27 | import static org.onosproject.k8snetworking.api.Constants.PORT_NAME_PREFIX_CONTAINER; |
| 28 | |
| 29 | /** |
| 30 | * An utility that used in kubernetes networking app. |
| 31 | */ |
| 32 | public final class K8sNetworkingUtil { |
| 33 | |
| 34 | private static final Logger log = LoggerFactory.getLogger(K8sNetworkingUtil.class); |
| 35 | |
| 36 | private K8sNetworkingUtil() { |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Checks that whether the port is associated with container interface. |
| 41 | * |
| 42 | * @param portName port name |
| 43 | * @return true if the port is associated with container; false otherwise |
| 44 | */ |
| 45 | public static boolean isContainer(String portName) { |
| 46 | return PORT_NAME_PREFIX_CONTAINER.equals(portName); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Prints out the JSON string in pretty format. |
| 51 | * |
| 52 | * @param mapper Object mapper |
| 53 | * @param jsonString JSON string |
| 54 | * @return pretty formatted JSON string |
| 55 | */ |
| 56 | public static String prettyJson(ObjectMapper mapper, String jsonString) { |
| 57 | try { |
| 58 | Object jsonObject = mapper.readValue(jsonString, Object.class); |
| 59 | return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject); |
| 60 | } catch (JsonParseException e) { |
| 61 | log.debug("JsonParseException caused by {}", e); |
| 62 | } catch (JsonMappingException e) { |
| 63 | log.debug("JsonMappingException caused by {}", e); |
| 64 | } catch (JsonProcessingException e) { |
| 65 | log.debug("JsonProcessingException caused by {}", e); |
| 66 | } catch (IOException e) { |
| 67 | log.debug("IOException caused by {}", e); |
| 68 | } |
| 69 | return null; |
| 70 | } |
| 71 | } |