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
9 * http://www.apache.org/licenses/LICENSE-2.0
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=================================================
19 package org.onap.optf.cmso.optimizer.clients.optimizer.models;
21 import com.google.common.base.CaseFormat;
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;
35 * The Class OptimizerResponseUtility.
37 public class OptimizerResponseUtility extends PropertyUtils {
40 * Parses the optimizer result.
42 * @param resultsFile the results file
43 * @return the optimizer results
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());
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));
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);
82 return optimizerSchedule;
88 * @param type the type
89 * @param name the name
90 * @return the property
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);