aboutsummaryrefslogtreecommitdiffstats
path: root/aai-core/src/main/java/org/onap/aai/ingestModel/CreateWidgetModels.java
blob: 1d66a8a58a5b80802dccd84a658001033e13821c (plain)
1
2
3
4
5
6
7
8
9
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.aai.ingestModel;

import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;

import javax.xml.transform.stream.StreamSource;

import org.onap.aai.introspection.Introspector;
import org.onap.aai.introspection.Loader;
import org.onap.aai.introspection.LoaderFactory;
import org.onap.aai.introspection.ModelType;
import org.onap.aai.setup.SchemaVersion;
import org.onap.aai.util.AAIConfig;
import org.onap.aai.util.AAIConstants;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * The Class CreateWidgetModels.
 */
public class CreateWidgetModels {
    /**
     * The main method.
     *
     * @param args the arguments
     * @throws Exception the exception
     */
    public static void main(String[] args) throws Exception {

        String _apiVersion = AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP);
        String widgetJsonDir = null;
        String modelVersion = null;
        if (args.length > 0) {
            if (args[0] != null) {
                _apiVersion = args[0];
            }
            if (args[1] != null) {
                widgetJsonDir = args[1];
            }
            if (args[2] != null) {
                modelVersion = args[2];
            }
        }

        if (widgetJsonDir == null) {
            System.err.println("You must specify a directory for widgetModelJson");
            System.exit(0);
        }
        if (modelVersion == null) {
            System.err.println("You must specify a modelVersion");
            System.exit(0);
        }

        AnnotationConfigApplicationContext ctx =
                new AnnotationConfigApplicationContext("org.onap.aai.config", "org.onap.aai.setup");

        LoaderFactory loaderFactory = ctx.getBean(LoaderFactory.class);
        Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, new SchemaVersion(_apiVersion));

        // iterate the collection of resources

        ArrayList<String> processedWidgets = new ArrayList<String>();
        for (Entry<String, Introspector> aaiResEnt : loader.getAllObjects().entrySet()) {
            Introspector meObject = loader.introspectorFromName("model");
            // no need for a ModelVers DynamicEntity

            Introspector aaiRes = aaiResEnt.getValue();

            if (!(aaiRes.isContainer() || aaiRes.getName().equals("aai-internal"))) {
                String resource = aaiRes.getName();

                if (processedWidgets.contains(resource)) {
                    continue;
                }

                Set<String> introspectorProperties = aaiRes.getProperties();

                if (!(introspectorProperties.contains("model-version-id")
                        && introspectorProperties.contains("model-invariant-id"))) {
                    System.out.println(aaiRes.getDbName() + " does not contain model properties so skipping");
                }
                processedWidgets.add(resource);

                String widgetName = resource;
                String filePathString = widgetJsonDir + "/" + widgetName + "-" + modelVersion + ".json";
                File f = new File(filePathString);

                String filePathString2 =
                        widgetJsonDir + "/../widget-model-json-old/" + widgetName + "-" + modelVersion + ".json";
                File f2 = new File(filePathString2);

                if (!f.exists() && !f.isDirectory()) {

                    if (f2.exists()) {
                        System.out.println("Using old file for " + resource + ".");

                        meObject = loader.unmarshal("model", new StreamSource(f2).getReader().toString());
                        // override, some of them are wrong
                        meObject.setValue("model-version", modelVersion);
                    } else {
                        System.out.println("Making new file for " + resource + ".");
                        meObject.setValue("model-invariant-id", UUID.randomUUID().toString());
                        meObject.setValue("model-type", "widget");
                        Introspector mevObject = loader.introspectorFromName("model-ver");
                        Introspector mevsObject = loader.introspectorFromName("model-vers");
                        mevObject.setValue("model-version-id", UUID.randomUUID().toString());
                        mevObject.setValue("model-version", modelVersion);
                        mevObject.setValue("model-Name", widgetName);
                        // make a list of dynamic Entities
                        List<Object> mevsList = new ArrayList<>();
                        // add this one, it will be the only one in the list in this case
                        mevsList.add(mevObject.getUnderlyingObject());
                        mevsObject.setValue("model-ver", mevsList);
                        // Have to figure out how to add my mev object to the mevsObject,
                        // the modelVers is a list of dynamic entities so we can just attach the array here
                        meObject.setValue("model-vers", mevsObject.getUnderlyingObject());
                    }

                    // put it out as JSON

                    PrintWriter out = new PrintWriter(f);
                    out.println(meObject.marshal(true));
                    out.close();

                } else {
                    System.out.println("File already exists for " + resource + ".  Skipping.");
                }
            }
        }
        System.exit(0);
    }
}