06474ec2126836360f62da390865709a609b85ed
[cli.git] / framework / src / main / java / org / onap / cli / fw / cmd / profile / OnapProfileUnsetCommand.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.input.cache.OnapCommandParamEntity;
27 import org.onap.cli.fw.schema.OnapCommandSchema;
28 import org.onap.cli.fw.store.OnapCommandProfileStore;
29
30 /**
31  * Refresh external schema.
32  *
33  */
34 @OnapCommandSchema(schema = "profile-unset.yaml")
35 public class OnapProfileUnsetCommand extends OnapCommand {
36
37     @Override
38     protected void run() throws OnapCommandException {
39          //mrkanag add feature to remove the profile at product or service or command level
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          List<String> params = (List<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          for (OnapCommandParamEntity paramsExisting : cache.getInstance().loadParamFromCache(profile)) {
60              map.put(paramsExisting.getProduct() + ":" + paramsExisting.getName(), paramsExisting);
61          }
62
63          for (String name: params) {
64              name = prefix + name;
65
66              if (map.containsKey(product + ":" + name))
67                  map.remove(product + ":" + name);
68          }
69
70          List<OnapCommandParamEntity> es = new ArrayList<>();
71          for (OnapCommandParamEntity e : map.values()) {
72              es.add(e);
73          }
74
75          cache.persistProfile( es, profile);
76     }
77 }