├── src └── main │ ├── resources │ ├── pyang.properties │ ├── application.properties │ └── log4j2.xml │ └── java │ └── com │ └── vzw │ ├── yang │ ├── test │ │ ├── AllTests.java │ │ ├── UCSMappersCiscoUT.java │ │ ├── Test.java │ │ └── UCSMappersCienaUT.java │ ├── services │ │ ├── TransactionStatus.java │ │ ├── TransformerStressService.java │ │ ├── TransformerDisplayCount.java │ │ └── TransformerStressTester.java │ ├── data │ │ └── JsonToYangJsonRequest.java │ ├── transformer │ │ ├── ucs │ │ │ ├── TransformJuniperAlarm.java │ │ │ ├── TransformCiscoControlPlane.java │ │ │ ├── TransformCiscoControlPlaneActiveAlarm.java │ │ │ ├── MappingDetail.java │ │ │ ├── TransformJuniperPerf.java │ │ │ └── YangTransformer.java │ │ ├── TransformCiscoControlPlane.java │ │ ├── TransformJuniperAlarm.java │ │ ├── MappingDetail.java │ │ ├── TransformJuniperPerf.java │ │ └── YangTransformer.java │ ├── util │ │ ├── InvokePyangScript.java │ │ └── YangTransformerService.java │ └── rest │ │ └── YangApiService.java │ ├── springboot │ └── util │ │ └── SwaggerConfig.java │ └── StartApplication.java ├── Dockerfile ├── Contributing.md ├── README.md └── pom.xml /src/main/resources/pyang.properties: -------------------------------------------------------------------------------- 1 | pyang.host = 10.134.214.149 2 | pyang.port = 8000 3 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM openjdk:8-jdk-alpine 2 | 3 | RUN addgroup -S torrera && adduser -S torrera -G torrera 4 | RUN mkdir logs 5 | RUN chgrp -R torrera logs 6 | RUN chown -R torrera logs 7 | 8 | ARG JAR_FILE=target/*.jar 9 | COPY ${JAR_FILE} app.jar 10 | RUN chgrp -R torrera /app.jar 11 | RUN chown -R torrera /app.jar 12 | 13 | USER torrera:torrera 14 | 15 | 16 | ENTRYPOINT ["java","-jar","/app.jar"] 17 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Logging Parameters 3 | # 4 | logging.level.org.springframework=INFO 5 | logging.level.com.zaxxer=DEBUG 6 | logging.level.root=ERROR 7 | logging.level.org.hibernate.SQL=DEBUG 8 | logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE 9 | logging.level.org.springframework.jdbc.core=TRACE 10 | 11 | logging.pattern.console=%-5level %logger{36} - %msg%n 12 | 13 | # 14 | # Server port 15 | # 16 | server.port=8082 17 | -------------------------------------------------------------------------------- /src/main/java/com/vzw/yang/test/AllTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright Verizon Inc. 3 | Licensed under the terms of the Apache License 2.0 license. See LICENSE file in project root for terms. 4 | */ 5 | package com.vzw.yang.test; 6 | 7 | import org.junit.runner.RunWith; 8 | import org.junit.runners.Suite; 9 | import org.junit.runners.Suite.SuiteClasses; 10 | 11 | @RunWith(Suite.class) 12 | @SuiteClasses({ UCSMappersCienaUT.class,UCSMappersCiscoUT.class }) 13 | 14 | 15 | public class AllTests { 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/vzw/yang/services/TransactionStatus.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright Verizon Inc. 3 | Licensed under the terms of the Apache License 2.0 license. See LICENSE file in project root for terms. 4 | */ 5 | package com.vzw.yang.services; 6 | 7 | public class TransactionStatus { 8 | 9 | private int counter = 0; 10 | private String name; 11 | 12 | 13 | 14 | public String getName() { 15 | return name; 16 | } 17 | 18 | public void setName(String name) { 19 | this.name = name; 20 | } 21 | 22 | public int getCounter() { 23 | return counter; 24 | } 25 | 26 | public void setCounter(int value) { 27 | counter = value; 28 | } 29 | 30 | public void resetCounter() { 31 | counter = 0; 32 | } 33 | 34 | public void addCounter() { 35 | counter = counter + 1; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/vzw/yang/data/JsonToYangJsonRequest.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright Verizon Inc. 3 | Licensed under the terms of the Apache License 2.0 license. See LICENSE file in project root for terms. 4 | */ 5 | package com.vzw.yang.data; 6 | 7 | public class JsonToYangJsonRequest { 8 | 9 | private String topic; 10 | private String jsonStr; 11 | 12 | public JsonToYangJsonRequest() { 13 | 14 | } 15 | 16 | public JsonToYangJsonRequest(String topic, String jsonStr) { 17 | this.topic = topic; 18 | this.jsonStr = jsonStr; 19 | } 20 | 21 | public String getTopic() { 22 | return topic; 23 | } 24 | public void setTopic(String topic) { 25 | this.topic = topic; 26 | } 27 | public String getJsonStr() { 28 | return jsonStr; 29 | } 30 | public void setJsonStr(String jsonStr) { 31 | this.jsonStr = jsonStr; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Contributing.md: -------------------------------------------------------------------------------- 1 | How to contribute 2 | ================= 3 | First, thanks for taking the time to contribute to our project! There are many ways you can help out. 4 | 5 | Questions 6 | ========= 7 | If you have a question that needs an answer, create an issue, and label it as a question. 8 | 9 | Issues for bugs or feature requests 10 | =================================== 11 | If you encounter any bugs in the code, or want to request a new feature or enhancement, please create an issue to report it. Kindly add a label to indicate what type of issue it is. 12 | 13 | Contribute Code 14 | =============== 15 | We welcome your pull requests for bug fixes. To implement something new, please create an issue first so we can discuss it together. 16 | 17 | Creating a Pull Request Please follow best practices for creating git commits. 18 | 19 | When your code is ready to be submitted, submit a pull request to begin the code review process. 20 | 21 | We only seek to accept code that you are authorized to contribute to the project. We have added a pull request template on our projects so that your contributions are made with the following confirmation: 22 | 23 | I confirm that this contribution is made under the terms of the license found in the root directory of this repository's source tree and that I have the authority necessary to make this contribution on behalf of its copyright owner. 24 | 25 | -------------------------------------------------------------------------------- /src/main/java/com/vzw/yang/transformer/ucs/TransformJuniperAlarm.java: -------------------------------------------------------------------------------- 1 | package com.vzw.yang.transformer.ucs; 2 | 3 | import java.util.Iterator; 4 | 5 | import org.json.JSONArray; 6 | import org.json.JSONObject; 7 | 8 | import com.fasterxml.jackson.databind.JsonNode; 9 | import com.fasterxml.jackson.databind.node.JsonNodeFactory; 10 | import com.fasterxml.jackson.databind.node.ObjectNode; 11 | 12 | public class TransformJuniperAlarm { 13 | 14 | public static ObjectNode transform(ObjectNode jsonObj) 15 | { 16 | ObjectNode retObj =JsonNodeFactory.instance.objectNode(); 17 | 18 | Iterator keys = jsonObj.fieldNames(); 19 | while (keys.hasNext()) { 20 | String key = keys.next(); 21 | if (!"kv".equals(key)) { 22 | JsonNode obj = jsonObj.get(key); 23 | retObj.set(key, obj); 24 | } else { 25 | JsonNode kvlist = jsonObj.get(key); 26 | if (kvlist.isArray()) 27 | { 28 | for (JsonNode jo : kvlist) { 29 | if (jo.has("uintValue")) { 30 | retObj.set(jo.get("key").asText(), jo.get("uintValue")); 31 | } 32 | if (jo.has("strValue")) { 33 | retObj.set(jo.get("key").asText(), jo.get("strValue")); 34 | } 35 | } 36 | } 37 | } 38 | } 39 | ObjectNode j = JsonNodeFactory.instance.objectNode(); 40 | j.set("data", retObj); 41 | retObj = JsonNodeFactory.instance.objectNode(); 42 | retObj.set("data", j); 43 | return retObj; 44 | } 45 | 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/main/resources/log4j2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /src/main/java/com/vzw/springboot/util/SwaggerConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright Verizon Inc. 3 | Licensed under the terms of the Apache License 2.0 license. See LICENSE file in project root for terms. 4 | */ 5 | package com.vzw.springboot.util; 6 | 7 | // The static import is used for the regex(..) method. 8 | import static springfox.documentation.builders.PathSelectors.regex; 9 | 10 | import org.springframework.context.annotation.Bean; 11 | import org.springframework.context.annotation.Configuration; 12 | 13 | import springfox.documentation.builders.ApiInfoBuilder; 14 | import springfox.documentation.builders.PathSelectors; 15 | import springfox.documentation.service.ApiInfo; 16 | import springfox.documentation.spi.DocumentationType; 17 | import springfox.documentation.spring.web.plugins.Docket; 18 | import springfox.documentation.swagger2.annotations.EnableSwagger2; 19 | 20 | @Configuration 21 | // This annotation enables the Swagger support in the application. 22 | @EnableSwagger2 23 | public class SwaggerConfig { 24 | 25 | @Bean 26 | public Docket postsApi() { 27 | //return new Docket(DocumentationType.SWAGGER_2).apiInfo(metadata()).select().paths(regex("/echo.*")).build(); 28 | return new Docket(DocumentationType.SWAGGER_2).apiInfo(metadata()).select().paths(PathSelectors.any()).build(); 29 | 30 | } 31 | 32 | @SuppressWarnings("deprecation") 33 | private ApiInfo metadata() { 34 | return new ApiInfoBuilder().title("YANG Transformer").description("API reference guide for developers").build(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/vzw/StartApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright Verizon Inc. 3 | Licensed under the terms of the Apache License 2.0 license. See LICENSE file in project root for terms. 4 | */ 5 | package com.vzw; 6 | 7 | import org.apache.logging.log4j.LogManager; 8 | import org.apache.logging.log4j.Logger; 9 | 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.boot.CommandLineRunner; 12 | import org.springframework.boot.SpringApplication; 13 | import org.springframework.boot.autoconfigure.SpringBootApplication; 14 | 15 | @SpringBootApplication 16 | public class StartApplication implements CommandLineRunner { 17 | 18 | private static final Logger logger = LogManager.getLogger(StartApplication.class); 19 | 20 | public static void main(String[] args) { 21 | SpringApplication.run(StartApplication.class, args); 22 | } 23 | 24 | @Override 25 | public void run(String... args) { 26 | 27 | logger.info("StartApplication..."); 28 | 29 | //repository.save(new Book("Java")); 30 | //repository.save(new Book("Node")); 31 | //repository.save(new Book("Python")); 32 | 33 | //System.out.println("\nfindAll()"); 34 | //repository.findAll().forEach(x -> System.out.println(x)); 35 | 36 | //System.out.println("\nfindById(300419481230L)"); 37 | //repository.findById(300419481230L).ifPresent(x -> logger.info(x)); 38 | 39 | //System.out.println("\nfindByName('Node')"); 40 | //repository.findByName("Node").forEach(x -> System.out.println(x)); 41 | 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/vzw/yang/transformer/TransformCiscoControlPlane.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright Verizon Inc. 3 | Licensed under the terms of the Apache License 2.0 license. See LICENSE file in project root for terms. 4 | */ 5 | package com.vzw.yang.transformer; 6 | 7 | import java.util.Iterator; 8 | import java.util.regex.*; 9 | 10 | 11 | import org.json.JSONArray; 12 | import org.json.JSONObject; 13 | import org.apache.logging.log4j.LogManager; 14 | import org.apache.logging.log4j.Logger; 15 | 16 | public class TransformCiscoControlPlane { 17 | 18 | private static final Logger logger = LogManager.getLogger(TransformCiscoControlPlane.class); 19 | 20 | static JSONObject transform(JSONObject jsonObj) { 21 | 22 | JSONObject append = new JSONObject(); 23 | append.put("severity", "INFO"); 24 | append.put("text","EPNM Control Plane message"); 25 | walkJSON(jsonObj,append); 26 | JSONObject retObj = jsonObj; 27 | 28 | JSONObject j = new JSONObject(); 29 | j.put("data", append); 30 | retObj = new JSONObject(); 31 | retObj.put("data", j); 32 | return retObj; 33 | } 34 | static void walkJSON(JSONObject jsonObj, JSONObject append) 35 | { 36 | Iterator keys = jsonObj.keys(); 37 | while (keys.hasNext()) { 38 | String key = keys.next(); 39 | Object obj = jsonObj.get(key); 40 | if (obj instanceof JSONObject) 41 | { 42 | walkJSON((JSONObject)obj,append); 43 | } 44 | if (key.contains("fdn")){ 45 | append.put("resource", jsonObj.get(key)); 46 | } 47 | if (key.contains("notification-id")){ 48 | append.put("id", ((Long)jsonObj.get(key)).toString()); 49 | } 50 | if (key.equals("push.time-of-update")){ 51 | append.put("timestamp", jsonObj.get(key)); 52 | } 53 | } 54 | 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/vzw/yang/transformer/ucs/TransformCiscoControlPlane.java: -------------------------------------------------------------------------------- 1 | package com.vzw.yang.transformer.ucs; 2 | 3 | import java.util.Iterator; 4 | 5 | 6 | import com.fasterxml.jackson.databind.JsonNode; 7 | import com.fasterxml.jackson.databind.node.JsonNodeFactory; 8 | import com.fasterxml.jackson.databind.node.ObjectNode; 9 | 10 | import org.apache.logging.log4j.LogManager; 11 | import org.apache.logging.log4j.Logger; 12 | 13 | public class TransformCiscoControlPlane { 14 | 15 | private static final Logger logger = LogManager.getLogger(TransformCiscoControlPlane.class); 16 | 17 | public static ObjectNode transform(ObjectNode jsonObj) { 18 | 19 | ObjectNode append = JsonNodeFactory.instance.objectNode(); 20 | append.put("severity", "INFO"); 21 | append.put("text","EPNM Control Plane message"); 22 | walkJSON(jsonObj,append); 23 | ObjectNode retObj = jsonObj; 24 | 25 | ObjectNode j = JsonNodeFactory.instance.objectNode(); 26 | j.set("data", append); 27 | retObj = JsonNodeFactory.instance.objectNode(); 28 | retObj.set("data", j); 29 | retObj.put("raw", jsonObj.toString()); 30 | return retObj; 31 | } 32 | static void walkJSON(ObjectNode jsonObj, ObjectNode append) 33 | { 34 | Iterator keys = jsonObj.fieldNames(); 35 | while (keys.hasNext()) { 36 | String key = keys.next(); 37 | JsonNode obj = jsonObj.get(key); 38 | if (obj instanceof ObjectNode) 39 | { 40 | walkJSON((ObjectNode)obj,append); 41 | } 42 | if (key.contains("fdn")){ 43 | append.set("resource", jsonObj.get(key)); 44 | } 45 | if (key.contains("notification-id")){ 46 | append.put("id", jsonObj.get(key).asLong()); 47 | } 48 | if (key.equals("push.time-of-update")){ 49 | append.set("timestamp", jsonObj.get(key)); 50 | } 51 | } 52 | 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/vzw/yang/transformer/TransformJuniperAlarm.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright Verizon Inc. 3 | Licensed under the terms of the Apache License 2.0 license. See LICENSE file in project root for terms. 4 | */ 5 | package com.vzw.yang.transformer; 6 | 7 | import java.util.Iterator; 8 | 9 | import org.json.JSONArray; 10 | import org.json.JSONObject; 11 | 12 | public class TransformJuniperAlarm { 13 | 14 | static JSONObject transform(JSONObject jsonObj) 15 | { 16 | JSONObject retObj = new JSONObject(); 17 | 18 | Iterator keys = jsonObj.keys(); 19 | while (keys.hasNext()) { 20 | String key = keys.next(); 21 | if (!"kv".equals(key)) { 22 | Object obj = jsonObj.get(key); 23 | if (obj instanceof String) { 24 | retObj.put(key, (String)obj); 25 | } else if (obj instanceof Long) { 26 | retObj.put(key, ((Long)obj).toString()); 27 | } else if (obj instanceof Integer) { 28 | retObj.put(key, ((Integer)obj).toString()); 29 | } else if (obj instanceof Float) { 30 | retObj.put(key, ((Float)obj).toString()); 31 | } else if (obj instanceof Double) { 32 | retObj.put(key, ((Double)obj).toString()); 33 | } else { 34 | retObj.put(key, obj.toString()); 35 | } 36 | } else { 37 | JSONArray kvlist = jsonObj.getJSONArray(key); 38 | for (int i = 0; i < kvlist.length(); i++) { 39 | JSONObject jo = kvlist.getJSONObject(i); 40 | if (jo.has("uintValue")) { 41 | retObj.put(jo.getString("key"), jo.getString("uintValue")); 42 | } 43 | if (jo.has("strValue")) { 44 | retObj.put(jo.getString("key"), jo.getString("strValue")); 45 | } 46 | } 47 | } 48 | } 49 | JSONObject j = new JSONObject(); 50 | j.put("data", retObj); 51 | retObj = new JSONObject(); 52 | retObj.put("data", j); 53 | return retObj; 54 | } 55 | 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/vzw/yang/transformer/ucs/TransformCiscoControlPlaneActiveAlarm.java: -------------------------------------------------------------------------------- 1 | package com.vzw.yang.transformer.ucs; 2 | 3 | import java.util.Iterator; 4 | import java.util.regex.*; 5 | 6 | 7 | import org.json.JSONArray; 8 | import org.json.JSONObject; 9 | 10 | import com.fasterxml.jackson.databind.JsonNode; 11 | import com.fasterxml.jackson.databind.node.JsonNodeFactory; 12 | import com.fasterxml.jackson.databind.node.ObjectNode; 13 | 14 | import org.apache.logging.log4j.LogManager; 15 | import org.apache.logging.log4j.Logger; 16 | 17 | public class TransformCiscoControlPlaneActiveAlarm { 18 | 19 | private static final Logger logger = LogManager.getLogger(TransformCiscoControlPlaneActiveAlarm.class); 20 | 21 | public static ObjectNode transform(ObjectNode jsonObj) { 22 | 23 | ObjectNode append = JsonNodeFactory.instance.objectNode();; 24 | append.put("severity", "INFO"); 25 | append.put("text","EPNM Control Plane Active Alarms"); 26 | walkJSON(jsonObj,append); 27 | ObjectNode retObj = jsonObj; 28 | 29 | ObjectNode j = JsonNodeFactory.instance.objectNode(); 30 | j.set("data", append); 31 | retObj = JsonNodeFactory.instance.objectNode();; 32 | retObj.set("data", j); 33 | retObj.put("raw", jsonObj.toString()); 34 | return retObj; 35 | } 36 | static void walkJSON(ObjectNode jsonObj, ObjectNode append) 37 | { 38 | Iterator keys = jsonObj.fieldNames(); 39 | while (keys.hasNext()) { 40 | String key = keys.next(); 41 | JsonNode obj = jsonObj.get(key); 42 | if (obj instanceof ObjectNode) 43 | { 44 | walkJSON((ObjectNode)obj,append); 45 | } 46 | if (key.contains("nd.name")){ 47 | append.set("resource", jsonObj.get(key)); 48 | } 49 | if (key.contains("nd.creation-time")){ 50 | append.set("id", jsonObj.get(key)); 51 | } 52 | if (key.equals("nd.creation-time")){ 53 | append.set("timestamp", jsonObj.get(key)); 54 | } 55 | } 56 | 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/vzw/yang/transformer/MappingDetail.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright Verizon Inc. 3 | Licensed under the terms of the Apache License 2.0 license. See LICENSE file in project root for terms. 4 | */ 5 | package com.vzw.yang.transformer; 6 | 7 | import org.json.JSONObject; 8 | 9 | public class MappingDetail { 10 | 11 | private String topic; 12 | private String mappingFile; 13 | private boolean equipmentTopic; 14 | private JSONObject mappingObj; 15 | 16 | /* 17 | private static MappingDetail[] mappingDetails = { 18 | new MappingDetail("ENMV_SAMSUNG5G_CPEDATA", "samsungCpePerfdataYangMap.json", false), 19 | new MappingDetail( "BNC-FILTERED-ALARMS", "bncFilteredAlarms.json" , false), 20 | new MappingDetail("UT_ALARMS_CIENA", "UT_Ciena_AlarmMapper.json" , false), 21 | new MappingDetail("UT_ALARMS_CIENA_OBJ", "UT_Ciena_AlarmMapperObject.json" , false), 22 | new MappingDetail("UT_ALARMS_CISCO", "UT_Cisco_AlarmMapper.json" , false), 23 | new MappingDetail("UT_ALARMS_CISCO_CCS", "UT_CiscoCCS_AlarmMapper.json" , false), 24 | new MappingDetail("UTS_EQUIPMENT", "UTS_EquipmentMapper.json" , false), 25 | new MappingDetail("MSE_ALARMS_CISCO", "MSE_Cisco_AlarmMapper.json" , false), 26 | new MappingDetail("MSE_ALARMS_JUNIPER", "MSE_Juniper_AlarmMapper.json" , false), 27 | new MappingDetail("UTS_EQUIPMENT_API", "UTS_EquipmentMapper.json" , true), 28 | new MappingDetail("UTS_CIRCUIT_API", "UTS_CircuitMapper.json" , false) 29 | }; 30 | */ 31 | 32 | public MappingDetail(String topic, String mappingFile, boolean equipmentTopic) { 33 | this.topic = topic; 34 | this.mappingFile = mappingFile; 35 | this.equipmentTopic = equipmentTopic; 36 | } 37 | 38 | public String getTopic() { 39 | return topic; 40 | } 41 | 42 | public void setTopic(String topic) { 43 | this.topic = topic; 44 | } 45 | 46 | public String getMappingFile() { 47 | return mappingFile; 48 | } 49 | 50 | public void setMappingFile(String mappingFile) { 51 | this.mappingFile = mappingFile; 52 | } 53 | 54 | public boolean isEquipmentTopic() { 55 | return equipmentTopic; 56 | } 57 | 58 | public void setEquipmentTopic(boolean equipmentTopic) { 59 | this.equipmentTopic = equipmentTopic; 60 | } 61 | 62 | 63 | 64 | public JSONObject getMappingObj() { 65 | return mappingObj; 66 | } 67 | 68 | public void setMappingObj(JSONObject mappingObj) { 69 | this.mappingObj = mappingObj; 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/com/vzw/yang/test/UCSMappersCiscoUT.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright Verizon Inc. 3 | Licensed under the terms of the Apache License 2.0 license. See LICENSE file in project root for terms. 4 | */ 5 | package com.vzw.yang.test; 6 | 7 | import static org.junit.Assert.*; 8 | 9 | import java.io.File; 10 | import java.io.IOException; 11 | 12 | import org.junit.Test; 13 | 14 | import com.fasterxml.jackson.core.JsonGenerationException; 15 | import com.fasterxml.jackson.databind.JsonMappingException; 16 | import com.fasterxml.jackson.databind.JsonNode; 17 | import com.fasterxml.jackson.databind.ObjectMapper; 18 | import com.fasterxml.jackson.databind.node.ObjectNode; 19 | import com.vzw.yang.transformer.ucs.YangTransformer; 20 | 21 | public class UCSMappersCiscoUT { 22 | 23 | private static final ObjectMapper mapper = new ObjectMapper(); 24 | 25 | @Test 26 | public void test1() { 27 | try { 28 | String file = "c:\\vendorC\\poc\\project\\python\\transformer\\CiscoUT.jsonn"; 29 | JsonNode root = mapper.readTree(new File(file)); 30 | System.out.println(root.toString()); 31 | YangTransformer transformer = new YangTransformer(); 32 | try { 33 | String json = transformer.jsonToYangJson("UT_ALARMS_CISCO", (ObjectNode) root, false) ; 34 | System.out.println(json); 35 | } catch (Exception e) { 36 | e.printStackTrace(); 37 | } 38 | } catch (JsonGenerationException e) { 39 | e.printStackTrace(); 40 | fail("JsonGenerationException"); 41 | } catch (JsonMappingException e) { 42 | e.printStackTrace(); 43 | fail("JsonMappingException"); 44 | } catch (IOException e) { 45 | e.printStackTrace(); 46 | fail("IOException"); 47 | } 48 | } 49 | @Test 50 | public void test2() { 51 | try { 52 | String file = "c:\\vendorC\\poc\\project\\python\\transformer\\CiscoUT.json"; 53 | JsonNode root = mapper.readTree(new File(file)); 54 | System.out.println(root.toString()); 55 | YangTransformer transformer = new YangTransformer(); 56 | try { 57 | String json = transformer.jsonToYangJson("UT_ALARMS_CISCO", (ObjectNode) root, false) ; 58 | System.out.println(json); 59 | } catch (Exception e) { 60 | e.printStackTrace(); 61 | } 62 | } catch (JsonGenerationException e) { 63 | e.printStackTrace(); 64 | fail("JsonGenerationException"); 65 | } catch (JsonMappingException e) { 66 | e.printStackTrace(); 67 | fail("JsonMappingException"); 68 | } catch (IOException e) { 69 | e.printStackTrace(); 70 | fail("IOException"); 71 | } 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/com/vzw/yang/services/TransformerStressService.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright Verizon Inc. 3 | Licensed under the terms of the Apache License 2.0 license. See LICENSE file in project root for terms. 4 | */ 5 | package com.vzw.yang.services; 6 | 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | 10 | import javax.annotation.PostConstruct; 11 | 12 | import org.apache.logging.log4j.LogManager; 13 | import org.apache.logging.log4j.Logger; 14 | import org.springframework.beans.factory.annotation.Autowired; 15 | import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; 16 | import org.springframework.stereotype.Service; 17 | 18 | 19 | /********************************************************************** 20 | ** Process responses from CAMEO 21 | **********************************************************************/ 22 | @Service("transformerStressService") 23 | public class TransformerStressService { 24 | 25 | private static final Logger logger = LogManager.getLogger(TransformerStressService.class); 26 | 27 | private long numThreads = 1; 28 | 29 | public TransformerStressService() { 30 | logger.debug("TransformerStressService: Entered"); 31 | 32 | logger.debug("TransformerStressService: Exited"); 33 | 34 | } 35 | 36 | @Autowired 37 | ThreadPoolTaskExecutor taskExecutor; 38 | 39 | //@PostConstruct 40 | public void init() { 41 | 42 | List statuses = new ArrayList(); 43 | logger.info("TransformerStressService.init: Entered"); 44 | 45 | do { 46 | 47 | for (int i = 0; i < numThreads; i++) { 48 | TransactionStatus status = new TransactionStatus(); 49 | status.setName("Stress Tester: " + i); 50 | statuses.add(status); 51 | taskExecutor.execute(new TransformerStressTester(status)); 52 | } 53 | 54 | taskExecutor.execute(new TransformerDisplayCount(statuses)); 55 | /* 56 | try { 57 | while (true) { 58 | try { 59 | Thread.sleep(60 * 1000); 60 | } catch(Exception e) { 61 | e.printStackTrace(); 62 | } 63 | int totalTrans = 0; 64 | for (TransactionStatus st : statuses) { 65 | totalTrans = totalTrans + st.getCounter(); 66 | st.resetCounter(); 67 | } 68 | logger.info("TransformerStressService.init: Total Transactions per minute is: " + totalTrans); 69 | } 70 | } catch(Exception e) { 71 | logger.error("TransformerStressService.init: Error waiting on threads: " + e.getMessage()); 72 | e.printStackTrace(); 73 | } 74 | */ 75 | //logger.info("TransformerStressService.init: Done waiting for threads to complete"); 76 | 77 | } while (false); 78 | logger.info("TransformerStressService.init: Exited: "); 79 | 80 | } 81 | 82 | 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/com/vzw/yang/services/TransformerDisplayCount.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright Verizon Inc. 3 | Licensed under the terms of the Apache License 2.0 license. See LICENSE file in project root for terms. 4 | */ 5 | package com.vzw.yang.services; 6 | 7 | import com.fasterxml.jackson.databind.ObjectMapper; 8 | 9 | import org.apache.logging.log4j.LogManager; 10 | import org.apache.logging.log4j.Logger; 11 | 12 | import java.util.List; 13 | 14 | /** 15 | * Created by randy on 8/25/2017. 16 | */ 17 | public class TransformerDisplayCount implements Runnable { 18 | private static final Logger logger = LogManager.getLogger(TransformerDisplayCount.class); 19 | 20 | private List statuses; 21 | 22 | public TransformerDisplayCount(List transStatus) { 23 | statuses = transStatus; 24 | } 25 | 26 | @Override 27 | public void run() { 28 | 29 | logger.info("TransformerDisplayCount.run: is running"); 30 | 31 | while (true) { 32 | while (true) { 33 | try { 34 | Thread.sleep(60 * 1000); 35 | } catch(Exception e) { 36 | e.printStackTrace(); 37 | } 38 | int totalTrans = 0; 39 | for (TransactionStatus st : statuses) { 40 | totalTrans = totalTrans + st.getCounter(); 41 | st.resetCounter(); 42 | } 43 | logger.info("TransformerDisplayCount.init: Total Transactions for " + statuses.size() + " threads per minute is: " + totalTrans); 44 | System.out.println("TransformerDisplayCount.init: Total Transactions for " + statuses.size() + " threads per minute is: " + totalTrans); 45 | 46 | } 47 | } 48 | 49 | //logger.info("TransformerDisplayCount.run: " + transactionStatus.getName() + " has ended"); 50 | 51 | } 52 | 53 | private void prettyPrint(String json) { 54 | try { 55 | ObjectMapper mapper = new ObjectMapper(); 56 | logger.info("KafkaJsonService.test1: Have request"); 57 | Object jsonObj = mapper.readValue(json, Object.class); 58 | logger.info(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObj)); 59 | } catch(Exception e) { 60 | 61 | } 62 | 63 | } 64 | 65 | public static void main(String[] args) { 66 | 67 | } 68 | 69 | private String testJsonStr = "{\"eventTime\": \"2019-03-23T02:50:00+00:00\", \"data\": [{\"valueUnit\": \"%\", \"typeId\": 0, \"typeValue\": \"2.066667\", \"valueType\": \"float\", \"typeName\": \"ControlCpuUsage\"}, {\"valueUnit\": \"%\", \"typeId\": 1, \"typeValue\": \"26.000000\", \"valueType\": \"float\", \"typeName\": \"MemoryUsage\"}, {\"valueUnit\": \"%\", \"typeId\": 2, \"typeValue\": \"10.000000\", \"valueType\": \"float\", \"typeName\": \"DiskUsage\"}, {\"valueUnit\": \"\u00b0C\", \"typeId\": 3, \"typeValue\": \"27.000000\", \"valueType\": \"float\", \"typeName\": \"Temperature\"}], \"annotatedFamilyId\": \"CPE_RESOURCE\", \"neId\": \"100\", \"indexes\": [{\"indexName\": \"CPE ID\", \"indexId\": 0, \"indexValue\": \"20dbab03f5ec\"}], \"neType\": \"cpefama\", \"familyId\": 601, \"neVersion\": \"v_0_2_3_28\"}"; 70 | 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/com/vzw/yang/services/TransformerStressTester.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright Verizon Inc. 3 | Licensed under the terms of the Apache License 2.0 license. See LICENSE file in project root for terms. 4 | */ 5 | package com.vzw.yang.services; 6 | 7 | import com.fasterxml.jackson.databind.ObjectMapper; 8 | 9 | import com.vzw.yang.transformer.YangTransformer; 10 | import com.vzw.yang.util.YangTransformerService; 11 | 12 | import org.apache.logging.log4j.LogManager; 13 | import org.apache.logging.log4j.Logger; 14 | import org.json.JSONObject; 15 | import org.springframework.beans.factory.annotation.Autowired; 16 | import org.springframework.beans.factory.annotation.Qualifier; 17 | 18 | /** 19 | * Created by randy on 8/25/2017. 20 | */ 21 | public class TransformerStressTester implements Runnable { 22 | private static final Logger logger = LogManager.getLogger(TransformerStressTester.class); 23 | 24 | @Autowired 25 | @Qualifier("yangTransformerService") 26 | private YangTransformerService transformer; 27 | 28 | @Autowired 29 | @Qualifier("yangTransformer") 30 | private YangTransformer yangTrans; 31 | 32 | 33 | private TransactionStatus transactionStatus; 34 | 35 | public TransformerStressTester(TransactionStatus transStatus) { 36 | transactionStatus = transStatus; 37 | } 38 | 39 | @Override 40 | public void run() { 41 | //InvokePyangScript script = new InvokePyangScript(); 42 | 43 | logger.info("TransformerStressTester.run: " + transactionStatus.getName() + " is running"); 44 | 45 | transactionStatus.resetCounter(); 46 | while (true) { 47 | try { 48 | //JsonToYangJsonRequest request = new JsonToYangJsonRequest("ENMV_SAMSUNG5G_CPEDATA", testJsonStr); 49 | //String yangJsonStr = transformer.jsonToYangJson(request); 50 | JSONObject response = yangTrans.jsonToYangJson("ENMV_SAMSUNG5G_CPEDATA", testJsonStr, false); 51 | transactionStatus.addCounter(); 52 | //prettyPrint(yangJsonStr); 53 | } catch(Exception e) { 54 | e.printStackTrace(); 55 | } 56 | } 57 | 58 | //logger.info("TransformerStressTester.run: " + transactionStatus.getName() + " has ended"); 59 | 60 | } 61 | 62 | private void prettyPrint(String json) { 63 | try { 64 | ObjectMapper mapper = new ObjectMapper(); 65 | logger.info("KafkaJsonService.test1: Have request"); 66 | Object jsonObj = mapper.readValue(json, Object.class); 67 | logger.info(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObj)); 68 | } catch(Exception e) { 69 | 70 | } 71 | 72 | } 73 | 74 | public static void main(String[] args) { 75 | 76 | } 77 | 78 | private String testJsonStr = "{\"eventTime\": \"2019-03-23T02:50:00+00:00\", \"data\": [{\"valueUnit\": \"%\", \"typeId\": 0, \"typeValue\": \"2.066667\", \"valueType\": \"float\", \"typeName\": \"ControlCpuUsage\"}, {\"valueUnit\": \"%\", \"typeId\": 1, \"typeValue\": \"26.000000\", \"valueType\": \"float\", \"typeName\": \"MemoryUsage\"}, {\"valueUnit\": \"%\", \"typeId\": 2, \"typeValue\": \"10.000000\", \"valueType\": \"float\", \"typeName\": \"DiskUsage\"}, {\"valueUnit\": \"\u00b0C\", \"typeId\": 3, \"typeValue\": \"27.000000\", \"valueType\": \"float\", \"typeName\": \"Temperature\"}], \"annotatedFamilyId\": \"CPE_RESOURCE\", \"neId\": \"100\", \"indexes\": [{\"indexName\": \"CPE ID\", \"indexId\": 0, \"indexValue\": \"20dbab03f5ec\"}], \"neType\": \"cpefama\", \"familyId\": 601, \"neVersion\": \"v_0_2_3_28\"}"; 79 | 80 | } 81 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | JSON to OpenConfig YANG JSON Transformer 2 | ===================================================== 3 | 4 | This directory contains the Java spring-boot json to OpenConfig json transformer. This software is used to help onboard equipent that is not yet fully OpenConfig compliant. Using this software enables companies that want to talk OpenConfig Json to be able to utilize these equipment without having to change their code as new devices are added. This enables quick deployment of equipment without having to change complex coding logic of handling telemetry, alarms, and configurations. 5 | 6 | The jtransformer does require the yang-validator to be running for access to data mappings. yang-validator can be found here: https://github.com/Verizon/YANG-validator. When running jtransformer, first start the yang-validator following its intructions. Then follow the jtransformer instructions below. There are sample test scripts below as well. Also added is a swagger UI for testing the validator. Should the yang-validator need to be started on a separate port, you can inform jtransformer of this port and ip location in pyang.properties. 7 | 8 | 9 | Build 10 | ----- 11 | mvn package 12 | 13 | Run 14 | --- 15 | mvn spring-boot:run 16 | 17 | Build and Run with Docker 18 | ------------------------- 19 | docker build -t jtransformer . 20 | docker run --rm -it -p 8088:8082 jtransformer 21 | 22 | Access Docker Logs 23 | ------------------ 24 | docker ps 25 | docker exec -it [value] bash 26 | 27 | To test you can use SWAGGER 28 | --------------------------- 29 | http://localhost:8082/swagger-ui.html 30 | 31 | 32 | Test Transformations 33 | -------------------- 34 | {"topic": "MSE_ALARMS_JUNIPER", "jsonStr": { "systemId": "MIAM2K01-BSYS-RE0", "componentId": 65535, "path": "sensor_1004:/junos/events/:/junos/events/:eventd", "sequenceNumber": "69910", "timestamp": "1564417305297", "kv": [ { "key": "__timestamp__", "uintValue": "1564420471913" }, { "key": "__junos_re_stream_creation_timestamp__", "uintValue": "1564417305297" }, { "key": "__junos_re_payload_get_timestamp__", "uintValue": "1564417305297" }, { "key": "__junos_re_event_timestamp__", "uintValue": "1564417305297" }, { "key": "__prefix__", "strValue": "/junos/events/event[id='PIC' and type='3' and facility='20']/" }, { "key": "timestamp/seconds", "uintValue": "1564417305" }, { "key": "timestamp/microseconds", "uintValue": "243533" }, { "key": "priority", "uintValue": "6" }, { "key": "pid", "uintValue": "0" }, { "key": "message", "strValue": "fpc11 tx lo pkt 0 : tx lo bytes 0 " }, { "key": "hostname", "strValue": "MIAM2K01-BSYS-RE0" }, { "key": "__prefix__", "strValue": "/junos/events/event[id='PIC' and type='3' and facility='20']/attributes[key='message']/" }, { "key": "value", "strValue": "fpc11 tx lo pkt 0 : tx lo bytes 0 " }, { "key": "__prefix__", "strValue": "/junos/events/event[id='PIC' and type='3' and facility='20']/" }, { "key": "logoptions", "intValue": "0" } ] }, "validate": false} 35 | 36 | 37 | Test Validation 38 | --------------- 39 | {"topic": "ENMV_SAMSUNG5G_CPEDATA", "jsonStr": {"perfdata": {"eventTime": "2019-03-23T02:50:00+00:00", "annotatedFamilyId": "CPE_RESOURCE", "neId": "100", "neType": "cpefama", "familyId": 601, "neVersion": "v_0_2_3_28", "indexData": {"0": {"indexId": 0, "indexName": "CPE ID", "indexValue": "20dbab03f5ec"}}, "payloadData": {"0": {"typeId": 0, "valueUnit": "%", "typeValue": "2.066667", "valueType": "float", "typeName": "ControlCpuUsage"}, "1": {"typeId": 1, "valueUnit": "%", "typeValue": "26.000000", "valueType": "float", "typeName": "MemoryUsage"}, "2": {"typeId": 2, "valueUnit": "%", "typeValue": "10.000000", "valueType": "float", "typeName": "DiskUsage"}, "3": {"typeId": 3, "valueUnit": "u00b0C", "typeValue": "27.000000", "valueType": "float", "typeName": "Temperature"}}}}} 40 | 41 | -------------------------------------------------------------------------------- /src/main/java/com/vzw/yang/test/Test.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright Verizon Inc. 3 | Licensed under the terms of the Apache License 2.0 license. See LICENSE file in project root for terms. 4 | */ 5 | package com.vzw.yang.test; 6 | 7 | import java.io.BufferedReader; 8 | import java.io.IOException; 9 | import java.io.InputStreamReader; 10 | import java.net.HttpURLConnection; 11 | import java.net.MalformedURLException; 12 | import java.net.URL; 13 | import java.util.ArrayList; 14 | import java.util.List; 15 | import java.util.Optional; 16 | 17 | import com.vzw.yang.services.TransactionStatus; 18 | import com.vzw.yang.services.TransformerDisplayCount; 19 | import com.vzw.yang.services.TransformerStressTester; 20 | 21 | //import org.opendaylight.yangtools.yang.model.api.SchemaContext; 22 | //import org.opendaylight.yangtools.yang.parser.repo.YangTextSchemaContextResolver; 23 | 24 | public class Test { 25 | 26 | public static void sendRequest(String request) { 27 | try { 28 | 29 | URL url = new URL("http://localhost:8080/jtransformer/rest/yangApiService/jsonToYangJson"); 30 | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 31 | conn.setRequestMethod("POST"); 32 | conn.setRequestProperty("Accept", "application/json"); 33 | conn.setDoOutput(true); 34 | 35 | conn.getOutputStream().write(request.getBytes()); 36 | 37 | if (conn.getResponseCode() != 200) { 38 | throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode()); 39 | } 40 | 41 | BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); 42 | 43 | String output; 44 | //System.out.println("Output from Server .... \n"); 45 | while ((output = br.readLine()) != null) { 46 | //System.out.println(output); 47 | } 48 | 49 | conn.disconnect(); 50 | 51 | } catch (MalformedURLException e) { 52 | 53 | e.printStackTrace(); 54 | 55 | } catch (IOException e) { 56 | 57 | e.printStackTrace(); 58 | 59 | } 60 | } 61 | 62 | public static void main(String args[]) { 63 | 64 | List statuses = new ArrayList(); 65 | 66 | Thread thread = new Thread(new TransformerDisplayCount(statuses)); 67 | thread.start(); 68 | 69 | TransactionStatus status = new TransactionStatus(); 70 | status.setName("Stress Tester: "); 71 | statuses.add(status); 72 | 73 | while (true) { 74 | status.addCounter(); 75 | Test.sendRequest("{\"topic\": \"MSE_ALARMS_JUNIPER\", \"jsonStr\": { \"systemId\": \"MIAM2K01-BSYS-RE0\", \"componentId\": 65535, \"path\": \"sensor_1004:/junos/events/:/junos/events/:eventd\", \"sequenceNumber\": \"69910\", \"timestamp\": \"1564417305297\", \"kv\": [ { \"key\": \"__timestamp__\", \"uintValue\": \"1564420471913\" }, { \"key\": \"__junos_re_stream_creation_timestamp__\", \"uintValue\": \"1564417305297\" }, { \"key\": \"__junos_re_payload_get_timestamp__\", \"uintValue\": \"1564417305297\" }, { \"key\": \"__junos_re_event_timestamp__\", \"uintValue\": \"1564417305297\" }, { \"key\": \"__prefix__\", \"strValue\": \"/junos/events/event[id='PIC' and type='3' and facility='20']/\" }, { \"key\": \"timestamp/seconds\", \"uintValue\": \"1564417305\" }, { \"key\": \"timestamp/microseconds\", \"uintValue\": \"243533\" }, { \"key\": \"priority\", \"uintValue\": \"6\" }, { \"key\": \"pid\", \"uintValue\": \"0\" }, { \"key\": \"message\", \"strValue\": \"fpc11 tx lo pkt 0 : tx lo bytes 0 \" }, { \"key\": \"hostname\", \"strValue\": \"MIAM2K01-BSYS-RE0\" }, { \"key\": \"__prefix__\", \"strValue\": \"/junos/events/event[id='PIC' and type='3' and facility='20']/attributes[key='message']/\" }, { \"key\": \"value\", \"strValue\": \"fpc11 tx lo pkt 0 : tx lo bytes 0 \" }, { \"key\": \"__prefix__\", \"strValue\": \"/junos/events/event[id='PIC' and type='3' and facility='20']/\" }, { \"key\": \"logoptions\", \"intValue\": \"0\" } ] }, \"validate\": false}"); 76 | } 77 | 78 | 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | jtransformer 8 | jar 9 | JAVA Yang Transformer 10 | 1.0 11 | 12 | 13 | org.springframework.boot 14 | spring-boot-starter-parent 15 | 2.2.2.RELEASE 16 | 17 | 18 | 19 | 1.8 20 | true 21 | true 22 | 23 | 24 | 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-logging 33 | 34 | 35 | 36 | 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-starter-web 41 | 42 | 43 | 44 | 45 | org.springframework.boot 46 | spring-boot-starter-log4j2 47 | 48 | 49 | 50 | 51 | com.lmax 52 | disruptor 53 | 3.4.2 54 | 55 | 56 | 57 | com.fasterxml.jackson.dataformat 58 | jackson-dataformat-yaml 59 | 60 | 61 | 62 | org.apache.commons 63 | commons-lang3 64 | 65 | 66 | 67 | org.json 68 | json 69 | 20190722 70 | 71 | 72 | 73 | junit 74 | junit 75 | 4.12 76 | 77 | 78 | 79 | 80 | 81 | io.springfox 82 | springfox-swagger-ui 83 | 2.9.2 84 | 85 | 86 | 87 | io.springfox 88 | springfox-swagger2 89 | 2.9.2 90 | 91 | 92 | 93 | 94 | org.springframework.boot 95 | spring-boot-starter-test 96 | test 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | org.springframework.boot 106 | spring-boot-maven-plugin 107 | 108 | 109 | 110 | org.apache.maven.plugins 111 | maven-surefire-plugin 112 | 2.22.0 113 | 114 | 115 | 116 | 117 | 118 | 119 | -------------------------------------------------------------------------------- /src/main/java/com/vzw/yang/transformer/ucs/MappingDetail.java: -------------------------------------------------------------------------------- 1 | package com.vzw.yang.transformer.ucs; 2 | 3 | 4 | import java.io.File; 5 | import java.io.IOException; 6 | 7 | import org.apache.logging.log4j.LogManager; 8 | import org.apache.logging.log4j.Logger; 9 | 10 | import com.fasterxml.jackson.databind.JsonNode; 11 | import com.fasterxml.jackson.databind.ObjectMapper; 12 | 13 | public class MappingDetail { 14 | private static final Logger logger = LogManager.getLogger(MappingDetail.class); 15 | private String topic; 16 | private String mappingFile; 17 | private boolean equipmentTopic; 18 | private JsonNode mappingObj; 19 | private String rootPath="C:\\vendorC\\yang-validator\\yang-validator\\transformer\\"; 20 | 21 | ObjectMapper mapper = new ObjectMapper(); 22 | 23 | private static MappingDetail[] mappingDetails = { 24 | new MappingDetail("ENMV_SAMSUNG5G_CPEDATA", "mappings\\samsungCpePerfdataYangMap.json", false), 25 | new MappingDetail( "BNC-FILTERED-ALARMS", "mappings\\bncFilteredAlarms.json" , false), 26 | new MappingDetail("UT_ALARMS_CIENA", "mappings\\UT_Ciena_AlarmMapper.json" , false), 27 | new MappingDetail("UT_ALARMS_CIENA_OBJ", "mappings\\UT_Ciena_AlarmMapperObject.json" , false), 28 | new MappingDetail("UT_ALARMS_CISCO", "mappings\\UT_Cisco_AlarmMapper.json" , false), 29 | new MappingDetail("UT_ALARMS_CISCO_CCS", "mappings\\UT_CiscoCCS_AlarmMapper.json" , false), 30 | new MappingDetail("UTS_EQUIPMENT", "mappings\\UTS_EquipmentMapper.json" , false), 31 | new MappingDetail("MSE_ALARMS_CISCO", "mappings\\MSE_Cisco_AlarmMapper.json" , false), 32 | new MappingDetail("UT_CISCO_STATE", "mappings\\UT_Cisco_StateMapper.json" , false), 33 | new MappingDetail("MSE_ALARMS_JUNIPER", "mappings\\MSE_Juniper_AlarmMapper.json" , false), 34 | new MappingDetail("UTS_EQUIPMENT_API", "mappings\\UTS_EquipmentMapper.json" , true), 35 | new MappingDetail("UTS_CIRCUIT_API", "mappings\\UTS_CircuitMapper.json" , false), 36 | new MappingDetail("MSE_PERF_JUNIPER", "mappings\\MSE_Juniper_PerfMapper.json" , false), 37 | new MappingDetail("EPNM_CONTROL_PLANE", "mappings\\EPNM_ControlPlaneMapper.json" , false), 38 | new MappingDetail("EPNM_CONTROL_PLANE_ACTIVE", "mappings\\EPNM_ControlPlaneMapperActiveAlarm.json" , false), 39 | new MappingDetail("SYSLOG_ALARMS", "mappings\\Syslog_AlarmMapper.json" , false) 40 | 41 | 42 | }; 43 | 44 | public MappingDetail(String topic, JsonNode obj){ 45 | this.topic = topic; 46 | this.mappingObj = obj; 47 | } 48 | public MappingDetail(String topic, String mappingFile, boolean equipmentTopic) { 49 | this.topic = topic; 50 | this.mappingFile = mappingFile; 51 | this.equipmentTopic = equipmentTopic; 52 | try { 53 | this.loadMappingDetails(); 54 | } catch ( IOException e) { 55 | logger.error("JsonProcessingException : ",e); 56 | } 57 | 58 | } 59 | 60 | public String getTopic() { 61 | return topic; 62 | } 63 | 64 | public void setTopic(String topic) { 65 | this.topic = topic; 66 | } 67 | 68 | public String getMappingFile() { 69 | return mappingFile; 70 | } 71 | 72 | public void setMappingFile(String mappingFile) { 73 | this.mappingFile = mappingFile; 74 | } 75 | 76 | public boolean isEquipmentTopic() { 77 | return equipmentTopic; 78 | } 79 | 80 | public void setEquipmentTopic(boolean equipmentTopic) { 81 | this.equipmentTopic = equipmentTopic; 82 | } 83 | 84 | 85 | 86 | public JsonNode getMappingObj() { 87 | return mappingObj; 88 | } 89 | 90 | public void setMappingObj(JsonNode mappingObj) { 91 | this.mappingObj = mappingObj; 92 | } 93 | 94 | public static MappingDetail findMappingDetail(String topic) { 95 | MappingDetail md = null; 96 | 97 | // Find the mapping 98 | for (MappingDetail m : mappingDetails) { 99 | if (topic.equals(m.getTopic())) { 100 | md = m; 101 | break; 102 | } 103 | } 104 | 105 | return md; 106 | } 107 | 108 | 109 | public boolean loadMappingDetails() throws IOException { 110 | boolean rc = false; 111 | 112 | MappingDetail md=this; 113 | logger.info("YangTransformer.loadMappingDetails: Entered"); 114 | 115 | if (md.getMappingObj() != null) { 116 | // already loaded 117 | logger.info("YangTransformer.loadMappingDetails: mappings already loaded"); 118 | 119 | }else { 120 | 121 | JsonNode rootNode=mapper.readTree(new File(rootPath+md.getMappingFile())); 122 | md.setMappingObj(rootNode); 123 | logger.info("YangTransformer.loadMappingDetails: mappings loaded"); 124 | logger.info("YangTransformer.loadMappingDetails: Exited"); 125 | } 126 | 127 | return rc; 128 | 129 | } 130 | 131 | 132 | 133 | 134 | } 135 | -------------------------------------------------------------------------------- /src/main/java/com/vzw/yang/util/InvokePyangScript.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright Verizon Inc. 3 | Licensed under the terms of the Apache License 2.0 license. See LICENSE file in project root for terms. 4 | */ 5 | package com.vzw.yang.util; 6 | 7 | import java.io.BufferedInputStream; 8 | import java.io.BufferedOutputStream; 9 | import java.io.ByteArrayOutputStream; 10 | import java.io.File; 11 | import java.io.PrintWriter; 12 | 13 | import org.apache.logging.log4j.LogManager; 14 | import org.apache.logging.log4j.Logger; 15 | 16 | public class InvokePyangScript { 17 | private static final Logger logger = LogManager.getLogger(InvokePyangScript.class); 18 | 19 | //private String pythonExecutable = "/Users/torrera/anaconda3/bin/python"; 20 | //private String pythonWorkingDirectory = "/Users/torrera/git/yang/poc/project/python/transformer"; 21 | private String pythonExecutable = "/usr/bin/python3"; 22 | private String pythonWorkingDirectory = "/transformer"; 23 | 24 | 25 | private Process process = null; 26 | private PrintWriter pw; 27 | BufferedInputStream bis; 28 | ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 29 | 30 | public InvokePyangScript() { 31 | logger.info("InvokePyangScript: Entered"); 32 | logger.info("InvokePyangScript: Exited"); 33 | } 34 | 35 | public String convertJsonToYangOld(String topic, String jsonStr) { 36 | String jsonResponse = null; 37 | String response = null; 38 | 39 | try { 40 | Process p = Runtime.getRuntime().exec(pythonExecutable + " jsonToYangParameter.py " + topic, null, new File(pythonWorkingDirectory)); 41 | BufferedInputStream bis = new BufferedInputStream(p.getInputStream()); 42 | ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 43 | byte[] bytes = new byte[2048]; 44 | while (true) { 45 | int len = bis.read(bytes); 46 | if (len <= 0) { 47 | break; 48 | } 49 | buffer.write(bytes, 0, len); 50 | //logger.info("Just read: " + buffer.toString()); 51 | if (buffer.toString().contains("Enter Json")) { 52 | PrintWriter pw = new PrintWriter(p.getOutputStream()); 53 | pw.println(jsonStr); 54 | pw.flush(); 55 | buffer = new ByteArrayOutputStream(); 56 | } 57 | } 58 | if (buffer.size() > 0) { 59 | jsonResponse = buffer.toString(); 60 | } 61 | 62 | int index = jsonResponse.indexOf("YANG_RESPONSE="); 63 | if (index >= 0) { 64 | response = jsonResponse.substring(index + 14); 65 | } 66 | 67 | //logger.info("InvokePyangScript.convertJsonToYang: response: " + response); 68 | } catch(Exception e) { 69 | e.printStackTrace(); 70 | } 71 | 72 | return response; 73 | } 74 | 75 | public void startPython() throws Exception { 76 | 77 | //logger.info("InvokePyangScript:startPython: Entered:"); 78 | process = Runtime.getRuntime().exec(pythonExecutable + " jsonToYangParameter.py ", null, new File(pythonWorkingDirectory)); 79 | bis = new BufferedInputStream(process.getInputStream()); 80 | buffer = new ByteArrayOutputStream(); 81 | pw = new PrintWriter(process.getOutputStream()); 82 | //logger.info("InvokePyangScript:startPython: Exited:"); 83 | 84 | 85 | } 86 | 87 | public String convertJsonToYang(String topic, String jsonStr) { 88 | String jsonResponse = null; 89 | String response = null; 90 | 91 | //logger.info("InvokePyangScript:convertJsonToYang: Entered: " + process); 92 | 93 | try { 94 | if (process == null) { 95 | startPython(); 96 | } 97 | 98 | // send topic 99 | pw.println(topic); 100 | 101 | // send json str 102 | pw.println(jsonStr); 103 | pw.flush(); 104 | 105 | // get output 106 | byte[] bytes = new byte[2048]; 107 | while (true) { 108 | int len = bis.read(bytes); 109 | if (len <= 0) { 110 | break; 111 | } 112 | buffer.write(bytes, 0, len); 113 | //logger.info("Just read: " + buffer.toString()); 114 | if (buffer.toString().contains("END PROCESSING")) { 115 | break; 116 | // PrintWriter pw = new PrintWriter(process.getOutputStream()); 117 | // pw.println(jsonStr); 118 | // pw.flush(); 119 | // buffer = new ByteArrayOutputStream(); 120 | } 121 | //logger.info("InvokePyangScript.convertJsonToYang: stdout: " + buffer.toString()); 122 | } 123 | if (buffer.size() > 0) { 124 | jsonResponse = buffer.toString(); 125 | } 126 | 127 | int index = jsonResponse.indexOf("YANG_RESPONSE="); 128 | if (index >= 0) { 129 | response = jsonResponse.substring(index + 14); 130 | } 131 | buffer = new ByteArrayOutputStream(); 132 | 133 | //logger.info("InvokePyangScript.convertJsonToYang: response: " + response); 134 | } catch(Exception e) { 135 | e.printStackTrace(); 136 | } 137 | //logger.info("InvokePyangScript:convertJsonToYang: Exited: " + response); 138 | 139 | 140 | return response; 141 | } 142 | 143 | 144 | } 145 | -------------------------------------------------------------------------------- /src/main/java/com/vzw/yang/transformer/TransformJuniperPerf.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright Verizon Inc. 3 | Licensed under the terms of the Apache License 2.0 license. See LICENSE file in project root for terms. 4 | */ 5 | package com.vzw.yang.transformer; 6 | 7 | import java.util.Iterator; 8 | import java.util.regex.*; 9 | 10 | 11 | import org.json.JSONArray; 12 | import org.json.JSONObject; 13 | import org.apache.logging.log4j.LogManager; 14 | import org.apache.logging.log4j.Logger; 15 | 16 | public class TransformJuniperPerf { 17 | 18 | private static final Logger logger = LogManager.getLogger(TransformJuniperPerf.class); 19 | private static Pattern p = Pattern.compile("(\\w+)\\[(\\w+)='(.*)'\\]"); 20 | 21 | static JSONObject transform(JSONObject jsonObj) { 22 | JSONObject retObj = new JSONObject(); 23 | Iterator keys = jsonObj.keys(); 24 | while (keys.hasNext()) { 25 | String key = keys.next(); 26 | if (!"kv".equals(key)) { 27 | Object obj = jsonObj.get(key); 28 | retObj.put(key,obj); 29 | } 30 | if ("kv".equals(key)){ 31 | JSONObject perfJSON = new JSONObject(); 32 | JSONArray kvList = jsonObj.getJSONArray(key); 33 | JSONObject currentJSON = null; 34 | for (int i=0; i< kvList.length();i++){ 35 | JSONObject item = kvList.getJSONObject(i); 36 | if (item.getString("key").equals("__prefix__")){ 37 | logger.info(item.getString("str_value")); 38 | currentJSON = buildJsonObject(perfJSON,item.getString("str_value"), null); 39 | } 40 | else if (item.getString("key").contains("/")){ 41 | String value = valueType(item); 42 | buildJsonObject(currentJSON,item.getString("key"), item.getString(value)); 43 | } 44 | } 45 | retObj.put("perf", perfJSON); 46 | } 47 | } 48 | return retObj; 49 | } 50 | static String valueType(JSONObject jSON) 51 | { 52 | String retVal = null; 53 | Iterator keys = jSON.keys(); 54 | while (keys.hasNext()) { 55 | String key = keys.next(); 56 | if (key.contains("_value")){ 57 | return key; 58 | } 59 | } 60 | return retVal; 61 | } 62 | static JSONObject buildJsonObject(JSONObject tsd, String prefix, String value) 63 | { 64 | prefix = encodeSlash(prefix); 65 | JSONObject currentJsonObject = tsd; 66 | String prefixArray[] = prefix.split("/"); 67 | 68 | 69 | for (int i=0;i keys = jsonObj.fieldNames(); 25 | while (keys.hasNext()) { 26 | String key = keys.next(); 27 | if (!"kv".equals(key)) { 28 | JsonNode obj = jsonObj.get(key); 29 | retObj.set(key,obj); 30 | } 31 | if ("kv".equals(key)){ 32 | ObjectNode perfJSON = JsonNodeFactory.instance.objectNode(); 33 | JsonNode kvList = jsonObj.get(key); 34 | ObjectNode currentJSON = null; 35 | for (JsonNode item : kvList){ 36 | if (item.get("key").asText().equals("__prefix__")){ 37 | logger.info(item.get("str_value")); 38 | currentJSON = buildJsonObject(perfJSON,item.get("str_value").asText(), null); 39 | } 40 | else if (item.get("key").asText().contains("/")){ 41 | String value = valueType((ObjectNode)item); 42 | buildJsonObject(currentJSON,item.get("key").asText(), item.get(value).asText()); 43 | } 44 | } 45 | retObj.set("perf", perfJSON); 46 | } 47 | } 48 | return retObj; 49 | } 50 | static String valueType(ObjectNode jSON) 51 | { 52 | String retVal = null; 53 | Iterator keys = jSON.fieldNames(); 54 | while (keys.hasNext()) { 55 | String key = keys.next(); 56 | if (key.contains("_value")){ 57 | return key; 58 | } 59 | } 60 | return retVal; 61 | } 62 | static ObjectNode buildJsonObject(ObjectNode tsd, String prefix, String value) 63 | { 64 | prefix = encodeSlash(prefix); 65 | ObjectNode currentJsonObject = tsd; 66 | String prefixArray[] = prefix.split("/"); 67 | 68 | 69 | for (int i=0;i body = getQueryMap(Txml); 69 | ///HttpEntity httpEntity = new HttpEntity(body, requestHeaders); 70 | 71 | HttpEntity httpEntity = new HttpEntity(requestStr, requestHeaders); 72 | String url = "http://" + host + ":" + port + "/" + api; 73 | Tresponse = restTemplate.postForObject(url, httpEntity, String.class); 74 | 75 | //String wellFormedJson = JsonSanitizer.sanitize(Tresponse); 76 | //JSONObject json = new JSONObject(Tresponse); 77 | //logger.info("Have criteria object as json: " + mapper.writerWithDefaultPrettyPrinter().writeValueAsString(Tresponse)); 78 | 79 | } catch(Exception e) { 80 | Terror = "Unable to open the URL: " + e.getMessage(); 81 | System.err.println("ValidateAddress.performAddressValidation " +Terror); 82 | e.printStackTrace(); 83 | break; 84 | } 85 | 86 | } while(false); 87 | 88 | if (Terror != null) { 89 | throw new Exception(Terror); 90 | } 91 | 92 | return (Tresponse); 93 | 94 | } 95 | 96 | public String validateYang(JsonToYangJsonRequest request) throws Exception { 97 | 98 | String Terror = null; 99 | String Tresponse = null; 100 | 101 | do { 102 | try { 103 | ObjectMapper mapper = new ObjectMapper(); 104 | String requestStr = mapper.writeValueAsString(request); 105 | 106 | RestTemplate restTemplate = new RestTemplate(); 107 | HttpHeaders requestHeaders = new HttpHeaders(); 108 | requestHeaders.setContentType(MediaType.APPLICATION_JSON); 109 | 110 | HttpEntity httpEntity = new HttpEntity(requestStr, requestHeaders); 111 | String url = "http://" + host + ":" + port + "/validateYang"; 112 | logger.info("YangTrnasfoerm.jsonToYangJson: Using url: " + url); 113 | 114 | Tresponse = restTemplate.postForObject(url, httpEntity, String.class); 115 | 116 | } catch(Exception e) { 117 | Terror = "Unable to open the URL: " + e.getMessage(); 118 | System.err.println("ValidateAddress.validateYang " +Terror); 119 | e.printStackTrace(); 120 | break; 121 | } 122 | 123 | } while(false); 124 | 125 | if (Terror != null) { 126 | throw new Exception(Terror); 127 | } 128 | 129 | return (Tresponse); 130 | 131 | } 132 | 133 | public String getMappingDetails() throws Exception { 134 | 135 | String Terror = null; 136 | String Tresponse = null; 137 | 138 | do { 139 | try { 140 | RestTemplate restTemplate = new RestTemplate(); 141 | 142 | String url = "http://" + host + ":" + (port + portIncrement) + "/getMappingDetails"; 143 | Tresponse = restTemplate.getForObject(url, String.class); 144 | } catch(Exception e) { 145 | Terror = "Unable to open the URL: " + e.getMessage(); 146 | System.err.println("ValidateAddress.validateYang " +Terror); 147 | e.printStackTrace(); 148 | break; 149 | } 150 | 151 | } while(false); 152 | 153 | if (Terror != null) { 154 | throw new Exception(Terror); 155 | } 156 | 157 | return (Tresponse); 158 | 159 | } 160 | 161 | 162 | } 163 | -------------------------------------------------------------------------------- /src/main/java/com/vzw/yang/rest/YangApiService.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright Verizon Inc. 3 | Licensed under the terms of the Apache License 2.0 license. See LICENSE file in project root for terms. 4 | */ 5 | package com.vzw.yang.rest; 6 | 7 | import org.apache.logging.log4j.LogManager; 8 | import org.apache.logging.log4j.Logger; 9 | import org.json.JSONObject; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.beans.factory.annotation.Qualifier; 12 | import org.springframework.stereotype.Controller; 13 | import org.springframework.web.bind.annotation.CrossOrigin; 14 | import org.springframework.web.bind.annotation.GetMapping; 15 | import org.springframework.web.bind.annotation.PostMapping; 16 | import org.springframework.web.bind.annotation.RequestBody; 17 | import org.springframework.web.bind.annotation.RequestMapping; 18 | import org.springframework.web.bind.annotation.ResponseBody; 19 | import org.springframework.web.bind.annotation.RestController; 20 | 21 | import com.fasterxml.jackson.databind.ObjectMapper; 22 | import com.vzw.yang.data.JsonToYangJsonRequest; 23 | import com.vzw.yang.transformer.YangTransformer; 24 | import com.vzw.yang.util.YangTransformerService; 25 | 26 | import javax.annotation.PostConstruct; 27 | import javax.servlet.http.HttpServletRequest; 28 | 29 | import java.io.IOException; 30 | import java.net.URLDecoder; 31 | import java.util.HashMap; 32 | import java.util.Map; 33 | 34 | 35 | /** 36 | * Restful web service for fetching issue specific questions. 37 | * 38 | */ 39 | 40 | 41 | @RestController 42 | @RequestMapping("/yangApiService") 43 | @CrossOrigin(origins = {"http://localhost:4200", "http://localhost:8080"}) 44 | public class YangApiService { 45 | private Logger logger = LogManager.getLogger(YangApiService.class); 46 | 47 | @Autowired 48 | @Qualifier("yangTransformer") 49 | private YangTransformer transformer; 50 | 51 | @Autowired 52 | @Qualifier("yangTransformerService") 53 | private YangTransformerService transService; 54 | 55 | @PostConstruct 56 | public void init() { 57 | logger.info("init: Entered:"); 58 | logger.info("init: Exited:"); 59 | } 60 | 61 | @PostMapping(value = "jsonToYangJson", produces="application/json; charset=UTF-8") 62 | public @ResponseBody String jsonToYangJson( 63 | HttpServletRequest request, @RequestBody String jsonRequest) throws IOException { 64 | 65 | JSONObject jsonResult = null; 66 | 67 | logger.info("YangApiService.jsonToYangJson: Entered: Request = " + jsonRequest); 68 | 69 | try { 70 | do { 71 | 72 | JSONObject jsonObj = new JSONObject(jsonRequest); 73 | String topic = jsonObj.getString("topic"); 74 | String jsonStr = jsonObj.getJSONObject("jsonStr").toString(); 75 | boolean validate = false; 76 | if (jsonObj.has("validate")) { 77 | validate = jsonObj.getBoolean("validate"); 78 | } 79 | 80 | jsonResult = transformer.jsonToYangJson(topic, jsonStr, validate); 81 | 82 | // display the string 83 | prettyPrintJson(jsonResult.toString()); 84 | 85 | } while (false); 86 | } catch(Exception e) { 87 | e.printStackTrace(); 88 | return getModelMapError(e.getMessage()); 89 | } 90 | logger.info("YangApiService.jsonToYangJson: Exited"); 91 | 92 | return getSuccessMap(jsonResult); 93 | 94 | } 95 | 96 | @PostMapping(value = "validate", produces="application/json; charset=UTF-8") 97 | public String validate(@RequestBody String jsonRequest) throws IOException { 98 | 99 | JSONObject jsonResult = new JSONObject(); 100 | 101 | logger.info("YangApiService.validate: Entered: Request = " + jsonRequest); 102 | 103 | try { 104 | do { 105 | 106 | JSONObject jsonObj = new JSONObject(jsonRequest); 107 | String topic = jsonObj.getString("topic"); 108 | String jsonStr = jsonObj.getJSONObject("jsonStr").toString(); 109 | 110 | JsonToYangJsonRequest request = new JsonToYangJsonRequest(); 111 | request.setTopic(topic); 112 | request.setJsonStr(jsonStr); 113 | 114 | String result = transService.validateYang(request); 115 | logger.info("validate: Validationg result: " + result); 116 | jsonResult.put("Result", result); 117 | 118 | } while (false); 119 | } catch(Exception e) { 120 | e.printStackTrace(); 121 | jsonResult.put("Error", e.getMessage()); 122 | } 123 | logger.info("YangApiService.validate: Exited"); 124 | 125 | return jsonResult.toString(); 126 | 127 | } 128 | 129 | @GetMapping(value = "getMappingDetails") 130 | public String getMappingDetails() { 131 | 132 | String mappings = ""; 133 | 134 | logger.info("YangApiService.getMappingDetails: Entered:"); 135 | 136 | try { 137 | mappings = transService.getMappingDetails(); 138 | prettyPrintJson(mappings); 139 | 140 | } catch(Exception e) { 141 | e.printStackTrace(); 142 | return getModelMapError(e.getMessage()); 143 | } 144 | logger.info("YangApiService.getMappingDetails: Exited"); 145 | 146 | return mappings; 147 | 148 | } 149 | 150 | @GetMapping(value = "reloadMappings") 151 | public String reloadMappings() { 152 | 153 | String mappings = ""; 154 | 155 | logger.info("YangApiService.reloadMappings: Entered:"); 156 | 157 | try { 158 | transformer.reloadMappings(); 159 | 160 | mappings = transService.getMappingDetails(); 161 | prettyPrintJson(mappings); 162 | 163 | } catch(Exception e) { 164 | e.printStackTrace(); 165 | return getModelMapError(e.getMessage()); 166 | } 167 | logger.info("YangApiService.reloadMappings: Exited"); 168 | 169 | return mappings; 170 | 171 | } 172 | 173 | 174 | private void prettyPrintJson(String jsonStr) { 175 | try { 176 | ObjectMapper mapper = new ObjectMapper(); 177 | Object jsonObj = mapper.readValue(jsonStr, Object.class); 178 | logger.info("Have criteria object as json: " + mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObj)); 179 | } catch(Exception e) { 180 | e.printStackTrace(); 181 | } 182 | } 183 | 184 | private String getSuccessMap(JSONObject jsonObj){ 185 | 186 | JSONObject resultObj = new JSONObject(); 187 | resultObj.put("response", jsonObj); 188 | resultObj.put("success", true); 189 | 190 | return resultObj.toString(); 191 | } 192 | 193 | private String getModelMapError(String msg){ 194 | 195 | JSONObject resultObj = new JSONObject(); 196 | resultObj.put("message", msg); 197 | resultObj.put("success", false); 198 | 199 | return resultObj.toString(); 200 | } 201 | 202 | } 203 | -------------------------------------------------------------------------------- /src/main/java/com/vzw/yang/test/UCSMappersCienaUT.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright Verizon Inc. 3 | Licensed under the terms of the Apache License 2.0 license. See LICENSE file in project root for terms. 4 | */ 5 | package com.vzw.yang.test; 6 | 7 | import static org.junit.Assert.*; 8 | 9 | import java.io.File; 10 | import java.io.IOException; 11 | 12 | import org.junit.Test; 13 | 14 | import com.fasterxml.jackson.core.JsonGenerationException; 15 | import com.fasterxml.jackson.databind.JsonMappingException; 16 | import com.fasterxml.jackson.databind.JsonNode; 17 | import com.fasterxml.jackson.databind.ObjectMapper; 18 | import com.fasterxml.jackson.databind.node.ObjectNode; 19 | import com.vzw.yang.transformer.ucs.YangTransformer; 20 | 21 | public class UCSMappersCienaUT { 22 | 23 | private static final ObjectMapper mapper = new ObjectMapper(); 24 | 25 | @Test 26 | public void test1() { 27 | try { 28 | String file = "c:\\vendorC\\poc\\project\\python\\transformer\\CienaUTArray.json"; 29 | JsonNode root = mapper.readTree(new File(file)); 30 | System.out.println(root.toString()); 31 | YangTransformer transformer = new YangTransformer(); 32 | try { 33 | String json = transformer.jsonToYangJson("UT_ALARMS_CIENA_OBJ", (ObjectNode) root, false) ; 34 | System.out.println(json); 35 | } catch (Exception e) { 36 | e.printStackTrace(); 37 | } 38 | } catch (JsonGenerationException e) { 39 | e.printStackTrace(); 40 | fail("JsonGenerationException"); 41 | } catch (JsonMappingException e) { 42 | e.printStackTrace(); 43 | fail("JsonMappingException"); 44 | } catch (IOException e) { 45 | e.printStackTrace(); 46 | fail("IOException"); 47 | } 48 | } 49 | 50 | @Test 51 | public void test2() { 52 | try { 53 | String file = "c:\\vendorC\\poc\\project\\python\\transformer\\CienaUTMeta.json"; 54 | JsonNode root = mapper.readTree(new File(file)); 55 | System.out.println(root.toString()); 56 | YangTransformer transformer = new YangTransformer(); 57 | try { 58 | String json = transformer.jsonToYangJson("UT_ALARMS_CIENA_OBJ", (ObjectNode) root, false) ; 59 | System.out.println(json); 60 | } catch (Exception e) { 61 | e.printStackTrace(); 62 | } 63 | } catch (JsonGenerationException e) { 64 | e.printStackTrace(); 65 | fail("JsonGenerationException"); 66 | } catch (JsonMappingException e) { 67 | e.printStackTrace(); 68 | fail("JsonMappingException"); 69 | } catch (IOException e) { 70 | e.printStackTrace(); 71 | fail("IOException"); 72 | } 73 | } 74 | 75 | @Test 76 | public void test3() { 77 | try { 78 | String file = "c:\\vendorC\\poc\\project\\python\\transformer\\CienaUTMetaAddAttrMissVal.json"; 79 | JsonNode root = mapper.readTree(new File(file)); 80 | System.out.println(root.toString()); 81 | YangTransformer transformer = new YangTransformer(); 82 | try { 83 | String json = transformer.jsonToYangJson("UT_ALARMS_CIENA_OBJ", (ObjectNode) root, false) ; 84 | System.out.println(json); 85 | } catch (Exception e) { 86 | e.printStackTrace(); 87 | } 88 | } catch (JsonGenerationException e) { 89 | e.printStackTrace(); 90 | fail("JsonGenerationException"); 91 | } catch (JsonMappingException e) { 92 | e.printStackTrace(); 93 | fail("JsonMappingException"); 94 | } catch (IOException e) { 95 | e.printStackTrace(); 96 | fail("IOException"); 97 | } 98 | } 99 | @Test 100 | public void test4() { 101 | try { 102 | String file = "c:\\vendorC\\poc\\project\\python\\transformer\\CienaUTMetaNoAddAttr.json"; 103 | JsonNode root = mapper.readTree(new File(file)); 104 | System.out.println(root.toString()); 105 | YangTransformer transformer = new YangTransformer(); 106 | try { 107 | String json = transformer.jsonToYangJson("UT_ALARMS_CIENA_OBJ", (ObjectNode) root, false) ; 108 | System.out.println(json); 109 | } catch (Exception e) { 110 | e.printStackTrace(); 111 | } 112 | } catch (JsonGenerationException e) { 113 | e.printStackTrace(); 114 | fail("JsonGenerationException"); 115 | } catch (JsonMappingException e) { 116 | e.printStackTrace(); 117 | fail("JsonMappingException"); 118 | } catch (IOException e) { 119 | e.printStackTrace(); 120 | fail("IOException"); 121 | } 122 | } 123 | @Test 124 | public void test5() { 125 | try { 126 | String file = "c:\\vendorC\\poc\\project\\python\\transformer\\CienaUTMetaNoAddAttr1.json"; 127 | JsonNode root = mapper.readTree(new File(file)); 128 | System.out.println(root.toString()); 129 | YangTransformer transformer = new YangTransformer(); 130 | try { 131 | String json = transformer.jsonToYangJson("UT_ALARMS_CIENA_OBJ", (ObjectNode) root, false) ; 132 | System.out.println(json); 133 | } catch (Exception e) { 134 | e.printStackTrace(); 135 | } 136 | } catch (JsonGenerationException e) { 137 | e.printStackTrace(); 138 | fail("JsonGenerationException"); 139 | } catch (JsonMappingException e) { 140 | e.printStackTrace(); 141 | fail("JsonMappingException"); 142 | } catch (IOException e) { 143 | e.printStackTrace(); 144 | fail("IOException"); 145 | } 146 | } 147 | @Test 148 | public void test6() { 149 | try { 150 | String file = "c:\\vendorC\\poc\\project\\python\\transformer\\CienaUTMetaNoAddAttr2.json"; 151 | JsonNode root = mapper.readTree(new File(file)); 152 | System.out.println(root.toString()); 153 | YangTransformer transformer = new YangTransformer(); 154 | try { 155 | String json = transformer.jsonToYangJson("UT_ALARMS_CIENA_OBJ", (ObjectNode) root, false) ; 156 | System.out.println(json); 157 | } catch (Exception e) { 158 | e.printStackTrace(); 159 | } 160 | } catch (JsonGenerationException e) { 161 | e.printStackTrace(); 162 | fail("JsonGenerationException"); 163 | } catch (JsonMappingException e) { 164 | e.printStackTrace(); 165 | fail("JsonMappingException"); 166 | } catch (IOException e) { 167 | e.printStackTrace(); 168 | fail("IOException"); 169 | } 170 | } 171 | @Test 172 | public void test7() { 173 | try { 174 | String file = "c:\\vendorC\\poc\\project\\python\\transformer\\CienaUTMetaNoAddAttr3.json"; 175 | JsonNode root = mapper.readTree(new File(file)); 176 | System.out.println(root.toString()); 177 | YangTransformer transformer = new YangTransformer(); 178 | try { 179 | String json = transformer.jsonToYangJson("UT_ALARMS_CIENA_OBJ", (ObjectNode) root, false) ; 180 | System.out.println(json); 181 | } catch (Exception e) { 182 | e.printStackTrace(); 183 | } 184 | } catch (JsonGenerationException e) { 185 | e.printStackTrace(); 186 | fail("JsonGenerationException"); 187 | } catch (JsonMappingException e) { 188 | e.printStackTrace(); 189 | fail("JsonMappingException"); 190 | } catch (IOException e) { 191 | e.printStackTrace(); 192 | fail("IOException"); 193 | } 194 | } 195 | @Test 196 | public void test8() { 197 | try { 198 | String file = "c:\\vendorC\\poc\\project\\python\\transformer\\CienaUTMetaNoAddAttr4.json"; 199 | JsonNode root = mapper.readTree(new File(file)); 200 | System.out.println(root.toString()); 201 | YangTransformer transformer = new YangTransformer(); 202 | try { 203 | String json = transformer.jsonToYangJson("UT_ALARMS_CIENA_OBJ", (ObjectNode) root, false) ; 204 | System.out.println(json); 205 | } catch (Exception e) { 206 | e.printStackTrace(); 207 | } 208 | } catch (JsonGenerationException e) { 209 | e.printStackTrace(); 210 | fail("JsonGenerationException"); 211 | } catch (JsonMappingException e) { 212 | e.printStackTrace(); 213 | fail("JsonMappingException"); 214 | } catch (IOException e) { 215 | e.printStackTrace(); 216 | fail("IOException"); 217 | } 218 | } 219 | @Test 220 | public void test9() { 221 | try { 222 | String file = "c:\\vendorC\\poc\\project\\python\\transformer\\CienaUTObject.json"; 223 | JsonNode root = mapper.readTree(new File(file)); 224 | System.out.println(root.toString()); 225 | YangTransformer transformer = new YangTransformer(); 226 | try { 227 | String json = transformer.jsonToYangJson("UT_ALARMS_CIENA_OBJ", (ObjectNode) root, false) ; 228 | System.out.println(json); 229 | } catch (Exception e) { 230 | e.printStackTrace(); 231 | } 232 | } catch (JsonGenerationException e) { 233 | e.printStackTrace(); 234 | fail("JsonGenerationException"); 235 | } catch (JsonMappingException e) { 236 | e.printStackTrace(); 237 | fail("JsonMappingException"); 238 | } catch (IOException e) { 239 | e.printStackTrace(); 240 | fail("IOException"); 241 | } 242 | } 243 | } 244 | 245 | 246 | -------------------------------------------------------------------------------- /src/main/java/com/vzw/yang/transformer/ucs/YangTransformer.java: -------------------------------------------------------------------------------- 1 | //package com.verizon.eclipse.stream.processor.yang; 2 | package com.vzw.yang.transformer.ucs; 3 | 4 | import java.lang.reflect.InvocationTargetException; 5 | import java.lang.reflect.Method; 6 | import java.text.SimpleDateFormat; 7 | import java.util.Calendar; 8 | import java.util.Hashtable; 9 | import java.util.Iterator; 10 | import java.util.regex.Matcher; 11 | import java.util.regex.Pattern; 12 | 13 | import org.apache.commons.lang3.StringUtils; 14 | import org.apache.logging.log4j.LogManager; 15 | import org.apache.logging.log4j.Logger; 16 | 17 | import org.json.JSONObject; 18 | 19 | import com.fasterxml.jackson.databind.JsonNode; 20 | import com.fasterxml.jackson.databind.ObjectMapper; 21 | import com.fasterxml.jackson.databind.node.ArrayNode; 22 | import com.fasterxml.jackson.databind.node.JsonNodeFactory; 23 | import com.fasterxml.jackson.databind.node.ObjectNode; 24 | //import com.verizon.eclipse.stream.mapper.MappingDetail; 25 | 26 | /********************************************************************** 27 | ** Process responses from CAMEO 28 | **********************************************************************/ 29 | 30 | public class YangTransformer { 31 | 32 | private static final Logger logger = LogManager.getLogger(YangTransformer.class); 33 | private static final Hashtable > hashTable = new Hashtable > (); 34 | ObjectMapper mapper = new ObjectMapper(); 35 | 36 | public String jsonToYangJson(String topic, ObjectNode rootNode, boolean validate) throws Exception { 37 | String yangJsonStr = null; 38 | String error = null; 39 | MappingDetail md = MappingDetail.findMappingDetail(topic); 40 | if (md == null) { 41 | error = "Unable to find topic: " + topic; 42 | 43 | } else if (md.isEquipmentTopic()) { 44 | error = "Equipment not supported yet: " + topic; 45 | 46 | }else { 47 | yangJsonStr = convertToYangJson(md, rootNode, validate); 48 | } 49 | if (error != null) { 50 | throw new Exception(error); 51 | } 52 | 53 | return yangJsonStr; 54 | } 55 | 56 | 57 | 58 | 59 | 60 | @SuppressWarnings("deprecation") 61 | public String convertToYangJson(MappingDetail md, ObjectNode jsonObj, boolean validate) throws Exception { 62 | String yangJsonStr = null; 63 | String error = null; 64 | 65 | 66 | JsonNode mapper = md.getMappingObj(); 67 | if (mapper == null) { 68 | error = "No mappings loaded"; 69 | 70 | }else { 71 | 72 | 73 | ObjectNode rootJson = jsonObj; 74 | 75 | // Check for basetransformer processing required 76 | if (mapper.has("basetransformer")) { 77 | // Perform base mapping logic here 78 | String baseTransformer = mapper.get("basetransformer").asText(); 79 | jsonObj = transformBaseJson(jsonObj, baseTransformer); 80 | } 81 | 82 | ObjectNode resultYangObj =JsonNodeFactory.instance.objectNode(); 83 | ObjectNode yangObj = JsonNodeFactory.instance.objectNode(); 84 | 85 | resultYangObj.put(mapper.get("rootNode").asText(), yangObj); 86 | 87 | if (hasDict(mapper)) 88 | convertToYangJsonWithMapping(mapper, yangObj, jsonObj, rootJson); 89 | else 90 | resultYangObj.put(mapper.get("rootNode").asText(), jsonObj); 91 | 92 | 93 | yangJsonStr = resultYangObj.toString(); 94 | 95 | //if(time>200) { 96 | //System.out.println("Processing Time : "+time); 97 | //} 98 | 99 | 100 | /*if (validate) { 101 | prettyPrintJson(yangJsonStr); 102 | 103 | JsonToYangJsonRequest request = new JsonToYangJsonRequest(); 104 | request.setTopic(md.getTopic()); 105 | request.setJsonStr(yangJsonStr); 106 | 107 | String result = transService.validateYang(request); 108 | logger.info("YangTransformer.convertToYangJson: Validationg result: " + result); 109 | }*/ 110 | 111 | } 112 | 113 | 114 | 115 | if (error != null) { 116 | throw new Exception(error); 117 | } 118 | 119 | return yangJsonStr; 120 | } 121 | 122 | private ObjectNode transformBaseJson(ObjectNode jsonObj, String baseTransformer) { 123 | ObjectNode retObj = jsonObj; 124 | 125 | logger.info("YangTransformer.transformBaseJson: Entered"); 126 | 127 | String[] moduleClassMethod = baseTransformer.split("\\."); 128 | try { 129 | Class klas = Class.forName("com.vzw.yang.transformer.ucs." + moduleClassMethod[1]); 130 | 131 | Method method = klas.getDeclaredMethod(moduleClassMethod[2], ObjectNode.class); 132 | retObj = (ObjectNode) method.invoke(null, jsonObj); 133 | } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException 134 | | IllegalArgumentException | InvocationTargetException e) { 135 | e.printStackTrace(); 136 | return (retObj); 137 | } 138 | logger.info(retObj.toString()); 139 | logger.info("YangTransformer.transformBaseJson: Exited"); 140 | return retObj; 141 | } 142 | 143 | @SuppressWarnings("deprecation") 144 | private void convertToYangJsonWithMapping(JsonNode mapper, ObjectNode yangObj, ObjectNode jsonObj, ObjectNode rootJson) throws Exception { 145 | String error = null; 146 | 147 | 148 | do { 149 | Iterator keys =mapper.fieldNames(); 150 | while (keys.hasNext()) { 151 | String key = keys.next(); 152 | if (key.equals("modelName") || key.equals("pyangObj") || key.equals("rootNode") || key.equals("basetransformer") || key.contentEquals("convert")) { 153 | continue; 154 | } 155 | 156 | JsonNode mObj = mapper.get(key); 157 | String type=mObj.get("type").asText(); 158 | if (type.equals("string")) { 159 | String result = getJsonStringValue(jsonObj, mObj.get("tag").asText(), mObj, rootJson); 160 | if (result != null) { 161 | yangObj.put(key, result); 162 | } 163 | } else if (type.equals("string-list")) { 164 | String result = getJsonStringValue(jsonObj, mObj.get("tag").asText(), mObj, rootJson); 165 | ArrayNode ja = JsonNodeFactory.instance.arrayNode(); 166 | yangObj.put(key, ja); 167 | String[] stringList = result.split(","); 168 | for (String str : stringList) { 169 | ja.add(str); 170 | } 171 | } else if (type.equals("int")) { 172 | Integer result = getJsonIntValue(jsonObj, mObj.get("tag").asText(), mObj, rootJson); 173 | if (result != null) { 174 | yangObj.put(key, result); 175 | } 176 | } else if (type.equals("rawAlarmText")) { 177 | 178 | String rawAlarm = null; 179 | if (rootJson.get("raw") != null ){ 180 | rawAlarm = rootJson.get("raw").asText(); 181 | } 182 | else 183 | { 184 | rawAlarm = rootJson.toString(); 185 | } 186 | rawAlarm = rawAlarm.replaceAll("\n", ""); 187 | yangObj.put(key, rawAlarm); 188 | } else if (type.equals("metaData")) { 189 | if (rootJson.has("metaData")) { 190 | String metaData = rootJson.get("metaData").toString(); 191 | if (metaData != null) { 192 | metaData = metaData.replaceAll("\n", ""); 193 | yangObj.put(key, metaData); 194 | } 195 | } 196 | } else if (type.equals("list")) { 197 | ObjectNode list = getJsonListValue(jsonObj, mObj.get("tag").asText(), mObj, rootJson); 198 | if (list != null) { 199 | yangObj.put(key, list); 200 | } 201 | } else if (type.equals("stringx")) { 202 | ObjectNode jo = this.getJsonStringXValue(jsonObj, mObj.get("tag").asText(), mObj, rootJson); 203 | if (jo != null) { 204 | yangObj.put(key, jo); 205 | } 206 | } else if (type.equals("grouping")) { 207 | ObjectNode groupJsonObj = getJsonObject(jsonObj, mObj.get("tag").asText()); 208 | JsonNode groupMObj = mObj.get("grouping"); 209 | ObjectNode newObj = JsonNodeFactory.instance.objectNode(); 210 | yangObj.put(key, newObj); 211 | 212 | ObjectNode saveRootObj = rootJson; 213 | convertToYangJsonWithMapping(groupMObj, newObj, groupJsonObj, rootJson); 214 | rootJson = saveRootObj; 215 | 216 | } 217 | } 218 | 219 | } while(false); 220 | 221 | 222 | if (error != null) { 223 | throw new Exception(error); 224 | } 225 | 226 | } 227 | private boolean hasDict(JsonNode mapper2) { 228 | boolean hasDict = false; 229 | Iterator keys = (Iterator) mapper2.fieldNames(); 230 | while (keys.hasNext()) { 231 | String key = keys.next(); 232 | 233 | Object mObj = mapper2.get(key); 234 | if (mObj instanceof ObjectNode) { 235 | hasDict = true; 236 | } 237 | } 238 | 239 | return hasDict; 240 | } 241 | private ObjectNode getJsonObject(ObjectNode jsonObj, String tag) { 242 | ObjectNode currentObj = jsonObj; 243 | try { 244 | String[] tags = tag.split("->"); 245 | for (String t : tags) { 246 | if (!currentObj.has(t)) { 247 | continue; 248 | } 249 | if (currentObj.get(t).isArray()) { 250 | for (final JsonNode objNode : currentObj.get(t)) { 251 | currentObj=(ObjectNode)objNode; 252 | break; 253 | } 254 | 255 | } else { 256 | currentObj = (ObjectNode)currentObj.get(t); 257 | } 258 | } 259 | } catch(Exception e) { 260 | e.printStackTrace(); 261 | currentObj = null; 262 | } 263 | return currentObj; 264 | } 265 | 266 | private String getJsonStringValue(ObjectNode jsonObj, String tag, JsonNode mObj, ObjectNode rootJson) { 267 | String retValue = null; 268 | ObjectNode currentObj = jsonObj; 269 | 270 | try { 271 | String[] tags = tag.split("->"); 272 | for (String t : tags) { 273 | if (retValue != null) { 274 | throw new Exception("bad json walk"); 275 | } 276 | if ((tags.length > 1) && t.equals("ROOT")) { 277 | currentObj = rootJson; 278 | continue; 279 | } 280 | if (!currentObj.has(t)) { 281 | continue; 282 | } 283 | if (currentObj.get(t).isArray()) { 284 | for (final JsonNode objNode : currentObj.get(t)) { 285 | currentObj=(ObjectNode)objNode; 286 | break; 287 | } 288 | } else { 289 | if (!currentObj.get(t).isValueNode()) { 290 | currentObj = (ObjectNode)currentObj.get(t); 291 | } else { 292 | retValue = currentObj.get(t).asText(); 293 | } 294 | } 295 | } 296 | } catch(Exception e) { 297 | e.printStackTrace(); 298 | currentObj = null; 299 | } 300 | 301 | if (mObj.has("convert") && !StringUtils.isEmpty(retValue)) { 302 | String convert = mObj.get("convert").asText(); 303 | if (convert.equals("re")) { 304 | JsonNode regexList = mObj.get("regex"); 305 | for (final JsonNode objNode : regexList) { 306 | 307 | 308 | String regex = objNode.asText(); 309 | Matcher matcher = Pattern.compile(regex).matcher(retValue); 310 | if (matcher != null) { 311 | try { 312 | retValue = matcher.group(); 313 | } catch (Exception ex){ 314 | // If no match, return the actual unparsed value. 315 | } 316 | } 317 | } 318 | } else { 319 | if (convert.equals("convert1")) { 320 | String[] severity = retValue.split("-"); 321 | retValue = severity[2].toUpperCase(); 322 | } else if (convert.equals("convert2")) { 323 | Calendar cal = Calendar.getInstance(); 324 | cal.setTimeInMillis(new Long(retValue)); 325 | SimpleDateFormat dateFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 326 | retValue = dateFmt.format(cal.getTime()); 327 | } else if (convert.equals("map")){ 328 | String hashName = mObj.get("mapname").asText(); 329 | if (!hashTable.containsKey(hashName)){ 330 | addToHashTable(hashName,mObj.get("maplist").asText()); 331 | } 332 | retValue = lookUpHashTable(hashName, retValue); 333 | } 334 | } 335 | } 336 | 337 | 338 | return retValue; 339 | } 340 | 341 | private ObjectNode getJsonStringXValue(ObjectNode jsonObj, String tag, JsonNode mObj, ObjectNode rootJson) { 342 | ObjectNode yangObject = JsonNodeFactory.instance.objectNode(); 343 | ObjectNode currentObj = jsonObj; 344 | 345 | try { 346 | String[] tags = tag.split("->"); 347 | for (String t : tags) { 348 | if (!currentObj.has(t)) { 349 | continue; 350 | } 351 | if (currentObj.get(t).isArray()) { 352 | for (final JsonNode objNode : currentObj.get(t)) { 353 | currentObj=(ObjectNode)objNode; 354 | break; 355 | } 356 | } else { 357 | if (!currentObj.get(t).isValueNode()) { 358 | currentObj = (ObjectNode)currentObj.get(t); 359 | } 360 | } 361 | } 362 | } catch(Exception e) { 363 | e.printStackTrace(); 364 | currentObj = null; 365 | } 366 | 367 | if (currentObj == null) { 368 | return yangObject; 369 | } 370 | 371 | ObjectNode item = JsonNodeFactory.instance.objectNode(); 372 | 373 | String keyTag = mObj.get("keys").get(mObj.get("key").asText()).get("tag").asText(); 374 | String value = getJsonStringValue(currentObj, keyTag, mObj.get("keys").get(mObj.get("key").asText()), rootJson); 375 | yangObject.put(value, item); 376 | 377 | Iterator keys = mObj.get("keys").fieldNames(); 378 | while (keys.hasNext()) { 379 | String key = keys.next(); 380 | JsonNode mo = mObj.get("keys").get(key); 381 | if (mo.get("type").asText().equals("string")) { 382 | String result = getJsonStringValue(currentObj, mo.get("tag").asText(), mo, rootJson); 383 | if (result != null) { 384 | item.put(key, result); 385 | } 386 | } else if (mo.get("type").asText().equals("string-list")) { 387 | String result = getJsonStringValue(jsonObj, mObj.get("tag").asText(), mObj, rootJson); 388 | ArrayNode ja = JsonNodeFactory.instance.arrayNode(); 389 | String[] stringList = result.split(","); 390 | for (String str : stringList) { 391 | ja.add(str); 392 | } 393 | } else if (mo.get("type").asText().equals("int")) { 394 | Integer result = getJsonIntValue(currentObj, mo.get("tag").asText(), mo, rootJson); 395 | if (result != null) { 396 | item.put(key, result); 397 | } 398 | } else if (mo.get("type").asText().equals("grouping")) { 399 | ObjectNode groupJsonObj = getJsonObject(currentObj, mo.get("tag").asText()); 400 | JsonNode groupMObj = mo.get("grouping"); 401 | ObjectNode newObj =JsonNodeFactory.instance.objectNode(); 402 | item.put(key, newObj); 403 | 404 | ObjectNode saveRootObj = rootJson; 405 | try { 406 | convertToYangJsonWithMapping(groupMObj, newObj, groupJsonObj, rootJson); 407 | } catch(Exception e) { 408 | e.printStackTrace(); 409 | } 410 | rootJson = saveRootObj; 411 | } 412 | } 413 | 414 | 415 | return yangObject; 416 | } 417 | 418 | private Integer getJsonIntValue(ObjectNode jsonObj, String tag, JsonNode mObj, ObjectNode rootJson) { 419 | Integer retValue = null; 420 | ObjectNode currentObj = jsonObj; 421 | 422 | try { 423 | String[] tags = tag.split("->"); 424 | for (String t : tags) { 425 | if (retValue != null) { 426 | throw new Exception("bad json walk"); 427 | } 428 | 429 | if (!currentObj.has(t)) { 430 | continue; 431 | } 432 | 433 | 434 | 435 | if (currentObj.get(t).isArray()) { 436 | for (final JsonNode objNode : currentObj.get(t)) { 437 | currentObj=(ObjectNode)objNode; 438 | break; 439 | } 440 | } else { 441 | if (!currentObj.get(t).isValueNode()) { 442 | currentObj = (ObjectNode)currentObj.get(t); 443 | } else { 444 | retValue = currentObj.get(t).asInt(); 445 | } 446 | } 447 | } 448 | } catch(Exception e) { 449 | e.printStackTrace(); 450 | currentObj = null; 451 | } 452 | 453 | return retValue; 454 | } 455 | 456 | private ObjectNode getJsonListValue(ObjectNode jsonObj, String tag, JsonNode mObj, ObjectNode rootJson) { 457 | ObjectNode currentObj = jsonObj; 458 | ArrayNode currentObjAry = JsonNodeFactory.instance.arrayNode(); 459 | ObjectNode yangObject = JsonNodeFactory.instance.objectNode(); 460 | Boolean isArray = Boolean.FALSE; 461 | try { 462 | String[] tags = tag.split("->"); 463 | for (String t : tags) { 464 | ObjectNode o = null; 465 | if (currentObj.isArray()) { 466 | for (final JsonNode objNode : currentObj.get(t)) { 467 | o=(ObjectNode)objNode; 468 | break; 469 | } 470 | } else { 471 | o = (ObjectNode)currentObj; 472 | } 473 | if (!o.has(t)) { 474 | break; 475 | } 476 | if (o.get(t).isArray()) { 477 | currentObjAry = (ArrayNode)o.get(t); 478 | isArray = Boolean.TRUE; 479 | } else { 480 | if (!o.get(t).isValueNode()) { 481 | currentObj =(ObjectNode) o.get(t); 482 | isArray = Boolean.FALSE; 483 | } 484 | } 485 | } 486 | } catch(Exception e) { 487 | e.printStackTrace(); 488 | currentObj = null; 489 | } 490 | 491 | if (!isArray) { 492 | return getJsonObjectValue( currentObj,(ObjectNode) mObj, rootJson); 493 | } 494 | 495 | for (final JsonNode a : currentObjAry) { 496 | 497 | ObjectNode co=(ObjectNode)a; 498 | ObjectNode item = JsonNodeFactory.instance.objectNode(); 499 | 500 | String keyTag = mObj.get("keys").get(mObj.get("key").asText()).get("tag").asText(); 501 | String value = getJsonStringValue(co, keyTag, mObj.get("keys").get(mObj.get("key").asText()), rootJson); 502 | yangObject.put(value, item); 503 | 504 | Iterator keys = mObj.get("keys").fieldNames(); 505 | while (keys.hasNext()) { 506 | String key = keys.next(); 507 | JsonNode mo = mObj.get("keys").get(key); 508 | if (mo.get("type").asText().equals("string")) { 509 | String result = getJsonStringValue(co, mo.get("tag").asText(), mo, rootJson); 510 | if (result != null) { 511 | item.put(key, result); 512 | } 513 | } else if (mo.get("type").asText().equals("string-list")) { 514 | String result = getJsonStringValue(co, mo.get("tag").asText(), mo, rootJson); 515 | ArrayNode ja =JsonNodeFactory.instance.arrayNode(); 516 | item.put(key, ja); 517 | String[] stringList = result.split(","); 518 | for (String str : stringList) { 519 | ja.add(str); 520 | } 521 | } else if (mo.get("type").asText().equals("int")) { 522 | Integer result = getJsonIntValue(co, mo.get("tag").asText(), mo, rootJson); 523 | if (result != null) { 524 | item.put(key, result); 525 | } 526 | } else if (mo.get("type").asText().equals("grouping")) { 527 | ObjectNode groupJsonObj = getJsonObject(co, mo.get("tag").asText()); 528 | JsonNode groupMObj = mo.get("grouping"); 529 | ObjectNode newObj = JsonNodeFactory.instance.objectNode(); 530 | item.put(key, newObj); 531 | 532 | ObjectNode saveRootObj = rootJson; 533 | try { 534 | convertToYangJsonWithMapping(groupMObj, newObj, groupJsonObj, rootJson); 535 | } catch(Exception e) { 536 | e.printStackTrace(); 537 | } 538 | rootJson = saveRootObj; 539 | } 540 | } 541 | } 542 | 543 | 544 | return yangObject; 545 | } 546 | 547 | @SuppressWarnings("deprecation") 548 | private ObjectNode getJsonObjectValue(ObjectNode jsonObj, ObjectNode mObj, ObjectNode rootJson) { 549 | ObjectNode yangObject = JsonNodeFactory.instance.objectNode(); 550 | ObjectNode item = JsonNodeFactory.instance.objectNode(); 551 | 552 | String keyTag = mObj.get("keys").get(mObj.get("key").asText()).get("tag").asText(); 553 | String value = getJsonStringValue(jsonObj, keyTag, mObj.get("keys").get(mObj.get("key").asText()), rootJson); 554 | yangObject.put(value, item); 555 | 556 | Iterator keys = mObj.get("keys").fieldNames(); 557 | while (keys.hasNext()) { 558 | String key = keys.next(); 559 | JsonNode mo = mObj.get("keys").get(key); 560 | String type=mo.get("type").asText(); 561 | if (type.equals("string")) { 562 | String result = getJsonStringValue(jsonObj, mo.get("tag").asText(), mo, rootJson); 563 | if (result != null) { 564 | item.put(key, result); 565 | } 566 | } else if (type.equals("string-list")) { 567 | String result = getJsonStringValue(jsonObj, mObj.get("tag").asText(), mObj, rootJson); 568 | ArrayNode ja = JsonNodeFactory.instance.arrayNode(); 569 | item.put(key, ja); 570 | String[] stringList = result.split(","); 571 | for (String str : stringList) { 572 | ja.add(str); 573 | } 574 | } else if (type.equals("int")) { 575 | Integer result = getJsonIntValue(jsonObj, mObj.get("tag").asText(), mObj, rootJson); 576 | if (result != null) { 577 | item.put(key, result); 578 | } 579 | } else if (type.equals("grouping")) { 580 | ObjectNode groupJsonObj = getJsonObject(jsonObj, mo.get("tag").asText()); 581 | JsonNode groupMObj = mo.get("grouping"); 582 | ObjectNode newObj = JsonNodeFactory.instance.objectNode(); 583 | item.put(key, newObj); 584 | 585 | ObjectNode saveRootObj = rootJson; 586 | try { 587 | convertToYangJsonWithMapping(groupMObj, newObj, groupJsonObj, rootJson); 588 | } catch(Exception e) { 589 | e.printStackTrace(); 590 | } 591 | rootJson = saveRootObj; 592 | } 593 | } 594 | 595 | return yangObject; 596 | } 597 | 598 | private void addToHashTable(String mapName, String mapList) 599 | { 600 | String[] items = mapList.split(","); 601 | Hashtable table = new Hashtable(); 602 | for (int i= 0;i< items.length;i++) 603 | { 604 | String[] keyVal = items[i].split("="); 605 | table.put(keyVal[0], keyVal[1]); 606 | } 607 | hashTable.put(mapName, table); 608 | } 609 | 610 | private String lookUpHashTable(String hashName,String retValue) 611 | { 612 | Hashtable table = hashTable.get(hashName); 613 | if (table.containsKey(retValue)){ 614 | retValue = table.get(retValue); 615 | } 616 | else{ 617 | retValue = table.get("def"); 618 | } 619 | return(retValue); 620 | } 621 | } 622 | -------------------------------------------------------------------------------- /src/main/java/com/vzw/yang/transformer/YangTransformer.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright Verizon Inc. 3 | Licensed under the terms of the Apache License 2.0 license. See LICENSE file in project root for terms. 4 | */ 5 | package com.vzw.yang.transformer; 6 | 7 | import java.io.IOException; 8 | import java.io.InputStream; 9 | import java.io.InputStreamReader; 10 | import java.io.LineNumberReader; 11 | import java.lang.reflect.InvocationTargetException; 12 | import java.lang.reflect.Method; 13 | import java.text.SimpleDateFormat; 14 | import java.util.ArrayList; 15 | import java.util.Calendar; 16 | import java.util.Iterator; 17 | import java.util.List; 18 | import java.util.regex.Matcher; 19 | import java.util.regex.Pattern; 20 | 21 | import javax.annotation.PostConstruct; 22 | 23 | import org.apache.logging.log4j.LogManager; 24 | import org.apache.logging.log4j.Logger; 25 | import org.apache.commons.lang3.StringUtils; 26 | import org.json.JSONArray; 27 | import org.json.JSONException; 28 | import org.json.JSONObject; 29 | import org.springframework.beans.factory.annotation.Autowired; 30 | import org.springframework.beans.factory.annotation.Qualifier; 31 | import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; 32 | import org.springframework.stereotype.Service; 33 | 34 | import com.fasterxml.jackson.databind.JsonNode; 35 | import com.fasterxml.jackson.databind.ObjectMapper; 36 | import com.fasterxml.jackson.databind.node.ObjectNode; 37 | import com.vzw.yang.data.JsonToYangJsonRequest; 38 | import com.vzw.yang.util.YangTransformerService; 39 | 40 | /********************************************************************** 41 | ** Process responses from CAMEO 42 | **********************************************************************/ 43 | @Service("yangTransformer") 44 | public class YangTransformer { 45 | 46 | private static final Logger logger = LogManager.getLogger(YangTransformer.class); 47 | 48 | @Autowired 49 | @Qualifier("yangTransformerService") 50 | private YangTransformerService transService; 51 | 52 | private static List mappingDetails = new ArrayList<>(); 53 | 54 | public YangTransformer() { 55 | logger.debug("YangTransformer: Entered"); 56 | 57 | logger.debug("YangTransformer: Exited"); 58 | 59 | } 60 | 61 | @Autowired 62 | ThreadPoolTaskExecutor taskExecutor; 63 | 64 | private synchronized void loadMappings() { 65 | logger.info("loadMappings: Entered"); 66 | 67 | if ((mappingDetails != null) && (mappingDetails.size() > 0)) { 68 | return; 69 | } 70 | 71 | try { 72 | String mappingDetails = transService.getMappingDetails(); 73 | setMappingDetails(mappingDetails); 74 | } catch(Exception e) { 75 | e.printStackTrace(); 76 | } 77 | logger.info("loadMappings: Exited"); 78 | 79 | } 80 | 81 | public synchronized void reloadMappings() { 82 | logger.info("reloadMappings: Entered"); 83 | 84 | mappingDetails.clear(); 85 | loadMappings(); 86 | 87 | logger.info("reloadMappings: Exited"); 88 | 89 | } 90 | 91 | public JSONObject jsonToYangJson(String topic, String jsonStr, boolean validate) throws Exception { 92 | JSONObject yangJson = null; 93 | String error = null; 94 | 95 | logger.info("YangTransformer.jsonToYangJson: Entered"); 96 | do { 97 | if ((mappingDetails == null) || (mappingDetails.size() == 0)) { 98 | loadMappings(); 99 | } 100 | MappingDetail md = findMappingDetail(topic); 101 | if (md == null) { 102 | error = "Unable to find topic: " + topic; 103 | break; 104 | } 105 | 106 | if (md.isEquipmentTopic()) { 107 | error = "Equipment not supported yet: " + topic; 108 | break; 109 | } 110 | 111 | // load the mapping details 112 | //loadMappingDetails(md); 113 | 114 | //yangJson = convertToYangJson(md, jsonStr, validate); 115 | ObjectMapper mapper = new ObjectMapper(); 116 | JsonNode jnMapperObject = mapper.readTree(md.getMappingObj().toString()); 117 | com.vzw.yang.transformer.ucs.MappingDetail ucsMappingDetail = new com.vzw.yang.transformer.ucs.MappingDetail(topic, jnMapperObject); 118 | JsonNode inputJson = mapper.readTree(jsonStr); 119 | com.vzw.yang.transformer.ucs.YangTransformer ucsTransformer = 120 | new com.vzw.yang.transformer.ucs.YangTransformer(); 121 | String yangJsonStr = ucsTransformer.convertToYangJson(ucsMappingDetail, (ObjectNode)inputJson, validate); 122 | yangJson = new JSONObject(yangJsonStr); 123 | 124 | 125 | if (validate) { 126 | prettyPrintJson(yangJsonStr); 127 | 128 | JsonToYangJsonRequest request = new JsonToYangJsonRequest(); 129 | request.setTopic(md.getTopic()); 130 | request.setJsonStr(yangJsonStr); 131 | 132 | String result = transService.validateYang(request); 133 | logger.info("YangTransformer.convertToYangJson: Validationg result: " + result); 134 | } 135 | 136 | } while(false); 137 | logger.info("YangTransformer.jsonToYangJson: Exited"); 138 | 139 | if (error != null) { 140 | throw new Exception(error); 141 | } 142 | 143 | return yangJson; 144 | } 145 | 146 | private boolean loadMappingDetails(MappingDetail md) { 147 | boolean rc = false; 148 | InputStream in = null; 149 | 150 | logger.info("YangTransformer.loadMappingDetails: Entered"); 151 | do { 152 | if (md.getMappingObj() != null) { 153 | // already loaded 154 | logger.info("YangTransformer.loadMappingDetails: mappings already loaded"); 155 | break; 156 | } 157 | 158 | logger.info("YangTransformer.loadMappingDetails: mappings not loaded yet"); 159 | in = this.getClass().getClassLoader().getResourceAsStream(md.getMappingFile()); 160 | if (in == null) { 161 | logger.info("YangTransformer.loadMappingDetails: could not find mappings: " + md.getMappingFile()); 162 | break; 163 | } 164 | 165 | String mappingJsonStr = loadFile(in); 166 | 167 | // Now convert to a jsonObject 168 | JSONObject json = new JSONObject(mappingJsonStr); 169 | 170 | // display the string 171 | prettyPrintJson(mappingJsonStr); 172 | 173 | md.setMappingObj(json); 174 | logger.info("YangTransformer.loadMappingDetails: mappings loaded"); 175 | 176 | } while(false); 177 | if (in != null) { 178 | try { 179 | in.close(); 180 | } catch(Exception e) { 181 | 182 | } 183 | } 184 | logger.info("YangTransformer.loadMappingDetails: Exited"); 185 | 186 | return rc; 187 | 188 | } 189 | 190 | private void prettyPrintJson(String jsonStr) { 191 | try { 192 | ObjectMapper mapper = new ObjectMapper(); 193 | Object jsonObj = mapper.readValue(jsonStr, Object.class); 194 | logger.info("Have criteria object as json: " + mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObj)); 195 | } catch(Exception e) { 196 | e.printStackTrace(); 197 | } 198 | } 199 | 200 | private String loadFile(InputStream in) { 201 | 202 | /*---- Local Variable Declares ----*/ 203 | String Txml = null; 204 | LineNumberReader Tinput = null; 205 | StringBuffer Tbuffer = new StringBuffer(); 206 | 207 | /*---- Start of Code ----*/ 208 | do { 209 | /*-----------------------------------------------------------------+ 210 | | Open the file | 211 | +-----------------------------------------------------------------*/ 212 | Tinput = new LineNumberReader(new InputStreamReader(in)); 213 | 214 | /*-----------------------------------------------------------------+ 215 | | Now loop through this file and process the records | 216 | +-----------------------------------------------------------------*/ 217 | while (true) { 218 | String Tline; 219 | try { 220 | Tline = Tinput.readLine(); 221 | } catch (IOException e) { 222 | logger.error("YangTransformer.loadFile: Unable to read file: " + e.getMessage()); 223 | break; 224 | } 225 | 226 | /*-----------------------------------------------------------------+ 227 | | If no more data then we are done | 228 | +-----------------------------------------------------------------*/ 229 | if (Tline == null) { 230 | break; 231 | } 232 | 233 | /*-----------------------------------------------------------------+ 234 | | Make sure this is not a blank line | 235 | +-----------------------------------------------------------------*/ 236 | Tbuffer.append(Tline + "\n"); 237 | } 238 | 239 | Txml = Tbuffer.toString(); 240 | 241 | } 242 | while (false); 243 | 244 | /*-----------------------------------------------------------------+ 245 | | Free Resources | 246 | +-----------------------------------------------------------------*/ 247 | if (Tinput != null) { 248 | try { 249 | Tinput.close(); 250 | } catch (IOException e) {} 251 | } 252 | 253 | return (Txml); 254 | 255 | } 256 | 257 | 258 | private JSONObject convertToYangJson(MappingDetail md, String jsonStr, boolean validate) throws Exception { 259 | String yangJsonStr = null; 260 | String error = null; 261 | JSONObject resultYangObj = new JSONObject(); 262 | 263 | 264 | logger.info("YangTransformer.convertToYangJson: Entered"); 265 | do { 266 | JSONObject mapper = md.getMappingObj(); 267 | if (mapper == null) { 268 | error = "No mappings loaded"; 269 | break; 270 | } 271 | 272 | JSONObject jsonObj = null; 273 | try { 274 | jsonObj = new JSONObject(jsonStr); 275 | } catch(JSONException e) { 276 | error = "Unable to convert entered json string: " + e.getMessage(); 277 | break; 278 | } 279 | JSONObject rootJson = jsonObj; 280 | // Check for basetransformer processing required 281 | if (mapper.has("basetransformer")) { 282 | // Perform base mapping logic here 283 | String baseTransformer = mapper.getString("basetransformer"); 284 | jsonObj = transformBaseJson(jsonObj, baseTransformer); 285 | } 286 | 287 | 288 | JSONObject yangObj = new JSONObject(); 289 | resultYangObj.put(mapper.getString("rootNode"), yangObj); 290 | 291 | if (hasDict(mapper)) 292 | convertToYangJsonWithMapping(mapper, yangObj, jsonObj, rootJson); 293 | else 294 | resultYangObj.put(mapper.getString("rootNode"), jsonObj); 295 | 296 | if (validate) { 297 | yangJsonStr = resultYangObj.toString(); 298 | prettyPrintJson(yangJsonStr); 299 | 300 | JsonToYangJsonRequest request = new JsonToYangJsonRequest(); 301 | request.setTopic(md.getTopic()); 302 | request.setJsonStr(yangJsonStr); 303 | 304 | String result = transService.validateYang(request); 305 | logger.info("YangTransformer.convertToYangJson: Validationg result: " + result); 306 | } 307 | 308 | } while(false); 309 | logger.info("YangTransformer.convertToYangJson: Exited"); 310 | 311 | if (error != null) { 312 | throw new Exception(error); 313 | } 314 | 315 | return resultYangObj; 316 | } 317 | 318 | private boolean hasDict(JSONObject mapper) { 319 | boolean hasDict = false; 320 | Iterator keys = (Iterator) mapper.keys(); 321 | while (keys.hasNext()) { 322 | String key = keys.next(); 323 | 324 | Object mObj = mapper.get(key); 325 | if (mObj instanceof JSONObject) { 326 | hasDict = true; 327 | } 328 | } 329 | 330 | return hasDict; 331 | } 332 | private JSONObject transformBaseJson(JSONObject jsonObj, String baseTransformer) { 333 | logger.info("YangTransformer.transformBaseJson: Entered"); 334 | 335 | JSONObject retObj = jsonObj; 336 | String[] moduleClassMethod = baseTransformer.split("\\."); 337 | try { 338 | Class klas = Class.forName("com.vzw.yang.transformer." + moduleClassMethod[1]); 339 | 340 | Method method = klas.getDeclaredMethod(moduleClassMethod[2], JSONObject.class); 341 | retObj = (JSONObject) method.invoke(null, jsonObj); 342 | } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException 343 | | IllegalArgumentException | InvocationTargetException e) { 344 | e.printStackTrace(); 345 | return (retObj); 346 | } 347 | logger.info(retObj.toString()); 348 | logger.info("YangTransformer.transformBaseJson: Exited"); 349 | return retObj; 350 | } 351 | 352 | private void convertToYangJsonWithMapping(JSONObject mapper, JSONObject yangObj, JSONObject jsonObj, JSONObject rootJson) throws Exception { 353 | String error = null; 354 | 355 | logger.info("YangTransformer.convertToYangJsonWithMapping: Entered"); 356 | do { 357 | Iterator keys = (Iterator)mapper.keys(); 358 | while (keys.hasNext()) { 359 | String key = keys.next(); 360 | if (key.equals("modelName") || key.equals("pyangObj") || key.equals("rootNode") || key.equals("basetransformer") || key.contentEquals("convert")) { 361 | continue; 362 | } 363 | 364 | JSONObject mObj = mapper.getJSONObject(key); 365 | if (mObj.getString("type").equals("string")) { 366 | String result = getJsonStringValue(jsonObj, mObj.getString("tag"), mObj, rootJson); 367 | if (result != null) { 368 | yangObj.put(key, result); 369 | } 370 | } else if (mObj.getString("type").equals("string-list")) { 371 | String result = getJsonStringValue(jsonObj, mObj.getString("tag"), mObj, rootJson); 372 | JSONArray ja = new JSONArray(); 373 | yangObj.put(key, ja); 374 | String[] stringList = result.split(","); 375 | for (String str : stringList) { 376 | ja.put(str); 377 | } 378 | } else if (mObj.getString("type").equals("int")) { 379 | Integer result = getJsonIntValue(jsonObj, mObj.getString("tag"), mObj, rootJson); 380 | if (result != null) { 381 | yangObj.put(key, result); 382 | } 383 | } else if (mObj.getString("type").equals("rawAlarmText")) { 384 | String rawAlarm = rootJson.toString(); 385 | rawAlarm = rawAlarm.replace("\n", ""); 386 | yangObj.put(key, rawAlarm); 387 | } else if (mObj.getString("type").equals("metaData")) { 388 | String rawAlarm = rootJson.optJSONObject("metaData").toString(); 389 | if (rawAlarm != null) { 390 | rawAlarm = rawAlarm.replace("\n", ""); 391 | yangObj.put(key, rawAlarm); 392 | } 393 | } else if (mObj.getString("type").equals("list")) { 394 | JSONObject list = getJsonListValue(jsonObj, mObj.getString("tag"), mObj, rootJson); 395 | if (list != null) { 396 | yangObj.put(key, list); 397 | } 398 | } else if (mObj.getString("type").equals("stringx")) { 399 | JSONObject jo = this.getJsonStringXValue(jsonObj, mObj.getString("tag"), mObj, rootJson); 400 | if (jo != null) { 401 | yangObj.put(key, jo); 402 | } 403 | } else if (mObj.getString("type").equals("grouping")) { 404 | JSONObject groupJsonObj = getJsonObject(jsonObj, mObj.getString("tag")); 405 | JSONObject groupMObj = mObj.getJSONObject("grouping"); 406 | JSONObject newObj = new JSONObject(); 407 | yangObj.put(key, newObj); 408 | 409 | JSONObject saveRootObj = rootJson; 410 | 411 | convertToYangJsonWithMapping(groupMObj, newObj, groupJsonObj, rootJson); 412 | rootJson = saveRootObj; 413 | 414 | } 415 | } 416 | 417 | } while(false); 418 | logger.info("YangTransformer.convertToYangJsonWithMapping: Exited"); 419 | 420 | if (error != null) { 421 | throw new Exception(error); 422 | } 423 | 424 | } 425 | 426 | private JSONObject getJsonObject(JSONObject jsonObj, String tag) { 427 | JSONObject currentObj = jsonObj; 428 | try { 429 | String[] tags = tag.split("->"); 430 | for (String t : tags) { 431 | if (!currentObj.has(t)) { 432 | continue; 433 | } 434 | if (currentObj.get(t) instanceof JSONArray) { 435 | currentObj = currentObj.getJSONArray(t).getJSONObject(0); 436 | } else { 437 | currentObj = currentObj.getJSONObject(t); 438 | } 439 | } 440 | } catch(Exception e) { 441 | e.printStackTrace(); 442 | currentObj = null; 443 | } 444 | return currentObj; 445 | } 446 | 447 | private String getJsonStringValue(JSONObject jsonObj, String tag, JSONObject mObj, JSONObject rootJson) { 448 | String retValue = null; 449 | JSONObject currentObj = jsonObj; 450 | 451 | try { 452 | String[] tags = tag.split("->"); 453 | for (String t : tags) { 454 | if (retValue != null) { 455 | throw new Exception("bad json walk"); 456 | } 457 | if ((tags.length > 1) && t.equals("ROOT")) { 458 | currentObj = rootJson; 459 | continue; 460 | } 461 | if (!currentObj.has(t)) { 462 | continue; 463 | } 464 | if (currentObj.get(t) instanceof JSONArray) { 465 | currentObj = currentObj.getJSONArray(t).getJSONObject(0); 466 | } else { 467 | if (currentObj.get(t) instanceof JSONObject) { 468 | currentObj = currentObj.getJSONObject(t); 469 | } else { 470 | retValue = currentObj.getString(t); 471 | } 472 | } 473 | } 474 | } catch(Exception e) { 475 | e.printStackTrace(); 476 | currentObj = null; 477 | } 478 | 479 | if (mObj.has("convert") && !StringUtils.isEmpty(retValue)) { 480 | String convert = mObj.getString("convert"); 481 | if (convert.equals("re")) { 482 | JSONArray regexList = mObj.getJSONArray("regex"); 483 | for (int i = 0; i < regexList.length(); i++) { 484 | String regex = regexList.getString(i); 485 | Matcher matcher = Pattern.compile(regex).matcher(retValue); 486 | if (matcher != null) { 487 | retValue = matcher.group(); 488 | } 489 | } 490 | } else { 491 | if (convert.equals("convert1")) { 492 | String[] severity = retValue.split("-"); 493 | retValue = severity[2].toUpperCase(); 494 | } else if (convert.equals("convert2")) { 495 | Calendar cal = Calendar.getInstance(); 496 | cal.setTimeInMillis(new Long(retValue)); 497 | SimpleDateFormat dateFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 498 | retValue = dateFmt.format(cal.getTime()); 499 | } 500 | } 501 | } 502 | 503 | 504 | return retValue; 505 | } 506 | 507 | private JSONObject getJsonStringXValue(JSONObject jsonObj, String tag, JSONObject mObj, JSONObject rootJson) { 508 | JSONObject yangObject = new JSONObject(); 509 | JSONObject currentObj = jsonObj; 510 | 511 | try { 512 | String[] tags = tag.split("->"); 513 | for (String t : tags) { 514 | if (!currentObj.has(t)) { 515 | continue; 516 | } 517 | if (currentObj.get(t) instanceof JSONArray) { 518 | currentObj = currentObj.getJSONArray(t).getJSONObject(0); 519 | } else { 520 | if (currentObj.get(t) instanceof JSONObject) { 521 | currentObj = currentObj.getJSONObject(t); 522 | } 523 | } 524 | } 525 | } catch(Exception e) { 526 | e.printStackTrace(); 527 | currentObj = null; 528 | } 529 | 530 | if (currentObj == null) { 531 | return yangObject; 532 | } 533 | 534 | JSONObject item = new JSONObject(); 535 | 536 | String keyTag = mObj.getJSONObject("keys").getJSONObject(mObj.getString("key")).getString("tag"); 537 | String value = getJsonStringValue(currentObj, keyTag, mObj.getJSONObject("keys").getJSONObject(mObj.getString("key")), rootJson); 538 | yangObject.put(value, item); 539 | 540 | Iterator keys = mObj.getJSONObject("keys").keys(); 541 | while (keys.hasNext()) { 542 | String key = keys.next(); 543 | JSONObject mo = mObj.getJSONObject("keys").getJSONObject(key); 544 | if (mo.getString("type").equals("string")) { 545 | String result = getJsonStringValue(currentObj, mo.getString("tag"), mo, rootJson); 546 | if (result != null) { 547 | item.put(key, result); 548 | } 549 | } else if (mo.getString("type").equals("string-list")) { 550 | String result = getJsonStringValue(jsonObj, mObj.getString("tag"), mObj, rootJson); 551 | JSONArray ja = new JSONArray(); 552 | String[] stringList = result.split(","); 553 | for (String str : stringList) { 554 | ja.put(str); 555 | } 556 | } else if (mo.getString("type").equals("int")) { 557 | Integer result = getJsonIntValue(currentObj, mo.getString("tag"), mo, rootJson); 558 | if (result != null) { 559 | item.put(key, result); 560 | } 561 | } else if (mo.getString("type").equals("grouping")) { 562 | JSONObject groupJsonObj = getJsonObject(currentObj, mo.getString("tag")); 563 | JSONObject groupMObj = mo.getJSONObject("grouping"); 564 | JSONObject newObj = new JSONObject(); 565 | item.put(key, newObj); 566 | 567 | JSONObject saveRootObj = rootJson; 568 | try { 569 | convertToYangJsonWithMapping(groupMObj, newObj, groupJsonObj, rootJson); 570 | } catch(Exception e) { 571 | e.printStackTrace(); 572 | } 573 | rootJson = saveRootObj; 574 | } 575 | } 576 | 577 | 578 | return yangObject; 579 | } 580 | 581 | private Integer getJsonIntValue(JSONObject jsonObj, String tag, JSONObject mObj, JSONObject rootJson) { 582 | Integer retValue = null; 583 | JSONObject currentObj = jsonObj; 584 | 585 | try { 586 | String[] tags = tag.split("->"); 587 | for (String t : tags) { 588 | if (retValue != null) { 589 | throw new Exception("bad json walk"); 590 | } 591 | 592 | if (!currentObj.has(t)) { 593 | continue; 594 | } 595 | if (currentObj.get(t) instanceof JSONArray) { 596 | currentObj = currentObj.getJSONArray(t).getJSONObject(0); 597 | } else { 598 | if (currentObj.get(t) instanceof JSONObject) { 599 | currentObj = currentObj.getJSONObject(t); 600 | } else { 601 | retValue = currentObj.getInt(t); 602 | } 603 | } 604 | } 605 | } catch(Exception e) { 606 | e.printStackTrace(); 607 | currentObj = null; 608 | } 609 | 610 | return retValue; 611 | } 612 | 613 | private JSONObject getJsonListValue(JSONObject jsonObj, String tag, JSONObject mObj, JSONObject rootJson) { 614 | Object currentObj = jsonObj; 615 | 616 | JSONObject yangObject = new JSONObject(); 617 | 618 | try { 619 | String[] tags = tag.split("->"); 620 | for (String t : tags) { 621 | JSONObject o = null; 622 | if (currentObj instanceof JSONArray) { 623 | o = ((JSONArray) currentObj).getJSONObject(0); 624 | } else { 625 | o = (JSONObject)currentObj; 626 | } 627 | if (!o.has(t)) { 628 | break; 629 | } 630 | if (o.get(t) instanceof JSONArray) { 631 | currentObj = o.get(t); 632 | } else { 633 | if (o.get(t) instanceof JSONObject) { 634 | currentObj = o.get(t); 635 | } 636 | } 637 | } 638 | } catch(Exception e) { 639 | e.printStackTrace(); 640 | currentObj = null; 641 | } 642 | 643 | if (!(currentObj instanceof JSONArray)) { 644 | return getJsonObjectValue((JSONObject)currentObj, mObj, rootJson); 645 | } 646 | 647 | JSONArray jArray = (JSONArray)currentObj; 648 | for (int i = 0; i < jArray.length(); i++) { 649 | JSONObject co = jArray.getJSONObject(i); 650 | JSONObject item = new JSONObject(); 651 | 652 | String keyTag = mObj.getJSONObject("keys").getJSONObject(mObj.getString("key")).getString("tag"); 653 | String value = getJsonStringValue(co, keyTag, mObj.getJSONObject("keys").getJSONObject(mObj.getString("key")), rootJson); 654 | yangObject.put(value, item); 655 | 656 | Iterator keys = mObj.getJSONObject("keys").keys(); 657 | while (keys.hasNext()) { 658 | String key = keys.next(); 659 | JSONObject mo = mObj.getJSONObject("keys").getJSONObject(key); 660 | if (mo.getString("type").equals("string")) { 661 | String result = getJsonStringValue(co, mo.getString("tag"), mo, rootJson); 662 | if (result != null) { 663 | item.put(key, result); 664 | } 665 | } else if (mo.getString("type").equals("string-list")) { 666 | String result = getJsonStringValue(co, mo.getString("tag"), mo, rootJson); 667 | JSONArray ja = new JSONArray(); 668 | item.put(key, ja); 669 | String[] stringList = result.split(","); 670 | for (String str : stringList) { 671 | ja.put(str); 672 | } 673 | } else if (mo.getString("type").equals("int")) { 674 | Integer result = getJsonIntValue(co, mo.getString("tag"), mo, rootJson); 675 | if (result != null) { 676 | item.put(key, result); 677 | } 678 | } else if (mo.getString("type").equals("grouping")) { 679 | JSONObject groupJsonObj = getJsonObject(jsonObj, mo.getString("tag")); 680 | JSONObject groupMObj = mo.getJSONObject("grouping"); 681 | JSONObject newObj = new JSONObject(); 682 | item.put(key, newObj); 683 | 684 | JSONObject saveRootObj = rootJson; 685 | try { 686 | convertToYangJsonWithMapping(groupMObj, newObj, groupJsonObj, rootJson); 687 | } catch(Exception e) { 688 | e.printStackTrace(); 689 | } 690 | rootJson = saveRootObj; 691 | } 692 | } 693 | } 694 | 695 | 696 | return yangObject; 697 | } 698 | 699 | private JSONObject getJsonObjectValue(JSONObject jsonObj, JSONObject mObj, JSONObject rootJson) { 700 | JSONObject yangObject = new JSONObject(); 701 | JSONObject item = new JSONObject(); 702 | 703 | 704 | String keyTag = mObj.getJSONObject("keys").getJSONObject(mObj.getString("key")).getString("tag"); 705 | 706 | String value = getJsonStringValue(jsonObj, keyTag, mObj.getJSONObject("keys").getJSONObject(mObj.getString("key")), rootJson); 707 | yangObject.put(value, item); 708 | 709 | 710 | Iterator keys = mObj.getJSONObject("keys").keys(); 711 | while (keys.hasNext()) { 712 | String key = keys.next(); 713 | JSONObject mo = mObj.getJSONObject("keys").getJSONObject(key); 714 | if (mo.getString("type").equals("string")) { 715 | String result = getJsonStringValue(jsonObj, mo.getString("tag"), mo, rootJson); 716 | if (result != null) { 717 | item.put(key, result); 718 | } 719 | } else if (mo.getString("type").equals("string-list")) { 720 | String result = getJsonStringValue(jsonObj, mObj.getString("tag"), mObj, rootJson); 721 | JSONArray ja = new JSONArray(); 722 | item.put(key, ja); 723 | String[] stringList = result.split(","); 724 | for (String str : stringList) { 725 | ja.put(str); 726 | } 727 | } else if (mo.getString("type").equals("int")) { 728 | Integer result = getJsonIntValue(jsonObj, mObj.getString("tag"), mObj, rootJson); 729 | if (result != null) { 730 | item.put(key, result); 731 | } 732 | } else if (mo.getString("type").equals("grouping")) { 733 | JSONObject groupJsonObj = getJsonObject(jsonObj, mo.getString("tag")); 734 | JSONObject groupMObj = mo.getJSONObject("grouping"); 735 | JSONObject newObj = new JSONObject(); 736 | item.put(key, newObj); 737 | 738 | JSONObject saveRootObj = rootJson; 739 | try { 740 | convertToYangJsonWithMapping(groupMObj, newObj, groupJsonObj, rootJson); 741 | } catch(Exception e) { 742 | e.printStackTrace(); 743 | } 744 | rootJson = saveRootObj; 745 | } 746 | } 747 | 748 | return yangObject; 749 | } 750 | 751 | public MappingDetail findMappingDetail(String topic) { 752 | MappingDetail md = null; 753 | 754 | // Find the mapping 755 | for (MappingDetail m : mappingDetails) { 756 | if (topic.equals(m.getTopic())) { 757 | md = m; 758 | break; 759 | } 760 | } 761 | 762 | return md; 763 | } 764 | 765 | public void setMappingDetails(String mappingDetailsJson) { 766 | JSONArray jsonArray = new JSONArray(mappingDetailsJson); 767 | 768 | mappingDetails.clear(); 769 | for (int i = 0; i < jsonArray.length(); i++) { 770 | JSONObject jsonObj = jsonArray.getJSONObject(i); 771 | 772 | MappingDetail m = new MappingDetail(jsonObj.getString("topic"), null, "true".equals(jsonObj.getString("isEquipmentTopic")) ? true :false); 773 | 774 | m.setMappingObj(jsonObj.getJSONObject("mappingObj")); 775 | 776 | mappingDetails.add(m); 777 | } 778 | 779 | } 780 | 781 | } 782 | --------------------------------------------------------------------------------