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