summaryrefslogtreecommitdiffstats
path: root/vpp-integration/api-docs/core/src/main/java/io/fd/hc2vpp/docs/core/CoverageGenerator.java
blob: ff6c9b78ba20467de36bcf91bea6a4fcd49d29f6 (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
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 *
 * 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.
 */

package io.fd.hc2vpp.docs.core;


import static io.fd.hc2vpp.docs.api.Operation.CrudOperation.DELETE;
import static io.fd.hc2vpp.docs.api.Operation.CrudOperation.UPDATE;
import static io.fd.hc2vpp.docs.api.Operation.CrudOperation.WRITE;
import static java.lang.String.format;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.TypeLiteral;
import io.fd.hc2vpp.docs.api.CoverageUnit;
import io.fd.hc2vpp.docs.api.JavaApiMessage;
import io.fd.hc2vpp.docs.api.Operation;
import io.fd.hc2vpp.docs.api.PluginCoverage;
import io.fd.hc2vpp.docs.api.YangType;
import io.fd.honeycomb.translate.write.WriterFactory;
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.reflections.ReflectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CoverageGenerator implements VppApiUtils {

    private static final Logger LOG = LoggerFactory.getLogger(CoverageGenerator.class);

    private final CollectingWriterBuilder writerBuilder;

    public CoverageGenerator() {
        writerBuilder = new CollectingWriterBuilder();
    }

    public PluginCoverage generateConfigCoverage(final Class<?> pluginClass,
                                                 final String version,
                                                 final List<Module> scannedModules,
                                                 final YangTypeLinkIndex yangTypeIndex,
                                                 final ClassPathTypeIndex classPathIndex) {
        LOG.info("Generating config VPP API to Yang mapping for plugin {}", pluginClass);
        getInjectedWriterFactories(scannedModules).forEach(writerFactory -> writerFactory.init(writerBuilder));

        final Set<CoverageUnit> coverageUnits = writerBuilder.getWriteHandlers().stream()
                .flatMap(handler -> {
                    LOG.debug("Processing writer handling nodes: {}", handler.getHandledNodes());
                    // extracts customizer class from handler
                    final Class<?> customizerClass = getCustomizerClass(handler.getWriter());

                    // scans within write method
                    final Set<PluginMethodReference> writeReferences =
                            new CoverageScanner(customizerClass, WRITE, pluginClass).scan();
                    LOG.debug("writeReferences: {}", writeReferences);

                    // scans within update method
                    final Set<PluginMethodReference> updateReferences =
                            new CoverageScanner(customizerClass, UPDATE, pluginClass).scan();
                    LOG.debug("updateReferences: {}", updateReferences);

                    // scans within delete method
                    final Set<PluginMethodReference> deleteReferences =
                            new CoverageScanner(customizerClass, DELETE, pluginClass).scan();
                    LOG.debug("deleteReferences: {}", deleteReferences);

                    return Stream.of(writeReferences.stream(), updateReferences.stream(), deleteReferences.stream())
                        .flatMap(pluginMethodReferenceStream -> pluginMethodReferenceStream)
                        .collect(Collectors.groupingBy(PluginMethodReference::getName)).entrySet().stream()
                        .map(vppMessageReferenceByName -> {
                            final String jvppMethodName = vppMessageReferenceByName.getKey();
                            final CoverageUnit.CoverageUnitBuilder builder = new CoverageUnit.CoverageUnitBuilder();

                            // binds vpp api name and generateLink bind with version
                            builder.setVppApi(fromJvppApi(version, jvppMethodName));

                            //binds java api reference
                            builder.setJavaApi(new JavaApiMessage(jvppMethodName));

                            // binds Yang types with links from pre-build index
                            // TODO - use deserialized yii e.g. /module:parent-node/child-node
                            builder.setYangTypes(handler.getHandledNodes().stream()
                                .map(type -> new YangType(type, yangTypeIndex.getLinkForType(type)))
                                .collect(Collectors.toList()));


                            final Set<Operation> supportedOperations = new HashSet<>();
                            vppMessageReferenceByName.getValue().stream().forEach(
                                reference -> {
                                    final String callerClassLink = classPathIndex.linkForClass(reference.getCaller());
                                    if (writeReferences.contains(reference)) {
                                        supportedOperations.add(new Operation(callerClassLink, WRITE));
                                    }
                                    if (updateReferences.contains(reference)) {
                                        supportedOperations.add(new Operation(callerClassLink, UPDATE));
                                    }
                                    if (deleteReferences.contains(reference)) {
                                        supportedOperations.add(new Operation(callerClassLink, DELETE));
                                    }
                                }
                            );
                            return builder.setSupportedOperations(supportedOperations).build();
                        });
                }).collect(Collectors.toSet());

        return new PluginCoverage(pluginClass.getSimpleName(), coverageUnits, true);
    }

    private static Class<?> getCustomizerClass(final Object handler) {
        try {
            final Set<Field> customizerFields =
                    ReflectionUtils.getAllFields(handler.getClass(), field -> "customizer".equals(field.getName()));
            final Field customizerField = customizerFields.iterator().next();
            customizerField.setAccessible(true);
            return customizerField.get(handler).getClass();
        } catch (IllegalAccessException e) {
            throw new IllegalStateException(format("Unable to get customizer from %s ", handler), e);
        }
    }

    private static Set<WriterFactory> getInjectedWriterFactories(final List<Module> scannedModules) {
        Injector injector = Guice.createInjector(scannedModules);
        TypeLiteral<Set<WriterFactory>> writerFactoryType = new TypeLiteral<Set<WriterFactory>>() {
        };
        return injector.getInstance(Key.get(writerFactoryType));
    }
}