Java Code Examples for org.codehaus.jackson.map.ObjectMapper#readTree()

The following examples show how to use org.codehaus.jackson.map.ObjectMapper#readTree() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CurrencyDaoYahooFinanceOfflineSampleImpl.java    From website with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
protected CurrencyRates loadCurrencyRates() throws Exception {
    logger.info("loading currency rates");
    ClassPathResource classPathResource = new ClassPathResource("yahoo-sample-currency-rates.json");
    
    try (InputStream instream = classPathResource.getInputStream();) {
        CurrencyRates currencyRates = new CurrencyRates();
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonRootNode = objectMapper.readTree(instream);
        List<JsonNode> resourceJsonNodes = jsonRootNode.findValues("resource");
        // resourceJsonNodes.
        for (JsonNode resourceJsonNode : resourceJsonNodes) {
            JsonNode fieldsNode = resourceJsonNode.findValue("fields");
            String currencyName = fieldsNode.get("name").getValueAsText();
            currencyName = StringUtils.remove(currencyName, "USD/");
            BigDecimal currencyRate = new BigDecimal(fieldsNode.get("price").getValueAsText());
            currencyRates.getRates().put(currencyName, currencyRate);
        }
        currencyRates.getRates().put("USD", new BigDecimal(1));
        setLastLoadedCurrencyRates(currencyRates);
        return getLastLoadedCurrencyRates();
    }
}
 
Example 2
Source File: StateDeserializer.java    From big-c with Apache License 2.0 6 votes vote down vote up
@Override
public StatePair deserialize(JsonParser parser, 
                             DeserializationContext context)
throws IOException, JsonProcessingException {
  ObjectMapper mapper = (ObjectMapper) parser.getCodec();
  // set the state-pair object tree
  ObjectNode statePairObject = (ObjectNode) mapper.readTree(parser);
  Class<?> stateClass = null;
  
  try {
    stateClass = 
      Class.forName(statePairObject.get("className").getTextValue().trim());
  } catch (ClassNotFoundException cnfe) {
    throw new RuntimeException("Invalid classname!", cnfe);
  }
  
  String stateJsonString = statePairObject.get("state").toString();
  State state = (State) mapper.readValue(stateJsonString, stateClass);
  
  return new StatePair(state);
}
 
Example 3
Source File: AvroInOutFormatsTest.java    From iow-hadoop-streaming with Apache License 2.0 6 votes vote down vote up
@Test
public void testAvroAsJsonFmt() throws IOException {

    AvroAsJsonOutputFormat outfmt = new AvroAsJsonOutputFormat();
    FileOutputFormat.setOutputPath(defaultConf, file2);
            RecordWriter<Text, NullWritable> writer = outfmt.getRecordWriter(file2.getFileSystem(defaultConf),
            defaultConf, fname2, new dummyReporter());

    writer.write(new Text(json), NullWritable.get());
    writer.close(null);

    FileInputFormat.setInputPaths(defaultConf, FileOutputFormat.getTaskOutputPath(defaultConf, fname2 +
        AvroOutputFormat.EXT));
    AvroAsJsonInputFormat informat = new AvroAsJsonInputFormat();
    RecordReader<Text, Text> reader = informat.getRecordReader(informat.getSplits(defaultConf, 1)[0],
            defaultConf, new dummyReporter());

    Text k = new Text();
    Text v = new Text();

    reader.next(k, v);
    ObjectMapper mapper = new ObjectMapper();
    JsonNode n0 = mapper.readTree(k.toString());
    JsonNode n1 = mapper.readTree(json);
    Assert.assertEquals("read back json", n0, n1);
}
 
Example 4
Source File: EventParser.java    From ingestion with Apache License 2.0 6 votes vote down vote up
private String getFieldName(Map<String, String> eventHeaders, String fieldName) {
    String value = null;
    if (fieldName.contains(".")) {
        ObjectMapper mapper = new ObjectMapper();
        final String[] fieldNameSplitted = fieldName.split("\\.");
        try {
            final String objectName = fieldNameSplitted[0];
            JsonNode jsonNode = mapper.readTree(eventHeaders.get(objectName));
            value = jsonNode.findValue(fieldNameSplitted[fieldNameSplitted.length - 1]).getTextValue();
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        value = eventHeaders.get(fieldName);
    }
    return value;
}
 
Example 5
Source File: VespaDocumentOperationTest.java    From vespa with Apache License 2.0 5 votes vote down vote up
@Test
public void requireThatUDFSupportsConditionalUpdateAssign() throws IOException {
    String json = getDocumentOperationJson("docid=id:<application>:metrics::<name>-<date>", "operation=update", "condition=clicks < <value>");
    ObjectMapper m = new ObjectMapper();
    JsonNode root = m.readTree(json);
    JsonNode fields = root.path("fields");

    assertEquals("id:testapp:metrics::clicks-20160112", root.get("update").getTextValue());
    assertEquals("clicks < 3", root.get("condition").getTextValue());
    assertEquals("testapp", fields.get("application").get("assign").getTextValue());
    assertEquals("clicks", fields.get("name").get("assign").getTextValue());
    assertEquals(3, fields.get("value").get("assign").getIntValue());
}
 
Example 6
Source File: LinkDeserializer.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
@Override
public Link deserialize(JsonParser parser, DeserializationContext context) throws IOException {
    
    ObjectMapper mapper = (ObjectMapper) parser.getCodec();
    ObjectNode root = (ObjectNode) mapper.readTree(parser);
    
    JsonNode relNode = root.get("rel");
    JsonNode hrefNode = root.get("href");
    return new BasicLink(relNode.asText(), new URL(hrefNode.asText()));
}
 
Example 7
Source File: HueSettings.java    From openhab1-addons with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Constructor of HueSettings. It takes the settings of the Hue bridge to enable the HueSettings to determine the
 * needed information about the bulbs.
 * 
 * @param settings
 *            This is the settings string in Json format returned by the Hue bridge.
 */
@SuppressWarnings("unchecked")
public HueSettings(String settings) {
    ObjectMapper mapper = new ObjectMapper();
    try {
        JsonNode rootNode = mapper.readTree(settings);
        if (!isAuthorizationError(rootNode)) {
            settingsData = new SettingsTree(mapper.readValue(rootNode, Map.class));
            isAuthorized = true;
        }
    } catch (IOException e) {
        logger.error("Could not read Settings-Json from Hue Bridge.", e);
    }
}
 
Example 8
Source File: QANARY.java    From chatbot with Apache License 2.0 5 votes vote down vote up
public QAService.Data search(String question) throws Exception {
    QAService.Data data = new QAService.Data();
    String response = makeRequest(question);
    if(response != null) {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode rootNode = mapper.readTree(response);
        JsonNode answers = mapper.readTree(rootNode.findValue("questions").get(0).get("question").get("answers").getTextValue());

        if (answers != null) {
            JsonNode bindings = answers.get("results").get("bindings");
            for(JsonNode binding : bindings) {
                Iterator<Map.Entry<String, JsonNode>> nodes = binding.getFields();
                while (nodes.hasNext()) {
                    Map.Entry<String, JsonNode> entry = nodes.next();
                    JsonNode value = entry.getValue();
                    switch(value.get("type").getTextValue()) {
                        case "uri":
                            data.addURI(value.get("value").getTextValue());
                            break;
                        case "typed-literal":
                            data.addLiteral(value.get("value").getTextValue());
                            break;
                    }
                }
            }
        }
    }
    return data;
}
 
Example 9
Source File: HttpRelativeLocationHeaderTestCase.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
@Test(groups = { "wso2.esb", "localOnly" }, description = "Http Location header")
public void testRelativeLocationHeader() throws Exception {
    String addUrl = getProxyServiceURLHttp("HTTPRelativeLocationService0Proxy");
    String query = "{\"employees\": [{\"id\": 0,\"name\": \"Carlene Pope\"},{\"id\": 1,\"name\": \"Jewell Richard\"}]}";
    String expectedResult = "{\"employees\":[{\"id\":0,\"name\":\"Carlene Pope\"},{\"id\":1,\"name\":\"Jewell Richard\"}]}";
    String actualResult = jsonclient.sendUserDefineRequest(addUrl, query).toString();
    final ObjectMapper mapper = new ObjectMapper();
    final JsonNode expectedJsonObject = mapper.readTree(expectedResult);
    final JsonNode actualJsonObject = mapper.readTree(actualResult);
    assertEquals(actualJsonObject, expectedJsonObject, "Could not process relative Location header.");
}
 
Example 10
Source File: VespaDocumentOperationTest.java    From vespa with Apache License 2.0 5 votes vote down vote up
@Test
public void requireThatUDFCorrectlyGeneratesAddTensorOperation() throws Exception {

    Schema schema = new Schema();
    Tuple tuple = TupleFactory.getInstance().newTuple();

    // Please refer to the tensor format documentation

    Map<String, Double> tensor = new HashMap<String, Double>() {{
        put("x:label1,y:label2,z:label4", 2.0);
        put("x:label3", 3.0);
    }};

    addToTuple("id", DataType.CHARARRAY, "123", schema, tuple);
    addToTuple("tensor", DataType.MAP, tensor, schema, tuple);

    VespaDocumentOperation docOp = new VespaDocumentOperation("docid=empty", "update-tensor-fields=tensor","operation=update");
    docOp.setInputSchema(schema);
    String json = docOp.exec(tuple);

    ObjectMapper m = new ObjectMapper();
    JsonNode root = m.readTree(json);
    JsonNode fields = root.get("fields");
    JsonNode tensorValue = fields.get("tensor");
    JsonNode add = tensorValue.get("add");
    JsonNode cells = add.get("cells");
    Iterator<JsonNode> cellsIterator = cells.getElements();

    JsonNode element = cellsIterator.next();
    assertEquals("label1", element.get("address").get("x").getTextValue());
    assertEquals("label2", element.get("address").get("y").getTextValue());
    assertEquals("label4", element.get("address").get("z").getTextValue());
    assertEquals("2.0", element.get("value").toString());

    element = cellsIterator.next();
    assertEquals("label3", element.get("address").get("x").getTextValue());
    assertEquals("3.0", element.get("value").toString());
}
 
Example 11
Source File: VespaDocumentOperationTest.java    From vespa with Apache License 2.0 5 votes vote down vote up
@Test
public void requireThatUDFCorrectlyGeneratesRemoveOperation() throws Exception {
    String json = getDocumentOperationJson("operation=remove", "docid=id:<application>:metrics::<name>-<date>");
    ObjectMapper m = new ObjectMapper();
    JsonNode root = m.readTree(json);
    JsonNode fields = root.get("fields");

    assertEquals("id:testapp:metrics::clicks-20160112", root.get("remove").getTextValue());
    assertNull(fields);
}
 
Example 12
Source File: WindowsEnrollment.java    From product-emm with Apache License 2.0 5 votes vote down vote up
@Test(groups = Constants.WindowsEnrollment.WINDOWS_ENROLLMENT_GROUP, description = "Test Windows MS XCEP post" +
        " request.", dependsOnMethods = {"testBST"})
public void testMSXCEP() throws Exception {
    base64Encoder = new Base64();
    ObjectMapper mapper = new ObjectMapper();
    JsonNode node = mapper.readTree(bsd);
    JsonNode token = node.get(UserToken);
    String encodedToken = base64Encoder.encodeToString(token.getTextValue().getBytes());
    String xml = readXML(Constants.WindowsEnrollment.MS_XCEP_FILE, Constants.UTF8);
    String payload = xml.replace(BSD_PLACEHOLDER, encodedToken);
    client.setHttpHeader(Constants.CONTENT_TYPE, Constants.APPLICATION_SOAP_XML);
    MDMResponse response = client.post(Constants.WindowsEnrollment.MS_EXCEP, payload);
    Assert.assertEquals(HttpStatus.SC_OK, response.getStatus());
}
 
Example 13
Source File: TPSDataLoadTask.java    From kardio with Apache License 2.0 4 votes vote down vote up
/**
 * A common method to Call the prometheous based on the queries passed (TPS/Latency query).
 * Iterate the prometheous output json, the response of this method is a Map with componentID as key and
 * TPS/Latency value as value of the Map.
 * @param promUrl
 * @param EnvName
 * @param promLookMap
 * @param platform
 * @return
 * @throws Exception
 */
private static Map<Integer, Float> getMatrxValuesFromPrometeous(String promUrl, String EnvName, Map<String, Integer> promLookMap, String platform) throws Exception{
	String lookUpPath = null;
	if(platform.equals("Mesos")){
		lookUpPath = "proxyname";
	}else if(platform.equals("K8s")){
		lookUpPath = "service";
	}else{
		logger.error("Not a valid platform");
	}
	String prometheusResponse = RestCommunicationHandler.getResponse(promUrl, false, null, null);
	ObjectMapper mapper = new ObjectMapper();
       JsonNode rootNode = mapper.readTree(prometheusResponse);
       if(rootNode.get("status") == null || !rootNode.get("status").getTextValue().equalsIgnoreCase("success")){
       	logger.error("Prometheus : Status Check Failed For Environment " + EnvName );
       	logger.error("Prometheus Response : \n" + prometheusResponse);
       	throw new GeneralException("Prometheus : Status Check Failed. Environment name " + EnvName);
       }
       if(rootNode.get("data") == null
       		|| rootNode.get("data").get("result") == null
       		|| !rootNode.get("data").get("result").isArray()){
       	logger.error("/data/result Node is missing in prometheus response. Environment name " + EnvName );
       	logger.error("Prometheus Response : \n" + prometheusResponse);
       	throw new GeneralException("/data/result Node is missing in prometheus response. Environment name " + EnvName);
       }
       
       Map<Integer, Float> valueMap = new HashMap<Integer, Float>();
       JsonNode valueNode = null;
       JsonNode metricNode = null;
       for (final JsonNode arrayNode : rootNode.get("data").get("result")) {
       	metricNode = arrayNode.get("metric");
       	if(metricNode == null){
       		logger.error("Metric Node cannot be null");
       		continue;
       	}
       	valueNode = arrayNode.get("value");
       	if(valueNode == null){
       		logger.error("/data/result/value Node is missing in prometheus response. Environment name " + EnvName);
               logger.error("Prometheus Response : \n" + prometheusResponse);
               throw new GeneralException("/data/result/value Node is missing in prometheus response. Environment name " + EnvName);
           }
       	String tpsValueString = valueNode.get(1).getTextValue();
           float tpsValue = Float.parseFloat(tpsValueString);
           if(metricNode.get(lookUpPath) == null){
           	logger.error("Metric Node without lookUpPath");
       		continue;
           }
           JsonNode lookupPathNode = metricNode.get(lookUpPath);
           String metricLookup = lookupPathNode.getTextValue();
       	if(promLookMap.containsKey(metricLookup)){
       		
       		int compId = promLookMap.get(metricLookup);
       		if(valueMap == null || valueMap.isEmpty()){
       			valueMap.put(compId, tpsValue);
       		}else if(valueMap.containsKey(compId)){
       			valueMap.put(compId, valueMap.get(compId)+tpsValue);
       		}else{
       			valueMap.put(compId, tpsValue);
       		}
       	}
       }
	return valueMap;
}
 
Example 14
Source File: VespaDocumentOperationTest.java    From vespa with Apache License 2.0 4 votes vote down vote up
@Test
public void requireThatUDFCorrectlyGeneratesRemoveTensorOperation() throws Exception {

    Schema schema = new Schema();
    Tuple tuple = TupleFactory.getInstance().newTuple();

    // Please refer to the tensor format documentation

    Map<String, Double> tensor = new HashMap<String, Double>() {{
        put("x:label1,y:label2,z:label4", 2.0);
        put("x:label3", 3.0);
    }};

    addToTuple("id", DataType.CHARARRAY, "123", schema, tuple);
    addToTuple("tensor", DataType.MAP, tensor, schema, tuple);

    VespaDocumentOperation docOp = new VespaDocumentOperation("docid=empty", "remove-tensor-fields=tensor","operation=update");
    docOp.setInputSchema(schema);
    String json = docOp.exec(tuple);

    ObjectMapper m = new ObjectMapper();
    JsonNode root = m.readTree(json);
    JsonNode fields = root.get("fields");
    JsonNode tensorValue = fields.get("tensor");
    JsonNode remove = tensorValue.get("remove");
    JsonNode address = remove.get("addresses");

    Iterator<JsonNode> addressIterator = address.getElements();

    JsonNode element = addressIterator.next();
    assertEquals("label1", element.get("x").getTextValue());

    element = addressIterator.next();
    assertEquals("label2", element.get("y").getTextValue());

    element = addressIterator.next();
    assertEquals("label4", element.get("z").getTextValue());

    element = addressIterator.next();
    assertEquals("label3", element.get("x").getTextValue());
}
 
Example 15
Source File: AbstractElasticsearchHttpProcessor.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
protected JsonNode parseJsonResponse(InputStream in) throws IOException {
    final ObjectMapper mapper = new ObjectMapper();
    return mapper.readTree(in);
}
 
Example 16
Source File: VespaDocumentOperationTest.java    From vespa with Apache License 2.0 4 votes vote down vote up
@Test
public void requireThatUDFGeneratesComplexDataTypes() throws Exception {
    Schema schema = new Schema();
    Tuple tuple = TupleFactory.getInstance().newTuple();

    Tuple intTuple = TupleFactory.getInstance().newTuple();
    int[] intArray = {1, 2, 3};
    for (int i : intArray) { intTuple.append(i); }

    Tuple stringTuple = TupleFactory.getInstance().newTuple();
    String[] stringArray = {"a", "b", "c"};
    for (String s : stringArray) { stringTuple.append(s); }

    DataBag bag = new SortedDataBag(null);
    bag.add(intTuple);
    bag.add(stringTuple);

    Map<String, Object> innerMap = new HashMap<String, Object>() {{
        put("a", "string");
        put("tuple", intTuple);
    }};

    DataByteArray bytes = new DataByteArray("testdata".getBytes());

    Map<String, Object> outerMap = new HashMap<String, Object>() {{
        put("string", "value");
        put("int", 3);
        put("float", 3.145);
        put("bool", true);
        put("byte", bytes);
        put("map", innerMap);
        put("bag", bag);
    }};

    addToTuple("map", DataType.MAP, outerMap, schema, tuple);

    VespaDocumentOperation docOp = new VespaDocumentOperation("docid=empty");
    docOp.setInputSchema(schema);
    String json = docOp.exec(tuple);

    ObjectMapper m = new ObjectMapper();
    JsonNode root = m.readTree(json);
    JsonNode fields = root.get("fields");
    JsonNode map = fields.get("map");

    assertEquals("value", map.get("string").getTextValue());
    assertEquals(3, map.get("int").getIntValue());
    assertEquals(3.145, map.get("float").getDoubleValue(), 1e-6);
    assertEquals(true, map.get("bool").getBooleanValue());
    assertEquals("dGVzdGRhdGE=", map.get("byte").getTextValue());

    assertEquals("string", map.get("map").get("a").getTextValue());
    for (int i = 0; i < intArray.length; ++i) {
        assertEquals(intArray[i], map.get("map").get("tuple").get(i).asInt());
    }

    JsonNode bagField = map.get("bag");
    for (int i = 0; i < intArray.length; ++i) {
        assertEquals(intArray[i], bagField.get(0).get(i).asInt());
    }
    for (int i = 0; i < stringArray.length; ++i) {
        assertEquals(stringArray[i], bagField.get(1).get(i).asText());
    }
}
 
Example 17
Source File: TextHandler.java    From chatbot with Apache License 2.0 4 votes vote down vote up
public ResponseGenerator handleTextMessage() throws Exception {
    ResponseGenerator responseGenerator = new ResponseGenerator();
    String[] rivescriptReply = helper.getRiveScriptBot().answer(request.getUserId(), textMessage);

    for(String reply : rivescriptReply) {
        if(Utility.isJSONObject(reply) == true) {
            ObjectMapper mapper = new ObjectMapper();
            JsonNode rootNode = mapper.readTree(reply);
            switch (rootNode.get("type").getTextValue()) {
                case RiveScriptReplyType.TEMPLATE_SCENARIO:
                    responseGenerator = new TemplateHandler(request, Utility.split(rootNode.get("name").getTextValue(), Utility.PARAMETER_SEPARATOR), helper)
                            .handleTemplateMessage();
                    break;
                case RiveScriptReplyType.LANGUAGE_SCENARIO:
                    responseGenerator = new LanguageHandler(request, rootNode.get("name").getTextValue(), helper)
                            .handleLanguageAbout();
                    break;
                case RiveScriptReplyType.STATUS_CHECK_SCENARIO:
                    responseGenerator = new StatusCheckHandler(request, rootNode.get("name").getTextValue(), helper).handleStatusCheck();
                    break;
                case RiveScriptReplyType.LOCATION_SCENARIO:
                    responseGenerator = new LocationHandler(request, rootNode.get("query").getTextValue(), helper).getLocation();
                    break;
                case RiveScriptReplyType.FALLBACK_SCENARIO:
                    // Eliza
                    if(textMessage.endsWith("!") || textMessage.endsWith(".")) {
                        responseGenerator.addTextResponse(new ResponseData(helper.getEliza().processInput(textMessage)));
                    }
                    else {
                        textMessage = rootNode.get("query").getTextValue(); // Use processed text message
                        responseGenerator = new NLHandler(request, textMessage, helper).answer();
                    }
                    break;
            }
        }
        else {
            responseGenerator.addTextResponse(new ResponseData(reply));
        }
    }

    // Fallback when everything else fails Eliza will answer
    if(responseGenerator.getResponse().size() == 0) {
        responseGenerator.addTextResponse(new ResponseData(helper.getEliza().processInput(textMessage)));
    }
    return responseGenerator;
}
 
Example 18
Source File: KubernetesBackUpTask.java    From kardio with Apache License 2.0 4 votes vote down vote up
/**
 * The function returns all Ingress as a Map, with key as service name & Ingress root node
 * @param ingressJson
 * @param eVo
 * @return
 * @throws Exception 
 */
private static Map<String, JsonNode> getIngressLookupMap(String authToken, EnvironmentVO eVo) throws Exception {
	// TODO Auto-generated method stub
	String ingressApiUrl = eVo.getK8sUrl() + PropertyUtil.getInstance().getValue(SurveillerConstants.K8S_API_INGRESS_PATH);
	String ingressJson = RestCommunicationHandler.getResponse(ingressApiUrl, true, SurveillerConstants.BEARER_TOKEN, authToken);
	ObjectMapper mapper = new ObjectMapper();
	JsonNode ingRootNode = mapper.readTree(ingressJson);
       ArrayNode ingressArrayNode = (ArrayNode) ingRootNode.get("items");
       if(ingressArrayNode == null || ingressArrayNode.size() == 0){
       	logger.info("No Ingress is available for the environment : "+eVo.getEnvironmentName());
		return null;
       }
       Iterator<JsonNode> ingressIterator = ingressArrayNode.getElements();
       Map<String, JsonNode> ingressMap = new HashMap<String, JsonNode>();
       while (ingressIterator.hasNext()) {
       	
       	JsonNode ingressInNode = ingressIterator.next();
       	JsonNode metadataNode = ingressInNode.get("metadata");
       	JsonNode nameNode = metadataNode.get("name");
       	String ingressName = nameNode.getValueAsText();
       	JsonNode namespaceNode = metadataNode.get("namespace");
       	String namespace = namespaceNode.getValueAsText();
       	if (!namespace.contains("-")) {
			logger.info("Excluding Ingress - " + ingressName + " in the namespace - " + namespace);
			continue;
		}
       	/*This particular block of code is to avoid code executions for the applications with multiple ingress
       	 * FIXME: Remove if this logic is not necessary
       	 * CASE: Services with multiple Ingress 
       	 * In our case the annotation "kubernetes.io/ingress.class" contains values "nginx-internal" or "nginx-external"
       	 * */
       	JsonNode annotNode = metadataNode.get("annotations");
       	if(annotNode == null){
       		logger.info("The annotations node is null for the Ingress - "+ingressName+" in the namespace - "+ namespace);
       	}else{
	        JsonNode ingClasstNode = annotNode.get("kubernetes.io/ingress.class");
            if(ingClasstNode == null || !ingClasstNode.getTextValue().equals("nginx-internal")){
            	logger.info("The hostname node is "+ingClasstNode+ "for the Ingress - "+ingressName+" in the namespace - "+ namespace);
            	continue;
            }
       	}   
           /******/
       	JsonNode specNode = ingressInNode.get("spec");
           if (specNode == null) {
			logger.info("The specNode is null for the ingress - " + ingressName + " in the namespace - "+ namespace);
			continue;
		}
           String serviceName = null;
           ArrayNode ruleArrayNode = (ArrayNode)specNode.get("rules");
           Iterator<JsonNode> ruleNodeItr = ruleArrayNode.getElements();
           while (ruleNodeItr.hasNext()) {
               JsonNode ruleNode = ruleNodeItr.next();
               JsonNode httpNode = ruleNode.get("http");
               if(httpNode == null){
               	logger.info("The httpNode is null for the ingress - " + ingressName + " in the namespace - "+ namespace);
               	continue;
               }
               ArrayNode pathArrayNode = (ArrayNode)httpNode.get("paths");
               Iterator<JsonNode> pathNodeItr = pathArrayNode.getElements();
               while (pathNodeItr.hasNext()) {
                JsonNode pathNode = pathNodeItr.next();
                JsonNode backendNode = pathNode.get("backend");
                JsonNode serviceNode = backendNode.get("serviceName");
                if (serviceNode == null) {
					logger.info("The serviceNode is null for the ingress - " + ingressName + " in the namespace - "+ namespace);
					continue;
				}
                serviceName = serviceNode.getTextValue();
                ingressMap.put(namespace+"/"+serviceName, ingressInNode);
               }    
          }	
       }
	return ingressMap;
}
 
Example 19
Source File: K8sAPIDashboardTask.java    From kardio with Apache License 2.0 4 votes vote down vote up
/**
 * Method to get all Deployment and MatchLabels of the given cluster
 * 
 * @param authToken
 * @param eVo
 * @param depLabelMap
 *
 */
private static void getDeploymentAndMatchLabels(String authToken, EnvironmentVO eVo, Map<String, List<ServiceLabelVO>> depLabelMap) throws IOException {

	String deployementApiUrl = eVo.getK8sUrl() + PropertyUtil.getInstance().getValue(SurveillerConstants.K8S_API_DEPLOYMENT_PATH);
	String deploymentJson = RestCommunicationHandler.getResponse(deployementApiUrl, true, SurveillerConstants.BEARER_TOKEN, authToken);
	ObjectMapper mapper = new ObjectMapper();
	JsonNode rootNode = mapper.readTree(deploymentJson);
	ArrayNode deploymentNode = (ArrayNode) rootNode.get("items");
	if (deploymentNode.size() == 0) {
		String errorString = "Surveiller task DOK8SAPIDASHBOARDTASK - FAILED : Enviroment -"
				+ eVo.getEnvironmentName() + " Deployment JSON Cannot be empty";
		logger.error(errorString);
		throw new GeneralException(errorString);
	}
	
	Iterator<JsonNode> deploymentIterator = deploymentNode.getElements();
	while (deploymentIterator.hasNext()) {
		JsonNode appsInNode = deploymentIterator.next();
		JsonNode depMetadataNode = appsInNode.get("metadata");
		JsonNode depNameNode = depMetadataNode.get("name");
		String depName = depNameNode.getValueAsText();
		JsonNode namespaceNode = depMetadataNode.get("namespace");
		String namespace = namespaceNode.getValueAsText();
		JsonNode specNode = appsInNode.get("spec");
		JsonNode selectorNode = specNode.get("selector");
		if (namespace.equals("default") || !namespace.contains("-")) {
			logger.info("Excluding deployment - " + depName + " in the namespace - " + namespace);
			continue;
		}
		JsonNode matchLabelsNode = selectorNode.get("matchLabels");
		if (matchLabelsNode == null) {
			logger.info("The matchLabelsNode is null for the deployment - " + depName + " in the namespace - "
					+ namespace);
			continue;
		}
		Map<String, String> labelMap = mapper.convertValue(matchLabelsNode, Map.class);
		ServiceLabelVO slVo = new ServiceLabelVO();
		slVo.setLabel(labelMap);
		String serviceName = namespace + "/" + depName;
		slVo.setServiceName(serviceName);
		String appName = namespace.substring(0, namespace.indexOf("-"));
		slVo.setAppName(appName);
		List<ServiceLabelVO> servLblList = null;
		if (depLabelMap.containsKey(namespace)) {
			servLblList = depLabelMap.get(namespace);
		} else {
			servLblList = new ArrayList<ServiceLabelVO>();
		}
		servLblList.add(slVo);
		depLabelMap.put(namespace, servLblList);
		
	}
}
 
Example 20
Source File: K8sAPIDashboardTask.java    From kardio with Apache License 2.0 4 votes vote down vote up
/**
 * Method to get all Pods and Containers associated to the deployment.
 * 
 * @param authToken
 * @param eVo
 * @param depLabelMap
 * @param externalPodsMap
 *
 */
private static void getPodsAndContainersOfDeployments(String authToken, EnvironmentVO eVo,
		Map<String, List<ServiceLabelVO>> depLabelMap, Map<String, ArrayList<Integer>> externalPodsMap) throws IOException {
	String podApiUrl = eVo.getK8sUrl() + PropertyUtil.getInstance().getValue(SurveillerConstants.K8S_API_PODS_PATH);
	/* Call - the Kube Pod api to get all the Pods in the Cluster */
	String podJson = RestCommunicationHandler.getResponse(podApiUrl, true, SurveillerConstants.BEARER_TOKEN, authToken);
	ObjectMapper mapper = new ObjectMapper();
	JsonNode podRootNode = mapper.readTree(podJson);
	ArrayNode appsNode = (ArrayNode) podRootNode.get("items");
	Iterator<JsonNode> podsIterator = appsNode.getElements();
	while (podsIterator.hasNext()) {
		JsonNode appsInNode = podsIterator.next();
		JsonNode metadataNode = appsInNode.get("metadata");
		JsonNode nameNode = metadataNode.get("name");
		String podName = nameNode.getValueAsText();
		JsonNode namespaceNode = metadataNode.get("namespace");
		String namespace = namespaceNode.getValueAsText();
		if (namespace.equals("default") || !namespace.contains("-")) {
			logger.info("Excluding Pods - " + podName + " in the namespace - " + namespace);
			continue;
		}
		JsonNode specNode = appsInNode.get("spec");
		ArrayNode contArrayNode = (ArrayNode) specNode.get("containers");
		/* Number of containers in Pod */
		int numCount = contArrayNode.size();
		//JsonNode ownerReferencesNode = 
		ArrayNode ownerReferencesArray = (ArrayNode) metadataNode.get("ownerReferences");
		if(ownerReferencesArray == null){
			loadPodsAndContainer("Kube-Systems",namespace, externalPodsMap, numCount);
			continue;
		}
		JsonNode ownerReferencesNode = ownerReferencesArray.get(0);
		JsonNode kindNode = ownerReferencesNode.get("kind");
		String kind = kindNode.getTextValue();
		if(!kind.equalsIgnoreCase("ReplicaSet")){
			loadPodsAndContainer(kind,namespace , externalPodsMap, numCount);
			continue;
		}
		
		JsonNode labelsNode = metadataNode.get("labels");
		if (labelsNode == null) {
			logger.info("The labelsNode is null for the pod - " + podName + " in the namespace - " + namespace);
			continue;
		}
		Map<String, String> podLabelMap = mapper.convertValue(labelsNode, Map.class);
		List<ServiceLabelVO> servLblList = depLabelMap.get(namespace);
		if(servLblList == null){
			continue;
		}
		serviceLabelLoop: for (ServiceLabelVO servLabelVo : servLblList) {
			int labelCount = 0;
			for (Entry<String, String> labelInfo : servLabelVo.getLabel().entrySet()) {
				if (podLabelMap.containsKey(labelInfo.getKey())) {
					if (podLabelMap.get(labelInfo.getKey()).equals(labelInfo.getValue())) {
						labelCount = labelCount + 1;
					} else {
						continue serviceLabelLoop;
					}
				} else {
					continue serviceLabelLoop;
				}
			}
			if (servLabelVo.getLabel().size() == labelCount) {
				
				servLabelVo.setNumOfContainers(servLabelVo.getNumOfContainers() + numCount);
				servLabelVo.setNumOfPods(servLabelVo.getNumOfPods() + 1);
				break;
			}
		}
	}
	
}