595bc0d1137f0b4543dce0c2ed61f0735a9e41e3
[optf/cmso.git] /
1 /*
2  *  ============LICENSE_START==============================================
3  *  Copyright (c) 2019 AT&T Intellectual Property.
4  *  =======================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
6  *  not use this file except in compliance with the License. You may obtain a
7  *  copy of the License at
8  *
9  *         http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing
15  * permissions and limitations under the License.
16  * ============LICENSE_END=================================================
17  */
18
19 package org.onap.optf.cmso.optimizer.clients.optimizer.models;
20
21 import com.google.common.base.CaseFormat;
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.InputStream;
25 import java.util.ArrayList;
26 import java.util.List;
27 import org.onap.observations.Observation;
28 import org.onap.optf.cmso.optimizer.common.LogMessages;
29 import org.yaml.snakeyaml.Yaml;
30 import org.yaml.snakeyaml.constructor.Constructor;
31 import org.yaml.snakeyaml.introspector.Property;
32 import org.yaml.snakeyaml.introspector.PropertyUtils;
33
34 /**
35  * The Class OptimizerResponseUtility.
36  */
37 public class OptimizerResponseUtility extends PropertyUtils {
38
39     /**
40      * Parses the optimizer result.
41      *
42      * @param resultsFile the results file
43      * @return the optimizer results
44      */
45     public OptimizerResults parseOptimizerResult(File resultsFile) {
46         OptimizerResults results = null;
47         try (InputStream input = new FileInputStream(resultsFile)) {
48             Constructor constructor = new Constructor(OptimizerOutResults.class);
49             constructor.setPropertyUtils(this);
50             Yaml yaml = new Yaml(constructor);
51             OptimizerOutResults optimizerOut = yaml.load(input);
52             results = marshall(optimizerOut);
53         } catch (Exception e) {
54             Observation.report(LogMessages.UNEXPECTED_EXCEPTION, e, e.getMessage());
55         }
56         return results;
57     }
58
59     private OptimizerResults marshall(OptimizerOutResults optimizerOut) {
60         OptimizerResults results = new OptimizerResults();
61         results.setElapsedMillis(optimizerOut.getElapsedMillis());
62         List<OptimizerSchedule> schedules = new ArrayList<>();
63         results.setSchedules(schedules);
64         for (OptimizerOutSchedule sch : optimizerOut.getResults()) {
65             schedules.add(marshall(sch));
66         }
67         return results;
68     }
69
70     private OptimizerSchedule marshall(OptimizerOutSchedule sch) {
71         OptimizerSchedule optimizerSchedule = new OptimizerSchedule();
72         optimizerSchedule.setNumScheduled(sch.getNumScheduled());
73         optimizerSchedule.setTotalCompletionTime(sch.getTotalCompletionTime());
74         String[] rows = sch.getElementSlotLoader().split("\n");
75         List<ElementSlot> slots = new ArrayList<>();
76         optimizerSchedule.setElementSlotLoader(slots);
77         for (String row : rows) {
78             String[] cols = row.split(",");
79             ElementSlot slot = new ElementSlot(cols);
80             slots.add(slot);
81         }
82         return optimizerSchedule;
83     }
84
85     /**
86      * Gets the property.
87      *
88      * @param type the type
89      * @param name the name
90      * @return the property
91      */
92     @Override
93     public Property getProperty(Class<? extends Object> type, String name) {
94         name = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, name);
95         return super.getProperty(type, name);
96     }
97
98 }