From 1645a5318fb80a959062e7bef6fb7e641e40a94b Mon Sep 17 00:00:00 2001 From: Eunju Kim Date: Fri, 8 May 2020 23:22:50 -0700 Subject: [PATCH 1/8] [Micro services] MAC manager --- services/mac_manager/pom.xml | 6 +- .../macmanager/config/RedisConfiguration.java | 110 ------- .../macmanager/dao/MacPoolRepository.java | 129 ++++++++ .../macmanager/dao/MacRangeRepository.java | 28 +- .../macmanager/dao/MacStateRepository.java | 27 +- .../dao/redis/MacPoolRedisRepository.java | 81 ----- .../dao/redis/MacRangeRedisRepository.java | 77 ----- .../dao/redis/MacStateRedisRepository.java | 82 ------ .../alcor/macmanager/entity/MacAddress.java | 2 + .../alcor/macmanager/entity/MacRange.java | 19 +- .../service/implement/MacServiceImpl.java | 107 ++++--- .../RedisMacRangePublisherServiceImpl.java | 43 --- .../implement/redis/RedisMacServiceImpl.java | 277 ------------------ .../redis/RedisPublisherServiceImpl.java | 45 --- .../src/main/resources/application.properties | 28 +- .../macmanager/swagger}/SwaggerConfig.java | 2 +- 16 files changed, 267 insertions(+), 796 deletions(-) delete mode 100644 services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/config/RedisConfiguration.java create mode 100644 services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/dao/MacPoolRepository.java delete mode 100644 services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/dao/redis/MacPoolRedisRepository.java delete mode 100644 services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/dao/redis/MacRangeRedisRepository.java delete mode 100644 services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/dao/redis/MacStateRedisRepository.java delete mode 100644 services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/service/implement/redis/RedisMacRangePublisherServiceImpl.java delete mode 100644 services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/service/implement/redis/RedisMacServiceImpl.java delete mode 100644 services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/service/implement/redis/RedisPublisherServiceImpl.java rename services/mac_manager/src/{main/java/com/futurewei/alcor/macmanager/config => test/java/com/futurewei/alcor/macmanager/swagger}/SwaggerConfig.java (97%) diff --git a/services/mac_manager/pom.xml b/services/mac_manager/pom.xml index e56f86cde..30799ec9f 100644 --- a/services/mac_manager/pom.xml +++ b/services/mac_manager/pom.xml @@ -88,11 +88,7 @@ io.springfox springfox-swagger2 2.6.1 - - - io.springfox - springfox-swagger-ui - 2.2.2 + test io.github.swagger2markup diff --git a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/config/RedisConfiguration.java b/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/config/RedisConfiguration.java deleted file mode 100644 index 6b0938cbb..000000000 --- a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/config/RedisConfiguration.java +++ /dev/null @@ -1,110 +0,0 @@ -/* -Copyright 2019 The Alcor Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - - -package com.futurewei.alcor.macmanager.config; - - -import com.futurewei.alcor.common.repo.ICachePublisher; -import com.futurewei.alcor.common.service.RedisListener; -import com.futurewei.alcor.macmanager.entity.MacRange; -import com.futurewei.alcor.macmanager.entity.MacState; -import com.futurewei.alcor.macmanager.service.implement.redis.RedisMacRangePublisherServiceImpl; -import com.futurewei.alcor.macmanager.service.implement.redis.RedisPublisherServiceImpl; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.autoconfigure.domain.EntityScan; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.data.redis.connection.RedisStandaloneConfiguration; -import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.data.redis.listener.ChannelTopic; -import org.springframework.data.redis.listener.RedisMessageListenerContainer; -import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; -import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; -import org.springframework.data.redis.serializer.StringRedisSerializer; - -@Configuration -@ComponentScan({"com.futurewei.alcor.macmanager.service", "com.futurewei.alcor.common.service"}) -@EntityScan({"com.futurewei.alcor.macmanager.entity}", "com.futurewei.common.entity"}) -public class RedisConfiguration { - - @Value("${spring.redis.host}") - private String redisHostName; - - @Value("${spring.redis.port}") - private int redisHostPort; - - @Bean - LettuceConnectionFactory lettuceConnectionFactory() { - RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(); - configuration.setHostName(redisHostName); - configuration.setPort(redisHostPort); - return new LettuceConnectionFactory(configuration); - } - - @Bean - public RedisTemplate redisMacTemplate() { - final RedisTemplate template = new RedisTemplate(); - template.setConnectionFactory(lettuceConnectionFactory()); - template.setKeySerializer(new StringRedisSerializer()); - template.setHashValueSerializer(new Jackson2JsonRedisSerializer(MacState.class)); - template.setValueSerializer(new Jackson2JsonRedisSerializer(MacState.class)); - template.setEnableTransactionSupport(true); - return template; - } - - @Bean - public RedisTemplate redisMacRangeTemplate() { - final RedisTemplate templateMacRange = new RedisTemplate(); - templateMacRange.setConnectionFactory(lettuceConnectionFactory()); - templateMacRange.setKeySerializer(new StringRedisSerializer()); - templateMacRange.setHashValueSerializer(new Jackson2JsonRedisSerializer(MacRange.class)); - templateMacRange.setValueSerializer(new Jackson2JsonRedisSerializer(MacRange.class)); - templateMacRange.setEnableTransactionSupport(true); - return templateMacRange; - } - - @Bean - MessageListenerAdapter redisListenerInstance() { - return new MessageListenerAdapter(new RedisListener()); - } - - @Bean - RedisMessageListenerContainer redisContainer() { - final RedisMessageListenerContainer container = new RedisMessageListenerContainer(); - container.setConnectionFactory(lettuceConnectionFactory()); - container.addMessageListener(redisListenerInstance(), topic()); - return container; - } - - @Bean - ICachePublisher redisMacPublisherInstance() { - return new RedisPublisherServiceImpl(redisMacTemplate(), topic()); - } - - @Bean - ICachePublisher redisMacRangePublisherInstance() { - return new RedisMacRangePublisherServiceImpl(redisMacRangeTemplate(), topic()); - } - - @Bean - ChannelTopic topic() { - return new ChannelTopic("pubsub:queue"); - } -} - diff --git a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/dao/MacPoolRepository.java b/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/dao/MacPoolRepository.java new file mode 100644 index 000000000..eeef442e9 --- /dev/null +++ b/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/dao/MacPoolRepository.java @@ -0,0 +1,129 @@ +/*Copyright 2019 The Alcor Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +package com.futurewei.alcor.macmanager.dao; + +import com.futurewei.alcor.common.db.CacheException; +import com.futurewei.alcor.common.db.CacheFactory; +import com.futurewei.alcor.common.db.ICache; +import com.futurewei.alcor.common.db.Transaction; +import com.futurewei.alcor.common.exception.ResourceNotFoundException; +import com.futurewei.alcor.common.repo.ICacheRepository; +import com.futurewei.alcor.macmanager.entity.MacAddress; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.stereotype.Repository; + +import javax.annotation.PostConstruct; +import java.util.Arrays; +import java.util.Map; +import java.util.Vector; +import java.util.concurrent.ThreadLocalRandom; + +@Repository +@ComponentScan(value = "com.futurewei.alcor.common.db") +public class MacPoolRepository implements ICacheRepository { + private ICache cache; + + @Autowired + public MacPoolRepository(CacheFactory cacheFactory) { + cache = cacheFactory.getCache(MacAddress.class); + } + + public ICache getCache() { + return cache; + } + + @PostConstruct + private void init() { + + } + + @Override + public MacAddress findItem(String strMacAddress) throws CacheException, ResourceNotFoundException { + if (cache.containsKey(strMacAddress)) + return cache.get(strMacAddress); + else + return null; + } + + @Override + public Map findAllItems() throws CacheException { + return cache.getAll(); + } + + /** + * add a MAC address to MAC pool repository + * + * @param macAddress MAC address + * @return void + * @throws Exception Db or cache operation exception + */ + @Override + public void addItem(MacAddress macAddress) throws CacheException { + if (cache.containsKey(macAddress.getMacAddress()) == false) { + try (Transaction tx = cache.getTransaction().start()) { + cache.put(macAddress.getMacAddress(), macAddress); + tx.commit(); + } catch (CacheException e) { + throw e; + } catch(Exception e1) + { + + } + } + } + + /** + * delete a MAC address from MAC pool repository + * + * @param strMacAddress MAC address + * @return void + * @throws Exception Db or cache operation exception + */ + @Override + public void deleteItem(String strMacAddress) throws CacheException { + try (Transaction tx = cache.getTransaction().start()) { + cache.remove(strMacAddress); + tx.commit(); + } catch (CacheException e) { + throw e; + } catch(Exception e) + { + + } + } + + public synchronized String getItem() throws CacheException { + String strMacAddress; + try { + long randomIndex = ThreadLocalRandom.current().nextLong(0, getSize()); + Vector sa = new Vector(Arrays.asList(cache.getAll().keySet().toArray())); + strMacAddress = sa.elementAt((int) randomIndex); + } catch (Exception e) { + throw e; + } + return strMacAddress; + } + + public synchronized long getSize() throws CacheException { + int nSize = 0; + try { + nSize = cache.getAll().size(); + } catch (Exception e) { + throw e; + } + return nSize; + } +} diff --git a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/dao/MacRangeRepository.java b/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/dao/MacRangeRepository.java index 0cb055fa1..9433c4492 100644 --- a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/dao/MacRangeRepository.java +++ b/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/dao/MacRangeRepository.java @@ -17,6 +17,7 @@ import com.futurewei.alcor.common.db.CacheException; import com.futurewei.alcor.common.db.CacheFactory; import com.futurewei.alcor.common.db.ICache; +import com.futurewei.alcor.common.db.Transaction; import com.futurewei.alcor.common.repo.ICacheRepository; import com.futurewei.alcor.macmanager.entity.MacRange; import com.futurewei.alcor.macmanager.entity.MacState; @@ -75,25 +76,48 @@ public Map findAllItems() throws CacheException { return hashMap; } + /** + * add a MAC range to MAC range repository + * + * @param macRange MAC range + * @return void + * @throws Exception Db or cache operation exception + */ @Override public void addItem(MacRange macRange) throws CacheException { - try { + try (Transaction tx = cache.getTransaction().start()) { cache.put(macRange.getRangeId(), macRange); logger.info("MacRangeRepository addItem() {}: ", macRange.getRangeId()); + tx.commit(); } catch (CacheException e) { logger.error("MacRangeRepository addItem() exception:", e); throw e; } + catch(Exception e) + { + + } } + /** + * delete a MAC range from MAC range repository + * + * @param rangeId MAC range identifier + * @return void + * @throws Exception Db or cache operation exception + */ @Override public void deleteItem(String rangeId) throws CacheException { - try { + try (Transaction tx = cache.getTransaction().start()) { cache.remove(rangeId); logger.info("MacRangeRepository deleteItem() {}: ", rangeId); + tx.commit(); } catch (CacheException e) { logger.error("MacRangeRepository deleteItem() exception:", e); throw e; + } catch(Exception e) + { + } } } \ No newline at end of file diff --git a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/dao/MacStateRepository.java b/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/dao/MacStateRepository.java index 26cca96ee..a4abbe6b3 100644 --- a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/dao/MacStateRepository.java +++ b/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/dao/MacStateRepository.java @@ -18,6 +18,7 @@ import com.futurewei.alcor.common.db.CacheException; import com.futurewei.alcor.common.db.CacheFactory; import com.futurewei.alcor.common.db.ICache; +import com.futurewei.alcor.common.db.Transaction; import com.futurewei.alcor.common.repo.ICacheRepository; import com.futurewei.alcor.macmanager.entity.MacState; import org.slf4j.Logger; @@ -74,25 +75,47 @@ public Map findAllItems() throws CacheException { return hashMap; } + /** + * add a new MAC-port allocation state to node repository + * + * @param macState MAC state + * @return void + * @throws Exception Db or cache operation exception + */ @Override public void addItem(MacState macState) throws CacheException { - try { + try (Transaction tx = cache.getTransaction().start()) { cache.put(macState.getMacAddress(), macState); + tx.commit(); logger.info("MacStateRepository addItem() {}: ", macState.getMacAddress()); } catch (CacheException e) { logger.error("MacStateRepository addItem() exception:", e); throw e; + } catch(Exception e) + { + } } + /** + * delete a MAC-port allocation state from node repository + * + * @param macAddress MAC address + * @return void + * @throws Exception Db or cache operation exception + */ @Override public void deleteItem(String macAddress) throws CacheException { - try { + try (Transaction tx = cache.getTransaction().start()) { cache.remove(macAddress); + tx.commit(); logger.info("MacStateRepository deleteItem() {}: ", macAddress); } catch (CacheException e) { logger.error("MacStateRepository deleteItem() exception:", e); throw e; + }catch(Exception e) + { + } } } diff --git a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/dao/redis/MacPoolRedisRepository.java b/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/dao/redis/MacPoolRedisRepository.java deleted file mode 100644 index bdbf1bf0d..000000000 --- a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/dao/redis/MacPoolRedisRepository.java +++ /dev/null @@ -1,81 +0,0 @@ -/*Copyright 2019 The Alcor Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ -package com.futurewei.alcor.macmanager.dao.redis; - -import com.futurewei.alcor.common.logging.Logger; -import com.futurewei.alcor.common.logging.LoggerFactory; -import com.futurewei.alcor.common.repo.ICacheRepository; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.data.redis.core.SetOperations; -import org.springframework.stereotype.Repository; - -import javax.annotation.PostConstruct; -import java.util.Map; -import java.util.logging.Level; - -@Repository -public class MacPoolRedisRepository implements ICacheRepository { - - private static final String KEY = "mac_pool"; - - private RedisTemplate redisTemplate; - - private SetOperations setOperations; - - @Autowired - public MacPoolRedisRepository(RedisTemplate redisTemplate) { - this.redisTemplate = redisTemplate; - } - - @PostConstruct - private void init() { - setOperations = redisTemplate.opsForSet(); - } - - @Override - public synchronized String findItem(String value) { - if (setOperations.isMember(KEY, value)) - return value; - else - return null; - } - - @Override - public synchronized Map findAllItems() { - return (Map) setOperations.members(KEY); - } - - @Override - public synchronized void addItem(String newItem) { - Logger logger = LoggerFactory.getLogger(); - logger.log(Level.INFO, newItem); - if (setOperations.isMember(KEY, newItem) == false) - setOperations.add(KEY, newItem); - } - - @Override - public synchronized void deleteItem(String value) { - setOperations.remove(KEY, value); - } - - public synchronized String getItem() { - return (String) setOperations.randomMember(KEY); - } - - public synchronized long getSize() { - return setOperations.size(KEY); - } -} diff --git a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/dao/redis/MacRangeRedisRepository.java b/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/dao/redis/MacRangeRedisRepository.java deleted file mode 100644 index a961f6ff3..000000000 --- a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/dao/redis/MacRangeRedisRepository.java +++ /dev/null @@ -1,77 +0,0 @@ -/*Copyright 2019 The Alcor Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ -package com.futurewei.alcor.macmanager.dao.redis; - -import com.futurewei.alcor.common.logging.Logger; -import com.futurewei.alcor.common.logging.LoggerFactory; -import com.futurewei.alcor.common.repo.ICacheRepository; -import com.futurewei.alcor.macmanager.entity.MacRange; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.redis.core.HashOperations; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.stereotype.Repository; - -import javax.annotation.PostConstruct; -import java.util.Map; -import java.util.logging.Level; - -@Repository -public class MacRangeRedisRepository implements ICacheRepository { - - private String KEY = "mac_range"; - - private RedisTemplate redisMacRangeTemplate; - - private HashOperations hashOperations; - - @Autowired - public MacRangeRedisRepository(RedisTemplate redisMacRangeTemplate) { - - this.redisMacRangeTemplate = redisMacRangeTemplate; - } - - @PostConstruct - private void init() { - hashOperations = redisMacRangeTemplate.opsForHash(); - } - - @Override - public synchronized MacRange findItem(String id) { - - return (MacRange) hashOperations.get(KEY, id); - } - - @Override - public synchronized Map findAllItems() { - return hashOperations.entries(KEY); - } - - @Override - public synchronized void addItem(MacRange newItem) { - Logger logger = LoggerFactory.getLogger(); - logger.log(Level.INFO, "mac address:" + newItem.getRangeId()); - hashOperations.putIfAbsent(KEY, newItem.getRangeId(), newItem); - } - - @Override - public synchronized void deleteItem(String id) { - hashOperations.delete(KEY, id); - } - - public synchronized void updateItem(MacRange newItem) { - hashOperations.put(KEY, newItem.getRangeId(), newItem); - } -} - diff --git a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/dao/redis/MacStateRedisRepository.java b/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/dao/redis/MacStateRedisRepository.java deleted file mode 100644 index 2a1e02c66..000000000 --- a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/dao/redis/MacStateRedisRepository.java +++ /dev/null @@ -1,82 +0,0 @@ -/*Copyright 2019 The Alcor Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package com.futurewei.alcor.macmanager.dao.redis; - -import com.futurewei.alcor.common.logging.Logger; -import com.futurewei.alcor.common.logging.LoggerFactory; -import com.futurewei.alcor.common.repo.ICacheRepository; -import com.futurewei.alcor.macmanager.entity.MacState; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.redis.core.HashOperations; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.stereotype.Repository; - -import javax.annotation.PostConstruct; -import java.util.Map; -import java.util.logging.Level; - -@Repository -public class MacStateRedisRepository implements ICacheRepository { - - private String KEY = "mac_state"; - - private RedisTemplate redisTemplate; - - private HashOperations hashOperations; - - @Autowired - public MacStateRedisRepository(RedisTemplate redisTemplate) { - - this.redisTemplate = redisTemplate; - } - - @PostConstruct - private void init() { - hashOperations = redisTemplate.opsForHash(); - } - - @Override - public synchronized MacState findItem(String id) { - - return (MacState) hashOperations.get(KEY, id); - } - - @Override - public synchronized Map findAllItems() { - return hashOperations.entries(KEY); - } - - @Override - public synchronized void addItem(MacState newItem) { - Logger logger = LoggerFactory.getLogger(); - logger.log(Level.INFO, "mac address:" + newItem.getMacAddress()); - hashOperations.putIfAbsent(KEY, newItem.getMacAddress(), newItem); - } - - @Override - public synchronized void deleteItem(String id) { - hashOperations.delete(KEY, id); - } - - public synchronized void updateItem(MacState newItem) { - hashOperations.put(KEY, newItem.getMacAddress(), newItem); - } - - public synchronized MacState findMac(String id) { - return (MacState) hashOperations.get(KEY, id); - } -} - diff --git a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/entity/MacAddress.java b/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/entity/MacAddress.java index fd41b10ff..64abc2cdd 100644 --- a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/entity/MacAddress.java +++ b/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/entity/MacAddress.java @@ -85,6 +85,8 @@ public static long macToLong(String mac) { return Long.valueOf(mac, 16); } + + public String getMacAddress() { String strMacAddress = oui + MAC_DELIMITER + nic; return strMacAddress; diff --git a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/entity/MacRange.java b/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/entity/MacRange.java index 12d50496c..b05728b95 100644 --- a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/entity/MacRange.java +++ b/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/entity/MacRange.java @@ -14,12 +14,17 @@ */ package com.futurewei.alcor.macmanager.entity; +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; -import com.futurewei.alcor.macmanager.utils.MacUtil; import lombok.Data; +import java.util.BitSet; + @Data +@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE, + setterVisibility = JsonAutoDetect.Visibility.NONE, creatorVisibility = JsonAutoDetect.Visibility.NONE) public class MacRange { @JsonProperty("range_id") private String rangeId; @@ -33,6 +38,9 @@ public class MacRange { @JsonProperty("state") private String state; + @JsonIgnore + private BitSet bitSet; + public MacRange() { } @@ -46,15 +54,6 @@ public MacRange(String rangeId, String from, String to, String state) { this.to = to; this.state = state; } - - public void createDefault(String oui) { - rangeId = MacUtil.DEFAULT_RANGE; - String strFrom = MacAddress.longToMac(0); - String strTo = MacAddress.longToMac((long)Math.pow(2,MacAddress.NIC_LENGTH) - 1); - from = new MacAddress(oui, strFrom).getMacAddress(); - to = new MacAddress(oui, strTo).getMacAddress(); - state = MacUtil.MAC_RANGE_STATE_ACTIVE; - } } diff --git a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/service/implement/MacServiceImpl.java b/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/service/implement/MacServiceImpl.java index 88b94e97e..2a9c29305 100644 --- a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/service/implement/MacServiceImpl.java +++ b/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/service/implement/MacServiceImpl.java @@ -17,6 +17,7 @@ import com.futurewei.alcor.common.exception.ParameterNullOrEmptyException; import com.futurewei.alcor.common.exception.ParameterUnexpectedValueException; import com.futurewei.alcor.common.exception.ResourceNotFoundException; +import com.futurewei.alcor.macmanager.dao.MacPoolRepository; import com.futurewei.alcor.macmanager.dao.MacRangeRepository; import com.futurewei.alcor.macmanager.dao.MacStateRepository; import com.futurewei.alcor.macmanager.entity.MacAddress; @@ -49,6 +50,9 @@ public class MacServiceImpl implements MacService { @Autowired private MacStateRepository macStateRepository; + @Autowired + private MacPoolRepository macPoolRepository; + @Value("${macmanager.oui}") private String oui; @@ -58,30 +62,27 @@ public class MacServiceImpl implements MacService { @Value("${macmanager.retrylimit}") private long nRetryLimit; - private Set macPool; - - public MacState getMacStateByMacAddress(String macAddress) throws Exception { + public MacState getMacStateByMacAddress(String macAddress) throws Exception { MacState macState = null; - if ( macAddress == null) + if (macAddress == null) throw (new ParameterNullOrEmptyException(MacUtil.MAC_EXCEPTION_PARAMETER_NULL_EMPTY)); - try{ + try { macState = macStateRepository.findItem(macAddress); - } catch(Exception e) - { + } catch (Exception e) { throw e; } return macState; } public MacState createMacState(MacState macState) throws Exception { - if ( macState == null) + if (macState == null) throw (new ParameterNullOrEmptyException(MacUtil.MAC_EXCEPTION_PARAMETER_NULL_EMPTY)); MacAddress macAddress = new MacAddress(); if (macState.getState() == null) macState.setState(MacUtil.MAC_STATE_ACTIVE); else if (macState.getState().trim().length() == 0) macState.setState(MacUtil.MAC_STATE_ACTIVE); - if (macPool.size() < (nMacPoolSize - 10)) { + if (macPoolRepository.getSize() < (nMacPoolSize - 10)) { CompletableFuture completableFuture = CompletableFuture.supplyAsync(() -> { long n = 0; try { @@ -123,7 +124,7 @@ else if (macState.getState().trim().length() == 0) public MacState updateMacState(String macAddress, MacState macState) throws Exception { if (macAddress == null || macState == null) throw (new ParameterNullOrEmptyException(MacUtil.MAC_EXCEPTION_PARAMETER_NULL_EMPTY)); - if(macAddress.equals(macState.getMacAddress()) == false) + if (macAddress.equals(macState.getMacAddress()) == false) throw (new ParameterUnexpectedValueException(MacUtil.MAC_EXCEPTION_PARAMETER_INVALID)); if (macStateRepository.findItem(macAddress) != null) { macStateRepository.addItem(macState); @@ -142,9 +143,8 @@ public String releaseMacState(String macAddress) throws Exception { } else { try { macStateRepository.deleteItem(macAddress); - macPool.add(macAddress); - }catch (Exception e) - { + macPoolRepository.addItem(new MacAddress(macAddress)); + } catch (Exception e) { throw e; } } @@ -152,14 +152,13 @@ public String releaseMacState(String macAddress) throws Exception { } @Override - public MacRange getMacRangeByMacRangeId(String macRangeId) throws Exception { + public MacRange getMacRangeByMacRangeId(String macRangeId) throws Exception { if (macRangeId == null) throw (new ParameterNullOrEmptyException(MacUtil.MAC_EXCEPTION_PARAMETER_NULL_EMPTY)); MacRange macRange = null; - try{ + try { macRange = macRangeRepository.findItem(macRangeId); - } catch(Exception e) - { + } catch (Exception e) { throw e; } return macRange; @@ -168,9 +167,9 @@ public MacRange getMacRangeByMacRangeId(String macRangeId) throws Exception { @Override public Map getAllMacRanges() throws Exception { Hashtable macRanges = new Hashtable(); - try{ + try { macRanges.putAll(macRangeRepository.findAllItems()); - }catch (Exception e){ + } catch (Exception e) { throw e; } return macRanges; @@ -205,8 +204,7 @@ public MacRange updateMacRange(MacRange macRange) throws Exception { activeMacRanges.remove(macRange.getRangeId(), macRange); else if (macRange.getState().equals(MacUtil.MAC_RANGE_STATE_ACTIVE) && activeMacRanges.containsKey(macRange.getRangeId()) == false) activeMacRanges.put(macRange.getRangeId(), macRange); - } catch(Exception e) - { + } catch (Exception e) { throw e; } return macRange; @@ -216,27 +214,26 @@ else if (macRange.getState().equals(MacUtil.MAC_RANGE_STATE_ACTIVE) && activeMac public String deleteMacRange(String rangeId) throws Exception { if (rangeId == null) throw (new ParameterNullOrEmptyException(MacUtil.MAC_EXCEPTION_PARAMETER_NULL_EMPTY)); - try{ + try { macRangeRepository.deleteItem(rangeId); activeMacRanges.remove(rangeId); - }catch (Exception e) - { + } catch (Exception e) { throw e; } return rangeId; } private String allocateMacState(MacState macState) throws Exception { -// if (macState == null) -// throw (new ParameterNullOrEmptyException(MacUtil.MAC_EXCEPTION_PARAMETER_NULL_EMPTY)); -// try{ -// -// } -// //String strMacAddress = macPool. -// if (strMacAddress != null) { -// // macPool.deleteItem(strMacAddress); -// } - return null; //strMacAddress; + String strMacAddress = null; + if (macState == null) + throw (new ParameterNullOrEmptyException(MacUtil.MAC_EXCEPTION_PARAMETER_NULL_EMPTY)); + try { + strMacAddress = macPoolRepository.getItem(); + updateMacAllocationStatus(strMacAddress); + } catch (Exception e) { + throw e; + } + return strMacAddress; } private String generateNic() throws Exception { @@ -257,7 +254,7 @@ private String generateNic() throws Exception { randomNic = ThreadLocalRandom.current().nextLong(from, to); String nicTemp = MacAddress.hexToMac(Long.toHexString(randomNic)); macAddress.setNic(nicTemp); - if ((macStateRepository.findItem(macAddress.getMacAddress()) == null) && (macPool.contains(macAddress.getMacAddress()) == false)) { + if ((macStateRepository.findItem(macAddress.getMacAddress()) == null) && (macPoolRepository.findItem(macAddress.getMacAddress()) == null)) { nic = nicTemp; } i++; @@ -267,7 +264,7 @@ private String generateNic() throws Exception { return nic; } - private MacRange getMacRange() throws Exception { + private MacRange getMacRange() throws Exception { MacRange macRange = new MacRange(); if (activeMacRanges.isEmpty()) getActiveMacRanges(); @@ -276,7 +273,7 @@ private MacRange getMacRange() throws Exception { return activeMacRanges.get(vector.elementAt(randomIndex)); } - public void getActiveMacRanges() throws Exception { + public void getActiveMacRanges() throws Exception { Hashtable macRanges = new Hashtable(macRangeRepository.findAllItems()); if (macRanges == null) macRanges = new Hashtable(); @@ -289,7 +286,7 @@ public void getActiveMacRanges() throws Exception { } } else if (macRanges != null) { MacRange newRange = new MacRange(); - newRange.createDefault(oui); + newRange = createDefaultRange(oui); macRangeRepository.addItem(newRange); activeMacRanges.put(newRange.getRangeId(), newRange); } @@ -303,6 +300,20 @@ private boolean isValidRange(MacRange macRange) { return from < to; } + public MacRange createDefaultRange(String oui) { + String rangeId = MacUtil.DEFAULT_RANGE; + long nNicLength = (long)Math.pow(2,MacAddress.NIC_LENGTH); + String strFrom = MacAddress.longToMac(0); + String strTo = MacAddress.longToMac(nNicLength - 1); + String from = new MacAddress(oui, strFrom).getMacAddress(); + String to = new MacAddress(oui, strTo).getMacAddress(); + String state = MacUtil.MAC_RANGE_STATE_ACTIVE; + BitSet bitSet = new BitSet((int)nNicLength); + MacRange defaultRange = new MacRange(rangeId, from, to, state); + defaultRange.setBitSet(bitSet); + return defaultRange; + } + public long generateMacInPool(int n) throws Exception { Exception exception = null; long nReturn = 0; @@ -317,7 +328,8 @@ public long generateMacInPool(int n) throws Exception { String strMacAddress = macAddress.getMacAddress(); MacState macState = macStateRepository.findItem(strMacAddress); if (macState == null) { - //macPool.addItem(strMacAddress); + updateMacAllocationStatus(strMacAddress); + macPoolRepository.addItem(new MacAddress(strMacAddress)); nReturn++; } } catch (RetryLimitExceedException e) { @@ -328,4 +340,21 @@ public long generateMacInPool(int n) throws Exception { throw exception; return nReturn; } + + public void updateMacAllocationStatus(String strMacAddress) throws Exception { + MacRange deafultRange = getMacRangeByMacRangeId(MacUtil.DEFAULT_RANGE); + BitSet bitSet = deafultRange.getBitSet(); + int ndx = macToIndex(deafultRange, strMacAddress); + bitSet.set(ndx); + macRangeRepository.addItem(deafultRange); + } + + public int macToIndex(MacRange range, String strMac) { + int ndx = 0; + MacAddress mac = new MacAddress(strMac); + long nMac1 = MacAddress.macToLong(strMac); + long nMac2 = MacAddress.macToLong(range.getFrom()); + ndx = (int) (nMac1 - nMac2); + return ndx; + } } diff --git a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/service/implement/redis/RedisMacRangePublisherServiceImpl.java b/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/service/implement/redis/RedisMacRangePublisherServiceImpl.java deleted file mode 100644 index ebf5d4ba0..000000000 --- a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/service/implement/redis/RedisMacRangePublisherServiceImpl.java +++ /dev/null @@ -1,43 +0,0 @@ -/*Copyright 2019 The Alcor Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ -package com.futurewei.alcor.macmanager.service.implement.redis; - -import com.futurewei.alcor.common.repo.ICachePublisher; -import com.futurewei.alcor.macmanager.entity.MacRange; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.data.redis.listener.ChannelTopic; -import org.springframework.stereotype.Service; - -@Service -public class RedisMacRangePublisherServiceImpl implements ICachePublisher { - @Autowired - private RedisTemplate redisTemplate; - - @Autowired - private ChannelTopic topic; - - public RedisMacRangePublisherServiceImpl() { - } - - public RedisMacRangePublisherServiceImpl(final RedisTemplate redisTemplate, final ChannelTopic topic2) { - this.redisTemplate = redisTemplate; - this.topic = topic2; - } - - public void publish(final String message) { - redisTemplate.convertAndSend(topic.getTopic(), message); - } -} diff --git a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/service/implement/redis/RedisMacServiceImpl.java b/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/service/implement/redis/RedisMacServiceImpl.java deleted file mode 100644 index b4b246112..000000000 --- a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/service/implement/redis/RedisMacServiceImpl.java +++ /dev/null @@ -1,277 +0,0 @@ -/*Copyright 2019 The Alcor Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ -package com.futurewei.alcor.macmanager.service.implement.redis; - -import com.futurewei.alcor.common.exception.ResourceNotFoundException; -import com.futurewei.alcor.macmanager.dao.redis.MacPoolRedisRepository; -import com.futurewei.alcor.macmanager.dao.redis.MacRangeRedisRepository; -import com.futurewei.alcor.macmanager.dao.redis.MacStateRedisRepository; -import com.futurewei.alcor.macmanager.entity.MacAddress; -import com.futurewei.alcor.macmanager.entity.MacRange; -import com.futurewei.alcor.macmanager.entity.MacState; -import com.futurewei.alcor.macmanager.exception.InvalidMacRangeException; -import com.futurewei.alcor.macmanager.exception.RetryLimitExceedException; -import com.futurewei.alcor.macmanager.exception.UniquenessViolationException; -import com.futurewei.alcor.macmanager.service.MacService; -import com.futurewei.alcor.macmanager.utils.MacUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Service; - -import java.util.ArrayList; -import java.util.Hashtable; -import java.util.Map; -import java.util.Vector; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ThreadLocalRandom; - -@Service -public class RedisMacServiceImpl implements MacService { - private static final Logger LOG = LoggerFactory.getLogger(RedisMacServiceImpl.class); - static public Hashtable activeMacRanges = new Hashtable(); - final String DELIMITER = "/"; - - @Autowired - private MacRangeRedisRepository macRangeRedisRepository; - - @Autowired - private MacPoolRedisRepository macPoolRedisRepository; - - @Autowired - private MacStateRedisRepository macStateRedisRepository; - - @Value("${macmanager.oui}") - private String oui; - - @Value("${macmanager.pool.size}") - private long nMacPoolSize; - - @Value("${macmanager.retrylimit}") - private long nRetryLimit; - - public MacState getMacStateByMacAddress(String macAddress) { - MacState macState = macStateRedisRepository.findItem(macAddress); - return macState; - } - - public MacState createMacState(MacState macState) throws Exception { - MacAddress macAddress = new MacAddress(); - if (macState.getState() == null) - macState.setState(MacUtil.MAC_STATE_ACTIVE); - else if (macState.getState().trim().length() == 0) - macState.setState(MacUtil.MAC_STATE_ACTIVE); - if (macPoolRedisRepository.getSize() < (nMacPoolSize - 10)) { - CompletableFuture completableFuture = CompletableFuture.supplyAsync(() -> { - long n = 0; - try { - n = generateMacInPool(20); - } catch (Exception e) { - e.printStackTrace(); - } - return n; - }); - long l = completableFuture.get(); - completableFuture.thenAccept(System.out::println); - completableFuture.join(); - LOG.info("{} New MAC addresses were created.", l); - } - - String strMacAddress = allocateMacState(macState); - if (strMacAddress != null) { - macState.setMacAddress(strMacAddress); - macStateRedisRepository.addItem(macState); - } else { - try { - String nic = generateNic(); - macAddress.setOui(oui); - macAddress.setNic(nic); - macState.setMacAddress(macAddress.getMacAddress()); - MacState macState2 = macStateRedisRepository.findItem(macAddress.getMacAddress()); - if (macStateRedisRepository.findItem(macAddress.getMacAddress()) != null) - throw (new UniquenessViolationException(MacUtil.MAC_EXCEPTION_UNIQUENESSSS_VILOATION + macAddress.getMacAddress() + macState2.getProjectId())); - else - macStateRedisRepository.addItem(macState); - } catch (Exception e) { - throw e; - } - } - return macState; - } - - @Override - public MacState updateMacState(String macAddress, MacState macState) throws Exception { - if (macState != null) - macStateRedisRepository.updateItem(macState); - return macState; - } - - public String releaseMacState(String macAddress) throws Exception { - MacState macState = macStateRedisRepository.findItem(macAddress); - if (macState == null) { - ResourceNotFoundException e = new ResourceNotFoundException("MAC address Not Found"); - throw e; - } else { - macStateRedisRepository.deleteItem(macAddress); - macPoolRedisRepository.addItem(macAddress); - } - return macState.getMacAddress(); - } - - @Override - public MacRange getMacRangeByMacRangeId(String macRangeId) { - MacRange macRange = macRangeRedisRepository.findItem(macRangeId); - return macRange; - } - - @Override - public Map getAllMacRanges() { - Map macRanges = macRangeRedisRepository.findAllItems(); - return macRanges; - } - - @Override - public MacRange createMacRange(MacRange macRange) throws Exception { - if (macRange != null) { - if (isValidRange(macRange)) { - macRangeRedisRepository.addItem(macRange); - if (macRange.getState().equals(MacUtil.MAC_RANGE_STATE_ACTIVE)) - activeMacRanges.put(macRange.getRangeId(), macRange); - } else - throw (new InvalidMacRangeException(MacUtil.MAC_EXCEPTION_RANGE_VALUE_INVALID)); - } - return macRange; - } - - @Override - public MacRange updateMacRange(MacRange macRange) throws Exception { - if (macRange != null) { - macRangeRedisRepository.updateItem(macRange); - if (macRange.getState().equals(MacUtil.MAC_RANGE_STATE_INACTIVE) && activeMacRanges.containsKey(macRange.getRangeId())) - activeMacRanges.remove(macRange.getRangeId(), macRange); - else if (macRange.getState().equals(MacUtil.MAC_RANGE_STATE_ACTIVE) && activeMacRanges.containsKey(macRange.getRangeId()) == false) - activeMacRanges.put(macRange.getRangeId(), macRange); - } - return macRange; - } - - @Override - public String deleteMacRange(String rangeId) throws Exception { - if (rangeId != null) { - macRangeRedisRepository.deleteItem(rangeId); - activeMacRanges.remove(rangeId); - } - return rangeId; - } - - private String allocateMacState(MacState macState) { - String strMacAddress = macPoolRedisRepository.getItem(); - if (strMacAddress != null) { - macPoolRedisRepository.deleteItem(strMacAddress); - } - return strMacAddress; - } - - private String generateNic() throws Exception { - String nic = null; - MacAddress macAddress = new MacAddress(); - long randomNic; - Long from = (long) 0; - Long to = (long) Math.pow(2, MacAddress.NIC_LENGTH); - - MacRange macRange = getMacRange(); - if (macRange != null) { - from = MacAddress.macToLong(new MacAddress(macRange.getFrom()).getNic()); - to = MacAddress.macToLong(new MacAddress(macRange.getTo()).getNic()); - } - - int i = 0; - while (nic == null && i < nRetryLimit) { - randomNic = ThreadLocalRandom.current().nextLong(from, to); - String nicTemp = MacAddress.hexToMac(Long.toHexString(randomNic)); - macAddress.setNic(nicTemp); - if (macStateRedisRepository.findMac(macAddress.getMacAddress()) == null && macPoolRedisRepository.findItem(macAddress.getMacAddress()) == null) { - nic = nicTemp; - } - i++; - } - if (nic == null && i >= nRetryLimit) - throw new RetryLimitExceedException(MacUtil.MAC_EXCEPTION_RETRY_LIMIT_EXCEED); - return nic; - } - - private MacRange getMacRange() { - MacRange macRange = new MacRange(); - if (activeMacRanges.isEmpty()) - getActiveMacRanges(); - int randomIndex = ThreadLocalRandom.current().nextInt(0, activeMacRanges.size()); - Vector vector = new Vector(activeMacRanges.keySet()); - return activeMacRanges.get(vector.elementAt(randomIndex)); - } - - public void getActiveMacRanges() { - Hashtable macRanges = new Hashtable(macRangeRedisRepository.findAllItems()); - if (macRanges == null) - macRanges = new Hashtable(); - int nSize = macRanges.size(); - if (nSize > 0) { - for (Map.Entry entry : macRanges.entrySet()) { - if (entry.getValue().getState().equals(MacUtil.MAC_RANGE_STATE_ACTIVE)) { - activeMacRanges.put(entry.getKey(), entry.getValue()); - } - } - } else if (macRanges != null) { - MacRange newRange = new MacRange(); - newRange.createDefault(oui); - macRangeRedisRepository.addItem(newRange); - activeMacRanges.put(newRange.getRangeId(), newRange); - } - } - - private boolean isValidRange(MacRange macRange) { - String strFrom = macRange.getFrom(); - String strTo = macRange.getTo(); - long from = MacAddress.macToLong(new MacAddress(strFrom).getNic()); - long to = MacAddress.macToLong(new MacAddress(strTo).getNic()); - return from < to; - } - - public long generateMacInPool(int n) throws Exception { - Exception exception = null; - long nReturn = 0; - ArrayList list = new ArrayList(); - if (n < 1) return nReturn; - MacAddress macAddress = new MacAddress(); - for (int i = 0; i < n; i++) { - try { - String nic = generateNic(); - macAddress.setOui(oui); - macAddress.setNic(nic); - String strMacAddress = macAddress.getMacAddress(); - MacState macState = macStateRedisRepository.findItem(strMacAddress); - if (macState == null) { - macPoolRedisRepository.addItem(strMacAddress); - nReturn++; - } - } catch (RetryLimitExceedException e) { - exception = e; - } - } - if (exception != null) - throw exception; - return nReturn; - } -} \ No newline at end of file diff --git a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/service/implement/redis/RedisPublisherServiceImpl.java b/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/service/implement/redis/RedisPublisherServiceImpl.java deleted file mode 100644 index 0bca8552f..000000000 --- a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/service/implement/redis/RedisPublisherServiceImpl.java +++ /dev/null @@ -1,45 +0,0 @@ -/* -Copyright 2019 The Alcor Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package com.futurewei.alcor.macmanager.service.implement.redis; - -import com.futurewei.alcor.common.repo.ICachePublisher; -import com.futurewei.alcor.macmanager.entity.MacState; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.data.redis.listener.ChannelTopic; -import org.springframework.stereotype.Service; - -@Service -public class RedisPublisherServiceImpl implements ICachePublisher { - @Autowired - private RedisTemplate redisTemplate; - - @Autowired - private ChannelTopic topic; - - public RedisPublisherServiceImpl() { - } - - public RedisPublisherServiceImpl(final RedisTemplate redisTemplate, final ChannelTopic topic) { - this.redisTemplate = redisTemplate; - this.topic = topic; - } - - public void publish(final String message) { - redisTemplate.convertAndSend(topic.getTopic(), message); - } -} diff --git a/services/mac_manager/src/main/resources/application.properties b/services/mac_manager/src/main/resources/application.properties index b53a07660..4d45411d5 100644 --- a/services/mac_manager/src/main/resources/application.properties +++ b/services/mac_manager/src/main/resources/application.properties @@ -1,26 +1,10 @@ #Machine configuration -#VPCManager.machine.config=classpath:.\\config\\machine.json -#Connecton configuration -#spring.redis.host=127.0.0.1 -spring.redis.host=redis2 -# Please check redis pod service cluster IP -#spring.redis.host=10.99.0.27 -spring.redis.port=6379 -ignite.host=127.0.0.1 -ignite.port=47500 -#apache.kafka.address=172.17.0.1:9092 -#Logging configuration -logging.level.root=info -logging.level.org.springframework.web=info -logging.file.path=. -logging.type=file -#Ignite configuration -#ignite.host=localhost -#ignite.port=10800 -#ignite.key-store-path=F:\\work\\alcor\\git\\chenpp\\alcor\\src\\resources\\keystore.jks -#ignite.key-store-password=123456 -#ignite.trust-store-path=F:\\work\\alcor\\git\\chenpp\\alcor\\src\\resources\\truststore.jks -#ignite.trust-store-password=123456 +ignite.host=localhost +ignite.port=10800 +ignite.key-store-path=keystore.jks +ignite.key-store-password=123456 +ignite.trust-store-path=truststore.jks +ignite.trust-store-password=123456 macmanager.oui = 00-AA-BB macmanager.pool.size = 100 macmanager.retrylimit = 10 \ No newline at end of file diff --git a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/config/SwaggerConfig.java b/services/mac_manager/src/test/java/com/futurewei/alcor/macmanager/swagger/SwaggerConfig.java similarity index 97% rename from services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/config/SwaggerConfig.java rename to services/mac_manager/src/test/java/com/futurewei/alcor/macmanager/swagger/SwaggerConfig.java index 7e77017e8..4135f1523 100644 --- a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/config/SwaggerConfig.java +++ b/services/mac_manager/src/test/java/com/futurewei/alcor/macmanager/swagger/SwaggerConfig.java @@ -14,7 +14,7 @@ limitations under the License. */ -package com.futurewei.alcor.macmanager.config; +package com.futurewei.alcor.macmanager.swagger; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; From a0360124c0907ec52d7c71752a21e4733050b473 Mon Sep 17 00:00:00 2001 From: Eunju Kim Date: Wed, 13 May 2020 18:04:02 -0700 Subject: [PATCH 2/8] [Micro services] MAC manager - updated pom.xml --- .../futurewei/alcor/macmanager/entity/MacAddress.java | 2 -- .../macmanager/service/implement/MacServiceImpl.java | 10 +++++----- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/entity/MacAddress.java b/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/entity/MacAddress.java index 64abc2cdd..fd41b10ff 100644 --- a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/entity/MacAddress.java +++ b/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/entity/MacAddress.java @@ -85,8 +85,6 @@ public static long macToLong(String mac) { return Long.valueOf(mac, 16); } - - public String getMacAddress() { String strMacAddress = oui + MAC_DELIMITER + nic; return strMacAddress; diff --git a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/service/implement/MacServiceImpl.java b/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/service/implement/MacServiceImpl.java index 2a9c29305..211704fef 100644 --- a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/service/implement/MacServiceImpl.java +++ b/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/service/implement/MacServiceImpl.java @@ -273,7 +273,7 @@ private MacRange getMacRange() throws Exception { return activeMacRanges.get(vector.elementAt(randomIndex)); } - public void getActiveMacRanges() throws Exception { + private void getActiveMacRanges() throws Exception { Hashtable macRanges = new Hashtable(macRangeRepository.findAllItems()); if (macRanges == null) macRanges = new Hashtable(); @@ -300,7 +300,7 @@ private boolean isValidRange(MacRange macRange) { return from < to; } - public MacRange createDefaultRange(String oui) { + private MacRange createDefaultRange(String oui) { String rangeId = MacUtil.DEFAULT_RANGE; long nNicLength = (long)Math.pow(2,MacAddress.NIC_LENGTH); String strFrom = MacAddress.longToMac(0); @@ -314,7 +314,7 @@ public MacRange createDefaultRange(String oui) { return defaultRange; } - public long generateMacInPool(int n) throws Exception { + private long generateMacInPool(int n) throws Exception { Exception exception = null; long nReturn = 0; ArrayList list = new ArrayList(); @@ -341,7 +341,7 @@ public long generateMacInPool(int n) throws Exception { return nReturn; } - public void updateMacAllocationStatus(String strMacAddress) throws Exception { + private void updateMacAllocationStatus(String strMacAddress) throws Exception { MacRange deafultRange = getMacRangeByMacRangeId(MacUtil.DEFAULT_RANGE); BitSet bitSet = deafultRange.getBitSet(); int ndx = macToIndex(deafultRange, strMacAddress); @@ -349,7 +349,7 @@ public void updateMacAllocationStatus(String strMacAddress) throws Exception { macRangeRepository.addItem(deafultRange); } - public int macToIndex(MacRange range, String strMac) { + private int macToIndex(MacRange range, String strMac) { int ndx = 0; MacAddress mac = new MacAddress(strMac); long nMac1 = MacAddress.macToLong(strMac); From d03f84e5ee39c8087f44b382b89518e9c5e25343 Mon Sep 17 00:00:00 2001 From: Eunju Kim Date: Wed, 13 May 2020 18:19:25 -0700 Subject: [PATCH 3/8] [Micro services] MAC manager - updated pom.xml & configuration --- services/mac_manager/pom.xml | 5 +++++ .../futurewei/alcor/macmanager/swagger/SwaggerConfig.java | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/services/mac_manager/pom.xml b/services/mac_manager/pom.xml index 30799ec9f..aa6baa780 100644 --- a/services/mac_manager/pom.xml +++ b/services/mac_manager/pom.xml @@ -90,6 +90,11 @@ 2.6.1 test + + io.springfox + springfox-swagger-ui + 2.2.2 + io.github.swagger2markup swagger2markup-spring-restdocs-ext diff --git a/services/mac_manager/src/test/java/com/futurewei/alcor/macmanager/swagger/SwaggerConfig.java b/services/mac_manager/src/test/java/com/futurewei/alcor/macmanager/swagger/SwaggerConfig.java index 4135f1523..7e77017e8 100644 --- a/services/mac_manager/src/test/java/com/futurewei/alcor/macmanager/swagger/SwaggerConfig.java +++ b/services/mac_manager/src/test/java/com/futurewei/alcor/macmanager/swagger/SwaggerConfig.java @@ -14,7 +14,7 @@ limitations under the License. */ -package com.futurewei.alcor.macmanager.swagger; +package com.futurewei.alcor.macmanager.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; From b321524166cd46663f401a02aeac381467d4a270 Mon Sep 17 00:00:00 2001 From: Eunju Kim Date: Wed, 13 May 2020 19:14:58 -0700 Subject: [PATCH 4/8] [Micro serices] MAC manager - add config file --- services/mac_manager/pom.xml | 29 +++++++++++ .../macmanager/config/SwaggerConfig.java | 49 +++++++++++++++++++ .../macmanager/swagger/SwaggerConfig.java | 2 +- 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/config/SwaggerConfig.java diff --git a/services/mac_manager/pom.xml b/services/mac_manager/pom.xml index 30799ec9f..6348f8f9d 100644 --- a/services/mac_manager/pom.xml +++ b/services/mac_manager/pom.xml @@ -90,6 +90,11 @@ 2.6.1 test + + io.springfox + springfox-swagger-ui + 2.2.2 + io.github.swagger2markup swagger2markup-spring-restdocs-ext @@ -102,6 +107,30 @@ 0.1.0-SNAPSHOT compile + + io.springfox + springfox-core + 2.6.1 + compile + + + io.springfox + springfox-core + 2.6.1 + compile + + + io.springfox + springfox-swagger2 + 2.6.1 + compile + + + io.springfox + springfox-swagger2 + 2.6.1 + compile + diff --git a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/config/SwaggerConfig.java b/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/config/SwaggerConfig.java new file mode 100644 index 000000000..0ce88b718 --- /dev/null +++ b/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/config/SwaggerConfig.java @@ -0,0 +1,49 @@ +/* +Copyright 2019 The Alcor Authors. +Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + + package com.futurewei.alcor.macmanager.config; + + import org.springframework.context.annotation.Bean; + import org.springframework.context.annotation.Configuration; + + import springfox.documentation.builders.PathSelectors; + import springfox.documentation.builders.RequestHandlerSelectors; + import springfox.documentation.service.ApiInfo; + import springfox.documentation.spi.DocumentationType; + import springfox.documentation.spring.web.plugins.Docket; + import springfox.documentation.builders.ApiInfoBuilder; + import springfox.documentation.swagger2.annotations.EnableSwagger2; + + @Configuration + @EnableSwagger2 + public class SwaggerConfig{ + @Bean + public Docket api(){ + return new Docket(DocumentationType.SWAGGER_2) + .apiInfo(apiInfo()) + .select() + .apis(RequestHandlerSelectors.any()) + .paths(PathSelectors.regex("(?!/error).+")) + .paths(PathSelectors.regex("(?!/actuator).+")) + .build(); + } + + private ApiInfo apiInfo(){ + return new ApiInfoBuilder() + .title("Virtual MAC Manager") + .description("Virtual MAC pool management") + .license("Apache 2.0") + .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html") + .build(); + } + } diff --git a/services/mac_manager/src/test/java/com/futurewei/alcor/macmanager/swagger/SwaggerConfig.java b/services/mac_manager/src/test/java/com/futurewei/alcor/macmanager/swagger/SwaggerConfig.java index 4135f1523..7e77017e8 100644 --- a/services/mac_manager/src/test/java/com/futurewei/alcor/macmanager/swagger/SwaggerConfig.java +++ b/services/mac_manager/src/test/java/com/futurewei/alcor/macmanager/swagger/SwaggerConfig.java @@ -14,7 +14,7 @@ limitations under the License. */ -package com.futurewei.alcor.macmanager.swagger; +package com.futurewei.alcor.macmanager.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; From 8e67eab0e9220d93a49cf9604dcfff9ccaacc72f Mon Sep 17 00:00:00 2001 From: Eunju Kim Date: Wed, 13 May 2020 19:37:18 -0700 Subject: [PATCH 5/8] [Micro services] MAC manager - moved swagger config to config package --- services/mac_manager/pom.xml | 24 -------- .../macmanager/config/SwaggerConfig.java | 61 +++++++++---------- .../macmanager/swagger/SwaggerConfig.java | 52 ---------------- 3 files changed, 30 insertions(+), 107 deletions(-) delete mode 100644 services/mac_manager/src/test/java/com/futurewei/alcor/macmanager/swagger/SwaggerConfig.java diff --git a/services/mac_manager/pom.xml b/services/mac_manager/pom.xml index 6348f8f9d..aa6baa780 100644 --- a/services/mac_manager/pom.xml +++ b/services/mac_manager/pom.xml @@ -107,30 +107,6 @@ 0.1.0-SNAPSHOT compile - - io.springfox - springfox-core - 2.6.1 - compile - - - io.springfox - springfox-core - 2.6.1 - compile - - - io.springfox - springfox-swagger2 - 2.6.1 - compile - - - io.springfox - springfox-swagger2 - 2.6.1 - compile - diff --git a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/config/SwaggerConfig.java b/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/config/SwaggerConfig.java index 0ce88b718..e4192e66f 100644 --- a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/config/SwaggerConfig.java +++ b/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/config/SwaggerConfig.java @@ -11,39 +11,38 @@ limitations under the License. */ - package com.futurewei.alcor.macmanager.config; +package com.futurewei.alcor.macmanager.config; - import org.springframework.context.annotation.Bean; - import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import springfox.documentation.builders.ApiInfoBuilder; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.service.ApiInfo; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; +import springfox.documentation.swagger2.annotations.EnableSwagger2; - import springfox.documentation.builders.PathSelectors; - import springfox.documentation.builders.RequestHandlerSelectors; - import springfox.documentation.service.ApiInfo; - import springfox.documentation.spi.DocumentationType; - import springfox.documentation.spring.web.plugins.Docket; - import springfox.documentation.builders.ApiInfoBuilder; - import springfox.documentation.swagger2.annotations.EnableSwagger2; - - @Configuration - @EnableSwagger2 - public class SwaggerConfig{ - @Bean - public Docket api(){ +@Configuration +@EnableSwagger2 +public class SwaggerConfig { + @Bean + public Docket api() { return new Docket(DocumentationType.SWAGGER_2) - .apiInfo(apiInfo()) - .select() - .apis(RequestHandlerSelectors.any()) - .paths(PathSelectors.regex("(?!/error).+")) - .paths(PathSelectors.regex("(?!/actuator).+")) - .build(); - } + .apiInfo(apiInfo()) + .select() + .apis(RequestHandlerSelectors.any()) + .paths(PathSelectors.regex("(?!/error).+")) + .paths(PathSelectors.regex("(?!/actuator).+")) + .build(); + } - private ApiInfo apiInfo(){ + private ApiInfo apiInfo() { return new ApiInfoBuilder() - .title("Virtual MAC Manager") - .description("Virtual MAC pool management") - .license("Apache 2.0") - .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html") - .build(); - } - } + .title("Virtual MAC Manager") + .description("Virtual MAC pool management") + .license("Apache 2.0") + .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html") + .build(); + } +} diff --git a/services/mac_manager/src/test/java/com/futurewei/alcor/macmanager/swagger/SwaggerConfig.java b/services/mac_manager/src/test/java/com/futurewei/alcor/macmanager/swagger/SwaggerConfig.java deleted file mode 100644 index 7e77017e8..000000000 --- a/services/mac_manager/src/test/java/com/futurewei/alcor/macmanager/swagger/SwaggerConfig.java +++ /dev/null @@ -1,52 +0,0 @@ -/* -Copyright 2019 The Alcor Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package com.futurewei.alcor.macmanager.config; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import springfox.documentation.builders.PathSelectors; -import springfox.documentation.builders.RequestHandlerSelectors; -import springfox.documentation.service.ApiInfo; -import springfox.documentation.spi.DocumentationType; -import springfox.documentation.spring.web.plugins.Docket; -import springfox.documentation.builders.ApiInfoBuilder; -import springfox.documentation.swagger2.annotations.EnableSwagger2; - -@Configuration -@EnableSwagger2 -public class SwaggerConfig{ - @Bean - public Docket api(){ - return new Docket(DocumentationType.SWAGGER_2) - .apiInfo(apiInfo()) - .select() - .apis(RequestHandlerSelectors.any()) - .paths(PathSelectors.regex("(?!/error).+")) - .paths(PathSelectors.regex("(?!/actuator).+")) - .build(); - } - - private ApiInfo apiInfo(){ - return new ApiInfoBuilder() - .title("Virtual MAC Manager") - .description("Virtual MAC pool management") - .license("Apache 2.0") - .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html") - .build(); - } -} \ No newline at end of file From 7ba14adf2d30a932aede4036083335a88c03a98d Mon Sep 17 00:00:00 2001 From: Eunju Kim Date: Wed, 13 May 2020 19:44:49 -0700 Subject: [PATCH 6/8] [Micro services] MAC manager - moved swagger config to config package --- .../com/futurewei/alcor/macmanager/config/SwaggerConfig.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/config/SwaggerConfig.java b/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/config/SwaggerConfig.java index e4192e66f..35c9fec97 100644 --- a/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/config/SwaggerConfig.java +++ b/services/mac_manager/src/main/java/com/futurewei/alcor/macmanager/config/SwaggerConfig.java @@ -11,6 +11,7 @@ limitations under the License. */ + package com.futurewei.alcor.macmanager.config; import org.springframework.context.annotation.Bean; @@ -45,4 +46,4 @@ private ApiInfo apiInfo() { .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html") .build(); } -} +} \ No newline at end of file From dc7677fd7665869a43cdb9fe5ec7b2e1ae60c604 Mon Sep 17 00:00:00 2001 From: Eunju Kim Date: Wed, 13 May 2020 20:14:43 -0700 Subject: [PATCH 7/8] [Micro services] MAC manager - removed duplicated dependency --- services/mac_manager/pom.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/services/mac_manager/pom.xml b/services/mac_manager/pom.xml index aa6baa780..e56f86cde 100644 --- a/services/mac_manager/pom.xml +++ b/services/mac_manager/pom.xml @@ -88,7 +88,6 @@ io.springfox springfox-swagger2 2.6.1 - test io.springfox From 67dee63a089edd52f3e66b48345f4d0d8aaabc6c Mon Sep 17 00:00:00 2001 From: Eunju Kim Date: Sun, 17 May 2020 02:59:03 -0700 Subject: [PATCH 8/8] [Micro services] Node manager - update UTs configuration with MockIgniteServer --- .../src/main/resources/application.properties | 5 ++++- .../controller/NodeControllerTest.java | 15 +++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/services/node_manager/src/main/resources/application.properties b/services/node_manager/src/main/resources/application.properties index a5e45f7a3..36e669569 100644 --- a/services/node_manager/src/main/resources/application.properties +++ b/services/node_manager/src/main/resources/application.properties @@ -1,6 +1,9 @@ +#redis configuration +spring.redis.host=localhost +spring.redis.port=6380 #ignite configuration ignite.host=localhost -ignite.port=10800 +ignite.port=10801 ignite.key-store-path=keystore.jks ignite.key-store-password=123456 ignite.trust-store-path=truststore.jks diff --git a/services/node_manager/src/test/java/com/futurewei/alcor/nodemanager/controller/NodeControllerTest.java b/services/node_manager/src/test/java/com/futurewei/alcor/nodemanager/controller/NodeControllerTest.java index 4ef156919..8f07a91be 100644 --- a/services/node_manager/src/test/java/com/futurewei/alcor/nodemanager/controller/NodeControllerTest.java +++ b/services/node_manager/src/test/java/com/futurewei/alcor/nodemanager/controller/NodeControllerTest.java @@ -15,6 +15,7 @@ package com.futurewei.alcor.nodemanager.controller; import com.fasterxml.jackson.databind.ObjectMapper; +import com.futurewei.alcor.common.db.ignite.MockIgniteServer; import com.futurewei.alcor.common.exception.ParameterUnexpectedValueException; import com.futurewei.alcor.nodemanager.service.NodeService; import com.futurewei.alcor.web.entity.NodeInfo; @@ -35,7 +36,9 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; -import static org.junit.Assert.assertTrue; +import java.util.Objects; + +import static org.junit.Assert.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -43,7 +46,7 @@ @RunWith(SpringRunner.class) @SpringBootTest() @AutoConfigureMockMvc -public class NodeControllerTest { +public class NodeControllerTest extends MockIgniteServer { private static final ObjectMapper om = new ObjectMapper(); @MockBean NodeService service; @@ -125,7 +128,7 @@ public void test_createNodeInfo_invalidInputNull() throws Exception { .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)) .andDo(print()) .andExpect(status().isBadRequest()) - .andExpect((rslt) -> assertTrue(rslt.getResolvedException().getClass() != null)); + .andExpect((rslt) -> assertNotNull(Objects.requireNonNull(rslt.getResolvedException()).getClass())); } @Test @@ -182,7 +185,7 @@ public void test_getNodeInfoByNodeId_invalidId() throws Exception { MvcResult result = this.mockMvc.perform(get("/nodes/" + strNodeId)) .andDo(print()) .andReturn(); - assertTrue(result.getResponse().getContentAsString().length() == 0); + assertEquals(0, result.getResponse().getContentAsString().length()); } @Test @@ -193,7 +196,7 @@ public void deleteNodeInfo() throws Exception { .andDo(print()) .andExpect(status().isOk()) .andReturn(); - assertTrue(result.getResponse().getContentAsString().equals(strNodeId)); + assertEquals(result.getResponse().getContentAsString(), strNodeId); } @Test @@ -202,6 +205,6 @@ public void deleteNodeInfo_invalidId() throws Exception { MvcResult result = this.mockMvc.perform(delete("/nodes/" + strNodeId)) .andDo(print()) .andReturn(); - assertTrue(result.getResponse().getContentAsString().length() == 0); + assertEquals(0, result.getResponse().getContentAsString().length()); } } \ No newline at end of file