summaryrefslogtreecommitdiffstats
path: root/dcaedt_catalog/commons/src/main/java/org/onap/sdc/dcae/catalog/commons/Recycler.java
blob: 2711722f37484efc6302647a228c2d372abf8376 (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package org.onap.sdc.dcae.catalog.commons;

import java.io.Reader;
import java.io.IOException;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.AbstractMap;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Collections;
import java.util.Spliterators;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import org.apache.commons.jxpath.Pointer;
import org.apache.commons.jxpath.JXPathContext;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.type.TypeReference;

import org.onap.sdc.common.onaplog.OnapLoggerDebug;
import org.onap.sdc.common.onaplog.Enums.LogLevel;
import org.yaml.snakeyaml.Yaml;


/**
 * Practically a copy of the Validator's service Recycler, minus the Spring framework aspects + picking up the
 * description of every node
 */
public class Recycler {

    private static final String PROPERTIES = "properties";
    private static final String VALUE = "value";
    private static final String ASSIGNMENT = "assignment";
    private static final String CAPABILITY = "capability";
    private static final String RELATIONSHIP = "relationship";
	private static final String NAME = "name";
	private static OnapLoggerDebug debugLogger = OnapLoggerDebug.getInstance();
    private List<Map> 	 imports;
    private List<String> metas;

    public Recycler() {
        withImports();
        withMetas(null);
    }

    public Recycler withImports(String... theImports) {
        debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Setting imports to {}", theImports);
        ListBuilder importsBuilder = new ListBuilder();
        for (int i = 0; i < theImports.length; i++) {
            importsBuilder.add(new MapBuilder()
                    .put("i" + i, theImports[i])
                    .build());
        }
        this.imports = importsBuilder.build();
        return this;
    }

    private List imports() {
        ListBuilder importsBuilder = new ListBuilder();
        for (Map e: this.imports) {
            importsBuilder.add(new MapBuilder()
                    .putAll(e)
                    .build());
        }
        return importsBuilder.build();
    }

    public Recycler withMetas(String... theMetas) {
        this.metas = (theMetas == null) ? Collections.emptyList() : Arrays.asList(theMetas);
        return this;
    }

    public Map recycle(final Reader theSource) throws IOException {
        return this.recycle(new ObjectMapper().readValue(theSource, (Class)HashMap.class));
    }
    
    private Map recycle(final Object theDump) {
  
        final JXPathContext jxroot = JXPathContext.newContext(theDump);
    	jxroot.setLenient(true);

        final Map<String, Object> nodeTemplates =
            (Map<String, Object>)new MapBuilder()
                .putAll(
                        StreamSupport
                            .stream(
                                Spliterators.spliteratorUnknownSize((Iterator<Pointer>)jxroot.iteratePointers("/nodes"), 16), false)
                            .map(p -> {
                                        JXPathContext jxnode = jxroot.getRelativeContext(p);
                                        return new AbstractMap.SimpleEntry<String,Object>(
                                            (String)jxnode.getValue(NAME) + "_" + (String)jxnode.getValue("nid"),
                                            new MapBuilder()
                                                    .put("type", jxnode.getValue("type/name"))
                                                    .put("description", jxnode.getValue("description"))
                                                    .putOpt("metadata", nodeMetadata(jxnode))
                                                    .putOpt(PROPERTIES, nodeProperties(jxnode))
                                                    .putOpt("requirements", nodeRequirements(jxnode))
                                                    .putOpt("capabilities", nodeCapabilities(jxnode))
                                                    .build());
                            })::iterator)
                .buildOpt();

            return new MapBuilder()
                                    .put("tosca_definitions_version", "tosca_simple_yaml_1_0_0")
                                    .put("imports", imports())
                                    .put("topology_template", new MapBuilder()
                                                                                            .putOpt("node_templates", nodeTemplates)
                                                                                            .build())
                                    .build();
    }

    /* */
    private Object nodeProperties(JXPathContext theNodeContext) {
        return
            new MapBuilder()
                .putAll(
                    StreamSupport.stream(
                        Spliterators.spliteratorUnknownSize((Iterator<Map>)theNodeContext.iterate(PROPERTIES), 16), false)
                                                    .map(m -> new AbstractMap.SimpleEntry(m.get(NAME), this.nodeProperty(m)))
                                                    .filter(e -> e.getValue() != null)
                            ::iterator)
                .buildOpt();
    }
    
    /* */
    private Object nodeProperty(final Map theSpec) {
        Object value = theSpec.get(VALUE);
        if (value == null) {
            value = theSpec.get("default");
            if (value == null) {
                /*final*/ Map assign = (Map)theSpec.get(ASSIGNMENT);
                if (assign != null) {
                    value = assign.get(VALUE);
                }
            }
        }
        String type = (String)theSpec.get("type");
        if (value != null && type != null && !"string".equals(type)) {
			value = getValueByType(value, type);
		}
        return value;
    }

	private Object getValueByType(Object value, String type) {

		try {
            if ("map".equals(type) && !(value instanceof Map)) {
				return new ObjectMapper().readValue(value.toString(), new TypeReference<Map>(){});
            }
            if ("list".equals(type) && !(value instanceof List)) {
				return new ObjectMapper().readValue(value.toString(), new TypeReference<List>(){});
            }
            if ("integer".equals(type) && (value instanceof String)) {
				return Integer.valueOf((String)value);
            }
            if ("float".equals(type) && (value instanceof String)) {
				return Double.valueOf((String)value); //double because that's how the yaml parser would encode it
            }
        }
        catch (NumberFormatException nfx) {
            debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Failed to process String representation {} of numeric data: {}", value, nfx);
        }
        catch (IOException iox) {
            debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Failed to process {} representation of a collection: {}", value.getClass().getName(), iox);
        }
		return value;
	}

	/* */
    private List nodeRequirements(JXPathContext theNodeContext) {
        return
            new ListBuilder()
                .addAll(
                    StreamSupport.stream(
                        Spliterators.spliteratorUnknownSize((Iterator<Map>)theNodeContext.iterate("requirements"), 16), false)
                                                    .flatMap(m -> this.nodeRequirement(m, theNodeContext).stream())
                    //nicer that the ListBuilder buy cannot handle the empty lists, i.e. it will generate empty requirement lists
                    //								.collect(Collectors.toList())
                                                    .toArray())
                .buildOpt();
    }

    /*
     * @param theSpec the requirement entry that appears within the node specification
     * @param theNodeContext .. Should I pass the root context instead of assuming that the nodes context has it as parent?
     * @return a List as one requirement (definition) could end up being instantiated multiple times
     */
    private List nodeRequirement(final Map theSpec, JXPathContext theNodeContext/*Iterator theTargets*/) {

        final ListBuilder value = new ListBuilder();

        final Map target = (Map)theSpec.get("target");
        final Map capability = (Map)theSpec.get(CAPABILITY);
        final Map relationship = (Map)theSpec.get(RELATIONSHIP);

        //this are actual assignments
        for (Iterator i = theNodeContext.getParentContext().iterate("/relations[@n2='" + theNodeContext.getValue("nid") + "']/meta[@p2='" + theSpec.get(NAME) +"']"); i.hasNext(); ) {

           String targetNodeName = (String)((Map)i.next()).get("n1");

           //make sure target exists
           Map targetNode = (Map)theNodeContext.getParentContext().getValue("/nodes[@nid='" + targetNodeName + "']");
           if (null == targetNode) {
                debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Relation points to non-existing node {}", targetNodeName);
                continue; //this risks of producing a partial template ..
           }

           value.add(new MapBuilder().put(theSpec.get(NAME), new MapBuilder()
                       .putOpt("node", targetNode.get(NAME) + "_" + targetNode.get("nid"))
                       .putOpt(CAPABILITY, capability == null ? null : capability.get(NAME))
                       .putOpt(RELATIONSHIP, relationship == null ? null : relationship.get("type"))
                       .build()).build());
        }
		addTemporary(theSpec, theNodeContext, value, capability, relationship);

		if (value.isEmpty()) {
           value.add(new MapBuilder().put(theSpec.get(NAME), new MapBuilder()
                       .putOpt("node", target == null ? null : target.get(NAME) + "_" + target.get("nid"))
                       .putOpt(CAPABILITY, capability == null ? null : capability.get(NAME))
                       .putOpt(RELATIONSHIP, relationship == null ? null : relationship.get("type"))
                       .build()).build());
        }

        return value.build();
    }

	private void addTemporary(Map theSpec, JXPathContext theNodeContext, ListBuilder value, Map capability, Map relationship) {
		//temporary
		for (Iterator i = theNodeContext.getParentContext().iterate("/relations[@n1='" + theNodeContext.getValue("nid") + "']/meta[@p1='" + theSpec.get(NAME) +"']"); i.hasNext(); ) {

           String targetNodeName = (String)((Map)i.next()).get("n2");

           Map targetNode = (Map)theNodeContext.getParentContext().getValue("/nodes[@nid='" + targetNodeName + "']");
           //make sure target exists
           if (null == targetNode) {
                debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Relation points to non-existing node {}", targetNode);
                continue; //this risks of producing a partial template ..
           }

           value.add(new MapBuilder().put(theSpec.get(NAME), new MapBuilder()
                       .putOpt("node", targetNode.get(NAME) + "_" + targetNode.get("nid"))
                       .putOpt(CAPABILITY, capability == null ? null : capability.get(NAME))
                       .putOpt(RELATIONSHIP, relationship == null ? null : relationship.get("type"))
                       .build()).build());
        }
		//end temporary
	}

	/* */
    private Map nodeCapabilities(JXPathContext theNodeContext) {
        return
            new MapBuilder()
                .putAll(
                    StreamSupport.stream(
                        Spliterators.spliteratorUnknownSize((Iterator<Map>)theNodeContext.iterate("capabilities"), 16), false)
                                             .map(m -> this.nodeCapability(m))
                                             .filter(c -> c != null)
                                             ::iterator)
                .buildOpt();
    }

    /**
     * this handles a capability assignment which only includes properties and attributes so unless there
     * are any properties/attributes assignments we might not generate anything
     */
    private Map.Entry nodeCapability(final Map theSpec) {
        List<Map> properties = (List<Map>) theSpec.get(PROPERTIES);
        if (properties == null || properties.isEmpty()) {
            return null;
        }

        return new AbstractMap.SimpleEntry(theSpec.get(NAME),
                new MapBuilder()
                        .put(PROPERTIES,
                                new MapBuilder().putAll(properties.stream()
                                        .filter(p -> p.containsKey(ASSIGNMENT) ||
                                                p.containsKey(VALUE))
                                        .map(p -> new AbstractMap.SimpleEntry(
                                                p.get(NAME),
                                                p.containsKey(ASSIGNMENT) ?
                                            ((Map) p.get(ASSIGNMENT)).get(VALUE)
                                                        : p.get(VALUE))
                                                )
                                        ::iterator)
                                .build())
                        .build());
    }


    /* */
    private Object nodeMetadata(JXPathContext theNodeContext) {
        return
            new MapBuilder()
                .putAll(
                        this.metas
                            .stream()
                            .flatMap(m -> {
                                                Object v = theNodeContext.getValue(m);
                                                if (v == null) {
                                                    return Stream.empty();
                                                }
                                                if (v instanceof Map) {
                                                    return ((Map) v).entrySet()
                                                            .stream()
                                                            .map(e -> new AbstractMap.SimpleEntry<String, Object>
                                                                    (((Map.Entry) e).getKey().toString(),
                                                                            ((Map.Entry) e).getValue().toString()));
                                                }
                                                return Stream.of(new AbstractMap.SimpleEntry<String,Object>(m, v.toString()));
                                            })
                        ::iterator)
                .buildOpt();
    }


    public static String toString(Object theVal) {
        return new Yaml().dump(theVal);
    }
  

    public static void main(String[] theArgs) throws Exception {
        debugLogger.log(LogLevel.DEBUG, Recycler.class.getName(),
                Recycler.toString(
                new Recycler().recycle(new java.io.FileReader(theArgs[0]))));
    }
}