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;
34 public class OptimizerResponseUtility extends PropertyUtils {
36 public OptimizerResults parseOptimizerResult(File resultsFile) {
37 OptimizerResults results = null;
38 try (InputStream input = new FileInputStream(resultsFile)) {
39 Constructor constructor = new Constructor(OptimizerOutResults.class);
40 constructor.setPropertyUtils(this);
41 Yaml yaml = new Yaml(constructor);
42 OptimizerOutResults optimizerOut = yaml.load(input);
43 results = marshall(optimizerOut);
44 } catch (Exception e) {
45 Observation.report(LogMessages.UNEXPECTED_EXCEPTION, e, e.getMessage());
50 private OptimizerResults marshall(OptimizerOutResults optimizerOut) {
51 OptimizerResults results = new OptimizerResults();
52 results.setElapsedMillis(optimizerOut.getElapsedMillis());
53 List<OptimizerSchedule> schedules = new ArrayList<>();
54 results.setSchedules(schedules);
55 for (OptimizerOutSchedule sch : optimizerOut.getResults()) {
56 schedules.add(marshall(sch));
61 private OptimizerSchedule marshall(OptimizerOutSchedule sch) {
62 OptimizerSchedule optimizerSchedule = new OptimizerSchedule();
63 optimizerSchedule.setNumScheduled(sch.getNumScheduled());
64 optimizerSchedule.setTotalCompletionTime(sch.getTotalCompletionTime());
65 String[] rows = sch.getElementSlotLoader().split("\n");
66 List<ElementSlot> slots = new ArrayList<>();
67 optimizerSchedule.setElementSlotLoader(slots);
68 for (String row : rows) {
69 String[] cols = row.split(",");
70 ElementSlot slot = new ElementSlot(cols);
73 return optimizerSchedule;
77 public Property getProperty(Class<? extends Object> type, String name) {
78 name = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, name);
79 return super.getProperty(type, name);