Assign image keyname and pubkey at vnf level
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / devicemanager / impl / src / main / java / org / opendaylight / mwtn / config / impl / GeoConfig.java
1 package org.opendaylight.mwtn.config.impl;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import org.opendaylight.mwtn.config.impl.AkkaConfig.ClusterRoleInfo;
8
9 import com.typesafe.config.Config;
10 import com.typesafe.config.ConfigFactory;
11
12 public class GeoConfig {
13
14
15         public static class RolesTableEntry
16         {
17                 @Override
18                 public String toString() {
19                         return "RolesTableEntry [role=" + role + ", ip=" + ip + "]";
20                 }
21                 private final AkkaConfig.ClusterRoleInfo role;
22                 private final String ip;
23                 public RolesTableEntry(Config c) throws Exception {
24                         this.role = new ClusterRoleInfo(c.getString("role"));
25                         this.ip=c.getString("ip");
26                 }
27         }
28         public static class RolesTable extends ArrayList<RolesTableEntry>
29         {
30                 /**
31                  *
32                  */
33                 private static final long serialVersionUID = -9146218864237487506L;
34
35                 public RolesTable(List<? extends Config> configList) throws Exception {
36                         for(Config c : configList)
37                         {
38                                 this.add(new RolesTableEntry(c));
39                         }
40                 }
41
42         }
43
44         private static final String DEFAULT_FILENAME = "configuration/initial/geo.conf";
45         private static final String LUMINA_ROOTNODENAME = "lumina-geo-cluster";
46         private final String filename;
47         private final String rootNodename;
48         private AkkaConfig.ClusterRoleInfoCollection primaryRoles;
49         private AkkaConfig.ClusterRoleInfoCollection secondayRoles;
50         private RolesTable rolesTable;
51         private GeoConfig()
52         {
53                 this(null);
54         }
55         @Override
56         public String toString() {
57                 return "GeoConfig [filename=" + filename + ", rootNodename=" + rootNodename + ", primaryRoles=" + primaryRoles
58                                 + ", secondayRoles=" + secondayRoles + ", rolesTable=" + rolesTable + "]";
59         }
60         private GeoConfig(String filename)
61         {
62                 this(filename,LUMINA_ROOTNODENAME);
63         }
64         private GeoConfig(String filename,String rootNodeName)
65         {
66                 this.filename=filename;
67                 this.rootNodename=rootNodeName;
68         }
69         public static boolean fileExists()
70         {
71                 File f=new File(DEFAULT_FILENAME);
72                 return f.exists();
73         }
74         public static GeoConfig load() throws Exception
75         {
76                 return load(DEFAULT_FILENAME);
77         }
78         public static GeoConfig load(String filename) throws Exception
79         {
80                 GeoConfig cfg=new GeoConfig(filename);
81                 cfg._load();
82                 return cfg;
83         }
84         private void _load() throws Exception
85         {
86                 this._load(ConfigFactory.parseFile(new File(this.filename)));
87         }
88         private void _load(Config cfg) throws Exception {
89                  this.primaryRoles=new AkkaConfig.ClusterRoleInfoCollection();
90                 List<String> a=cfg.getConfig(this.rootNodename).getStringList("primary_roles");
91
92                 for (int i = 0; i < a.size(); i++) {
93                         ClusterRoleInfo s = new ClusterRoleInfo(a.get(i));
94                         this.primaryRoles.add(s);
95                 }
96                 this.secondayRoles=new AkkaConfig.ClusterRoleInfoCollection();
97                 a=cfg.getConfig(this.rootNodename).getStringList("secondary_roles");
98                 for (int i = 0; i < a.size(); i++) {
99                         ClusterRoleInfo s = new ClusterRoleInfo(a.get(i));
100                         this.secondayRoles.add(s);
101                 }
102                 this.checkDuplicateRoleEntries();
103                 this.rolesTable = new RolesTable(cfg.getConfig(this.rootNodename).getConfigList("ip_roles_table"));
104         }
105         private void checkDuplicateRoleEntries() throws Exception {
106                 AkkaConfig.ClusterRoleInfoCollection duplicateEntries=new AkkaConfig.ClusterRoleInfoCollection();
107                 for(ClusterRoleInfo primaryRole: this.primaryRoles)
108                 {
109                         if(this.secondayRoles.contains(primaryRole))
110                                 duplicateEntries.add(primaryRole);
111                 }
112                 if(duplicateEntries.size()>0)
113                         throw new Exception("duplicate entries found: "+duplicateEntries.toString());
114
115         }
116         public static GeoConfig parse(String content) throws Exception {
117                 GeoConfig cfg=new GeoConfig();
118                 cfg._load(ConfigFactory.parseString(content));
119                 return cfg;
120         }
121         public AkkaConfig.ClusterRoleInfoCollection getPrimaryRoles() {
122                 return this.primaryRoles;
123         }
124         public AkkaConfig.ClusterRoleInfoCollection getSecondaryRoles() {
125                 return this.secondayRoles;
126         }
127         public boolean isPrimary(ClusterRoleInfo roleMember) {
128                 return !this.isSecondary(roleMember);
129         }
130         private boolean isSecondary(ClusterRoleInfo roleMember) {
131                 if(roleMember==null)
132                         return false;
133                 for(ClusterRoleInfo info:this.secondayRoles)
134                 {
135                         if(info.equals(roleMember))
136                                 return true;
137                 }
138                 return false;
139         }
140
141 }