aboutsummaryrefslogtreecommitdiffstats
path: root/cmso-optimizer/src/main/java/org/onap/optf/cmso/optimizer/clients/optimizer/models/OptimizerResponseUtility.java
blob: 595bc0d1137f0b4543dce0c2ed61f0735a9e41e3 (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
/*
 *  ============LICENSE_START==============================================
 *  Copyright (c) 2019 AT&T Intellectual Property.
 *  =======================================================================
 *  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.optf.cmso.optimizer.clients.optimizer.models;

import com.google.common.base.CaseFormat;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.onap.observations.Observation;
import org.onap.optf.cmso.optimizer.common.LogMessages;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.introspector.Property;
import org.yaml.snakeyaml.introspector.PropertyUtils;

/**
 * The Class OptimizerResponseUtility.
 */
public class OptimizerResponseUtility extends PropertyUtils {

    /**
     * Parses the optimizer result.
     *
     * @param resultsFile the results file
     * @return the optimizer results
     */
    public OptimizerResults parseOptimizerResult(File resultsFile) {
        OptimizerResults results = null;
        try (InputStream input = new FileInputStream(resultsFile)) {
            Constructor constructor = new Constructor(OptimizerOutResults.class);
            constructor.setPropertyUtils(this);
            Yaml yaml = new Yaml(constructor);
            OptimizerOutResults optimizerOut = yaml.load(input);
            results = marshall(optimizerOut);
        } catch (Exception e) {
            Observation.report(LogMessages.UNEXPECTED_EXCEPTION, e, e.getMessage());
        }
        return results;
    }

    private OptimizerResults marshall(OptimizerOutResults optimizerOut) {
        OptimizerResults results = new OptimizerResults();
        results.setElapsedMillis(optimizerOut.getElapsedMillis());
        List<OptimizerSchedule> schedules = new ArrayList<>();
        results.setSchedules(schedules);
        for (OptimizerOutSchedule sch : optimizerOut.getResults()) {
            schedules.add(marshall(sch));
        }
        return results;
    }

    private OptimizerSchedule marshall(OptimizerOutSchedule sch) {
        OptimizerSchedule optimizerSchedule = new OptimizerSchedule();
        optimizerSchedule.setNumScheduled(sch.getNumScheduled());
        optimizerSchedule.setTotalCompletionTime(sch.getTotalCompletionTime());
        String[] rows = sch.getElementSlotLoader().split("\n");
        List<ElementSlot> slots = new ArrayList<>();
        optimizerSchedule.setElementSlotLoader(slots);
        for (String row : rows) {
            String[] cols = row.split(",");
            ElementSlot slot = new ElementSlot(cols);
            slots.add(slot);
        }
        return optimizerSchedule;
    }

    /**
     * Gets the property.
     *
     * @param type the type
     * @param name the name
     * @return the property
     */
    @Override
    public Property getProperty(Class<? extends Object> type, String name) {
        name = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, name);
        return super.getProperty(type, name);
    }

}