Update the license for 2017-2018 license
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / introspection / PojoLoader.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 org.eclipse.persistence.jaxb.JAXBContextFactory;
26 import org.eclipse.persistence.jaxb.UnmarshallerProperties;
27 import org.onap.aai.db.props.AAIProperties;
28 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
29 import org.onap.aai.introspection.exceptions.AAIUnmarshallingException;
30 import org.onap.aai.logging.ErrorLogHelper;
31 import org.onap.aai.logging.LogFormatTools;
32 import org.onap.aai.restcore.MediaType;
33 import org.onap.aai.workarounds.NamingExceptions;
34
35 import javax.xml.bind.JAXBContext;
36 import javax.xml.bind.JAXBException;
37 import javax.xml.bind.Unmarshaller;
38 import javax.xml.transform.stream.StreamSource;
39 import java.io.StringReader;
40 import java.util.Map;
41
42 public class PojoLoader extends Loader {
43
44         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(PojoLoader.class);
45         private static final String POJO_BASE_PACKAGE_NAME = "org.onap.aai.domain.yang";
46
47         protected JAXBContext context;
48         private final String pojoPackageName;
49
50         protected PojoLoader(Version version) {
51                 super(version, ModelType.POJO);
52
53                 if (!version.equals(AAIProperties.LATEST)) {
54                         pojoPackageName = POJO_BASE_PACKAGE_NAME + "." + version;
55                 } else {
56                         pojoPackageName = POJO_BASE_PACKAGE_NAME;
57                 }
58
59                 try {
60                         context = JAXBContextFactory.createContext(pojoPackageName, this.getClass().getClassLoader());
61                 } catch (JAXBException e) {
62                         LOGGER.error("JAXBException while instantiation contect for PojoLoader " + LogFormatTools.getStackTop(e));
63                 }
64         }
65
66         @Override
67         public Introspector introspectorFromName(String name) throws AAIUnknownObjectException {
68                 return IntrospectorFactory.newInstance(ModelType.POJO, objectFromName(name));
69         }
70         
71         @Override
72         public Object objectFromName(String name) throws AAIUnknownObjectException {
73
74                 if (name == null) {
75                         throw new AAIUnknownObjectException("null name passed in");
76                 }
77                 final String sanitizedName = NamingExceptions.getInstance().getObjectName(name);
78                 final String upperCamel;
79
80                 //Contains any uppercase, then assume it's upper camel
81                 if (sanitizedName.matches(".*[A-Z].*")) {
82                         upperCamel = sanitizedName;
83                 } else {
84                         upperCamel = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, sanitizedName);
85                 }
86
87                 final String objectClassName;
88                 
89                 if (!upperCamel.contains(pojoPackageName)) {
90                         objectClassName = pojoPackageName + "." + upperCamel;
91                 } else {
92                         objectClassName = upperCamel;
93                 }
94
95                 try {
96                         return Class.forName(objectClassName).newInstance();
97                 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
98                         throw new AAIUnknownObjectException("Unrecognized AAI object " + name);
99                 }
100         }
101
102         @Override
103         protected void process(Version version) {
104                 LOGGER.warn("PojoLoader.process(Version) has not been implemented");
105         }
106
107         @Override
108         public Introspector unmarshal(String type, String json, MediaType mediaType) throws AAIUnmarshallingException {
109
110                  try {
111                         final Unmarshaller unmarshaller = context.createUnmarshaller();
112
113                         if (mediaType.equals(MediaType.APPLICATION_JSON_TYPE)) {
114                         unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
115                         unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
116                                 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
117                         }
118
119                         final Object clazz = objectFromName(type);
120                         final Object obj = unmarshaller.unmarshal(new StreamSource(new StringReader(json)), clazz.getClass()).getValue();
121
122                         return IntrospectorFactory.newInstance(ModelType.POJO, obj);
123                  } catch (JAXBException e) {
124                         ErrorLogHelper.logError("AAI_4007", "Could not unmarshall: " + e.getMessage());
125                         throw new AAIUnmarshallingException("Could not unmarshall: " + e.getMessage());
126                 } catch (AAIUnknownObjectException e) {
127                         throw new AAIUnmarshallingException("Could not unmarshall: " + e.getMessage(), e);
128                 }
129         }
130
131         @Override
132         public Map<String, Introspector> getAllObjects() {
133                 //TODO
134                 return null;
135         }
136
137 }