Update the license for 2017-2018 license
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / introspection / MoxyLoader.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aai.introspection;
21
22 import com.att.eelf.configuration.EELFLogger;
23 import com.att.eelf.configuration.EELFManager;
24 import com.google.common.base.CaseFormat;
25 import com.google.common.collect.ImmutableMap;
26 import org.eclipse.persistence.dynamic.DynamicEntity;
27 import org.eclipse.persistence.jaxb.UnmarshallerProperties;
28 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
29 import org.onap.aai.exceptions.AAIException;
30 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
31 import org.onap.aai.introspection.exceptions.AAIUnmarshallingException;
32 import org.onap.aai.logging.ErrorLogHelper;
33 import org.onap.aai.logging.LogFormatTools;
34 import org.onap.aai.restcore.MediaType;
35 import org.onap.aai.util.AAIConstants;
36 import org.onap.aai.workarounds.NamingExceptions;
37 import org.w3c.dom.Document;
38 import org.w3c.dom.NodeList;
39 import org.xml.sax.SAXException;
40
41 import javax.xml.XMLConstants;
42 import javax.xml.bind.JAXBException;
43 import javax.xml.bind.Unmarshaller;
44 import javax.xml.parsers.DocumentBuilder;
45 import javax.xml.parsers.DocumentBuilderFactory;
46 import javax.xml.parsers.ParserConfigurationException;
47 import javax.xml.transform.stream.StreamSource;
48 import java.io.*;
49 import java.util.HashSet;
50 import java.util.Map;
51 import java.util.Set;
52
53 public class MoxyLoader extends Loader {
54
55         private DynamicJAXBContext jaxbContext = null;
56         private EELFLogger LOGGER = EELFManager.getInstance().getLogger(MoxyLoader.class);
57         private Map<String, Introspector> allObjs = null;
58
59         /**
60          * Instantiates a new moxy loader.
61          *
62          * @param version the version
63          * @param llBuilder the ll builder
64          */
65         protected MoxyLoader(Version version) {
66                 super(version, ModelType.MOXY);
67                 process(version);
68         }
69
70         /**
71          * {@inheritDoc}
72          * @throws AAIUnknownObjectException 
73          */
74         @Override
75         public Introspector introspectorFromName(String name) throws AAIUnknownObjectException {
76
77                 return IntrospectorFactory.newInstance(ModelType.MOXY, objectFromName(name));
78         }
79         
80         /**
81          * {@inheritDoc}
82          */
83         @Override
84         public Object objectFromName(String name) throws AAIUnknownObjectException {
85
86                 if (name == null) {
87                         throw new AAIUnknownObjectException("null name passed in");
88                 }
89                 final String sanitizedName = NamingExceptions.getInstance().getObjectName(name);
90                 final String upperCamel;
91
92                 //Contains any uppercase, then assume it's upper camel
93                 if (name.matches(".*[A-Z].*")) {
94                         upperCamel = sanitizedName;
95                 } else {
96                         upperCamel = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, sanitizedName);
97                 }
98                 
99                 try {
100                         final DynamicEntity result = jaxbContext.newDynamicEntity(upperCamel);
101
102                         if (result == null) throw new AAIUnknownObjectException("Unrecognized AAI object " + name);
103
104                         return result;
105                 } catch (IllegalArgumentException e) {
106                         //entity does not exist
107                         throw new AAIUnknownObjectException("Unrecognized AAI object " + name, e);
108                 }
109         }
110
111         /**
112          * {@inheritDoc}
113          */
114         @Override
115         protected void process(Version version) {
116                 ModelInjestor injestor = ModelInjestor.getInstance();
117                 jaxbContext = injestor.getContextForVersion(version);
118                 
119         }
120
121         /**
122          * {@inheritDoc}
123          */
124         @Override
125         public Introspector unmarshal(String type, String json, MediaType mediaType) throws AAIUnmarshallingException {         
126                 try {
127                         final Object clazz = objectFromName(type);
128                         final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
129
130                         if (mediaType.equals(MediaType.APPLICATION_JSON_TYPE)) {
131                                 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
132                                 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
133                                 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
134                         }
135
136                         final DynamicEntity entity = (DynamicEntity) unmarshaller.unmarshal(new StreamSource(new StringReader(json)), clazz.getClass()).getValue();
137                         return IntrospectorFactory.newInstance(ModelType.MOXY, entity);
138                 } catch (JAXBException e) {
139                         AAIException ex = new AAIException("AAI_4007", e);
140                         ErrorLogHelper.logException(ex);
141                         throw new AAIUnmarshallingException("Could not unmarshall: " + e.getMessage(), ex);
142                 } catch (AAIUnknownObjectException e) {
143                         throw new AAIUnmarshallingException("Could not unmarshall: " + e.getMessage(), e);
144                 }
145         }
146         
147         @Override
148         public Map<String, Introspector> getAllObjects() {
149                 if (this.allObjs != null) {
150                         return allObjs;
151                 } else {
152                         ImmutableMap.Builder<String, Introspector> map = new ImmutableMap.Builder<String, Introspector>();
153                         Set<String> objs = objectsInVersion();
154                         for (String objName : objs) {
155                                 try {
156                                         Introspector introspector = this.introspectorFromName(objName);
157                                         map.put(introspector.getDbName(), introspector);
158                                 } catch (AAIUnknownObjectException e) {
159                                         LOGGER.warn("Unexpected AAIUnknownObjectException while running getAllObjects() " + LogFormatTools.getStackTop(e));
160                                 }
161                         }
162                         allObjs = map.build();
163                         return allObjs;
164                 }
165         }
166         
167         private Set<String> objectsInVersion() {
168                 final Set<String> result = new HashSet<>();
169
170                 try {
171                         final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
172                         final String fileName = ModelInjestor.getInstance().getOXMFileName(getVersion());
173
174                         docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
175
176                         final DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
177
178                         InputStream inputStream;
179                         File oxmFile = new File(AAIConstants.AAI_HOME_ETC + fileName);
180
181                         if(oxmFile.exists()){
182                             LOGGER.info("Oxm file exists on the system {}", oxmFile);
183                                 inputStream = new FileInputStream(oxmFile);
184                         } else {
185                                 LOGGER.warn("Unable to find oxm file {} on the system so using classloader", oxmFile);
186                                 inputStream = getClass().getClassLoader().getResourceAsStream(fileName);
187                         }
188
189                         final Document doc = docBuilder.parse(inputStream);
190                         final NodeList list = doc.getElementsByTagName("java-type");
191
192                         for (int i = 0; i < list.getLength(); i++) {
193                                 result.add(list.item(i).getAttributes().getNamedItem("name").getNodeValue());
194                         }
195                 } catch (ParserConfigurationException | SAXException | IOException e) {
196                         LOGGER.warn("Exception while enumerating objects for API version " + getVersion() + " (returning partial results) " + LogFormatTools.getStackTop(e));
197                 }
198
199                 //result.remove("EdgePropNames");
200                 return result;
201         }
202         
203         public DynamicJAXBContext getJAXBContext() {
204                 return this.jaxbContext;
205         }
206
207 }