Add CLI in verigraph.
[parser.git] / verigraph / src / it / polito / verigraph / tosca / XmlParsingUtils.java
1 /*******************************************************************************
2  * Copyright (c) 2017/18 Politecnico di Torino and others.
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Apache License, Version 2.0
6  * which accompanies this distribution, and is available at
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *******************************************************************************/
9 package it.polito.verigraph.tosca;
10
11 import java.io.FileInputStream;
12 import java.io.IOException;
13 import java.io.StringWriter;
14 import java.util.List;
15 import java.util.stream.Collectors;
16
17 import javax.xml.bind.JAXBContext;
18 import javax.xml.bind.JAXBElement;
19 import javax.xml.bind.JAXBException;
20 import javax.xml.bind.Marshaller;
21 import javax.xml.bind.Unmarshaller;
22 import javax.xml.transform.Source;
23 import javax.xml.transform.stream.StreamSource;
24
25 import com.sun.research.ws.wadl.ObjectFactory;
26
27 import it.polito.verigraph.exception.DataNotFoundException;
28 import it.polito.tosca.jaxb.Configuration;
29 import it.polito.tosca.jaxb.Definitions;
30 import it.polito.tosca.jaxb.TDefinitions;
31 import it.polito.tosca.jaxb.TEntityTemplate;
32 import it.polito.tosca.jaxb.TExtensibleElements;
33 import it.polito.tosca.jaxb.TNodeTemplate;
34 import it.polito.tosca.jaxb.TRelationshipTemplate;
35 import it.polito.tosca.jaxb.TServiceTemplate;
36 import it.polito.tosca.jaxb.TTopologyTemplate;
37 import it.polito.verigraph.tosca.converter.grpc.ToscaGrpcUtils;
38
39
40 public class XmlParsingUtils {
41
42     /** Returns a List of TServiceTemplate JAXB-generated objects, parsed from a TOSCA-compliant XML. */
43     public static Definitions obtainDefinitions(String file) throws JAXBException, IOException, ClassCastException, DataNotFoundException {
44         // Create a JAXBContext capable of handling the generated classes
45         JAXBContext jc = JAXBContext.newInstance(ObjectFactory.class, TDefinitions.class, Configuration.class);
46         Unmarshaller u = jc.createUnmarshaller();
47
48         //Retrieve the TDefinitions object
49         Source source = new StreamSource(new FileInputStream(file));
50
51         JAXBElement<Definitions> rootElement = (JAXBElement<Definitions>)u.unmarshal(source, Definitions.class);
52         Definitions definitions = rootElement.getValue();
53         return definitions;
54     }
55
56     /** Returns a List of TServiceTemplate JAXB-generated objects, parsed from a TOSCA-compliant XML. */
57     public static List<TServiceTemplate> obtainServiceTemplates(String file) throws JAXBException, IOException, ClassCastException, DataNotFoundException {
58         // Create a JAXBContext capable of handling the generated classes
59         JAXBContext jc = JAXBContext.newInstance(ObjectFactory.class, TDefinitions.class, Configuration.class);
60         Unmarshaller u = jc.createUnmarshaller();
61
62         //Retrieve the TDefinitions object
63         Source source = new StreamSource(new FileInputStream(file));
64
65         JAXBElement<TDefinitions> rootElement = (JAXBElement<TDefinitions>)u.unmarshal(source, TDefinitions.class);
66         TDefinitions definitions = rootElement.getValue();
67         List<TExtensibleElements> elements = definitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation();
68
69         //Retrieve the list of ServiceTemplate in Definitions
70         List<TServiceTemplate> serviceTemplates = elements.stream()
71                 .filter(p -> p instanceof TServiceTemplate)
72                 .map(obj -> (TServiceTemplate) obj).collect(Collectors.toList());
73
74         if (serviceTemplates.isEmpty())
75             throw new DataNotFoundException("There is no ServiceTemplate into the TOSCA XML file");
76         return serviceTemplates; // Could be an empty list if there are no TServiceTemplate objects
77     }
78
79
80     /** Returns a List of TNodeTemplate JAXB-generated TOSCA objects. */
81     public static List<TNodeTemplate> obtainNodeTemplates(TServiceTemplate serviceTemplate) throws DataNotFoundException {
82         TTopologyTemplate topologyTemplate = serviceTemplate.getTopologyTemplate();
83
84         // Retrieving a list of TNodeTemplate and TRelationshipTemplate JAXB objects
85         List<TEntityTemplate> entities = topologyTemplate.getNodeTemplateOrRelationshipTemplate();
86
87         // Retrieving a List containing only TNodeTemplates objects
88         List<TNodeTemplate> nodeTemplates = entities.stream()
89                 .filter(p -> p instanceof TNodeTemplate)
90                 .map(obj -> (TNodeTemplate) obj).collect(Collectors.toList());
91
92         if (nodeTemplates.isEmpty())
93             throw new DataNotFoundException("There is no NodeTemplate into ServiceTemplate " + serviceTemplate.toString() + " and TopologyTemplate " + topologyTemplate.toString());
94         return nodeTemplates; // Could be an empty list if there are no TNodeTemplate objects
95     }
96
97
98     /** Returns a List of TRelationshipTemplate JAXB-generated TOSCA objects. */
99     public static List<TRelationshipTemplate> obtainRelationshipTemplates(TServiceTemplate serviceTemplate) throws DataNotFoundException {
100         TTopologyTemplate topologyTemplate = serviceTemplate.getTopologyTemplate();
101
102         // Retrieving a List of TNodeTemplate and TRelationshipTemplate JAXB objects
103         List<TEntityTemplate> entities = topologyTemplate.getNodeTemplateOrRelationshipTemplate();
104
105         // Retrieving a List containing only TRelationshipTemplate objects
106         List<TRelationshipTemplate> relationshipTemplates = entities.stream()
107                 .filter(p -> p instanceof TRelationshipTemplate)
108                 .map(obj -> (TRelationshipTemplate) obj).collect(Collectors.toList());
109
110         if (relationshipTemplates.isEmpty())
111             throw new DataNotFoundException("There is no RelationshipTemplate into ServiceTemplate " + serviceTemplate.toString() + " and TopologyTemplate " + topologyTemplate.toString());
112         return relationshipTemplates; // Could be an empty list if there are no TRelationshipTemplate objects
113     }
114
115
116     /** Returns the it.polito.tosca.jaxb.Configuration JAXB-generated TOSCA object of a TOSCA NodeTemplate. */
117     public static Configuration obtainConfiguration(TNodeTemplate nodeTemplate) {
118         try {
119             Configuration configuration = (Configuration)nodeTemplate.getProperties().getAny();
120
121             //This could be eventually used to cross check node type and configuration type
122             //String typename = nodeTemplate.getType().getLocalPart().toLowerCase();
123             return configuration;
124
125
126         } catch (NullPointerException | ClassCastException ex) {
127             //To be eventually defined a mechanism to distinguish hostnode from forwarder
128             System.out.println("[Warning] Node " + nodeTemplate.getId().toString()
129                     + ": missing or invalid configuration, the node will be configured as a forwarder!" );
130             Configuration defConf = new Configuration();
131             defConf.setConfDescr(ToscaGrpcUtils.defaultDescr);
132             defConf.setConfID(ToscaGrpcUtils.defaultConfID);
133
134             Configuration.FieldmodifierConfiguration defaultForward = new Configuration.FieldmodifierConfiguration();
135             defaultForward.setName("DefaultForwarder");
136
137             defConf.setFieldmodifierConfiguration(defaultForward);
138             return defConf;
139         }
140     }
141
142 }