summaryrefslogtreecommitdiffstats
path: root/dcaedt_catalog/api/src/main/java/org/onap/sdc/dcae/catalog/Catalog.java
blob: d7db9e64caede683fe26eb226cefc8982eae5fe9 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package org.onap.sdc.dcae.catalog;

import org.json.JSONArray;
import org.json.JSONObject;
import org.onap.sdc.dcae.catalog.commons.Action;
import org.onap.sdc.dcae.catalog.commons.Future;
import org.onap.sdc.dcae.catalog.commons.Proxies;
import org.onap.sdc.dcae.composition.restmodels.sdc.ResourceDetailed;

import java.util.Iterator;
import java.util.LinkedList;

public interface Catalog {

    <T> T proxy(JSONObject theData, Class<T> theType);


    /* Base class for all Catalog objects. */
    interface Element<T extends Element<T>> {

        default Class<T> selfClass() {
            return (Class<T>)getClass().getInterfaces()[0];
        }

        Catalog catalog();

        String id();

        /**
         * Direct access to the underlying JSON object.
         * Warning: Modifications to the JSON object are reflected in the Element.
         */
        JSONObject data();

		/* Allows for typed deep exploration of the backing JSON data structure
         * @arg theName name of a JSON entry ; It must map another JSONObject.
         * @arg theType the expected wrapping catalog artifact type
         * @return the JSON entry wrapped in the specified type
         */
        default <E extends Element<E>> E element(String theName, Class<E> theType) {
            JSONObject elemData = data().optJSONObject(theName);
            if (elemData == null) {
                return null;
            }
            else {
                return catalog().proxy(elemData, theType);
            }
        }

        /* Similar to {@link #element(String,Class)} but for collection wrapping. */
        default <E extends Elements> E elements(String theName, Class<E> theType) {
            JSONArray elemsData = data().optJSONArray(theName);
            if (elemsData == null) {
                return null;
            }
            else {
                Class etype = Proxies.typeArgument(theType);
                Elements elems;
                try {
                    elems = theType.newInstance();
                }
                catch (ReflectiveOperationException rox) {
                    throw new RuntimeException("Failed to instantiate " + theType, rox);
                }

                try{
                    for (Iterator i = elemsData.iterator(); i.hasNext();) {
                        JSONObject elemData = (JSONObject)i.next();
                        elems.add(catalog().proxy(elemData,	etype));
                    }
                }
                catch(Exception e){
                    throw new RuntimeException("Failed to fetch json data ", e);
                }
                return (E)elems;
            }
        }
    }

    /* Base class for all collections of elements. */
    class Elements<T extends Element>
                                                extends LinkedList<T> {
        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder("[");
            for (Element el: this) {
                sb.append(el.selfClass().getSimpleName())
                    .append("(")
                    .append(el.data())
                    .append("),");
            }
            sb.append("]");
            return sb.toString();
        }
    }

    /*
     * We need this contraption in order to store a mix of Folders and CatalogItem
     * instances (Elements in self is not good because it is defined around a
     * type variable so we cannot use reflection to determine the type at runtime
     * - generics are resolved compile time)
     */
    class Mixels extends Elements<Element> {}

    interface Item<T extends Item<T>> extends Element<T> {
        String name();
        String description();
    }

    /*
     * Collection of catalog items.
     */
    class Items extends Elements<Item> {}

    interface Folder extends Element<Folder> {

        String name();

        String description();

        String itemId();

    }

    class Folders extends Elements<Folder> {}

    //no predefined properties here
    interface Annotation extends Element<Annotation> {}

  class Annotations extends Elements<Annotation> {
  }

    /**
     * A TOSCA teamplate.
     * When a deep loading method is used to obtain a Template its collection
     * of inputs and nodes will be immediately available (and 'cached' within
     * the backing JSON object). It can be retrieved through a call to
     * {@link Element#elements(String,Class)} as in:
     *	elements("inputs", Inputs.class)
     * or
     *  elements("nodes", Nodes.class)
     *
     * The same result will be obtained through one of the methods of the
     * navigation interface. in this case
     * the result does not become part of the backing JSONObject.
     */
    interface Template extends Element<Template> {
        String name();

        String version();

        String description();
    }

    /**
     * Collection of {@link Catalog.Template template} instances.
     */
    class Templates extends Elements<Template> {
    }


    /**
     * A TOSCA type declaration.
     */
    interface Type extends Element<Type> {
        String name();
    }

    /**
     * Collection of {@link Catalog.Type type} instances.
     */
    class Types extends Elements<Type> {
    }


    interface TemplateAction extends Action<Template> {

        TemplateAction withInputs();

        TemplateAction withOutputs();

        TemplateAction withNodes();

        TemplateAction withNodeProperties();

        TemplateAction withNodeRequirements();

        TemplateAction withNodePropertiesAssignments();

        TemplateAction withNodeCapabilities();

        TemplateAction withNodeCapabilityProperties();

        TemplateAction withNodeCapabilityPropertyAssignments();

        TemplateAction withPolicies();

        TemplateAction withPolicyProperties();

        TemplateAction withPolicyPropertiesAssignments();

        @Override
         Future<Template> execute();
    }

    interface TypeAction extends Action<Type> {

        TypeAction withHierarchy();

        TypeAction withRequirements();

        TypeAction withCapabilities();

        @Override
        Future<Type> execute();
    }


    TemplateAction template(ResourceDetailed resourceData);

    TypeAction type(String theNamespace, String theTypeName);
}