fa83ace3b3fbf51f4088a0c3a67daa56a85a131b
[cli.git] / framework / src / main / java / org / onap / cli / fw / cmd / profile / OnapProfileSetCommand.java
1 /*
2  * Copyright 2018 Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.cli.fw.cmd.profile;
18
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.onap.cli.fw.cmd.OnapCommand;
25 import org.onap.cli.fw.error.OnapCommandException;
26 import org.onap.cli.fw.error.OnapCommandProfileNotFound;
27 import org.onap.cli.fw.input.cache.OnapCommandParamEntity;
28 import org.onap.cli.fw.schema.OnapCommandSchema;
29 import org.onap.cli.fw.store.OnapCommandProfileStore;
30
31 /**
32  * Refresh external schema.
33  *
34  */
35 @OnapCommandSchema(schema = "profile-set.yaml")
36 public class OnapProfileSetCommand extends OnapCommand {
37
38     @Override
39     protected void run() throws OnapCommandException {
40          String product = getParametersMap().get("product").getValue().toString();
41          String profile = getParametersMap().get("profile").getValue().toString();
42          String service = getParametersMap().get("service").getValue().toString();
43          String command = getParametersMap().get("command").getValue().toString();
44
45          Map<String, String> params = (Map<String, String>)getParametersMap().get("parameter").getValue();
46
47          OnapCommandProfileStore cache = OnapCommandProfileStore.getInstance();
48
49          String prefix = "";
50          if (service.length() > 0) {
51              prefix += service + ":";
52          }
53          if (command.length() > 0) {
54              prefix += command + ":";
55          }
56
57          Map<String, OnapCommandParamEntity> map = new HashMap<>();
58
59          try {
60              for (OnapCommandParamEntity paramsExisting : cache.getInstance().loadParamFromCache(profile)) {
61                  map.put(paramsExisting.getProduct() + ":" + paramsExisting.getName(), paramsExisting);
62              }
63          } catch (OnapCommandProfileNotFound e) {
64              //ignore amd proceed to create
65          }
66
67          for (Map.Entry<String, String> entry: params.entrySet()) {
68              String name = prefix + entry.getKey();
69              OnapCommandParamEntity entity = new OnapCommandParamEntity();
70              entity.setProduct(product);
71              entity.setName(name);
72              entity.setValue(entry.getValue());
73
74              map.put(entity.getProduct() + ":" + entity.getName(), entity);
75          }
76
77          List<OnapCommandParamEntity> es = new ArrayList<>();
78          for (OnapCommandParamEntity e : map.values()) {
79              es.add(e);
80          }
81
82          cache.persistProfile( es, profile);
83     }
84
85 }